/* atoi with error detection */ static int getn(const char *s) { char *p; long r; errno = 0; r = strtol(s, &p, 10); if (errno != 0) bb_error_msg_and_die("%s: out of range", s); /* p = bb_skip_whitespace(p); avoid const warning */ if (*(bb_skip_whitespace(p))) bb_error_msg_and_die("%s: bad number", s); return (int) r; }
static void bb_dump_addfile(char *name) { register char *p; FILE *fp; char *buf; fp = bb_xfopen(name, "r"); while ((buf = bb_get_chomped_line_from_file(fp)) != NULL) { p = (char *) bb_skip_whitespace(buf); if (*p && (*p != '#')) { bb_dump_add(p); } free(buf); } fclose(fp); }
void bb_dump_add(const char *fmt) { register const char *p; register char *p1; register char *p2; static FS **nextfs; FS *tfs; FU *tfu, **nextfu; const char *savep; /* start new linked list of format units */ /* NOSTRICT */ tfs = (FS *) xcalloc(1,sizeof(FS)); /*DBU:[[email protected]] start out NULL */ if (!bb_dump_fshead) { bb_dump_fshead = tfs; } else { *nextfs = tfs; } nextfs = &tfs->nextfs; nextfu = &tfs->nextfu; /* take the format string and break it up into format units */ for (p = fmt;;) { /* bb_dump_skip leading white space */ p = bb_skip_whitespace(p); if (!*p) { break; } /* allocate a new format unit and link it in */ /* NOSTRICT */ /* DBU:[[email protected]] calloc so that forward pointers start out NULL */ tfu = (FU *) xcalloc(1,sizeof(FU)); *nextfu = tfu; nextfu = &tfu->nextfu; tfu->reps = 1; /* if leading digit, repetition count */ if (isdigit(*p)) { for (savep = p; isdigit(*p); ++p); if (!isspace(*p) && *p != '/') { bb_error_msg_and_die("bad format {%s}", fmt); } /* may overwrite either white space or slash */ tfu->reps = atoi(savep); tfu->flags = F_SETREP; /* bb_dump_skip trailing white space */ p = bb_skip_whitespace(++p); } /* bb_dump_skip slash and trailing white space */ if (*p == '/') { p = bb_skip_whitespace(++p); } /* byte count */ if (isdigit(*p)) { for (savep = p; isdigit(*p); ++p); if (!isspace(*p)) { bb_error_msg_and_die("bad format {%s}", fmt); } tfu->bcnt = atoi(savep); /* bb_dump_skip trailing white space */ p = bb_skip_whitespace(++p); } /* format */ if (*p != '"') { bb_error_msg_and_die("bad format {%s}", fmt); } for (savep = ++p; *p != '"';) { if (*p++ == 0) { bb_error_msg_and_die("bad format {%s}", fmt); } } tfu->fmt = xmalloc(p - savep + 1); strncpy(tfu->fmt, savep, p - savep); tfu->fmt[p - savep] = '\0'; /* escape(tfu->fmt); */ p1 = tfu->fmt; /* alphabetic escape sequences have to be done in place */ for (p2 = p1;; ++p1, ++p2) { if (!*p1) { *p2 = *p1; break; } if (*p1 == '\\') { const char *cs = conv_str + 4; ++p1; *p2 = *p1; do { if (*p1 == cs[2]) { *p2 = cs[0]; break; } cs += 4; } while (*cs); } } p++; } }
int uniq_main(int argc, char **argv) { FILE *in, *out; /* Note: Ignore the warning about dups and e0 being used uninitialized. * They will be initialized on the fist pass of the loop (since s0 is NULL). */ unsigned long dups, skip_fields, skip_chars, i; const char *s0, *e0, *s1, *e1, *input_filename; int opt; int uniq_flags = 6; /* -u */ skip_fields = skip_chars = 0; while ((opt = getopt(argc, argv, uniq_opts)) > 0) { if (opt == 'f') { skip_fields = bb_xgetularg10(optarg); } else if (opt == 's') { skip_chars = bb_xgetularg10(optarg); } else if ((s0 = strchr(uniq_opts, opt)) != NULL) { uniq_flags &= s0[4]; uniq_flags |= s0[7]; } else { bb_show_usage(); } } input_filename = *(argv += optind); in = xgetoptfile_sort_uniq(argv, "r"); if (*argv) { ++argv; } out = xgetoptfile_sort_uniq(argv, "w"); if (*argv && argv[1]) { bb_show_usage(); } s0 = NULL; /* gnu uniq ignores newlines */ while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) { e1 = s1; for (i=skip_fields ; i ; i--) { e1 = bb_skip_whitespace(e1); while (*e1 && !isspace(*e1)) { ++e1; } } for (i = skip_chars ; *e1 && i ; i--) { ++e1; } if (s0) { if (strcmp(e0, e1) == 0) { ++dups; /* Note: Testing for overflow seems excessive. */ continue; } DO_LAST: if ((dups && (uniq_flags & 2)) || (!dups && (uniq_flags & 4))) { bb_fprintf(out, "\0%7d\t" + (uniq_flags & 1), dups + 1); bb_fprintf(out, "%s\n", s0); } free((void *)s0); } s0 = s1; e0 = e1; dups = 0; } if (s0) { e1 = NULL; goto DO_LAST; } bb_xferror(in, input_filename); bb_fflush_stdout_and_exit(EXIT_SUCCESS); }
int uniq_main(int argc, char **argv) { FILE *in, *out; unsigned long dups, skip_fields, skip_chars, i, uniq_flags; const char *s0, *e0, *s1, *e1, *input_filename; int opt; uniq_flags = skip_fields = skip_chars = 0; while ((opt = getopt(argc, argv, uniq_opts)) > 0) { if ((opt == 'f') || (opt == 's')) { int t = bb_xgetularg10(optarg); if (opt == 'f') { skip_fields = t; } else { skip_chars = t; } } else if ((s0 = strchr(uniq_opts, opt)) != NULL) { uniq_flags |= s0[4]; } else { bb_show_usage(); } } input_filename = *(argv += optind); in = xgetoptfile_uniq_s(argv, 0); if (*argv) { ++argv; } out = xgetoptfile_uniq_s(argv, 2); if (*argv && argv[1]) { bb_show_usage(); } s1 = e1 = NULL; /* prime the pump */ do { s0 = s1; e0 = e1; dups = 0; /* gnu uniq ignores newlines */ while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) { e1 = s1; for (i=skip_fields ; i ; i--) { e1 = bb_skip_whitespace(e1); while (*e1 && !isspace(*e1)) { ++e1; } } for (i = skip_chars ; *e1 && i ; i--) { ++e1; } if (!s0 || strcmp(e0, e1)) { break; } ++dups; /* Note: Testing for overflow seems excessive. */ } if (s0) { if (!(uniq_flags & (2 << !!dups))) { bb_fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1); bb_fprintf(out, "%s\n", s0); } free((void *)s0); } } while (s1); bb_xferror(in, input_filename); bb_fflush_stdout_and_exit(EXIT_SUCCESS); }