| 1 | static char *sccsid = "@(#)uniq.c 4.1 (Berkeley) %G%"; |
| 2 | /* |
| 3 | * Deal with duplicated lines in a file |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <ctype.h> |
| 7 | int fields; |
| 8 | int letters; |
| 9 | int linec; |
| 10 | char mode; |
| 11 | int uniq; |
| 12 | char *skip(); |
| 13 | |
| 14 | main(argc, argv) |
| 15 | int argc; |
| 16 | char *argv[]; |
| 17 | { |
| 18 | static char b1[1000], b2[1000]; |
| 19 | |
| 20 | while(argc > 1) { |
| 21 | if(*argv[1] == '-') { |
| 22 | if (isdigit(argv[1][1])) |
| 23 | fields = atoi(&argv[1][1]); |
| 24 | else mode = argv[1][1]; |
| 25 | argc--; |
| 26 | argv++; |
| 27 | continue; |
| 28 | } |
| 29 | if(*argv[1] == '+') { |
| 30 | letters = atoi(&argv[1][1]); |
| 31 | argc--; |
| 32 | argv++; |
| 33 | continue; |
| 34 | } |
| 35 | if (freopen(argv[1], "r", stdin) == NULL) |
| 36 | printe("cannot open %s\n", argv[1]); |
| 37 | break; |
| 38 | } |
| 39 | if(argc > 2 && freopen(argv[2], "w", stdout) == NULL) |
| 40 | printe("cannot create %s\n", argv[2]); |
| 41 | |
| 42 | if(gline(b1)) |
| 43 | exit(0); |
| 44 | for(;;) { |
| 45 | linec++; |
| 46 | if(gline(b2)) { |
| 47 | pline(b1); |
| 48 | exit(0); |
| 49 | } |
| 50 | if(!equal(b1, b2)) { |
| 51 | pline(b1); |
| 52 | linec = 0; |
| 53 | do { |
| 54 | linec++; |
| 55 | if(gline(b1)) { |
| 56 | pline(b2); |
| 57 | exit(0); |
| 58 | } |
| 59 | } while(equal(b1, b2)); |
| 60 | pline(b2); |
| 61 | linec = 0; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | gline(buf) |
| 67 | register char buf[]; |
| 68 | { |
| 69 | register c; |
| 70 | |
| 71 | while((c = getchar()) != '\n') { |
| 72 | if(c == EOF) |
| 73 | return(1); |
| 74 | *buf++ = c; |
| 75 | } |
| 76 | *buf = 0; |
| 77 | return(0); |
| 78 | } |
| 79 | |
| 80 | pline(buf) |
| 81 | register char buf[]; |
| 82 | { |
| 83 | |
| 84 | switch(mode) { |
| 85 | |
| 86 | case 'u': |
| 87 | if(uniq) { |
| 88 | uniq = 0; |
| 89 | return; |
| 90 | } |
| 91 | break; |
| 92 | |
| 93 | case 'd': |
| 94 | if(uniq) break; |
| 95 | return; |
| 96 | |
| 97 | case 'c': |
| 98 | printf("%4d ", linec); |
| 99 | } |
| 100 | uniq = 0; |
| 101 | fputs(buf, stdout); |
| 102 | putchar('\n'); |
| 103 | } |
| 104 | |
| 105 | equal(b1, b2) |
| 106 | register char b1[], b2[]; |
| 107 | { |
| 108 | register char c; |
| 109 | |
| 110 | b1 = skip(b1); |
| 111 | b2 = skip(b2); |
| 112 | while((c = *b1++) != 0) |
| 113 | if(c != *b2++) return(0); |
| 114 | if(*b2 != 0) |
| 115 | return(0); |
| 116 | uniq++; |
| 117 | return(1); |
| 118 | } |
| 119 | |
| 120 | char * |
| 121 | skip(s) |
| 122 | register char *s; |
| 123 | { |
| 124 | register nf, nl; |
| 125 | |
| 126 | nf = nl = 0; |
| 127 | while(nf++ < fields) { |
| 128 | while(*s == ' ' || *s == '\t') |
| 129 | s++; |
| 130 | while( !(*s == ' ' || *s == '\t' || *s == 0) ) |
| 131 | s++; |
| 132 | } |
| 133 | while(nl++ < letters && *s != 0) |
| 134 | s++; |
| 135 | return(s); |
| 136 | } |
| 137 | |
| 138 | printe(p,s) |
| 139 | char *p,*s; |
| 140 | { |
| 141 | fprintf(stderr, p, s); |
| 142 | exit(1); |
| 143 | } |