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