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$");
60 static char dcharmb[MB_LEN_MAX + 1];
67 static size_t autostart, autostop, maxval;
68 static char * positions;
70 static int b_cut(FILE *, const char *);
71 static int b_n_cut(FILE *, const char *);
72 static int c_cut(FILE *, const char *);
73 static int f_cut(FILE *, const char *);
74 static void get_list(char *);
75 static int is_delim(wchar_t);
76 static void needpos(size_t);
77 static void usage(void);
80 main(int argc, char *argv[])
83 int (*fcn)(FILE *, const char *);
87 setlocale(LC_ALL, "");
90 dchar = '\t'; /* default delimiter is \t */
91 strcpy(dcharmb, "\t");
93 while ((ch = getopt(argc, argv, "b:c:d:f:snw")) != -1)
104 n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL);
105 if (dchar == '\0' || n != strlen(optarg))
106 errx(1, "bad delimiter");
107 strcpy(dcharmb, optarg);
131 if (bflag || cflag || nflag || (wflag && dflag))
133 } else if (!(bflag || cflag) || dflag || sflag || wflag)
135 else if (!bflag && nflag)
141 fcn = MB_CUR_MAX > 1 ? c_cut : b_cut;
143 fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut;
147 for (; *argv; ++argv) {
148 if (strcmp(*argv, "-") == 0)
149 rval |= fcn(stdin, "stdin");
151 if (!(fp = fopen(*argv, "r"))) {
161 rval = fcn(stdin, "stdin");
168 size_t setautostart, start, stop;
173 * set a byte in the positions array to indicate if a field or
174 * column is to be selected; use +1, it's 1-based, not 0-based.
175 * Numbers and number ranges may be overlapping, repeated, and in
176 * any order. We handle "-3-5" although there's no real reason to.
178 for (; (p = strsep(&list, ", \t")) != NULL;) {
179 setautostart = start = stop = 0;
184 if (isdigit((unsigned char)*p)) {
185 start = stop = strtol(p, &p, 10);
186 if (setautostart && start > autostart)
190 if (isdigit((unsigned char)p[1]))
191 stop = strtol(p + 1, &p, 10);
194 if (!autostop || autostop > stop)
199 errx(1, "[-bcf] list: illegal list value");
201 errx(1, "[-bcf] list: values may not include zero");
206 for (pos = positions + start; start++ <= stop; *pos++ = 1);
209 /* overlapping ranges */
210 if (autostop && maxval > autostop) {
217 memset(positions + 1, '1', autostart);
226 /* Grow the positions array to at least the specified size. */
233 if ((positions = realloc(positions, npos)) == NULL)
235 memset((char *)positions + oldnpos, 0, npos - oldnpos);
240 b_cut(FILE *fp, const char *fname __unused)
248 for (col = maxval; col; --col) {
249 if ((ch = getc(fp)) == EOF)
258 while ((ch = getc(fp)) != EOF && ch != '\n')
261 while ((ch = getc(fp)) != EOF && ch != '\n');
269 * Cut based on byte positions, taking care not to split multibyte characters.
270 * Although this function also handles the case where -n is not specified,
271 * b_cut() ought to be much faster.
274 b_n_cut(FILE *fp, const char *fname)
276 size_t col, i, lbuflen;
278 int canwrite, clen, warned;
281 memset(&mbs, 0, sizeof(mbs));
283 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
284 for (col = 0; lbuflen > 0; col += clen) {
285 if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
290 memset(&mbs, 0, sizeof(mbs));
293 if (clen == 0 || *lbuf == '\n')
295 if (col < maxval && !positions[1 + col]) {
297 * Print the character if (1) after an initial
298 * segment of un-selected bytes, the rest of
299 * it is selected, and (2) the last byte is
303 while (i < col + clen && i < maxval &&
306 canwrite = i < col + clen;
307 for (; i < col + clen && i < maxval; i++)
308 canwrite &= positions[1 + i];
310 fwrite(lbuf, 1, clen, stdout);
313 * Print the character if all of it has
317 for (i = col; i < col + clen; i++)
318 if ((i >= maxval && !autostop) ||
319 (i < maxval && !positions[1 + i])) {
324 fwrite(lbuf, 1, clen, stdout);
336 c_cut(FILE *fp, const char *fname)
345 for (col = maxval; col; --col) {
346 if ((ch = getwc(fp)) == WEOF)
355 while ((ch = getwc(fp)) != WEOF && ch != '\n')
358 while ((ch = getwc(fp)) != WEOF && ch != '\n');
360 (void)putwchar('\n');
374 if (ch == ' ' || ch == '\t')
384 f_cut(FILE *fp, const char *fname)
387 int field, i, isdelim;
391 size_t clen, lbuflen, reallen;
394 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
396 /* Assert EOL has a newline. */
397 if (*(lbuf + lbuflen - 1) != '\n') {
398 /* Can't have > 1 line with no trailing newline. */
399 mlbuf = malloc(lbuflen + 1);
402 memcpy(mlbuf, lbuf, lbuflen);
403 *(mlbuf + lbuflen) = '\n';
408 for (isdelim = 0, p = lbuf;; p += clen) {
409 clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
410 if (clen == (size_t)-1 || clen == (size_t)-2) {
411 warnc(EILSEQ, "%s", fname);
417 /* this should work if newline is delimiter */
421 if (!isdelim && !sflag)
422 (void)fwrite(lbuf, lbuflen, 1, stdout);
430 for (field = maxval, p = lbuf; field; --field, ++pos) {
431 if (*pos && output++)
432 for (i = 0; dcharmb[i] != '\0'; i++)
435 clen = mbrtowc(&ch, p, lbuf + reallen - p,
437 if (clen == (size_t)-1 || clen == (size_t)-2) {
438 warnc(EILSEQ, "%s", fname);
445 if (ch == '\n' || is_delim(ch)) {
446 /* compress whitespace */
447 if (wflag && ch != '\n')
453 for (i = 0; i < (int)clen; i++)
454 putchar(p[i - clen]);
462 for (i = 0; dcharmb[i] != '\0'; i++)
464 for (; (ch = *p) != '\n'; ++p)
467 for (; (ch = *p) != '\n'; ++p);
478 (void)fprintf(stderr, "%s\n%s\n%s\n",
479 "usage: cut -b list [-n] [file ...]",
480 " cut -c list [file ...]",
481 " cut -f list [-s] [-w | -d delim] [file ...]");