-static const char uniq_usage[] =
- "uniq [OPTION]... [INPUT [OUTPUT]]\n\n"
- "Discard all but one of successive identical lines from INPUT\n"
- "(or standard input), writing to OUTPUT (or standard output).\n";
-
-/* max chars in line */
-#define UNIQ_MAX 4096
-
-typedef void (Print) (FILE *, const char *);
-
-typedef int (Decide) (const char *, const char *);
-
-/* container for two lines to be compared */
-typedef struct {
- char *a;
- char *b;
- int recurrence;
- FILE *in;
- FILE *out;
- void *func;
-} Subject;
-
-/* set up all the variables of a uniq operation */
-static Subject *subject_init(Subject * self, FILE * in, FILE * out,
- void *func)
-{
- self->a = NULL;
- self->b = NULL;
- self->in = in;
- self->out = out;
- self->func = func;
- self->recurrence = 0;
- return self;
-}
-
-/* point a and b to the appropriate lines;
- * count the recurrences (if any) of a string;
- */
-static Subject *subject_next(Subject * self)
-{
- /* tmp line holders */
- static char line[2][UNIQ_MAX];
- static int alternator = 0;
-
- if (fgets(line[alternator], UNIQ_MAX, self->in)) {
- self->a = self->b;
- self->b = line[alternator];
- alternator ^= 1;
- return self;
- }
-
- return NULL;
-}
-
-static Subject *subject_last(Subject * self)
-{
- self->a = self->b;
- self->b = NULL;
- return self;
-}
-
-static Subject *subject_study(Subject * self)
-{
- if (self->a == NULL) {
- return self;
- }
- if (self->b == NULL) {
- fprintf(self->out, "%s", self->a);
- return self;
- }
- if (strcmp(self->a, self->b) == 0) {
- self->recurrence++;
- } else {
- fprintf(self->out, "%s", self->a);
- self->recurrence = 0;
- }
- return self;
-}