summaryrefslogtreecommitdiff
path: root/src/uniq.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1995-10-30 21:00:36 +0000
committerJim Meyering <jim@meyering.net>1995-10-30 21:00:36 +0000
commit1e30b2f8c4b0ccb5597cd31aca1b51e6951a3bdf (patch)
tree5467bdd4b3cf63748e85668a006ba9f8dbca4673 /src/uniq.c
parent84df8be86408de0466357f5543ba866477036c77 (diff)
downloadcoreutils-1e30b2f8c4b0ccb5597cd31aca1b51e6951a3bdf.tar.xz
Reorder functions to obviate forward dcls.
Remove forward dcls.
Diffstat (limited to 'src/uniq.c')
-rw-r--r--src/uniq.c356
1 files changed, 175 insertions, 181 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);
}