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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
37 static const char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
56 static char dcharmb[MB_LEN_MAX + 1];
63 static size_t autostart, autostop, maxval;
64 static char * positions;
66 static int b_cut(FILE *, const char *);
67 static int b_n_cut(FILE *, const char *);
68 static int c_cut(FILE *, const char *);
69 static int f_cut(FILE *, const char *);
70 static void get_list(char *);
71 static int is_delim(wchar_t);
72 static void needpos(size_t);
73 static void usage(void);
76 main(int argc, char *argv[])
79 int (*fcn)(FILE *, const char *);
83 setlocale(LC_ALL, "");
86 dchar = '\t'; /* default delimiter is \t */
87 strcpy(dcharmb, "\t");
89 while ((ch = getopt(argc, argv, "b:c:d:f:snw")) != -1)
100 n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL);
101 if (dchar == '\0' || n != strlen(optarg))
102 errx(1, "bad delimiter");
103 strcpy(dcharmb, optarg);
127 if (bflag || cflag || nflag || (wflag && dflag))
129 } else if (!(bflag || cflag) || dflag || sflag || wflag)
131 else if (!bflag && nflag)
137 fcn = MB_CUR_MAX > 1 ? c_cut : b_cut;
139 fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut;
143 for (; *argv; ++argv) {
144 if (strcmp(*argv, "-") == 0)
145 rval |= fcn(stdin, "stdin");
147 if (!(fp = fopen(*argv, "r"))) {
157 rval = fcn(stdin, "stdin");
164 size_t setautostart, start, stop;
169 * set a byte in the positions array to indicate if a field or
170 * column is to be selected; use +1, it's 1-based, not 0-based.
171 * Numbers and number ranges may be overlapping, repeated, and in
172 * any order. We handle "-3-5" although there's no real reason to.
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, "[-bcf] list: illegal list value");
197 errx(1, "[-bcf] 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 b_cut(FILE *fp, const char *fname __unused)
244 for (col = maxval; col; --col) {
245 if ((ch = getc(fp)) == EOF)
254 while ((ch = getc(fp)) != EOF && ch != '\n')
257 while ((ch = getc(fp)) != EOF && ch != '\n');
265 * Cut based on byte positions, taking care not to split multibyte characters.
266 * Although this function also handles the case where -n is not specified,
267 * b_cut() ought to be much faster.
270 b_n_cut(FILE *fp, const char *fname)
272 size_t col, i, lbuflen;
274 int canwrite, clen, warned;
277 memset(&mbs, 0, sizeof(mbs));
279 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
280 for (col = 0; lbuflen > 0; col += clen) {
281 if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
286 memset(&mbs, 0, sizeof(mbs));
289 if (clen == 0 || *lbuf == '\n')
291 if (col < maxval && !positions[1 + col]) {
293 * Print the character if (1) after an initial
294 * segment of un-selected bytes, the rest of
295 * it is selected, and (2) the last byte is
299 while (i < col + clen && i < maxval &&
302 canwrite = i < col + clen;
303 for (; i < col + clen && i < maxval; i++)
304 canwrite &= positions[1 + i];
306 fwrite(lbuf, 1, clen, stdout);
309 * Print the character if all of it has
313 for (i = col; i < col + clen; i++)
314 if ((i >= maxval && !autostop) ||
315 (i < maxval && !positions[1 + i])) {
320 fwrite(lbuf, 1, clen, stdout);
332 c_cut(FILE *fp, const char *fname)
341 for (col = maxval; col; --col) {
342 if ((ch = getwc(fp)) == WEOF)
351 while ((ch = getwc(fp)) != WEOF && ch != '\n')
354 while ((ch = getwc(fp)) != WEOF && ch != '\n');
356 (void)putwchar('\n');
370 if (ch == ' ' || ch == '\t')
380 f_cut(FILE *fp, const char *fname)
383 int field, i, isdelim;
387 size_t clen, lbuflen, reallen;
390 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
392 /* Assert EOL has a newline. */
393 if (*(lbuf + lbuflen - 1) != '\n') {
394 /* Can't have > 1 line with no trailing newline. */
395 mlbuf = malloc(lbuflen + 1);
398 memcpy(mlbuf, lbuf, lbuflen);
399 *(mlbuf + lbuflen) = '\n';
404 for (isdelim = 0, p = lbuf;; p += clen) {
405 clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
406 if (clen == (size_t)-1 || clen == (size_t)-2) {
407 warnc(EILSEQ, "%s", fname);
413 /* this should work if newline is delimiter */
417 if (!isdelim && !sflag)
418 (void)fwrite(lbuf, lbuflen, 1, stdout);
426 for (field = maxval, p = lbuf; field; --field, ++pos) {
427 if (*pos && output++)
428 for (i = 0; dcharmb[i] != '\0'; i++)
431 clen = mbrtowc(&ch, p, lbuf + reallen - p,
433 if (clen == (size_t)-1 || clen == (size_t)-2) {
434 warnc(EILSEQ, "%s", fname);
441 if (ch == '\n' || is_delim(ch)) {
442 /* compress whitespace */
443 if (wflag && ch != '\n')
449 for (i = 0; i < (int)clen; i++)
450 putchar(p[i - clen]);
458 for (i = 0; dcharmb[i] != '\0'; i++)
460 for (; (ch = *p) != '\n'; ++p)
463 for (; (ch = *p) != '\n'; ++p);
474 (void)fprintf(stderr, "%s\n%s\n%s\n",
475 "usage: cut -b list [-n] [file ...]",
476 " cut -c list [file ...]",
477 " cut -f list [-s] [-w | -d delim] [file ...]");