/* Name : UNIQ.C Date : 13 December, 1989 Author : Kim Moser System : Microsoft C 5.1 Descrip: Writes either unique or non-unique lines from stdin. Assumes lines are < MAX chars. */ #include #include #include #include #define MAX_LINE_LEN 2048 /* Longest line allowed */ static char BUF[MAX_LINE_LEN+1] = {"\0"}, PREV[MAX_LINE_LEN+1] = {"\0"}; static int DUPES = 0; /* Whether to print duplicate lines only */ static int ONCE = 0; static int COLUMN = -1; /* Set at runtime */ static int WIDTH = -1; /* Set at runtime */ static void usage(void); static void usage(void) { fputs("UNIQ v1.0 Copyright (c) 1989 by Kim Moser All Rights Reserved\n\ Usage: UNIQ [-o|-d -c -w]\n\n\ No switches: writes lines which differ from previous line (strips dupes).\n\ -o writes only lines which are unique.\n\ -d writes one copy of only duplicate lines (strips uniques).\n\ -c specifies which column to compare (default=1)\n\ -w specifies maximum characters to compare (default=entire line)\n\ ", stderr ); exit(-1); } static void parse(char *s); static void parse(char *s) { if (s[0]=='-') { switch(toupper(s[1])) { case 'D': DUPES = 1; break; case 'O': ONCE = 1; break; case 'C': if ((COLUMN = atoi(s+2)) < 0) { usage(); } COLUMN--; /* Make it offset from 0 */ break; case 'W': if ((WIDTH = atoi(s+2)) <= 0) { usage(); } break; default: usage(); } } else { usage(); } } static void uniq(void); static void uniq(void) { while (!feof(stdin)) { strcpy(PREV, BUF); if (gets(BUF) == NULL) break; if (strncmp(strlen(BUF) > COLUMN ? BUF+COLUMN : "", strlen(PREV) > COLUMN ? PREV+COLUMN : "", WIDTH)) puts(BUF); } } static void once(void); static void once(void) { int sameasprev=0; int first=1; while (!feof(stdin)) { strcpy(PREV, BUF); gets(BUF); if (strncmp(strlen(BUF) > COLUMN ? BUF+COLUMN : "", strlen(PREV) > COLUMN ? PREV+COLUMN : "", WIDTH)) { /* They're different */ if (!sameasprev) { if (!first) puts(PREV); } sameasprev = 0; } else { /* They're the same */ sameasprev = 1; } first = 0; } } static void dupes(void); static void dupes(void) { int thisgroup=0; /* Whether one of these identical lines was printed */ while (!feof(stdin)) { strcpy(PREV, BUF); if (gets(BUF) == NULL) break; if (strncmp(strlen(BUF) > COLUMN ? BUF+COLUMN : "", strlen(PREV) > COLUMN ? PREV+COLUMN : "", WIDTH)) { thisgroup = 0; } else { if (!thisgroup) { thisgroup = 1; puts(BUF); } } } } void main(int argc, char **argv) { int i; for (i=1; i