* Copyright (c) 1989 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)uniq.c 5.3 (Berkeley) %G%";
int numchars
, numfields
, repeats
;
#define MAXLINELEN (2048 + 1)
FILE *ifp
, *ofp
, *file();
char *prevline
, *thisline
, *malloc(), *skip();
while ((ch
= getopt(argc
, argv
, "-cdu123456789")) != EOF
)
* since -n is a valid option that could be picked up by
* getopt, but is better handled by the +n and -n code, we
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
/* if no flags are set, default is -d -u */
} else if (!dflag
&& !uflag
)
/* because of the +, getopt is messed up */
for (; argc
> 0 && (**argv
== '+' || **argv
== '-'); ++argv
, --argc
)
if ((numchars
= atoi(*argv
+ 1)) < 0)
if ((numfields
= atoi(*argv
+ 1)) < 0) {
negerr
: (void)fprintf(stderr
,
"uniq: negative field/char skip value.\n");
ifp
= file(argv
[0], "r");
ifp
= file(argv
[0], "r");
ofp
= file(argv
[1], "w");
prevline
= malloc(MAXLINELEN
);
thisline
= malloc(MAXLINELEN
);
(void)fgets(prevline
, MAXLINELEN
, ifp
);
while (fgets(thisline
, MAXLINELEN
, ifp
)) {
/* if requested get the chosen fields + character offsets */
if (numfields
|| numchars
) {
/* if different, print; set previous to new value */
* output a line depending on the flags and number of repetitions
(void)fprintf(ofp
, "%4d %s", repeats
+ 1, str
);
if (dflag
&& repeats
|| uflag
&& !repeats
)
(void)fprintf(ofp
, "%s", str
);
register int infield
, nchars
, nfields
;
for (nfields
= numfields
, infield
= 0; nfields
&& *str
; ++str
)
for (nchars
= numchars
; nchars
-- && *str
; ++str
);
if (!(fp
= fopen(name
, mode
))) {
(void)fprintf(stderr
, "uniq: can't open %s.\n", name
);
"usage: uniq [-c | -du] [- #fields] [+ #chars] [input [output]]\n");