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 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 static char sccsid[] = "@(#)uniq.c 8.1 (Berkeley) 6/6/93"; 45 #endif /* not lint */ 46 47 #include <errno.h> 48 #include <stdio.h> 49 #include <ctype.h> 50 #include <stdlib.h> 51 #include <string.h> 52 53 #define MAXLINELEN (8 * 1024) 54 55 int cflag, dflag, uflag; 56 int numchars, numfields, repeats; 57 58 void err __P((const char *, ...)); 59 FILE *file __P((char *, char *)); 60 void show __P((FILE *, char *)); 61 char *skip __P((char *)); 62 void obsolete __P((char *[])); 63 void usage __P((void)); 64 65 int 66 main (argc, argv) 67 int argc; 68 char *argv[]; 69 { 70 register char *t1, *t2; 71 FILE *ifp, *ofp; 72 int ch; 73 char *prevline, *thisline, *p; 74 75 obsolete(argv); 76 while ((ch = getopt(argc, argv, "-cdf:s:u")) != EOF) 77 switch (ch) { 78 case '-': 79 --optind; 80 goto done; 81 case 'c': 82 cflag = 1; 83 break; 84 case 'd': 85 dflag = 1; 86 break; 87 case 'f': 88 numfields = strtol(optarg, &p, 10); 89 if (numfields < 0 || *p) 90 err("illegal field skip value: %s", optarg); 91 break; 92 case 's': 93 numchars = strtol(optarg, &p, 10); 94 if (numchars < 0 || *p) 95 err("illegal character skip value: %s", optarg); 96 break; 97 case 'u': 98 uflag = 1; 99 break; 100 case '?': 101 default: 102 usage(); 103 } 104 105 done: argc -= optind; 106 argv +=optind; 107 108 /* If no flags are set, default is -d -u. */ 109 if (cflag) { 110 if (dflag || uflag) 111 usage(); 112 } else if (!dflag && !uflag) 113 dflag = uflag = 1; 114 115 switch(argc) { 116 case 0: 117 ifp = stdin; 118 ofp = stdout; 119 break; 120 case 1: 121 ifp = file(argv[0], "r"); 122 ofp = stdout; 123 break; 124 case 2: 125 ifp = file(argv[0], "r"); 126 ofp = file(argv[1], "w"); 127 break; 128 default: 129 usage(); 130 } 131 132 prevline = malloc(MAXLINELEN); 133 thisline = malloc(MAXLINELEN); 134 if (prevline == NULL || thisline == NULL) 135 err("%s", strerror(errno)); 136 137 if (fgets(prevline, MAXLINELEN, ifp) == NULL) 138 exit(0); 139 140 while (fgets(thisline, MAXLINELEN, ifp)) { 141 /* If requested get the chosen fields + character offsets. */ 142 if (numfields || numchars) { 143 t1 = skip(thisline); 144 t2 = skip(prevline); 145 } else { 146 t1 = thisline; 147 t2 = prevline; 148 } 149 150 /* If different, print; set previous to new value. */ 151 if (strcmp(t1, t2)) { 152 show(ofp, prevline); 153 t1 = prevline; 154 prevline = thisline; 155 thisline = t1; 156 repeats = 0; 157 } else 158 ++repeats; 159 } 160 show(ofp, prevline); 161 exit(0); 162 } 163 164 /* 165 * show -- 166 * Output a line depending on the flags and number of repetitions 167 * of the line. 168 */ 169 void 170 show(ofp, str) 171 FILE *ofp; 172 char *str; 173 { 174 if (cflag) 175 (void)fprintf(ofp, "%4d %s", repeats + 1, str); 176 if (dflag && repeats || uflag && !repeats) 177 (void)fprintf(ofp, "%s", str); 178 } 179 180 char * 181 skip(str) 182 register char *str; 183 { 184 register int infield, nchars, nfields; 185 186 for (nfields = numfields, infield = 0; nfields && *str; ++str) 187 if (isspace(*str)) { 188 if (infield) { 189 infield = 0; 190 --nfields; 191 } 192 } else if (!infield) 193 infield = 1; 194 for (nchars = numchars; nchars-- && *str; ++str); 195 return(str); 196 } 197 198 FILE * 199 file(name, mode) 200 char *name, *mode; 201 { 202 FILE *fp; 203 204 if ((fp = fopen(name, mode)) == NULL) 205 err("%s: %s", name, strerror(errno)); 206 return(fp); 207 } 208 209 void 210 obsolete(argv) 211 char *argv[]; 212 { 213 int len; 214 char *ap, *p, *start; 215 216 while (ap = *++argv) { 217 /* Return if "--" or not an option of any form. */ 218 if (ap[0] != '-') { 219 if (ap[0] != '+') 220 return; 221 } else if (ap[1] == '-') 222 return; 223 if (!isdigit(ap[1])) 224 continue; 225 /* 226 * Digit signifies an old-style option. Malloc space for dash, 227 * new option and argument. 228 */ 229 len = strlen(ap); 230 if ((start = p = malloc(len + 3)) == NULL) 231 err("%s", strerror(errno)); 232 *p++ = '-'; 233 *p++ = ap[0] == '+' ? 's' : 'f'; 234 (void)strcpy(p, ap + 1); 235 *argv = start; 236 } 237 } 238 239 void 240 usage() 241 { 242 (void)fprintf(stderr, 243 "usage: uniq [-c | -du] [-f fields] [-s chars] [input [output]]\n"); 244 exit(1); 245 } 246 247 #if __STDC__ 248 #include <stdarg.h> 249 #else 250 #include <varargs.h> 251 #endif 252 253 void 254 #if __STDC__ 255 err(const char *fmt, ...) 256 #else 257 err(fmt, va_alist) 258 char *fmt; 259 va_dcl 260 #endif 261 { 262 va_list ap; 263 #if __STDC__ 264 va_start(ap, fmt); 265 #else 266 va_start(ap); 267 #endif 268 (void)fprintf(stderr, "uniq: "); 269 (void)vfprintf(stderr, fmt, ap); 270 va_end(ap); 271 (void)fprintf(stderr, "\n"); 272 exit(1); 273 /* NOTREACHED */ 274 } 275