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 44df3f5d9dSPeter Wemm static char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95"; 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> 53df3f5d9dSPeter Wemm #include <unistd.h> 549b50d902SRodney W. Grimes 559b50d902SRodney W. Grimes int cflag; 569b50d902SRodney W. Grimes char dchar; 579b50d902SRodney W. Grimes int dflag; 589b50d902SRodney W. Grimes int fflag; 599b50d902SRodney W. Grimes int sflag; 609b50d902SRodney W. Grimes 619b50d902SRodney W. Grimes void c_cut __P((FILE *, char *)); 629b50d902SRodney W. Grimes void err __P((const char *, ...)); 639b50d902SRodney W. Grimes void f_cut __P((FILE *, char *)); 649b50d902SRodney W. Grimes void get_list __P((char *)); 659b50d902SRodney W. Grimes void usage __P((void)); 669b50d902SRodney W. Grimes 679b50d902SRodney W. Grimes int 689b50d902SRodney W. Grimes main(argc, argv) 699b50d902SRodney W. Grimes int argc; 709b50d902SRodney W. Grimes char *argv[]; 719b50d902SRodney W. Grimes { 729b50d902SRodney W. Grimes FILE *fp; 739b50d902SRodney W. Grimes void (*fcn) __P((FILE *, char *)); 749b50d902SRodney W. Grimes int ch; 759b50d902SRodney W. Grimes 769b50d902SRodney W. Grimes dchar = '\t'; /* default delimiter is \t */ 779b50d902SRodney W. Grimes 789b50d902SRodney W. Grimes while ((ch = getopt(argc, argv, "c:d:f:s")) != EOF) 799b50d902SRodney W. Grimes switch(ch) { 809b50d902SRodney W. Grimes case 'c': 819b50d902SRodney W. Grimes fcn = c_cut; 829b50d902SRodney W. Grimes get_list(optarg); 839b50d902SRodney W. Grimes cflag = 1; 849b50d902SRodney W. Grimes break; 859b50d902SRodney W. Grimes case 'd': 869b50d902SRodney W. Grimes dchar = *optarg; 879b50d902SRodney W. Grimes dflag = 1; 889b50d902SRodney W. Grimes break; 899b50d902SRodney W. Grimes case 'f': 909b50d902SRodney W. Grimes get_list(optarg); 919b50d902SRodney W. Grimes fcn = f_cut; 929b50d902SRodney W. Grimes fflag = 1; 939b50d902SRodney W. Grimes break; 949b50d902SRodney W. Grimes case 's': 959b50d902SRodney W. Grimes sflag = 1; 969b50d902SRodney W. Grimes break; 979b50d902SRodney W. Grimes case '?': 989b50d902SRodney W. Grimes default: 999b50d902SRodney W. Grimes usage(); 1009b50d902SRodney W. Grimes } 1019b50d902SRodney W. Grimes argc -= optind; 1029b50d902SRodney W. Grimes argv += optind; 1039b50d902SRodney W. Grimes 1049b50d902SRodney W. Grimes if (fflag) { 1059b50d902SRodney W. Grimes if (cflag) 1069b50d902SRodney W. Grimes usage(); 1079b50d902SRodney W. Grimes } else if (!cflag || dflag || sflag) 1089b50d902SRodney W. Grimes usage(); 1099b50d902SRodney W. Grimes 1109b50d902SRodney W. Grimes if (*argv) 1119b50d902SRodney W. Grimes for (; *argv; ++argv) { 1129b50d902SRodney W. Grimes if (!(fp = fopen(*argv, "r"))) 1139b50d902SRodney W. Grimes err("%s: %s\n", *argv, strerror(errno)); 1149b50d902SRodney W. Grimes fcn(fp, *argv); 1159b50d902SRodney W. Grimes (void)fclose(fp); 1169b50d902SRodney W. Grimes } 1179b50d902SRodney W. Grimes else 1189b50d902SRodney W. Grimes fcn(stdin, "stdin"); 1199b50d902SRodney W. Grimes exit(0); 1209b50d902SRodney W. Grimes } 1219b50d902SRodney W. Grimes 1229b50d902SRodney W. Grimes int autostart, autostop, maxval; 1239b50d902SRodney W. Grimes 1249b50d902SRodney W. Grimes char positions[_POSIX2_LINE_MAX + 1]; 1259b50d902SRodney W. Grimes 1269b50d902SRodney W. Grimes void 1279b50d902SRodney W. Grimes get_list(list) 1289b50d902SRodney W. Grimes char *list; 1299b50d902SRodney W. Grimes { 1309b50d902SRodney W. Grimes register int setautostart, start, stop; 1319b50d902SRodney W. Grimes register char *pos; 1329b50d902SRodney W. Grimes char *p; 1339b50d902SRodney W. Grimes 1349b50d902SRodney W. Grimes /* 1359b50d902SRodney W. Grimes * set a byte in the positions array to indicate if a field or 1369b50d902SRodney W. Grimes * column is to be selected; use +1, it's 1-based, not 0-based. 1379b50d902SRodney W. Grimes * This parser is less restrictive than the Draft 9 POSIX spec. 1389b50d902SRodney W. Grimes * POSIX doesn't allow lists that aren't in increasing order or 1399b50d902SRodney W. Grimes * overlapping lists. We also handle "-3-5" although there's no 1409b50d902SRodney W. Grimes * real reason too. 1419b50d902SRodney W. Grimes */ 1429b50d902SRodney W. Grimes for (; p = strtok(list, ", \t"); list = NULL) { 1439b50d902SRodney W. Grimes setautostart = start = stop = 0; 1449b50d902SRodney W. Grimes if (*p == '-') { 1459b50d902SRodney W. Grimes ++p; 1469b50d902SRodney W. Grimes setautostart = 1; 1479b50d902SRodney W. Grimes } 1489b50d902SRodney W. Grimes if (isdigit(*p)) { 1499b50d902SRodney W. Grimes start = stop = strtol(p, &p, 10); 1509b50d902SRodney W. Grimes if (setautostart && start > autostart) 1519b50d902SRodney W. Grimes autostart = start; 1529b50d902SRodney W. Grimes } 1539b50d902SRodney W. Grimes if (*p == '-') { 1549b50d902SRodney W. Grimes if (isdigit(p[1])) 1559b50d902SRodney W. Grimes stop = strtol(p + 1, &p, 10); 1569b50d902SRodney W. Grimes if (*p == '-') { 1579b50d902SRodney W. Grimes ++p; 1589b50d902SRodney W. Grimes if (!autostop || autostop > stop) 1599b50d902SRodney W. Grimes autostop = stop; 1609b50d902SRodney W. Grimes } 1619b50d902SRodney W. Grimes } 1629b50d902SRodney W. Grimes if (*p) 1639b50d902SRodney W. Grimes err("[-cf] list: illegal list value\n"); 1649b50d902SRodney W. Grimes if (!stop || !start) 1659b50d902SRodney W. Grimes err("[-cf] list: values may not include zero\n"); 1669b50d902SRodney W. Grimes if (stop > _POSIX2_LINE_MAX) 1679b50d902SRodney W. Grimes err("[-cf] list: %d too large (max %d)\n", 1689b50d902SRodney W. Grimes stop, _POSIX2_LINE_MAX); 1699b50d902SRodney W. Grimes if (maxval < stop) 1709b50d902SRodney W. Grimes maxval = stop; 1719b50d902SRodney W. Grimes for (pos = positions + start; start++ <= stop; *pos++ = 1); 1729b50d902SRodney W. Grimes } 1739b50d902SRodney W. Grimes 1749b50d902SRodney W. Grimes /* overlapping ranges */ 1759b50d902SRodney W. Grimes if (autostop && maxval > autostop) 1769b50d902SRodney W. Grimes maxval = autostop; 1779b50d902SRodney W. Grimes 1789b50d902SRodney W. Grimes /* set autostart */ 1799b50d902SRodney W. Grimes if (autostart) 1809b50d902SRodney W. Grimes memset(positions + 1, '1', autostart); 1819b50d902SRodney W. Grimes } 1829b50d902SRodney W. Grimes 1839b50d902SRodney W. Grimes /* ARGSUSED */ 1849b50d902SRodney W. Grimes void 1859b50d902SRodney W. Grimes c_cut(fp, fname) 1869b50d902SRodney W. Grimes FILE *fp; 1879b50d902SRodney W. Grimes char *fname; 1889b50d902SRodney W. Grimes { 1899b50d902SRodney W. Grimes register int ch, col; 1909b50d902SRodney W. Grimes register char *pos; 1919b50d902SRodney W. Grimes 1929b50d902SRodney W. Grimes for (;;) { 1939b50d902SRodney W. Grimes pos = positions + 1; 1949b50d902SRodney W. Grimes for (col = maxval; col; --col) { 1959b50d902SRodney W. Grimes if ((ch = getc(fp)) == EOF) 1969b50d902SRodney W. Grimes return; 1979b50d902SRodney W. Grimes if (ch == '\n') 1989b50d902SRodney W. Grimes break; 1999b50d902SRodney W. Grimes if (*pos++) 2009b50d902SRodney W. Grimes (void)putchar(ch); 2019b50d902SRodney W. Grimes } 2029b50d902SRodney W. Grimes if (ch != '\n') 2039b50d902SRodney W. Grimes if (autostop) 2049b50d902SRodney W. Grimes while ((ch = getc(fp)) != EOF && ch != '\n') 2059b50d902SRodney W. Grimes (void)putchar(ch); 2069b50d902SRodney W. Grimes else 2079b50d902SRodney W. Grimes while ((ch = getc(fp)) != EOF && ch != '\n'); 2089b50d902SRodney W. Grimes (void)putchar('\n'); 2099b50d902SRodney W. Grimes } 2109b50d902SRodney W. Grimes } 2119b50d902SRodney W. Grimes 2129b50d902SRodney W. Grimes void 2139b50d902SRodney W. Grimes f_cut(fp, fname) 2149b50d902SRodney W. Grimes FILE *fp; 2159b50d902SRodney W. Grimes char *fname; 2169b50d902SRodney W. Grimes { 2179b50d902SRodney W. Grimes register int ch, field, isdelim; 2189b50d902SRodney W. Grimes register char *pos, *p, sep; 2199b50d902SRodney W. Grimes int output; 2209b50d902SRodney W. Grimes char lbuf[_POSIX2_LINE_MAX + 1]; 2219b50d902SRodney W. Grimes 222eaf92380SAndrey A. Chernov for (sep = dchar; fgets(lbuf, sizeof(lbuf), fp);) { 223eaf92380SAndrey A. Chernov output = 0; 2249b50d902SRodney W. Grimes for (isdelim = 0, p = lbuf;; ++p) { 2259b50d902SRodney W. Grimes if (!(ch = *p)) 2269b50d902SRodney W. Grimes err("%s: line too long.\n", fname); 2279b50d902SRodney W. Grimes /* this should work if newline is delimiter */ 2289b50d902SRodney W. Grimes if (ch == sep) 2299b50d902SRodney W. Grimes isdelim = 1; 2309b50d902SRodney W. Grimes if (ch == '\n') { 2319b50d902SRodney W. Grimes if (!isdelim && !sflag) 2329b50d902SRodney W. Grimes (void)printf("%s", lbuf); 2339b50d902SRodney W. Grimes break; 2349b50d902SRodney W. Grimes } 2359b50d902SRodney W. Grimes } 2369b50d902SRodney W. Grimes if (!isdelim) 2379b50d902SRodney W. Grimes continue; 2389b50d902SRodney W. Grimes 2399b50d902SRodney W. Grimes pos = positions + 1; 2409b50d902SRodney W. Grimes for (field = maxval, p = lbuf; field; --field, ++pos) { 2419b50d902SRodney W. Grimes if (*pos) { 2429b50d902SRodney W. Grimes if (output++) 2439b50d902SRodney W. Grimes (void)putchar(sep); 2449b50d902SRodney W. Grimes while ((ch = *p++) != '\n' && ch != sep) 2459b50d902SRodney W. Grimes (void)putchar(ch); 2469b50d902SRodney W. Grimes } else 2479b50d902SRodney W. Grimes while ((ch = *p++) != '\n' && ch != sep); 2489b50d902SRodney W. Grimes if (ch == '\n') 2499b50d902SRodney W. Grimes break; 2509b50d902SRodney W. Grimes } 2519b50d902SRodney W. Grimes if (ch != '\n') 2529b50d902SRodney W. Grimes if (autostop) { 2539b50d902SRodney W. Grimes if (output) 2549b50d902SRodney W. Grimes (void)putchar(sep); 2559b50d902SRodney W. Grimes for (; (ch = *p) != '\n'; ++p) 2569b50d902SRodney W. Grimes (void)putchar(ch); 2579b50d902SRodney W. Grimes } else 2589b50d902SRodney W. Grimes for (; (ch = *p) != '\n'; ++p); 2599b50d902SRodney W. Grimes (void)putchar('\n'); 2609b50d902SRodney W. Grimes } 2619b50d902SRodney W. Grimes } 2629b50d902SRodney W. Grimes 2639b50d902SRodney W. Grimes void 2649b50d902SRodney W. Grimes usage() 2659b50d902SRodney W. Grimes { 2669b50d902SRodney W. Grimes (void)fprintf(stderr, 2679b50d902SRodney W. Grimes "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n"); 2689b50d902SRodney W. Grimes exit(1); 2699b50d902SRodney W. Grimes } 2709b50d902SRodney W. Grimes 2719b50d902SRodney W. Grimes #if __STDC__ 2729b50d902SRodney W. Grimes #include <stdarg.h> 2739b50d902SRodney W. Grimes #else 2749b50d902SRodney W. Grimes #include <varargs.h> 2759b50d902SRodney W. Grimes #endif 2769b50d902SRodney W. Grimes 2779b50d902SRodney W. Grimes void 2789b50d902SRodney W. Grimes #if __STDC__ 2799b50d902SRodney W. Grimes err(const char *fmt, ...) 2809b50d902SRodney W. Grimes #else 2819b50d902SRodney W. Grimes err(fmt, va_alist) 2829b50d902SRodney W. Grimes char *fmt; 2839b50d902SRodney W. Grimes va_dcl 2849b50d902SRodney W. Grimes #endif 2859b50d902SRodney W. Grimes { 2869b50d902SRodney W. Grimes va_list ap; 2879b50d902SRodney W. Grimes #if __STDC__ 2889b50d902SRodney W. Grimes va_start(ap, fmt); 2899b50d902SRodney W. Grimes #else 2909b50d902SRodney W. Grimes va_start(ap); 2919b50d902SRodney W. Grimes #endif 2929b50d902SRodney W. Grimes (void)fprintf(stderr, "cut: "); 2939b50d902SRodney W. Grimes (void)vfprintf(stderr, fmt, ap); 2949b50d902SRodney W. Grimes va_end(ap); 2959b50d902SRodney W. Grimes (void)fprintf(stderr, "\n"); 2969b50d902SRodney W. Grimes exit(1); 2979b50d902SRodney W. Grimes /* NOTREACHED */ 2989b50d902SRodney W. Grimes } 299