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