1 /* uniq - compact repeated lines Author: John Woods */
2 /* Uniq [-udc] [-n] [+n] [infile [outfile]]
4 * Written 02/08/86 by John Woods, placed into public domain. Enjoy.
8 /* If the symbol WRITE_ERROR is defined, uniq will exit(1) if it gets a
9 * write error on the output. This is not (of course) how V7 uniq does it,
10 * so undefine the symbol if you want to lose your output to a full disk
21 int uflag
= 1; /* default is union of -d and -u outputs */
22 int dflag
= 1; /* flags are mutually exclusive */
27 _PROTOTYPE(int main
, (int argc
, char **argv
));
28 _PROTOTYPE(FILE *xfopen
, (char *fn
, char *mode
));
29 _PROTOTYPE(char *skip
, (char *s
));
30 _PROTOTYPE(int equal
, (char *s1
, char *s2
));
31 _PROTOTYPE(void show
, (char *line
, int count
));
32 _PROTOTYPE(int uniq
, (void));
33 _PROTOTYPE(void usage
, (void));
34 _PROTOTYPE(int getline
, (char *buf
, int count
));
36 FILE *xfopen(fn
, mode
)
41 if ((p
= fopen(fn
, mode
)) == NULL
) {
56 setbuf(stdout
, buffer
);
57 for (--argc
, ++argv
; argc
> 0 && (**argv
== '-' || **argv
== '+');
60 chars
= atoi(*argv
+ 1);
61 else if (isdigit(argv
[0][1]))
62 fields
= atoi(*argv
+ 1);
63 else if (argv
[0][1] == '\0')
64 inf
= 0; /* - is stdin */
66 for (p
= *argv
+ 1; *p
; p
++) {
76 case 'c': cflag
= 1; break;
85 else if (inf
== -1) { /* if - was not given */
109 for (n
= fields
; n
> 0; --n
) {
111 while (*s
&& (*s
== ' ' || *s
== '\t')) s
++;
113 while (*s
&& (*s
!= ' ' && *s
!= '\t')) s
++;
117 /* Skip characters */
118 for (n
= chars
; n
> 0; --n
) {
128 return !strcmp(skip(s1
), skip(s2
));
131 void show(line
, count
)
136 printf("%4d %s", count
, line
);
138 if ((uflag
&& count
== 1) || (dflag
&& count
!= 1))
143 /* The meat of the whole affair */
144 char *nowline
, *prevline
, buf1
[1024], buf2
[1024];
153 if (getline(prevline
, 1024) < 0) return(0);
157 /* Get nowline and compare if not equal, dump prevline and swap
158 * pointers else continue, bumping seen count */
159 while (getline(nowline
, 1024) > 0) {
160 if (!equal(prevline
, nowline
)) {
161 show(prevline
, seen
);
169 show(prevline
, seen
);
175 fprintf(stderr
, "Usage: uniq [-udc] [+n] [-n] [input [output]]\n");
178 int getline(buf
, count
)
185 while (ct
++ < count
) {
187 if (c
< 0) return(-1);