Re: [dev] [sbase] [patch v3] Add strings(1)

From: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
Date: Thu, 15 Aug 2013 13:06:25 +0200

> +dostrings(FILE *fp, const char *fname)
> +{
> + unsigned char buf[BUFSIZ];
> + int c, i = 0;
> + off_t offset = 0;
> +
> + while ((c = getc(fp)) != EOF) {
> + offset++;
> + if (isprint(c)) {
> + buf[i++] = c;
> + if (i == sizeof(buf) - 1) {
> + buf[i] = '\0';
> + printf("%8ld: %s\n", (long)offset - i - 1, buf);
> + i = 0;
> + }
> + } else {
> + if (i >= 6) {
> + buf[i] = '\0';
> + printf("%8ld: %s\n", (long)offset - i - 1, buf);
> + }
> + i = 0;
> + }
> + }
> + if (i >= 6) {
> + buf[i] = '\0';
> + printf("%8ld: %s\n", (long)offset - i - 1, buf);
> + }
> + if (ferror(fp))
> + eprintf("%s read error:", fname);
> +}


Only other sugestion, this loop could be done better with a do while:

dostrings(FILE *fp, const char *fname)
{
        unsigned char buf[BUFSIZ];
        int c, i = 0;
        off_t offset = 0;
        do {
                offset++;
                if (isprint(c = getc(fp)))
                        buf[i++] = c

                if ((!isprint(c) && i >= 6) || i == sizeof(buf) - 1) {
                        buf[i] = '\0';
                        printf("%8ld: %s\n", (long)offset - i - 1, buf);
                        i = 0;
                }
        } while (c != EOF);
        
        if (ferror(fp))
                eprintf("%s read error:", fname);
        }
}
        
        --
Roberto E. Vargas Caballero
----------------------------
k0ga_AT_shike2.com
http://www.shike2.com
Received on Thu Aug 15 2013 - 13:06:25 CEST

This archive was generated by hypermail 2.3.0 : Thu Aug 15 2013 - 13:12:18 CEST