diff options
author | Jim Meyering <jim@meyering.net> | 1995-10-30 21:00:36 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1995-10-30 21:00:36 +0000 |
commit | 1e30b2f8c4b0ccb5597cd31aca1b51e6951a3bdf (patch) | |
tree | 5467bdd4b3cf63748e85668a006ba9f8dbca4673 /src | |
parent | 84df8be86408de0466357f5543ba866477036c77 (diff) | |
download | coreutils-1e30b2f8c4b0ccb5597cd31aca1b51e6951a3bdf.tar.xz |
Reorder functions to obviate forward dcls.
Remove forward dcls.
Diffstat (limited to 'src')
-rw-r--r-- | src/uniq.c | 356 | ||||
-rw-r--r-- | src/wc.c | 208 |
2 files changed, 277 insertions, 287 deletions
diff --git a/src/uniq.c b/src/uniq.c index 7c4aa1c0d..5cb8c59b7 100644 --- a/src/uniq.c +++ b/src/uniq.c @@ -34,12 +34,6 @@ #undef min #define min(x, y) ((x) < (y) ? (x) : (y)) -static char *find_field (); -static int different (); -static void check_file (); -static void usage (); -static void writeline (); - /* The name this program was run with. */ char *program_name; @@ -90,103 +84,117 @@ static struct option const longopts[] = {"version", no_argument, &show_version, 1}, {NULL, 0, NULL, 0} }; - -void -main (argc, argv) - int argc; - char *argv[]; -{ - int optc; - char *infile = "-", *outfile = "-"; - - program_name = argv[0]; - skip_chars = 0; - skip_fields = 0; - check_chars = 0; - mode = output_all; - countmode = count_none; - while ((optc = getopt_long (argc, argv, "0123456789cdf:s:uw:", longopts, - (int *) 0)) != EOF) +static void +usage (status) + int status; +{ + if (status != 0) + fprintf (stderr, _("Try `%s --help' for more information.\n"), + program_name); + else { - switch (optc) - { - case 0: - break; - - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - skip_fields = skip_fields * 10 + optc - '0'; - break; - - case 'c': - countmode = count_occurrences; - break; - - case 'd': - mode = output_repeated; - break; + printf (_("\ +Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\ +"), + program_name); + printf (_("\ +Discard all but one of successive identical lines from INPUT (or\n\ +standard input), writing to OUTPUT (or standard output).\n\ +\n\ + -c, --count prefix lines by the number of occurrences\n\ + -d, --repeated only print duplicate lines\n\ + -f, --skip-fields=N avoid comparing the N first fields\n\ + -s, --skip-chars=N avoid comparing the N first characters\n\ + -u, --unique only print unique lines\n\ + -w, --check-chars=N compare no more than N characters in lines\n\ + -N same as -f N\n\ + +N same as -s N\n\ + --help display this help and exit\n\ + --version output version information and exit\n\ +\n\ +A field is a run of whitespace, than non-whitespace characters.\n\ +Fields are skipped before chars. \n\ +")); + } + exit (status); +} - case 'f': /* Like '-#'. */ - skip_fields = atoi (optarg); - break; +/* Given a linebuffer LINE, + return a pointer to the beginning of the line's field to be compared. */ - case 's': /* Like '+#'. */ - skip_chars = atoi (optarg); - break; +static char * +find_field (line) + struct linebuffer *line; +{ + register int count; + register char *lp = line->buffer; + register int size = line->length; + register int i = 0; - case 'u': - mode = output_unique; - break; + for (count = 0; count < skip_fields && i < size; count++) + { + while (i < size && ISBLANK (lp[i])) + i++; + while (i < size && !ISBLANK (lp[i])) + i++; + } - case 'w': - check_chars = atoi (optarg); - break; + for (count = 0; count < skip_chars && i < size; count++) + i++; - default: - usage (1); - } - } + return lp + i; +} - if (show_version) - { - printf ("uniq - %s\n", version_string); - exit (0); - } +/* Return zero if two strings OLD and NEW match, nonzero if not. + OLD and NEW point not to the beginnings of the lines + but rather to the beginnings of the fields to compare. + OLDLEN and NEWLEN are their lengths. */ - if (show_help) - usage (0); +static int +different (old, new, oldlen, newlen) + char *old; + char *new; + int oldlen; + int newlen; +{ + register int order; - if (optind >= 2 && strcmp (argv[optind - 1], "--") != 0) + if (check_chars) { - /* Interpret non-option arguments with leading `+' only - if we haven't seen `--'. */ - while (optind < argc && argv[optind][0] == '+') - skip_chars = atoi (argv[optind++]); + if (oldlen > check_chars) + oldlen = check_chars; + if (newlen > check_chars) + newlen = check_chars; } + order = memcmp (old, new, min (oldlen, newlen)); + if (order == 0) + return oldlen - newlen; + return order; +} + +/* Output the line in linebuffer LINE to stream STREAM + provided that the switches say it should be output. + If requested, print the number of times it occurred, as well; + LINECOUNT + 1 is the number of times that the line occurred. */ - if (optind < argc) - infile = argv[optind++]; - - if (optind < argc) - outfile = argv[optind++]; - - if (optind < argc) - usage (1); /* Extra arguments. */ +static void +writeline (line, stream, linecount) + struct linebuffer *line; + FILE *stream; + int linecount; +{ + if ((mode == output_unique && linecount != 0) + || (mode == output_repeated && linecount == 0)) + return; - check_file (infile, outfile); + if (countmode == count_occurrences) + fprintf (stream, "%7d\t", linecount + 1); - exit (0); + fwrite (line->buffer, sizeof (char), line->length, stream); + putc ('\n', stream); } - + /* Process input file INFILE with output to OUTFILE. If either is "-", use the standard I/O stream for it instead. */ @@ -260,113 +268,99 @@ check_file (infile, outfile) free (lb1.buffer); free (lb2.buffer); } - -/* Given a linebuffer LINE, - return a pointer to the beginning of the line's field to be compared. */ -static char * -find_field (line) - struct linebuffer *line; +void +main (argc, argv) + int argc; + char *argv[]; { - register int count; - register char *lp = line->buffer; - register int size = line->length; - register int i = 0; + int optc; + char *infile = "-", *outfile = "-"; - for (count = 0; count < skip_fields && i < size; count++) + program_name = argv[0]; + skip_chars = 0; + skip_fields = 0; + check_chars = 0; + mode = output_all; + countmode = count_none; + + while ((optc = getopt_long (argc, argv, "0123456789cdf:s:uw:", longopts, + (int *) 0)) != EOF) { - while (i < size && ISBLANK (lp[i])) - i++; - while (i < size && !ISBLANK (lp[i])) - i++; - } + switch (optc) + { + case 0: + break; - for (count = 0; count < skip_chars && i < size; count++) - i++; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + skip_fields = skip_fields * 10 + optc - '0'; + break; - return lp + i; -} + case 'c': + countmode = count_occurrences; + break; -/* Return zero if two strings OLD and NEW match, nonzero if not. - OLD and NEW point not to the beginnings of the lines - but rather to the beginnings of the fields to compare. - OLDLEN and NEWLEN are their lengths. */ + case 'd': + mode = output_repeated; + break; -static int -different (old, new, oldlen, newlen) - char *old; - char *new; - int oldlen; - int newlen; -{ - register int order; + case 'f': /* Like '-#'. */ + skip_fields = atoi (optarg); + break; - if (check_chars) - { - if (oldlen > check_chars) - oldlen = check_chars; - if (newlen > check_chars) - newlen = check_chars; + case 's': /* Like '+#'. */ + skip_chars = atoi (optarg); + break; + + case 'u': + mode = output_unique; + break; + + case 'w': + check_chars = atoi (optarg); + break; + + default: + usage (1); + } } - order = memcmp (old, new, min (oldlen, newlen)); - if (order == 0) - return oldlen - newlen; - return order; -} - -/* Output the line in linebuffer LINE to stream STREAM - provided that the switches say it should be output. - If requested, print the number of times it occurred, as well; - LINECOUNT + 1 is the number of times that the line occurred. */ -static void -writeline (line, stream, linecount) - struct linebuffer *line; - FILE *stream; - int linecount; -{ - if ((mode == output_unique && linecount != 0) - || (mode == output_repeated && linecount == 0)) - return; + if (show_version) + { + printf ("uniq - %s\n", version_string); + exit (0); + } - if (countmode == count_occurrences) - fprintf (stream, "%7d\t", linecount + 1); + if (show_help) + usage (0); - fwrite (line->buffer, sizeof (char), line->length, stream); - putc ('\n', stream); -} - -static void -usage (status) - int status; -{ - if (status != 0) - fprintf (stderr, _("Try `%s --help' for more information.\n"), - program_name); - else + if (optind >= 2 && strcmp (argv[optind - 1], "--") != 0) { - printf (_("\ -Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\ -"), - program_name); - printf (_("\ -Discard all but one of successive identical lines from INPUT (or\n\ -standard input), writing to OUTPUT (or standard output).\n\ -\n\ - -c, --count prefix lines by the number of occurrences\n\ - -d, --repeated only print duplicate lines\n\ - -f, --skip-fields=N avoid comparing the N first fields\n\ - -s, --skip-chars=N avoid comparing the N first characters\n\ - -u, --unique only print unique lines\n\ - -w, --check-chars=N compare no more than N characters in lines\n\ - -N same as -f N\n\ - +N same as -s N\n\ - --help display this help and exit\n\ - --version output version information and exit\n\ -\n\ -A field is a run of whitespace, than non-whitespace characters.\n\ -Fields are skipped before chars. \n\ -")); + /* Interpret non-option arguments with leading `+' only + if we haven't seen `--'. */ + while (optind < argc && argv[optind][0] == '+') + skip_chars = atoi (argv[optind++]); } - exit (status); + + if (optind < argc) + infile = argv[optind++]; + + if (optind < argc) + outfile = argv[optind++]; + + if (optind < argc) + usage (1); /* Extra arguments. */ + + check_file (infile, outfile); + + exit (0); } @@ -32,10 +32,6 @@ int safe_read (); -static void wc (); -static void wc_file (); -static void write_counts (); - /* The name this program was run with. */ char *program_name; @@ -95,100 +91,28 @@ read standard input.\n\ exit (status); } -void -main (argc, argv) - int argc; - char **argv; -{ - int optc; - int nfiles; - - program_name = argv[0]; - exit_status = 0; - print_lines = print_words = print_chars = 0; - total_lines = total_words = total_chars = 0; - - while ((optc = getopt_long (argc, argv, "clw", longopts, (int *) 0)) != EOF) - switch (optc) - { - case 0: - break; - - case 'c': - print_chars = 1; - break; - - case 'l': - print_lines = 1; - break; - - case 'w': - print_words = 1; - break; - - default: - usage (1); - } - - if (show_version) - { - printf ("wc - %s\n", version_string); - exit (0); - } - - if (show_help) - usage (0); - - if (print_lines + print_words + print_chars == 0) - print_lines = print_words = print_chars = 1; - - nfiles = argc - optind; - - if (nfiles == 0) - { - have_read_stdin = 1; - wc (0, ""); - } - else - { - for (; optind < argc; ++optind) - wc_file (argv[optind]); - - if (nfiles > 1) - write_counts (total_lines, total_words, total_chars, _("total")); - } - - if (have_read_stdin && close (0)) - error (1, errno, "-"); - - exit (exit_status); -} - static void -wc_file (file) +write_counts (lines, words, chars, file) + unsigned long lines, words, chars; char *file; { - if (!strcmp (file, "-")) + if (print_lines) + printf ("%7lu", lines); + if (print_words) { - have_read_stdin = 1; - wc (0, file); + if (print_lines) + putchar (' '); + printf ("%7lu", words); } - else + if (print_chars) { - int fd = open (file, O_RDONLY); - if (fd == -1) - { - error (0, errno, "%s", file); - exit_status = 1; - return; - } - wc (fd, file); - if (close (fd)) - { - error (0, errno, "%s", file); - exit_status = 1; - } + if (print_lines || print_words) + putchar (' '); + printf ("%7lu", chars); } + if (*file) + printf (" %s", file); + putchar ('\n'); } static void @@ -309,25 +233,97 @@ wc (fd, file) } static void -write_counts (lines, words, chars, file) - unsigned long lines, words, chars; +wc_file (file) char *file; { - if (print_lines) - printf ("%7lu", lines); - if (print_words) + if (!strcmp (file, "-")) { - if (print_lines) - putchar (' '); - printf ("%7lu", words); + have_read_stdin = 1; + wc (0, file); } - if (print_chars) + else { - if (print_lines || print_words) - putchar (' '); - printf ("%7lu", chars); + int fd = open (file, O_RDONLY); + if (fd == -1) + { + error (0, errno, "%s", file); + exit_status = 1; + return; + } + wc (fd, file); + if (close (fd)) + { + error (0, errno, "%s", file); + exit_status = 1; + } } - if (*file) - printf (" %s", file); - putchar ('\n'); +} + +void +main (argc, argv) + int argc; + char **argv; +{ + int optc; + int nfiles; + + program_name = argv[0]; + exit_status = 0; + print_lines = print_words = print_chars = 0; + total_lines = total_words = total_chars = 0; + + while ((optc = getopt_long (argc, argv, "clw", longopts, (int *) 0)) != EOF) + switch (optc) + { + case 0: + break; + + case 'c': + print_chars = 1; + break; + + case 'l': + print_lines = 1; + break; + + case 'w': + print_words = 1; + break; + + default: + usage (1); + } + + if (show_version) + { + printf ("wc - %s\n", version_string); + exit (0); + } + + if (show_help) + usage (0); + + if (print_lines + print_words + print_chars == 0) + print_lines = print_words = print_chars = 1; + + nfiles = argc - optind; + + if (nfiles == 0) + { + have_read_stdin = 1; + wc (0, ""); + } + else + { + for (; optind < argc; ++optind) + wc_file (argv[optind]); + + if (nfiles > 1) + write_counts (total_lines, total_words, total_chars, _("total")); + } + + if (have_read_stdin && close (0)) + error (1, errno, "-"); + + exit (exit_status); } |