Home | History | Annotate | Line # | Download | only in uniq
uniq.c revision 1.6
      1  1.6      jtc /*	$NetBSD: uniq.c,v 1.6 1994/12/06 07:51:17 jtc Exp $	*/
      2  1.6      jtc 
      3  1.1      cgd /*
      4  1.6      jtc  * Copyright (c) 1989, 1993
      5  1.6      jtc  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * This code is derived from software contributed to Berkeley by
      8  1.1      cgd  * Case Larsen.
      9  1.1      cgd  *
     10  1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1      cgd  * modification, are permitted provided that the following conditions
     12  1.1      cgd  * are met:
     13  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1      cgd  *    must display the following acknowledgement:
     20  1.1      cgd  *	This product includes software developed by the University of
     21  1.1      cgd  *	California, Berkeley and its contributors.
     22  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1      cgd  *    may be used to endorse or promote products derived from this software
     24  1.1      cgd  *    without specific prior written permission.
     25  1.1      cgd  *
     26  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1      cgd  * SUCH DAMAGE.
     37  1.1      cgd  */
     38  1.1      cgd 
     39  1.1      cgd #ifndef lint
     40  1.6      jtc static char copyright[] =
     41  1.6      jtc "@(#) Copyright (c) 1989, 1993\n\
     42  1.6      jtc 	The Regents of the University of California.  All rights reserved.\n";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd #ifndef lint
     46  1.6      jtc #if 0
     47  1.6      jtc static char sccsid[] = "@(#)uniq.c	8.1 (Berkeley) 6/6/93";
     48  1.6      jtc #endif
     49  1.6      jtc static char rcsid[] = "$NetBSD: uniq.c,v 1.6 1994/12/06 07:51:17 jtc Exp $";
     50  1.1      cgd #endif /* not lint */
     51  1.1      cgd 
     52  1.4  mycroft #include <errno.h>
     53  1.1      cgd #include <stdio.h>
     54  1.1      cgd #include <ctype.h>
     55  1.4  mycroft #include <stdlib.h>
     56  1.4  mycroft #include <string.h>
     57  1.1      cgd 
     58  1.4  mycroft #define	MAXLINELEN	(8 * 1024)
     59  1.4  mycroft 
     60  1.1      cgd int cflag, dflag, uflag;
     61  1.1      cgd int numchars, numfields, repeats;
     62  1.1      cgd 
     63  1.4  mycroft void	 err __P((const char *, ...));
     64  1.4  mycroft FILE	*file __P((char *, char *));
     65  1.4  mycroft void	 show __P((FILE *, char *));
     66  1.4  mycroft char	*skip __P((char *));
     67  1.4  mycroft void	 obsolete __P((char *[]));
     68  1.4  mycroft void	 usage __P((void));
     69  1.1      cgd 
     70  1.4  mycroft int
     71  1.4  mycroft main (argc, argv)
     72  1.1      cgd 	int argc;
     73  1.4  mycroft 	char *argv[];
     74  1.1      cgd {
     75  1.4  mycroft 	register char *t1, *t2;
     76  1.4  mycroft 	FILE *ifp, *ofp;
     77  1.1      cgd 	int ch;
     78  1.4  mycroft 	char *prevline, *thisline, *p;
     79  1.1      cgd 
     80  1.4  mycroft 	obsolete(argv);
     81  1.4  mycroft 	while ((ch = getopt(argc, argv, "-cdf:s:u")) != EOF)
     82  1.1      cgd 		switch (ch) {
     83  1.1      cgd 		case '-':
     84  1.1      cgd 			--optind;
     85  1.1      cgd 			goto done;
     86  1.1      cgd 		case 'c':
     87  1.1      cgd 			cflag = 1;
     88  1.1      cgd 			break;
     89  1.1      cgd 		case 'd':
     90  1.1      cgd 			dflag = 1;
     91  1.1      cgd 			break;
     92  1.4  mycroft 		case 'f':
     93  1.4  mycroft 			numfields = strtol(optarg, &p, 10);
     94  1.4  mycroft 			if (numfields < 0 || *p)
     95  1.4  mycroft 				err("illegal field skip value: %s", optarg);
     96  1.4  mycroft 			break;
     97  1.4  mycroft 		case 's':
     98  1.4  mycroft 			numchars = strtol(optarg, &p, 10);
     99  1.4  mycroft 			if (numchars < 0 || *p)
    100  1.4  mycroft 				err("illegal character skip value: %s", optarg);
    101  1.4  mycroft 			break;
    102  1.1      cgd 		case 'u':
    103  1.1      cgd 			uflag = 1;
    104  1.1      cgd 			break;
    105  1.1      cgd 		case '?':
    106  1.1      cgd 		default:
    107  1.1      cgd 			usage();
    108  1.1      cgd 	}
    109  1.1      cgd 
    110  1.1      cgd done:	argc -= optind;
    111  1.1      cgd 	argv +=optind;
    112  1.1      cgd 
    113  1.4  mycroft 	/* If no flags are set, default is -d -u. */
    114  1.1      cgd 	if (cflag) {
    115  1.1      cgd 		if (dflag || uflag)
    116  1.1      cgd 			usage();
    117  1.1      cgd 	} else if (!dflag && !uflag)
    118  1.1      cgd 		dflag = uflag = 1;
    119  1.1      cgd 
    120  1.1      cgd 	switch(argc) {
    121  1.1      cgd 	case 0:
    122  1.1      cgd 		ifp = stdin;
    123  1.1      cgd 		ofp = stdout;
    124  1.1      cgd 		break;
    125  1.1      cgd 	case 1:
    126  1.1      cgd 		ifp = file(argv[0], "r");
    127  1.1      cgd 		ofp = stdout;
    128  1.1      cgd 		break;
    129  1.1      cgd 	case 2:
    130  1.1      cgd 		ifp = file(argv[0], "r");
    131  1.1      cgd 		ofp = file(argv[1], "w");
    132  1.1      cgd 		break;
    133  1.1      cgd 	default:
    134  1.1      cgd 		usage();
    135  1.1      cgd 	}
    136  1.1      cgd 
    137  1.1      cgd 	prevline = malloc(MAXLINELEN);
    138  1.1      cgd 	thisline = malloc(MAXLINELEN);
    139  1.6      jtc 	if (prevline == NULL || thisline == NULL)
    140  1.6      jtc 		err("%s", strerror(errno));
    141  1.6      jtc 
    142  1.6      jtc 	if (fgets(prevline, MAXLINELEN, ifp) == NULL)
    143  1.3  deraadt 		exit(0);
    144  1.1      cgd 
    145  1.1      cgd 	while (fgets(thisline, MAXLINELEN, ifp)) {
    146  1.4  mycroft 		/* If requested get the chosen fields + character offsets. */
    147  1.1      cgd 		if (numfields || numchars) {
    148  1.1      cgd 			t1 = skip(thisline);
    149  1.1      cgd 			t2 = skip(prevline);
    150  1.1      cgd 		} else {
    151  1.1      cgd 			t1 = thisline;
    152  1.1      cgd 			t2 = prevline;
    153  1.1      cgd 		}
    154  1.1      cgd 
    155  1.4  mycroft 		/* If different, print; set previous to new value. */
    156  1.1      cgd 		if (strcmp(t1, t2)) {
    157  1.1      cgd 			show(ofp, prevline);
    158  1.1      cgd 			t1 = prevline;
    159  1.1      cgd 			prevline = thisline;
    160  1.1      cgd 			thisline = t1;
    161  1.1      cgd 			repeats = 0;
    162  1.4  mycroft 		} else
    163  1.1      cgd 			++repeats;
    164  1.1      cgd 	}
    165  1.1      cgd 	show(ofp, prevline);
    166  1.1      cgd 	exit(0);
    167  1.1      cgd }
    168  1.1      cgd 
    169  1.1      cgd /*
    170  1.1      cgd  * show --
    171  1.4  mycroft  *	Output a line depending on the flags and number of repetitions
    172  1.1      cgd  *	of the line.
    173  1.1      cgd  */
    174  1.4  mycroft void
    175  1.1      cgd show(ofp, str)
    176  1.1      cgd 	FILE *ofp;
    177  1.1      cgd 	char *str;
    178  1.1      cgd {
    179  1.1      cgd 	if (cflag)
    180  1.1      cgd 		(void)fprintf(ofp, "%4d %s", repeats + 1, str);
    181  1.1      cgd 	if (dflag && repeats || uflag && !repeats)
    182  1.1      cgd 		(void)fprintf(ofp, "%s", str);
    183  1.1      cgd }
    184  1.1      cgd 
    185  1.1      cgd char *
    186  1.1      cgd skip(str)
    187  1.1      cgd 	register char *str;
    188  1.1      cgd {
    189  1.1      cgd 	register int infield, nchars, nfields;
    190  1.1      cgd 
    191  1.1      cgd 	for (nfields = numfields, infield = 0; nfields && *str; ++str)
    192  1.1      cgd 		if (isspace(*str)) {
    193  1.1      cgd 			if (infield) {
    194  1.1      cgd 				infield = 0;
    195  1.1      cgd 				--nfields;
    196  1.1      cgd 			}
    197  1.1      cgd 		} else if (!infield)
    198  1.1      cgd 			infield = 1;
    199  1.1      cgd 	for (nchars = numchars; nchars-- && *str; ++str);
    200  1.1      cgd 	return(str);
    201  1.1      cgd }
    202  1.1      cgd 
    203  1.1      cgd FILE *
    204  1.1      cgd file(name, mode)
    205  1.1      cgd 	char *name, *mode;
    206  1.1      cgd {
    207  1.1      cgd 	FILE *fp;
    208  1.1      cgd 
    209  1.4  mycroft 	if ((fp = fopen(name, mode)) == NULL)
    210  1.4  mycroft 		err("%s: %s", name, strerror(errno));
    211  1.4  mycroft 	return(fp);
    212  1.4  mycroft }
    213  1.4  mycroft 
    214  1.4  mycroft void
    215  1.4  mycroft obsolete(argv)
    216  1.4  mycroft 	char *argv[];
    217  1.4  mycroft {
    218  1.4  mycroft 	int len;
    219  1.4  mycroft 	char *ap, *p, *start;
    220  1.4  mycroft 
    221  1.4  mycroft 	while (ap = *++argv) {
    222  1.4  mycroft 		/* Return if "--" or not an option of any form. */
    223  1.4  mycroft 		if (ap[0] != '-') {
    224  1.4  mycroft 			if (ap[0] != '+')
    225  1.4  mycroft 				return;
    226  1.4  mycroft 		} else if (ap[1] == '-')
    227  1.4  mycroft 			return;
    228  1.4  mycroft 		if (!isdigit(ap[1]))
    229  1.4  mycroft 			continue;
    230  1.4  mycroft 		/*
    231  1.4  mycroft 		 * Digit signifies an old-style option.  Malloc space for dash,
    232  1.4  mycroft 		 * new option and argument.
    233  1.4  mycroft 		 */
    234  1.4  mycroft 		len = strlen(ap);
    235  1.4  mycroft 		if ((start = p = malloc(len + 3)) == NULL)
    236  1.4  mycroft 			err("%s", strerror(errno));
    237  1.4  mycroft 		*p++ = '-';
    238  1.4  mycroft 		*p++ = ap[0] == '+' ? 's' : 'f';
    239  1.4  mycroft 		(void)strcpy(p, ap + 1);
    240  1.4  mycroft 		*argv = start;
    241  1.1      cgd 	}
    242  1.1      cgd }
    243  1.1      cgd 
    244  1.4  mycroft void
    245  1.1      cgd usage()
    246  1.1      cgd {
    247  1.1      cgd 	(void)fprintf(stderr,
    248  1.4  mycroft 	    "usage: uniq [-c | -du] [-f fields] [-s chars] [input [output]]\n");
    249  1.4  mycroft 	exit(1);
    250  1.4  mycroft }
    251  1.4  mycroft 
    252  1.4  mycroft #if __STDC__
    253  1.4  mycroft #include <stdarg.h>
    254  1.4  mycroft #else
    255  1.4  mycroft #include <varargs.h>
    256  1.4  mycroft #endif
    257  1.4  mycroft 
    258  1.4  mycroft void
    259  1.4  mycroft #if __STDC__
    260  1.4  mycroft err(const char *fmt, ...)
    261  1.4  mycroft #else
    262  1.4  mycroft err(fmt, va_alist)
    263  1.4  mycroft 	char *fmt;
    264  1.4  mycroft         va_dcl
    265  1.4  mycroft #endif
    266  1.4  mycroft {
    267  1.4  mycroft 	va_list ap;
    268  1.4  mycroft #if __STDC__
    269  1.4  mycroft 	va_start(ap, fmt);
    270  1.4  mycroft #else
    271  1.4  mycroft 	va_start(ap);
    272  1.4  mycroft #endif
    273  1.4  mycroft 	(void)fprintf(stderr, "uniq: ");
    274  1.4  mycroft 	(void)vfprintf(stderr, fmt, ap);
    275  1.4  mycroft 	va_end(ap);
    276  1.4  mycroft 	(void)fprintf(stderr, "\n");
    277  1.1      cgd 	exit(1);
    278  1.4  mycroft 	/* NOTREACHED */
    279  1.1      cgd }
    280