2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)cut.c 8.3 (Berkeley) 5/4/95
38 * $FreeBSD: src/usr.bin/cut/cut.c,v 1.9.2.3 2001/07/30 09:59:16 dd Exp $
39 * $DragonFly: src/usr.bin/cut/cut.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
57 void c_cut (FILE *, const char *);
58 void f_cut (FILE *, const char *);
59 void get_list (char *);
60 int main (int, char **);
61 static void usage (void);
69 void (*fcn) (FILE *, const char *) = NULL;
73 setlocale (LC_ALL, "");
75 dchar = '\t'; /* default delimiter is \t */
77 /* Since we don't support multi-byte characters, the -c and -b
78 options are equivalent, and the -n option is meaningless. */
79 while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
111 } else if (!cflag || dflag || sflag)
115 for (; *argv; ++argv) {
116 if (!(fp = fopen(*argv, "r")))
126 size_t autostart, autostop, maxval;
128 char positions[_POSIX2_LINE_MAX + 1];
134 size_t setautostart, start, stop;
139 * set a byte in the positions array to indicate if a field or
140 * column is to be selected; use +1, it's 1-based, not 0-based.
141 * This parser is less restrictive than the Draft 9 POSIX spec.
142 * POSIX doesn't allow lists that aren't in increasing order or
143 * overlapping lists. We also handle "-3-5" although there's no
146 for (; (p = strsep(&list, ", \t")) != NULL;) {
147 setautostart = start = stop = 0;
152 if (isdigit((unsigned char)*p)) {
153 start = stop = strtol(p, &p, 10);
154 if (setautostart && start > autostart)
158 if (isdigit((unsigned char)p[1]))
159 stop = strtol(p + 1, &p, 10);
162 if (!autostop || autostop > stop)
167 errx(1, "[-cf] list: illegal list value");
169 errx(1, "[-cf] list: values may not include zero");
170 if (stop > _POSIX2_LINE_MAX)
171 errx(1, "[-cf] list: %ld too large (max %d)",
172 (long)stop, _POSIX2_LINE_MAX);
175 for (pos = positions + start; start++ <= stop; *pos++ = 1);
178 /* overlapping ranges */
179 if (autostop && maxval > autostop)
184 memset(positions + 1, '1', autostart);
200 for (col = maxval; col; --col) {
201 if ((ch = getc(fp)) == EOF)
210 while ((ch = getc(fp)) != EOF && ch != '\n')
213 while ((ch = getc(fp)) != EOF && ch != '\n');
222 const char *fname __unused;
224 int ch, field, isdelim;
227 char *lbuf, *mlbuf = NULL;
230 for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
231 /* Assert EOL has a newline. */
232 if (*(lbuf + lbuflen - 1) != '\n') {
233 /* Can't have > 1 line with no trailing newline. */
234 mlbuf = malloc(lbuflen + 1);
237 memcpy(mlbuf, lbuf, lbuflen);
238 *(mlbuf + lbuflen) = '\n';
242 for (isdelim = 0, p = lbuf;; ++p) {
244 /* this should work if newline is delimiter */
248 if (!isdelim && !sflag)
249 (void)fwrite(lbuf, lbuflen, 1, stdout);
257 for (field = maxval, p = lbuf; field; --field, ++pos) {
261 while ((ch = *p++) != '\n' && ch != sep)
264 while ((ch = *p++) != '\n' && ch != sep)
274 for (; (ch = *p) != '\n'; ++p)
277 for (; (ch = *p) != '\n'; ++p);
288 (void)fprintf(stderr, "%s\n%s\n%s\n",
289 "usage: cut -b list [-n] [file ...]",
290 " cut -c list [file ...]",
291 " cut -f list [-s] [-d delim] [file ...]");