cut.c revision 98012
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 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue. 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 38static const char copyright[] = 39"@(#) Copyright (c) 1989, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41static const char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95"; 42static const char rcsid[] = 43 "$FreeBSD: head/usr.bin/cut/cut.c 98012 2002-06-07 09:13:43Z tjr $"; 44#endif /* not lint */ 45 46#include <ctype.h> 47#include <err.h> 48#include <limits.h> 49#include <locale.h> 50#include <stdio.h> 51#include <stdlib.h> 52#include <string.h> 53#include <unistd.h> 54 55int cflag; 56char dchar; 57int dflag; 58int fflag; 59int sflag; 60 61void c_cut(FILE *, const char *); 62void f_cut(FILE *, const char *); 63void get_list(char *); 64int main(int, char **); 65void needpos(size_t); 66static void usage(void); 67 68int 69main(argc, argv) 70 int argc; 71 char *argv[]; 72{ 73 FILE *fp; 74 void (*fcn)(FILE *, const char *); 75 int ch, rval; 76 77 setlocale(LC_ALL, ""); 78 79 fcn = NULL; 80 dchar = '\t'; /* default delimiter is \t */ 81 82 /* Since we don't support multi-byte characters, the -c and -b 83 options are equivalent, and the -n option is meaningless. */ 84 while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1) 85 switch(ch) { 86 case 'b': 87 case 'c': 88 fcn = c_cut; 89 get_list(optarg); 90 cflag = 1; 91 break; 92 case 'd': 93 dchar = *optarg; 94 dflag = 1; 95 break; 96 case 'f': 97 get_list(optarg); 98 fcn = f_cut; 99 fflag = 1; 100 break; 101 case 's': 102 sflag = 1; 103 break; 104 case 'n': 105 break; 106 case '?': 107 default: 108 usage(); 109 } 110 argc -= optind; 111 argv += optind; 112 113 if (fflag) { 114 if (cflag) 115 usage(); 116 } else if (!cflag || dflag || sflag) 117 usage(); 118 119 rval = 0; 120 if (*argv) 121 for (; *argv; ++argv) { 122 if (strcmp(*argv, "-") == 0) 123 fcn(stdin, "stdin"); 124 else { 125 if (!(fp = fopen(*argv, "r"))) { 126 warn("%s", *argv); 127 rval = 1; 128 continue; 129 } 130 fcn(fp, *argv); 131 (void)fclose(fp); 132 } 133 } 134 else 135 fcn(stdin, "stdin"); 136 exit(rval); 137} 138 139size_t autostart, autostop, maxval; 140 141char *positions; 142 143void 144get_list(list) 145 char *list; 146{ 147 size_t setautostart, start, stop; 148 char *pos; 149 char *p; 150 151 /* 152 * set a byte in the positions array to indicate if a field or 153 * column is to be selected; use +1, it's 1-based, not 0-based. 154 * This parser is less restrictive than the Draft 9 POSIX spec. 155 * POSIX doesn't allow lists that aren't in increasing order or 156 * overlapping lists. We also handle "-3-5" although there's no 157 * real reason too. 158 */ 159 for (; (p = strsep(&list, ", \t")) != NULL;) { 160 setautostart = start = stop = 0; 161 if (*p == '-') { 162 ++p; 163 setautostart = 1; 164 } 165 if (isdigit((unsigned char)*p)) { 166 start = stop = strtol(p, &p, 10); 167 if (setautostart && start > autostart) 168 autostart = start; 169 } 170 if (*p == '-') { 171 if (isdigit((unsigned char)p[1])) 172 stop = strtol(p + 1, &p, 10); 173 if (*p == '-') { 174 ++p; 175 if (!autostop || autostop > stop) 176 autostop = stop; 177 } 178 } 179 if (*p) 180 errx(1, "[-cf] list: illegal list value"); 181 if (!stop || !start) 182 errx(1, "[-cf] list: values may not include zero"); 183 if (maxval < stop) { 184 maxval = stop; 185 needpos(maxval + 1); 186 } 187 for (pos = positions + start; start++ <= stop; *pos++ = 1); 188 } 189 190 /* overlapping ranges */ 191 if (autostop && maxval > autostop) { 192 maxval = autostop; 193 needpos(maxval + 1); 194 } 195 196 /* set autostart */ 197 if (autostart) 198 memset(positions + 1, '1', autostart); 199} 200 201void 202needpos(size_t n) 203{ 204 static size_t npos; 205 size_t oldnpos; 206 207 /* Grow the positions array to at least the specified size. */ 208 if (n > npos) { 209 oldnpos = npos; 210 if (npos == 0) 211 npos = n; 212 while (n > npos) 213 npos *= 2; 214 if ((positions = realloc(positions, npos)) == NULL) 215 err(1, "realloc"); 216 memset((char *)positions + oldnpos, 0, npos - oldnpos); 217 } 218} 219 220void 221c_cut(fp, fname) 222 FILE *fp; 223 const char *fname __unused; 224{ 225 int ch, col; 226 char *pos; 227 228 ch = 0; 229 for (;;) { 230 pos = positions + 1; 231 for (col = maxval; col; --col) { 232 if ((ch = getc(fp)) == EOF) 233 return; 234 if (ch == '\n') 235 break; 236 if (*pos++) 237 (void)putchar(ch); 238 } 239 if (ch != '\n') { 240 if (autostop) 241 while ((ch = getc(fp)) != EOF && ch != '\n') 242 (void)putchar(ch); 243 else 244 while ((ch = getc(fp)) != EOF && ch != '\n'); 245 } 246 (void)putchar('\n'); 247 } 248} 249 250void 251f_cut(fp, fname) 252 FILE *fp; 253 const char *fname __unused; 254{ 255 int ch, field, isdelim; 256 char *pos, *p, sep; 257 int output; 258 char *lbuf, *mlbuf; 259 size_t lbuflen; 260 261 mlbuf = NULL; 262 for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) { 263 /* Assert EOL has a newline. */ 264 if (*(lbuf + lbuflen - 1) != '\n') { 265 /* Can't have > 1 line with no trailing newline. */ 266 mlbuf = malloc(lbuflen + 1); 267 if (mlbuf == NULL) 268 err(1, "malloc"); 269 memcpy(mlbuf, lbuf, lbuflen); 270 *(mlbuf + lbuflen) = '\n'; 271 lbuf = mlbuf; 272 } 273 output = 0; 274 for (isdelim = 0, p = lbuf;; ++p) { 275 ch = *p; 276 /* this should work if newline is delimiter */ 277 if (ch == sep) 278 isdelim = 1; 279 if (ch == '\n') { 280 if (!isdelim && !sflag) 281 (void)fwrite(lbuf, lbuflen, 1, stdout); 282 break; 283 } 284 } 285 if (!isdelim) 286 continue; 287 288 pos = positions + 1; 289 for (field = maxval, p = lbuf; field; --field, ++pos) { 290 if (*pos) { 291 if (output++) 292 (void)putchar(sep); 293 while ((ch = *p++) != '\n' && ch != sep) 294 (void)putchar(ch); 295 } else { 296 while ((ch = *p++) != '\n' && ch != sep) 297 continue; 298 } 299 if (ch == '\n') 300 break; 301 } 302 if (ch != '\n') { 303 if (autostop) { 304 if (output) 305 (void)putchar(sep); 306 for (; (ch = *p) != '\n'; ++p) 307 (void)putchar(ch); 308 } else 309 for (; (ch = *p) != '\n'; ++p); 310 } 311 (void)putchar('\n'); 312 } 313 if (mlbuf != NULL) 314 free(mlbuf); 315} 316 317static void 318usage() 319{ 320 (void)fprintf(stderr, "%s\n%s\n%s\n", 321 "usage: cut -b list [-n] [file ...]", 322 " cut -c list [file ...]", 323 " cut -f list [-s] [-d delim] [file ...]"); 324 exit(1); 325} 326