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) {
215 memset(positions + 1, '1', autostart);
224 /* Grow the positions array to at least the specified size. */
231 if ((positions = realloc(positions, npos)) == NULL)
233 memset((char *)positions + oldnpos, 0, npos - oldnpos);
238 b_cut(FILE *fp, const char *fname __unused)
246 for (col = maxval; col; --col) {
247 if ((ch = getc(fp)) == EOF)
256 while ((ch = getc(fp)) != EOF && ch != '\n')
259 while ((ch = getc(fp)) != EOF && ch != '\n');
267 * Cut based on byte positions, taking care not to split multibyte characters.
268 * Although this function also handles the case where -n is not specified,
269 * b_cut() ought to be much faster.
272 b_n_cut(FILE *fp, const char *fname)
274 size_t col, i, lbuflen;
276 int canwrite, clen, warned;
279 memset(&mbs, 0, sizeof(mbs));
281 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
282 for (col = 0; lbuflen > 0; col += clen) {
283 if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
288 memset(&mbs, 0, sizeof(mbs));
291 if (clen == 0 || *lbuf == '\n')
293 if (col < maxval && !positions[1 + col]) {
295 * Print the character if (1) after an initial
296 * segment of un-selected bytes, the rest of
297 * it is selected, and (2) the last byte is
301 while (i < col + clen && i < maxval &&
304 canwrite = i < col + clen;
305 for (; i < col + clen && i < maxval; i++)
306 canwrite &= positions[1 + i];
308 fwrite(lbuf, 1, clen, stdout);
311 * Print the character if all of it has
315 for (i = col; i < col + clen; i++)
316 if ((i >= maxval && !autostop) ||
317 (i < maxval && !positions[1 + i])) {
322 fwrite(lbuf, 1, clen, stdout);
334 c_cut(FILE *fp, const char *fname)
343 for (col = maxval; col; --col) {
344 if ((ch = getwc(fp)) == WEOF)
353 while ((ch = getwc(fp)) != WEOF && ch != '\n')
356 while ((ch = getwc(fp)) != WEOF && ch != '\n');
358 (void)putwchar('\n');
372 if (ch == ' ' || ch == '\t')
382 f_cut(FILE *fp, const char *fname)
385 int field, i, isdelim;
389 size_t clen, lbuflen, reallen;
392 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
394 /* Assert EOL has a newline. */
395 if (*(lbuf + lbuflen - 1) != '\n') {
396 /* Can't have > 1 line with no trailing newline. */
397 mlbuf = malloc(lbuflen + 1);
400 memcpy(mlbuf, lbuf, lbuflen);
401 *(mlbuf + lbuflen) = '\n';
406 for (isdelim = 0, p = lbuf;; p += clen) {
407 clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
408 if (clen == (size_t)-1 || clen == (size_t)-2) {
409 warnc(EILSEQ, "%s", fname);
415 /* this should work if newline is delimiter */
419 if (!isdelim && !sflag)
420 (void)fwrite(lbuf, lbuflen, 1, stdout);
428 for (field = maxval, p = lbuf; field; --field, ++pos) {
429 if (*pos && output++)
430 for (i = 0; dcharmb[i] != '\0'; i++)
433 clen = mbrtowc(&ch, p, lbuf + reallen - p,
435 if (clen == (size_t)-1 || clen == (size_t)-2) {
436 warnc(EILSEQ, "%s", fname);
443 if (ch == '\n' || is_delim(ch)) {
444 /* compress whitespace */
445 if (wflag && ch != '\n')
451 for (i = 0; i < (int)clen; i++)
452 putchar(p[i - clen]);
460 for (i = 0; dcharmb[i] != '\0'; i++)
462 for (; (ch = *p) != '\n'; ++p)
465 for (; (ch = *p) != '\n'; ++p);
476 (void)fprintf(stderr, "%s\n%s\n%s\n",
477 "usage: cut -b list [-n] [file ...]",
478 " cut -c list [file ...]",
479 " cut -f list [-s] [-w | -d delim] [file ...]");