19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1989, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 69b50d902SRodney W. Grimes * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue. 79b50d902SRodney W. Grimes * 89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 109b50d902SRodney W. Grimes * are met: 119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 169b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 179b50d902SRodney W. Grimes * must display the following acknowledgement: 189b50d902SRodney W. Grimes * This product includes software developed by the University of 199b50d902SRodney W. Grimes * California, Berkeley and its contributors. 209b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 219b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 229b50d902SRodney W. Grimes * without specific prior written permission. 239b50d902SRodney W. Grimes * 249b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 259b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 269b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 279b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 289b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 299b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 309b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 319b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 329b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 339b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 349b50d902SRodney W. Grimes * SUCH DAMAGE. 359b50d902SRodney W. Grimes */ 369b50d902SRodney W. Grimes 379b50d902SRodney W. Grimes #ifndef lint 389b50d902SRodney W. Grimes static char copyright[] = 399b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 409b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 419b50d902SRodney W. Grimes #endif /* not lint */ 429b50d902SRodney W. Grimes 439b50d902SRodney W. Grimes #ifndef lint 449b50d902SRodney W. Grimes static char sccsid[] = "@(#)cut.c 8.1 (Berkeley) 6/6/93"; 459b50d902SRodney W. Grimes #endif /* not lint */ 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes #include <ctype.h> 489b50d902SRodney W. Grimes #include <errno.h> 499b50d902SRodney W. Grimes #include <limits.h> 509b50d902SRodney W. Grimes #include <stdio.h> 519b50d902SRodney W. Grimes #include <stdlib.h> 529b50d902SRodney W. Grimes #include <string.h> 539b50d902SRodney W. Grimes 549b50d902SRodney W. Grimes int cflag; 559b50d902SRodney W. Grimes char dchar; 569b50d902SRodney W. Grimes int dflag; 579b50d902SRodney W. Grimes int fflag; 589b50d902SRodney W. Grimes int sflag; 599b50d902SRodney W. Grimes 609b50d902SRodney W. Grimes void c_cut __P((FILE *, char *)); 619b50d902SRodney W. Grimes void err __P((const char *, ...)); 629b50d902SRodney W. Grimes void f_cut __P((FILE *, char *)); 639b50d902SRodney W. Grimes void get_list __P((char *)); 649b50d902SRodney W. Grimes void usage __P((void)); 659b50d902SRodney W. Grimes 669b50d902SRodney W. Grimes int 679b50d902SRodney W. Grimes main(argc, argv) 689b50d902SRodney W. Grimes int argc; 699b50d902SRodney W. Grimes char *argv[]; 709b50d902SRodney W. Grimes { 719b50d902SRodney W. Grimes FILE *fp; 729b50d902SRodney W. Grimes void (*fcn) __P((FILE *, char *)); 739b50d902SRodney W. Grimes int ch; 749b50d902SRodney W. Grimes 759b50d902SRodney W. Grimes dchar = '\t'; /* default delimiter is \t */ 769b50d902SRodney W. Grimes 779b50d902SRodney W. Grimes while ((ch = getopt(argc, argv, "c:d:f:s")) != EOF) 789b50d902SRodney W. Grimes switch(ch) { 799b50d902SRodney W. Grimes case 'c': 809b50d902SRodney W. Grimes fcn = c_cut; 819b50d902SRodney W. Grimes get_list(optarg); 829b50d902SRodney W. Grimes cflag = 1; 839b50d902SRodney W. Grimes break; 849b50d902SRodney W. Grimes case 'd': 859b50d902SRodney W. Grimes dchar = *optarg; 869b50d902SRodney W. Grimes dflag = 1; 879b50d902SRodney W. Grimes break; 889b50d902SRodney W. Grimes case 'f': 899b50d902SRodney W. Grimes get_list(optarg); 909b50d902SRodney W. Grimes fcn = f_cut; 919b50d902SRodney W. Grimes fflag = 1; 929b50d902SRodney W. Grimes break; 939b50d902SRodney W. Grimes case 's': 949b50d902SRodney W. Grimes sflag = 1; 959b50d902SRodney W. Grimes break; 969b50d902SRodney W. Grimes case '?': 979b50d902SRodney W. Grimes default: 989b50d902SRodney W. Grimes usage(); 999b50d902SRodney W. Grimes } 1009b50d902SRodney W. Grimes argc -= optind; 1019b50d902SRodney W. Grimes argv += optind; 1029b50d902SRodney W. Grimes 1039b50d902SRodney W. Grimes if (fflag) { 1049b50d902SRodney W. Grimes if (cflag) 1059b50d902SRodney W. Grimes usage(); 1069b50d902SRodney W. Grimes } else if (!cflag || dflag || sflag) 1079b50d902SRodney W. Grimes usage(); 1089b50d902SRodney W. Grimes 1099b50d902SRodney W. Grimes if (*argv) 1109b50d902SRodney W. Grimes for (; *argv; ++argv) { 1119b50d902SRodney W. Grimes if (!(fp = fopen(*argv, "r"))) 1129b50d902SRodney W. Grimes err("%s: %s\n", *argv, strerror(errno)); 1139b50d902SRodney W. Grimes fcn(fp, *argv); 1149b50d902SRodney W. Grimes (void)fclose(fp); 1159b50d902SRodney W. Grimes } 1169b50d902SRodney W. Grimes else 1179b50d902SRodney W. Grimes fcn(stdin, "stdin"); 1189b50d902SRodney W. Grimes exit(0); 1199b50d902SRodney W. Grimes } 1209b50d902SRodney W. Grimes 1219b50d902SRodney W. Grimes int autostart, autostop, maxval; 1229b50d902SRodney W. Grimes 1239b50d902SRodney W. Grimes char positions[_POSIX2_LINE_MAX + 1]; 1249b50d902SRodney W. Grimes 1259b50d902SRodney W. Grimes void 1269b50d902SRodney W. Grimes get_list(list) 1279b50d902SRodney W. Grimes char *list; 1289b50d902SRodney W. Grimes { 1299b50d902SRodney W. Grimes register int setautostart, start, stop; 1309b50d902SRodney W. Grimes register char *pos; 1319b50d902SRodney W. Grimes char *p; 1329b50d902SRodney W. Grimes 1339b50d902SRodney W. Grimes /* 1349b50d902SRodney W. Grimes * set a byte in the positions array to indicate if a field or 1359b50d902SRodney W. Grimes * column is to be selected; use +1, it's 1-based, not 0-based. 1369b50d902SRodney W. Grimes * This parser is less restrictive than the Draft 9 POSIX spec. 1379b50d902SRodney W. Grimes * POSIX doesn't allow lists that aren't in increasing order or 1389b50d902SRodney W. Grimes * overlapping lists. We also handle "-3-5" although there's no 1399b50d902SRodney W. Grimes * real reason too. 1409b50d902SRodney W. Grimes */ 1419b50d902SRodney W. Grimes for (; p = strtok(list, ", \t"); list = NULL) { 1429b50d902SRodney W. Grimes setautostart = start = stop = 0; 1439b50d902SRodney W. Grimes if (*p == '-') { 1449b50d902SRodney W. Grimes ++p; 1459b50d902SRodney W. Grimes setautostart = 1; 1469b50d902SRodney W. Grimes } 1479b50d902SRodney W. Grimes if (isdigit(*p)) { 1489b50d902SRodney W. Grimes start = stop = strtol(p, &p, 10); 1499b50d902SRodney W. Grimes if (setautostart && start > autostart) 1509b50d902SRodney W. Grimes autostart = start; 1519b50d902SRodney W. Grimes } 1529b50d902SRodney W. Grimes if (*p == '-') { 1539b50d902SRodney W. Grimes if (isdigit(p[1])) 1549b50d902SRodney W. Grimes stop = strtol(p + 1, &p, 10); 1559b50d902SRodney W. Grimes if (*p == '-') { 1569b50d902SRodney W. Grimes ++p; 1579b50d902SRodney W. Grimes if (!autostop || autostop > stop) 1589b50d902SRodney W. Grimes autostop = stop; 1599b50d902SRodney W. Grimes } 1609b50d902SRodney W. Grimes } 1619b50d902SRodney W. Grimes if (*p) 1629b50d902SRodney W. Grimes err("[-cf] list: illegal list value\n"); 1639b50d902SRodney W. Grimes if (!stop || !start) 1649b50d902SRodney W. Grimes err("[-cf] list: values may not include zero\n"); 1659b50d902SRodney W. Grimes if (stop > _POSIX2_LINE_MAX) 1669b50d902SRodney W. Grimes err("[-cf] list: %d too large (max %d)\n", 1679b50d902SRodney W. Grimes stop, _POSIX2_LINE_MAX); 1689b50d902SRodney W. Grimes if (maxval < stop) 1699b50d902SRodney W. Grimes maxval = stop; 1709b50d902SRodney W. Grimes for (pos = positions + start; start++ <= stop; *pos++ = 1); 1719b50d902SRodney W. Grimes } 1729b50d902SRodney W. Grimes 1739b50d902SRodney W. Grimes /* overlapping ranges */ 1749b50d902SRodney W. Grimes if (autostop && maxval > autostop) 1759b50d902SRodney W. Grimes maxval = autostop; 1769b50d902SRodney W. Grimes 1779b50d902SRodney W. Grimes /* set autostart */ 1789b50d902SRodney W. Grimes if (autostart) 1799b50d902SRodney W. Grimes memset(positions + 1, '1', autostart); 1809b50d902SRodney W. Grimes } 1819b50d902SRodney W. Grimes 1829b50d902SRodney W. Grimes /* ARGSUSED */ 1839b50d902SRodney W. Grimes void 1849b50d902SRodney W. Grimes c_cut(fp, fname) 1859b50d902SRodney W. Grimes FILE *fp; 1869b50d902SRodney W. Grimes char *fname; 1879b50d902SRodney W. Grimes { 1889b50d902SRodney W. Grimes register int ch, col; 1899b50d902SRodney W. Grimes register char *pos; 1909b50d902SRodney W. Grimes 1919b50d902SRodney W. Grimes for (;;) { 1929b50d902SRodney W. Grimes pos = positions + 1; 1939b50d902SRodney W. Grimes for (col = maxval; col; --col) { 1949b50d902SRodney W. Grimes if ((ch = getc(fp)) == EOF) 1959b50d902SRodney W. Grimes return; 1969b50d902SRodney W. Grimes if (ch == '\n') 1979b50d902SRodney W. Grimes break; 1989b50d902SRodney W. Grimes if (*pos++) 1999b50d902SRodney W. Grimes (void)putchar(ch); 2009b50d902SRodney W. Grimes } 2019b50d902SRodney W. Grimes if (ch != '\n') 2029b50d902SRodney W. Grimes if (autostop) 2039b50d902SRodney W. Grimes while ((ch = getc(fp)) != EOF && ch != '\n') 2049b50d902SRodney W. Grimes (void)putchar(ch); 2059b50d902SRodney W. Grimes else 2069b50d902SRodney W. Grimes while ((ch = getc(fp)) != EOF && ch != '\n'); 2079b50d902SRodney W. Grimes (void)putchar('\n'); 2089b50d902SRodney W. Grimes } 2099b50d902SRodney W. Grimes } 2109b50d902SRodney W. Grimes 2119b50d902SRodney W. Grimes void 2129b50d902SRodney W. Grimes f_cut(fp, fname) 2139b50d902SRodney W. Grimes FILE *fp; 2149b50d902SRodney W. Grimes char *fname; 2159b50d902SRodney W. Grimes { 2169b50d902SRodney W. Grimes register int ch, field, isdelim; 2179b50d902SRodney W. Grimes register char *pos, *p, sep; 2189b50d902SRodney W. Grimes int output; 2199b50d902SRodney W. Grimes char lbuf[_POSIX2_LINE_MAX + 1]; 2209b50d902SRodney W. Grimes 221eaf92380SAndrey A. Chernov for (sep = dchar; fgets(lbuf, sizeof(lbuf), fp);) { 222eaf92380SAndrey A. Chernov output = 0; 2239b50d902SRodney W. Grimes for (isdelim = 0, p = lbuf;; ++p) { 2249b50d902SRodney W. Grimes if (!(ch = *p)) 2259b50d902SRodney W. Grimes err("%s: line too long.\n", fname); 2269b50d902SRodney W. Grimes /* this should work if newline is delimiter */ 2279b50d902SRodney W. Grimes if (ch == sep) 2289b50d902SRodney W. Grimes isdelim = 1; 2299b50d902SRodney W. Grimes if (ch == '\n') { 2309b50d902SRodney W. Grimes if (!isdelim && !sflag) 2319b50d902SRodney W. Grimes (void)printf("%s", lbuf); 2329b50d902SRodney W. Grimes break; 2339b50d902SRodney W. Grimes } 2349b50d902SRodney W. Grimes } 2359b50d902SRodney W. Grimes if (!isdelim) 2369b50d902SRodney W. Grimes continue; 2379b50d902SRodney W. Grimes 2389b50d902SRodney W. Grimes pos = positions + 1; 2399b50d902SRodney W. Grimes for (field = maxval, p = lbuf; field; --field, ++pos) { 2409b50d902SRodney W. Grimes if (*pos) { 2419b50d902SRodney W. Grimes if (output++) 2429b50d902SRodney W. Grimes (void)putchar(sep); 2439b50d902SRodney W. Grimes while ((ch = *p++) != '\n' && ch != sep) 2449b50d902SRodney W. Grimes (void)putchar(ch); 2459b50d902SRodney W. Grimes } else 2469b50d902SRodney W. Grimes while ((ch = *p++) != '\n' && ch != sep); 2479b50d902SRodney W. Grimes if (ch == '\n') 2489b50d902SRodney W. Grimes break; 2499b50d902SRodney W. Grimes } 2509b50d902SRodney W. Grimes if (ch != '\n') 2519b50d902SRodney W. Grimes if (autostop) { 2529b50d902SRodney W. Grimes if (output) 2539b50d902SRodney W. Grimes (void)putchar(sep); 2549b50d902SRodney W. Grimes for (; (ch = *p) != '\n'; ++p) 2559b50d902SRodney W. Grimes (void)putchar(ch); 2569b50d902SRodney W. Grimes } else 2579b50d902SRodney W. Grimes for (; (ch = *p) != '\n'; ++p); 2589b50d902SRodney W. Grimes (void)putchar('\n'); 2599b50d902SRodney W. Grimes } 2609b50d902SRodney W. Grimes } 2619b50d902SRodney W. Grimes 2629b50d902SRodney W. Grimes void 2639b50d902SRodney W. Grimes usage() 2649b50d902SRodney W. Grimes { 2659b50d902SRodney W. Grimes (void)fprintf(stderr, 2669b50d902SRodney W. Grimes "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n"); 2679b50d902SRodney W. Grimes exit(1); 2689b50d902SRodney W. Grimes } 2699b50d902SRodney W. Grimes 2709b50d902SRodney W. Grimes #if __STDC__ 2719b50d902SRodney W. Grimes #include <stdarg.h> 2729b50d902SRodney W. Grimes #else 2739b50d902SRodney W. Grimes #include <varargs.h> 2749b50d902SRodney W. Grimes #endif 2759b50d902SRodney W. Grimes 2769b50d902SRodney W. Grimes void 2779b50d902SRodney W. Grimes #if __STDC__ 2789b50d902SRodney W. Grimes err(const char *fmt, ...) 2799b50d902SRodney W. Grimes #else 2809b50d902SRodney W. Grimes err(fmt, va_alist) 2819b50d902SRodney W. Grimes char *fmt; 2829b50d902SRodney W. Grimes va_dcl 2839b50d902SRodney W. Grimes #endif 2849b50d902SRodney W. Grimes { 2859b50d902SRodney W. Grimes va_list ap; 2869b50d902SRodney W. Grimes #if __STDC__ 2879b50d902SRodney W. Grimes va_start(ap, fmt); 2889b50d902SRodney W. Grimes #else 2899b50d902SRodney W. Grimes va_start(ap); 2909b50d902SRodney W. Grimes #endif 2919b50d902SRodney W. Grimes (void)fprintf(stderr, "cut: "); 2929b50d902SRodney W. Grimes (void)vfprintf(stderr, fmt, ap); 2939b50d902SRodney W. Grimes va_end(ap); 2949b50d902SRodney W. Grimes (void)fprintf(stderr, "\n"); 2959b50d902SRodney W. Grimes exit(1); 2969b50d902SRodney W. Grimes /* NOTREACHED */ 2979b50d902SRodney W. Grimes } 298