* 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.4 (Berkeley) %G%";
#define MAXLINELEN (8 * 1024)
int numchars
, numfields
, repeats
;
void err
__P((const char *, ...));
FILE *file
__P((char *, char *));
void show
__P((FILE *, char *));
char *skip
__P((char *));
void obsolete
__P((char *[]));
char *prevline
, *thisline
, *p
;
while ((ch
= getopt(argc
, argv
, "-cdf:s:u")) != EOF
)
numfields
= strtol(optarg
, &p
, 10);
err("illegal field skip value: %s", optarg
);
numchars
= strtol(optarg
, &p
, 10);
err("illegal character skip value: %s", optarg
);
/* If no flags are set, default is -d -u. */
} else if (!dflag
&& !uflag
)
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
)) == NULL
)
err("%s: %s", name
, strerror(errno
));
/* Return if "--" or not an option of any form. */
* Digit signifies an old-style option. Malloc space for dash,
* new option and argument.
if ((start
= p
= malloc(len
+ 3)) == NULL
)
err("%s", strerror(errno
));
*p
++ = ap
[0] == '+' ? 's' : 'f';
"usage: uniq [-c | -du] [-f fields] [-s chars] [input [output]]\n");
err(const char *fmt
, ...)
(void)fprintf(stderr
, "uniq: ");
(void)vfprintf(stderr
, fmt
, ap
);
(void)fprintf(stderr
, "\n");