toybox

changeset 1349:bf2e5591bdf0

Cleanup strings.
author Rob Landley <rob@landley.net>
date Wed, 11 Jun 2014 08:02:55 -0500
parents 63d8181b0f92
children bb9c601122b8
files toys/pending/strings.c
diffstat 1 files changed, 21 insertions(+), 20 deletions(-) [+]
line diff
     1.1 --- a/toys/pending/strings.c	Tue Jun 10 21:57:05 2014 -0500
     1.2 +++ b/toys/pending/strings.c	Wed Jun 11 08:02:55 2014 -0500
     1.3 @@ -4,21 +4,24 @@
     1.4   * Copyright 2014 Kyungwan Han <asura321@gmail.com>
     1.5   *
     1.6   * No Standard
     1.7 + * TODO: utf8 strings
     1.8 + * TODO: posix -t
     1.9  
    1.10 -USE_STRINGS(NEWTOY(strings, "n#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN))
    1.11 +USE_STRINGS(NEWTOY(strings, "an#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN))
    1.12  
    1.13  config STRINGS
    1.14    bool "strings"
    1.15    default n
    1.16    help
    1.17 -    usage: strings [-fo] [-n count] [FILE] ...
    1.18 +    usage: strings [-fo] [-n LEN] [FILE...]
    1.19  
    1.20      Display printable strings in a binary file
    1.21  
    1.22 -    -f Precede strings with filenames
    1.23 -    -n [LEN] At least LEN characters form a string (default 4)
    1.24 -    -o Precede strings with decimal offsets
    1.25 +    -f	Precede strings with filenames
    1.26 +    -n	At least LEN characters form a string (default 4)
    1.27 +    -o	Precede strings with decimal offsets
    1.28  */
    1.29 +
    1.30  #define FOR_strings
    1.31  #include "toys.h"
    1.32  
    1.33 @@ -28,29 +31,28 @@
    1.34  
    1.35  void do_strings(int fd, char *filename)
    1.36  {
    1.37 -  int nread, i, wlen, count = 0;
    1.38 +  int nread, i, wlen = TT.num, count = 0;
    1.39    off_t offset = 0;
    1.40 -  char *string;
    1.41 +  char *string = xzalloc(wlen + 1);
    1.42  
    1.43 -  string = xzalloc(TT.num + 1);
    1.44 -  wlen = TT.num - 1;
    1.45 -
    1.46 -  while ((nread = read(fd,toybuf,sizeof(toybuf)))>0 ) {
    1.47 +  for (;;) {
    1.48 +    nread = read(fd, toybuf, sizeof(toybuf));
    1.49 +    if (nread < 0) perror_msg("%s", filename);
    1.50 +    if (nread < 1) break;
    1.51      for (i = 0; i < nread; i++, offset++) {
    1.52        if (((toybuf[i] >= 32) && (toybuf[i] <= 126)) || (toybuf[i] == '\t')) {
    1.53 -        if (count > wlen) xputc(toybuf[i]);
    1.54 +        if (count == wlen) fputc(toybuf[i], stdout);
    1.55          else {
    1.56 -          string[count] = toybuf[i];
    1.57 +          string[count++] = toybuf[i];
    1.58            if (count == wlen) {
    1.59 -            if (toys.optflags & FLAG_f) xprintf("%s: ", filename);
    1.60 +            if (toys.optflags & FLAG_f) printf("%s: ", filename);
    1.61              if (toys.optflags & FLAG_o)
    1.62 -              xprintf("%7lld ",(long long)(offset - wlen));
    1.63 -            xprintf("%s",string);
    1.64 +              printf("%7lld ",(long long)(offset - wlen));
    1.65 +            printf("%s", string);
    1.66            }
    1.67 -          count++;
    1.68          }
    1.69        } else {
    1.70 -        if (count > wlen) xputc('\n');
    1.71 +        if (count == wlen) xputc('\n');
    1.72          count = 0;
    1.73        }
    1.74      }
    1.75 @@ -61,6 +63,5 @@
    1.76  
    1.77  void strings_main(void)
    1.78  {
    1.79 -  loopfiles(toys.optargs,  do_strings);
    1.80 +  loopfiles(toys.optargs, do_strings);
    1.81  }
    1.82 -