-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathuniq.c
177 lines (154 loc) · 3.26 KB
/
uniq.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* uniq - compact repeated lines Author: John Woods */
/* Uniq [-udc] [-n] [+n] [infile [outfile]]
*
* Written 02/08/86 by John Woods, placed into public domain. Enjoy.
*
*/
/* If the symbol WRITE_ERROR is defined, uniq will exit(1) if it gets a
* write error on the output. This is not (of course) how V7 uniq does it,
* so undefine the symbol if you want to lose your output to a full disk
*/
#define WRITE_ERROR 1
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static char buffer[BUFSIZ];
static int uflag = 1; /* default is union of -d and -u outputs */
static int dflag = 1; /* flags are mutually exclusive */
static int cflag = 0;
static int fields = 0;
static int chars = 0;
/* The meat of the whole affair */
static char *nowline, *prevline, buf1[1024], buf2[1024];
static int ourgetline(char *buf, int count);
static FILE *xfopen(const char *fn, const char *mode)
{
FILE *p;
if ((p = fopen(fn, mode)) == NULL) {
perror("uniq");
fflush(stdout);
exit(1);
}
return(p);
}
static char *skip(char *s)
{
int n;
/* Skip fields */
for (n = fields; n > 0; --n) {
/* Skip blanks */
while (*s && (*s == ' ' || *s == '\t')) s++;
if (!*s) return s;
while (*s && (*s != ' ' && *s != '\t')) s++;
if (!*s) return s;
}
/* Skip characters */
for (n = chars; n > 0; --n) {
if (!*s) return s;
s++;
}
return s;
}
static int equal(char *s1, char *s2)
{
return !strcmp(skip(s1), skip(s2));
}
static void show(char *line, int count)
{
if (cflag)
printf("%4d %s", count, line);
else {
if ((uflag && count == 1) || (dflag && count != 1))
printf("%s", line);
}
}
static int uniq(void)
{
char *p;
int seen;
/* Setup */
prevline = buf1;
if (ourgetline(prevline, 1024) < 0) return(0);
seen = 1;
nowline = buf2;
/* Get nowline and compare if not equal, dump prevline and swap
* pointers else continue, bumping seen count */
while (ourgetline(nowline, 1024) > 0) {
if (!equal(prevline, nowline)) {
show(prevline, seen);
seen = 1;
p = nowline;
nowline = prevline;
prevline = p;
} else
seen += 1;
}
show(prevline, seen);
return 0;
}
static int ourgetline(char *buf, int count)
{
int c;
int ct = 0;
while (ct++ < count) {
c = getc(stdin);
if (c < 0) return(-1);
*buf++ = c;
if (c == '\n') {
*buf++ = 0;
return(ct);
}
}
return(ct);
}
int main(int argc, char **argv)
{
char *p;
int inf = -1;
setbuf(stdout, buffer);
for (--argc, ++argv; argc > 0 && (**argv == '-' || **argv == '+');
--argc, ++argv) {
if (**argv == '+')
chars = atoi(*argv + 1);
else if (isdigit(argv[0][1]))
fields = atoi(*argv + 1);
else if (argv[0][1] == '\0')
inf = 0; /* - is stdin */
else
for (p = *argv + 1; *p; p++) {
switch (*p) {
case 'd':
dflag = 1;
uflag = 0;
break;
case 'u':
uflag = 1;
dflag = 0;
break;
case 'c': cflag = 1; break;
default: goto usage;
}
}
}
/* Input file */
if (argc == 0)
inf = 0;
else if (inf == -1) { /* if - was not given */
fclose(stdin);
xfopen(*argv++, "r");
argc--;
}
if (argc > 0) {
fclose(stdout);
xfopen(*argv++, "w");
argc--;
}
uniq();
fflush(stdout);
exit(0);
usage:
fprintf(stderr, "Usage: uniq [-udc] [+n] [-n] [input [output]]\n");
exit(1);
}