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";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
63 void b_n_cut(FILE *, const char *);
64 void c_cut(FILE *, const char *);
65 void f_cut(FILE *, const char *);
66 void get_list(char *);
67 int main(int, char **);
69 static void usage(void);
77 void (*fcn)(FILE *, const char *);
80 setlocale(LC_ALL, "");
83 dchar = '\t'; /* default delimiter is \t */
86 * Since we don't support multi-byte characters, the -c and -b
87 * options are equivalent.
89 while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
124 if (bflag || cflag || nflag)
126 } else if (!(bflag || cflag) || dflag || sflag)
128 else if (!bflag && nflag)
136 for (; *argv; ++argv) {
137 if (strcmp(*argv, "-") == 0)
140 if (!(fp = fopen(*argv, "r"))) {
154 size_t autostart, autostop, maxval;
162 size_t setautostart, start, stop;
167 * set a byte in the positions array to indicate if a field or
168 * column is to be selected; use +1, it's 1-based, not 0-based.
169 * This parser is less restrictive than the Draft 9 POSIX spec.
170 * POSIX doesn't allow lists that aren't in increasing order or
171 * overlapping lists. We also handle "-3-5" although there's no
174 for (; (p = strsep(&list, ", \t")) != NULL;) {
175 setautostart = start = stop = 0;
180 if (isdigit((unsigned char)*p)) {
181 start = stop = strtol(p, &p, 10);
182 if (setautostart && start > autostart)
186 if (isdigit((unsigned char)p[1]))
187 stop = strtol(p + 1, &p, 10);
190 if (!autostop || autostop > stop)
195 errx(1, "[-cf] list: illegal list value");
197 errx(1, "[-cf] list: values may not include zero");
202 for (pos = positions + start; start++ <= stop; *pos++ = 1);
205 /* overlapping ranges */
206 if (autostop && maxval > autostop) {
213 memset(positions + 1, '1', autostart);
222 /* Grow the positions array to at least the specified size. */
229 if ((positions = realloc(positions, npos)) == NULL)
231 memset((char *)positions + oldnpos, 0, npos - oldnpos);
236 * Cut based on byte positions, taking care not to split multibyte characters.
237 * Although this function also handles the case where -n is not specified,
238 * c_cut() ought to be much faster.
245 size_t col, i, lbuflen;
247 int canwrite, clen, warned;
250 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
251 for (col = 0; lbuflen > 0; col += clen) {
252 if ((clen = mblen(lbuf, lbuflen)) < 0) {
259 if (clen == 0 || *lbuf == '\n')
261 if (col < maxval && !positions[1 + col]) {
263 * Print the character if (1) after an initial
264 * segment of un-selected bytes, the rest of
265 * it is selected, and (2) the last byte is
269 while (i < col + clen && i < maxval &&
272 canwrite = i < col + clen;
273 for (; i < col + clen && i < maxval; i++)
274 canwrite &= positions[1 + i];
276 fwrite(lbuf, 1, clen, stdout);
279 * Print the character if all of it has
283 for (i = col; i < col + clen; i++)
284 if ((i >= maxval && !autostop) ||
285 (i < maxval && !positions[1 + i])) {
290 fwrite(lbuf, 1, clen, stdout);
303 const char *fname __unused;
311 for (col = maxval; col; --col) {
312 if ((ch = getc(fp)) == EOF)
321 while ((ch = getc(fp)) != EOF && ch != '\n')
324 while ((ch = getc(fp)) != EOF && ch != '\n');
333 const char *fname __unused;
335 int ch, field, isdelim;
342 for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
343 /* Assert EOL has a newline. */
344 if (*(lbuf + lbuflen - 1) != '\n') {
345 /* Can't have > 1 line with no trailing newline. */
346 mlbuf = malloc(lbuflen + 1);
349 memcpy(mlbuf, lbuf, lbuflen);
350 *(mlbuf + lbuflen) = '\n';
354 for (isdelim = 0, p = lbuf;; ++p) {
356 /* this should work if newline is delimiter */
360 if (!isdelim && !sflag)
361 (void)fwrite(lbuf, lbuflen, 1, stdout);
369 for (field = maxval, p = lbuf; field; --field, ++pos) {
373 while ((ch = *p++) != '\n' && ch != sep)
376 while ((ch = *p++) != '\n' && ch != sep)
386 for (; (ch = *p) != '\n'; ++p)
389 for (; (ch = *p) != '\n'; ++p);
400 (void)fprintf(stderr, "%s\n%s\n%s\n",
401 "usage: cut -b list [-n] [file ...]",
402 " cut -c list [file ...]",
403 " cut -f list [-s] [-d delim] [file ...]");