aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-04-14 22:34:34 -0500
committerRob Landley <rob@landley.net>2012-04-14 22:34:34 -0500
commite692fc11a2c8bb8d10001aa80441baf94bd85f98 (patch)
treee1fa36c3a4d4419b13ce1a3d4e1c75fbae769c02
parent1bcdf538bead59ef4d0d6c9a47b7bc5153a21667 (diff)
downloadtoybox-e692fc11a2c8bb8d10001aa80441baf94bd85f98.tar.gz
Refactor uniq.c a bit.
-rw-r--r--toys/uniq.c63
1 files changed, 20 insertions, 43 deletions
diff --git a/toys/uniq.c b/toys/uniq.c
index 03913e71..c16c08f1 100644
--- a/toys/uniq.c
+++ b/toys/uniq.c
@@ -45,61 +45,38 @@ DEFINE_GLOBALS(
static char *skip(char *str)
{
- int field = 0;
- long nchars = TT.nchars;
- long nfields = TT.nfields;
+ long nchars = TT.nchars, nfields;
+
// Skip fields first
- while (nfields && *str) {
- if (isspace((unsigned char)*str)) {
- if (field) {
- field = 0;
- nfields--;
- }
- } else if (!field) {
- field = 1;
- }
- str++;
+ for (nfields = TT.nfields; nfields; str++) {
+ while (*str && isspace(*str)) str++;
+ while (*str && !isspace(*str)) str++;
+ nfields--;
}
// Skip chars
- while (nchars-- && *str)
- str++;
+ while (*str && nchars--) str++;
+
return str;
}
static void print_line(FILE *f, char *line)
{
- if (TT.repeats == 0 && (toys.optflags & FLAG_d))
- return;
- if (TT.repeats > 0 && (toys.optflags & FLAG_u))
- return;
- if ((toys.optflags & FLAG_c)) {
- fprintf(f, "%7lu %s", TT.repeats + 1, line);
- } else {
- fprintf(f, "%s", line);
- }
- if (toys.optflags & FLAG_z)
- fprintf(f, "%c", '\0');
+ if (toys.optflags & (TT.repeats ? FLAG_u : FLAG_d)) return;
+ if (toys.optflags & FLAG_c) fprintf(f, "%7lu ", TT.repeats + 1);
+ fputs(line, f);
+ if (toys.optflags & FLAG_z) fputc(0, f);
}
void uniq_main(void)
{
- FILE *infile = stdin;
- FILE *outfile = stdout;
- char *thisline = NULL;
- char *prevline = NULL;
- size_t thissize, prevsize = 0;
- char *tmpline;
- char eol = '\n';
- size_t tmpsize;
-
- if (toys.optc >= 1)
- infile = xfopen(toys.optargs[0], "r");
-
- if (toys.optc >= 2)
- outfile = xfopen(toys.optargs[1], "w");
-
- if (toys.optflags & FLAG_z)
- eol = '\0';
+ FILE *infile = stdin, *outfile = stdout;
+ char *thisline = NULL, *prevline = NULL, *tmpline, eol = '\n';
+ size_t thissize, prevsize = 0, tmpsize;
+
+ if (toys.optc >= 1) infile = xfopen(toys.optargs[0], "r");
+ if (toys.optc >= 2) outfile = xfopen(toys.optargs[1], "w");
+
+ if (toys.optflags & FLAG_z) eol = 0;
// If first line can't be read
if (getdelim(&prevline, &prevsize, eol, infile) < 0)