2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39 static const char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
58 static char dcharmb[MB_LEN_MAX + 1];
65 static size_t autostart, autostop, maxval;
66 static char * positions;
68 static int b_cut(FILE *, const char *);
69 static int b_n_cut(FILE *, const char *);
70 static int c_cut(FILE *, const char *);
71 static int f_cut(FILE *, const char *);
72 static void get_list(char *);
73 static int is_delim(wchar_t);
74 static void needpos(size_t);
75 static void usage(void);
78 main(int argc, char *argv[])
81 int (*fcn)(FILE *, const char *);
85 setlocale(LC_ALL, "");
88 dchar = '\t'; /* default delimiter is \t */
89 strcpy(dcharmb, "\t");
91 while ((ch = getopt(argc, argv, "b:c:d:f:snw")) != -1)
102 n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL);
103 if (dchar == '\0' || n != strlen(optarg))
104 errx(1, "bad delimiter");
105 strcpy(dcharmb, optarg);
129 if (bflag || cflag || nflag || (wflag && dflag))
131 } else if (!(bflag || cflag) || dflag || sflag || wflag)
133 else if (!bflag && nflag)
139 fcn = MB_CUR_MAX > 1 ? c_cut : b_cut;
141 fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut;
145 for (; *argv; ++argv) {
146 if (strcmp(*argv, "-") == 0)
147 rval |= fcn(stdin, "stdin");
149 if (!(fp = fopen(*argv, "r"))) {
159 rval = fcn(stdin, "stdin");
166 size_t setautostart, start, stop;
171 * set a byte in the positions array to indicate if a field or
172 * column is to be selected; use +1, it's 1-based, not 0-based.
173 * Numbers and number ranges may be overlapping, repeated, and in
174 * any order. We handle "-3-5" although there's no real reason to.
176 for (; (p = strsep(&list, ", \t")) != NULL;) {
177 setautostart = start = stop = 0;
182 if (isdigit((unsigned char)*p)) {
183 start = stop = strtol(p, &p, 10);
184 if (setautostart && start > autostart)
188 if (isdigit((unsigned char)p[1]))
189 stop = strtol(p + 1, &p, 10);
192 if (!autostop || autostop > stop)
197 errx(1, "[-bcf] list: illegal list value");
199 errx(1, "[-bcf] list: values may not include zero");
204 for (pos = positions + start; start++ <= stop; *pos++ = 1);
207 /* overlapping ranges */
208 if (autostop && maxval > autostop) {
213 /* reversed range with autostart */
214 if (maxval < autostart) {
221 memset(positions + 1, '1', autostart);
230 /* Grow the positions array to at least the specified size. */
237 if ((positions = realloc(positions, npos)) == NULL)
239 memset((char *)positions + oldnpos, 0, npos - oldnpos);
244 b_cut(FILE *fp, const char *fname __unused)
252 for (col = maxval; col; --col) {
253 if ((ch = getc(fp)) == EOF)
262 while ((ch = getc(fp)) != EOF && ch != '\n')
265 while ((ch = getc(fp)) != EOF && ch != '\n');
273 * Cut based on byte positions, taking care not to split multibyte characters.
274 * Although this function also handles the case where -n is not specified,
275 * b_cut() ought to be much faster.
278 b_n_cut(FILE *fp, const char *fname)
280 size_t col, i, lbuflen;
282 int canwrite, clen, warned;
285 memset(&mbs, 0, sizeof(mbs));
287 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
288 for (col = 0; lbuflen > 0; col += clen) {
289 if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
294 memset(&mbs, 0, sizeof(mbs));
297 if (clen == 0 || *lbuf == '\n')
299 if (col < maxval && !positions[1 + col]) {
301 * Print the character if (1) after an initial
302 * segment of un-selected bytes, the rest of
303 * it is selected, and (2) the last byte is
307 while (i < col + clen && i < maxval &&
310 canwrite = i < col + clen;
311 for (; i < col + clen && i < maxval; i++)
312 canwrite &= positions[1 + i];
314 fwrite(lbuf, 1, clen, stdout);
317 * Print the character if all of it has
321 for (i = col; i < col + clen; i++)
322 if ((i >= maxval && !autostop) ||
323 (i < maxval && !positions[1 + i])) {
328 fwrite(lbuf, 1, clen, stdout);
340 c_cut(FILE *fp, const char *fname)
349 for (col = maxval; col; --col) {
350 if ((ch = getwc(fp)) == WEOF)
359 while ((ch = getwc(fp)) != WEOF && ch != '\n')
362 while ((ch = getwc(fp)) != WEOF && ch != '\n');
364 (void)putwchar('\n');
378 if (ch == ' ' || ch == '\t')
388 f_cut(FILE *fp, const char *fname)
391 int field, i, isdelim;
395 size_t clen, lbuflen, reallen;
398 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
400 /* Assert EOL has a newline. */
401 if (*(lbuf + lbuflen - 1) != '\n') {
402 /* Can't have > 1 line with no trailing newline. */
403 mlbuf = malloc(lbuflen + 1);
406 memcpy(mlbuf, lbuf, lbuflen);
407 *(mlbuf + lbuflen) = '\n';
412 for (isdelim = 0, p = lbuf;; p += clen) {
413 clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
414 if (clen == (size_t)-1 || clen == (size_t)-2) {
415 warnc(EILSEQ, "%s", fname);
421 /* this should work if newline is delimiter */
425 if (!isdelim && !sflag)
426 (void)fwrite(lbuf, lbuflen, 1, stdout);
434 for (field = maxval, p = lbuf; field; --field, ++pos) {
435 if (*pos && output++)
436 for (i = 0; dcharmb[i] != '\0'; i++)
439 clen = mbrtowc(&ch, p, lbuf + reallen - p,
441 if (clen == (size_t)-1 || clen == (size_t)-2) {
442 warnc(EILSEQ, "%s", fname);
449 if (ch == '\n' || is_delim(ch)) {
450 /* compress whitespace */
451 if (wflag && ch != '\n')
457 for (i = 0; i < (int)clen; i++)
458 putchar(p[i - clen]);
466 for (i = 0; dcharmb[i] != '\0'; i++)
468 for (; (ch = *p) != '\n'; ++p)
471 for (; (ch = *p) != '\n'; ++p);
482 (void)fprintf(stderr, "%s\n%s\n%s\n",
483 "usage: cut -b list [-n] [file ...]",
484 " cut -c list [file ...]",
485 " cut -f list [-s] [-w | -d delim] [file ...]");