-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathantiuniq.c
96 lines (88 loc) · 1.92 KB
/
antiuniq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#define MAX 65536
char matchbuf[MAX];
char buf[MAX];
int firstcmp, lastcmp;
docmp(char *s1, char *s2) {
int len1, len2, f, l;
if (firstcmp == -1 || lastcmp == -1)
return (strcmp(s1, s2));
len1 = strlen(s1);
len2 = strlen(s2);
if (len1 > len2)
len1 = len2;
if (firstcmp > len1)
f = len1;
else
f = firstcmp;
if (lastcmp > len1)
l = len1;
else
l = lastcmp;
return (memcmp(s1 + f - 1, s2 + f - 1, l - f + 1));
}
main(int ac, char **av) {
char *name;
FILE *fp;
int firstflag; /* nonzero if working on the first line */
int matchout; /* count of identical lines output so far */
int ch;
int sflag = 0; /* nonzero to print out single lines */
firstcmp = lastcmp = -1;
while ((ch = getopt(ac, av, "sf:l:")) != -1)
switch (ch) {
case 'f':
firstcmp = atoi(optarg);
break;
case 'l':
lastcmp = atoi(optarg);
break;
case 's':
sflag = 1;
break;
case '?':
default:
fprintf(stderr, "usage: antiuniq [-s] [-f <first>] [-l <last>] file ...\n");
exit(1);
}
if (firstcmp > 0 && lastcmp > 0 && firstcmp > lastcmp) {
fprintf(stderr, "antiuniq: %d is > %d\n", firstcmp, lastcmp);
exit(1);
}
ac -= optind;
av += optind;
while (name = *av++) {
if ((fp = fopen(name, "r")) == (FILE *)0) {
fprintf(stderr, "antiuniq: can't open \"%s\"\n", name);
continue;
}
#ifndef __linux
setproctitle("processing - %s", name);
#endif
buf[0] = matchbuf[0] = '\0';
firstflag = 1;
matchout = 0;
while (fgets(buf, sizeof buf, fp)) {
if (matchbuf[0] == '\0') {
strcpy(matchbuf, buf);
} else if (docmp(matchbuf, buf) == 0) {
fputs(buf, stdout);
++matchout;
} else {
if (matchout == 0 && sflag)
fputs(buf, stdout);
strcpy(matchbuf, buf);
matchout = 0;
}
firstflag = 0;
}
if (matchout == 0 && sflag)
fputs(buf, stdout);
fclose(fp);
}
exit(0);
}