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