Home | History | Annotate | Line # | Download | only in uniq
      1  1.22  christos /*	$NetBSD: uniq.c,v 1.22 2019/04/23 17:35:10 christos 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.11       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35   1.8     lukem #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.15     lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
     38  1.15     lukem  The Regents of the University of California.  All rights reserved.");
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #ifndef lint
     42   1.6       jtc #if 0
     43   1.7       jtc static char sccsid[] = "@(#)uniq.c	8.3 (Berkeley) 5/4/95";
     44   1.6       jtc #endif
     45  1.22  christos __RCSID("$NetBSD: uniq.c,v 1.22 2019/04/23 17:35:10 christos Exp $");
     46   1.1       cgd #endif /* not lint */
     47   1.1       cgd 
     48   1.8     lukem #include <err.h>
     49   1.4   mycroft #include <errno.h>
     50   1.1       cgd #include <stdio.h>
     51   1.1       cgd #include <ctype.h>
     52   1.4   mycroft #include <stdlib.h>
     53   1.4   mycroft #include <string.h>
     54   1.7       jtc #include <unistd.h>
     55   1.1       cgd 
     56  1.12  christos static int cflag, dflag, uflag;
     57  1.12  christos static int numchars, numfields, repeats;
     58   1.1       cgd 
     59  1.12  christos static FILE *file(const char *, const char *);
     60  1.12  christos static void show(FILE *, const char *);
     61  1.19   abhinav static const char *skip(const char *, size_t *);
     62  1.12  christos static void obsolete(char *[]);
     63  1.14     perry static void usage(void) __dead;
     64   1.1       cgd 
     65   1.4   mycroft int
     66  1.12  christos main (int argc, char *argv[])
     67   1.1       cgd {
     68  1.20   abhinav 	const char *prevp, *thisp;
     69   1.4   mycroft 	FILE *ifp, *ofp;
     70   1.1       cgd 	int ch;
     71   1.4   mycroft 	char *prevline, *thisline, *p;
     72  1.13  christos 	size_t prevlinesize, thislinesize, psize;
     73  1.20   abhinav 	size_t prevlen, thislen;
     74   1.1       cgd 
     75  1.12  christos 	setprogname(argv[0]);
     76   1.8     lukem 	ifp = ofp = NULL;
     77   1.4   mycroft 	obsolete(argv);
     78  1.21       uwe 	while ((ch = getopt(argc, argv, "cdf:s:u")) != -1)
     79   1.1       cgd 		switch (ch) {
     80   1.1       cgd 		case 'c':
     81   1.1       cgd 			cflag = 1;
     82   1.1       cgd 			break;
     83   1.1       cgd 		case 'd':
     84   1.1       cgd 			dflag = 1;
     85   1.1       cgd 			break;
     86   1.4   mycroft 		case 'f':
     87   1.4   mycroft 			numfields = strtol(optarg, &p, 10);
     88   1.4   mycroft 			if (numfields < 0 || *p)
     89   1.8     lukem 				errx(1, "illegal field skip value: %s", optarg);
     90   1.4   mycroft 			break;
     91   1.4   mycroft 		case 's':
     92   1.4   mycroft 			numchars = strtol(optarg, &p, 10);
     93   1.4   mycroft 			if (numchars < 0 || *p)
     94   1.8     lukem 				errx(1, "illegal character skip value: %s",
     95   1.8     lukem 				    optarg);
     96   1.4   mycroft 			break;
     97   1.1       cgd 		case 'u':
     98   1.1       cgd 			uflag = 1;
     99   1.1       cgd 			break;
    100   1.1       cgd 		case '?':
    101   1.1       cgd 		default:
    102   1.1       cgd 			usage();
    103   1.1       cgd 	}
    104   1.1       cgd 
    105  1.22  christos 	argc -= optind;
    106   1.1       cgd 	argv +=optind;
    107   1.1       cgd 
    108   1.1       cgd 	switch(argc) {
    109   1.1       cgd 	case 0:
    110   1.1       cgd 		ifp = stdin;
    111   1.1       cgd 		ofp = stdout;
    112   1.1       cgd 		break;
    113   1.1       cgd 	case 1:
    114   1.1       cgd 		ifp = file(argv[0], "r");
    115   1.1       cgd 		ofp = stdout;
    116   1.1       cgd 		break;
    117   1.1       cgd 	case 2:
    118   1.1       cgd 		ifp = file(argv[0], "r");
    119   1.1       cgd 		ofp = file(argv[1], "w");
    120   1.1       cgd 		break;
    121   1.1       cgd 	default:
    122   1.1       cgd 		usage();
    123   1.1       cgd 	}
    124   1.1       cgd 
    125  1.13  christos 	if ((p = fgetln(ifp, &psize)) == NULL)
    126  1.13  christos 		return 0;
    127  1.20   abhinav 	prevlinesize = prevlen = psize;
    128  1.13  christos 	if ((prevline = malloc(prevlinesize + 1)) == NULL)
    129  1.13  christos 		err(1, "malloc");
    130  1.13  christos 	(void)memcpy(prevline, p, prevlinesize);
    131  1.13  christos 	prevline[prevlinesize] = '\0';
    132  1.20   abhinav 
    133  1.20   abhinav 	if (numfields || numchars)
    134  1.20   abhinav 		prevp = skip(prevline, &prevlen);
    135  1.20   abhinav 	else
    136  1.20   abhinav 		prevp = prevline;
    137  1.13  christos 
    138  1.13  christos 	thislinesize = psize;
    139  1.13  christos 	if ((thisline = malloc(thislinesize + 1)) == NULL)
    140   1.8     lukem 		err(1, "malloc");
    141   1.6       jtc 
    142  1.13  christos 	while ((p = fgetln(ifp, &psize)) != NULL) {
    143  1.13  christos 		if (psize > thislinesize) {
    144  1.13  christos 			if ((thisline = realloc(thisline, psize + 1)) == NULL)
    145  1.13  christos 				err(1, "realloc");
    146  1.13  christos 			thislinesize = psize;
    147  1.13  christos 		}
    148  1.20   abhinav 		thislen = psize;
    149  1.13  christos 		(void)memcpy(thisline, p, psize);
    150  1.13  christos 		thisline[psize] = '\0';
    151   1.1       cgd 
    152   1.4   mycroft 		/* If requested get the chosen fields + character offsets. */
    153   1.1       cgd 		if (numfields || numchars) {
    154  1.20   abhinav 			thisp = skip(thisline, &thislen);
    155   1.1       cgd 		} else {
    156  1.20   abhinav 			thisp = thisline;
    157   1.1       cgd 		}
    158   1.1       cgd 
    159   1.4   mycroft 		/* If different, print; set previous to new value. */
    160  1.20   abhinav 		if (thislen != prevlen || strcmp(thisp, prevp)) {
    161  1.12  christos 			char *t;
    162  1.13  christos 			size_t ts;
    163  1.13  christos 
    164   1.1       cgd 			show(ofp, prevline);
    165  1.12  christos 			t = prevline;
    166   1.1       cgd 			prevline = thisline;
    167  1.12  christos 			thisline = t;
    168  1.13  christos 			ts = prevlinesize;
    169  1.13  christos 			prevlinesize = thislinesize;
    170  1.13  christos 			thislinesize = ts;
    171  1.20   abhinav 			prevp = thisp;
    172  1.20   abhinav 			prevlen = thislen;
    173   1.1       cgd 			repeats = 0;
    174   1.4   mycroft 		} else
    175   1.1       cgd 			++repeats;
    176   1.1       cgd 	}
    177   1.1       cgd 	show(ofp, prevline);
    178  1.13  christos 	free(prevline);
    179  1.13  christos 	free(thisline);
    180  1.12  christos 	return 0;
    181   1.1       cgd }
    182   1.1       cgd 
    183   1.1       cgd /*
    184   1.1       cgd  * show --
    185   1.4   mycroft  *	Output a line depending on the flags and number of repetitions
    186   1.1       cgd  *	of the line.
    187   1.1       cgd  */
    188  1.12  christos static void
    189  1.12  christos show(FILE *ofp, const char *str)
    190   1.1       cgd {
    191   1.7       jtc 
    192  1.16  dholland 	if ((dflag && repeats == 0) || (uflag && repeats > 0))
    193  1.16  dholland 		return;
    194  1.16  dholland 	if (cflag) {
    195   1.1       cgd 		(void)fprintf(ofp, "%4d %s", repeats + 1, str);
    196  1.16  dholland 	} else {
    197   1.1       cgd 		(void)fprintf(ofp, "%s", str);
    198  1.16  dholland 	}
    199   1.1       cgd }
    200   1.1       cgd 
    201  1.12  christos static const char *
    202  1.19   abhinav skip(const char *str, size_t *linesize)
    203   1.1       cgd {
    204   1.8     lukem 	int infield, nchars, nfields;
    205  1.19   abhinav 	size_t ls = *linesize;
    206   1.1       cgd 
    207  1.19   abhinav 	for (nfields = numfields, infield = 0; nfields && *str; ++str, --ls)
    208   1.9  christos 		if (isspace((unsigned char)*str)) {
    209   1.1       cgd 			if (infield) {
    210   1.1       cgd 				infield = 0;
    211   1.1       cgd 				--nfields;
    212   1.1       cgd 			}
    213   1.1       cgd 		} else if (!infield)
    214   1.1       cgd 			infield = 1;
    215  1.19   abhinav 	for (nchars = numchars; nchars-- && *str; ++str, --ls)
    216  1.12  christos 		continue;
    217  1.19   abhinav 	*linesize = ls;
    218  1.12  christos 	return str;
    219   1.1       cgd }
    220   1.1       cgd 
    221  1.12  christos static FILE *
    222  1.12  christos file(const char *name, const char *mode)
    223   1.1       cgd {
    224   1.1       cgd 	FILE *fp;
    225   1.1       cgd 
    226   1.4   mycroft 	if ((fp = fopen(name, mode)) == NULL)
    227   1.8     lukem 		err(1, "%s", name);
    228   1.4   mycroft 	return(fp);
    229   1.4   mycroft }
    230   1.4   mycroft 
    231  1.12  christos static void
    232  1.12  christos obsolete(char *argv[])
    233   1.4   mycroft {
    234   1.4   mycroft 	char *ap, *p, *start;
    235   1.4   mycroft 
    236   1.8     lukem 	while ((ap = *++argv) != NULL) {
    237   1.4   mycroft 		/* Return if "--" or not an option of any form. */
    238   1.4   mycroft 		if (ap[0] != '-') {
    239   1.4   mycroft 			if (ap[0] != '+')
    240   1.4   mycroft 				return;
    241   1.4   mycroft 		} else if (ap[1] == '-')
    242   1.4   mycroft 			return;
    243   1.9  christos 		if (!isdigit((unsigned char)ap[1]))
    244   1.4   mycroft 			continue;
    245   1.4   mycroft 		/*
    246   1.4   mycroft 		 * Digit signifies an old-style option.  Malloc space for dash,
    247   1.4   mycroft 		 * new option and argument.
    248   1.4   mycroft 		 */
    249  1.12  christos 		(void)asprintf(&p, "-%c%s", ap[0] == '+' ? 's' : 'f', ap + 1);
    250  1.10    itojun 		if (!p)
    251   1.8     lukem 			err(1, "malloc");
    252  1.10    itojun 		start = p;
    253   1.4   mycroft 		*argv = start;
    254   1.1       cgd 	}
    255   1.1       cgd }
    256   1.1       cgd 
    257  1.12  christos static void
    258  1.12  christos usage(void)
    259   1.1       cgd {
    260  1.18       wiz 	(void)fprintf(stderr, "usage: %s [-cdu] [-f fields] [-s chars] "
    261  1.18       wiz 	    "[input_file [output_file]]\n", getprogname());
    262   1.4   mycroft 	exit(1);
    263   1.1       cgd }
    264