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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95";
42 static const char rcsid[] =
61 void c_cut (FILE *, const char *);
62 void f_cut (FILE *, const char *);
63 void get_list (char *);
64 int main (int, char **);
65 static void usage (void);
73 void (*fcn) (FILE *, const char *) = NULL;
77 setlocale (LC_ALL, "");
79 dchar = '\t'; /* default delimiter is \t */
81 /* Since we don't support multi-byte characters, the -c and -b
82 options are equivalent, and the -n option is meaningless. */
83 while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
115 } else if (!cflag || dflag || sflag)
119 for (; *argv; ++argv) {
120 if (!(fp = fopen(*argv, "r")))
130 size_t autostart, autostop, maxval;
132 char positions[_POSIX2_LINE_MAX + 1];
138 size_t setautostart, start, stop;
143 * set a byte in the positions array to indicate if a field or
144 * column is to be selected; use +1, it's 1-based, not 0-based.
145 * This parser is less restrictive than the Draft 9 POSIX spec.
146 * POSIX doesn't allow lists that aren't in increasing order or
147 * overlapping lists. We also handle "-3-5" although there's no
150 for (; (p = strsep(&list, ", \t")) != NULL;) {
151 setautostart = start = stop = 0;
156 if (isdigit((unsigned char)*p)) {
157 start = stop = strtol(p, &p, 10);
158 if (setautostart && start > autostart)
162 if (isdigit((unsigned char)p[1]))
163 stop = strtol(p + 1, &p, 10);
166 if (!autostop || autostop > stop)
171 errx(1, "[-cf] list: illegal list value");
173 errx(1, "[-cf] list: values may not include zero");
174 if (stop > _POSIX2_LINE_MAX)
175 errx(1, "[-cf] list: %ld too large (max %d)",
176 (long)stop, _POSIX2_LINE_MAX);
179 for (pos = positions + start; start++ <= stop; *pos++ = 1);
182 /* overlapping ranges */
183 if (autostop && maxval > autostop)
188 memset(positions + 1, '1', autostart);
204 for (col = maxval; col; --col) {
205 if ((ch = getc(fp)) == EOF)
214 while ((ch = getc(fp)) != EOF && ch != '\n')
217 while ((ch = getc(fp)) != EOF && ch != '\n');
226 const char *fname __unused;
228 int ch, field, isdelim;
231 char *lbuf, *mlbuf = NULL;
234 for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
235 /* Assert EOL has a newline. */
236 if (*(lbuf + lbuflen - 1) != '\n') {
237 /* Can't have > 1 line with no trailing newline. */
238 mlbuf = malloc(lbuflen + 1);
241 memcpy(mlbuf, lbuf, lbuflen);
242 *(mlbuf + lbuflen) = '\n';
246 for (isdelim = 0, p = lbuf;; ++p) {
248 /* this should work if newline is delimiter */
252 if (!isdelim && !sflag)
253 (void)fwrite(lbuf, lbuflen, 1, stdout);
261 for (field = maxval, p = lbuf; field; --field, ++pos) {
265 while ((ch = *p++) != '\n' && ch != sep)
268 while ((ch = *p++) != '\n' && ch != sep)
278 for (; (ch = *p) != '\n'; ++p)
281 for (; (ch = *p) != '\n'; ++p);
292 (void)fprintf(stderr, "%s\n%s\n%s\n",
293 "usage: cut -b list [-n] [file ...]",
294 " cut -c list [file ...]",
295 " cut -f list [-s] [-d delim] [file ...]");