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