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 char dcharmb[MB_LEN_MAX + 1];
62 size_t autostart, autostop, maxval;
65 int b_cut(FILE *, const char *);
66 int b_n_cut(FILE *, const char *);
67 int c_cut(FILE *, const char *);
68 int f_cut(FILE *, const char *);
69 void get_list(char *);
71 static void usage(void);
74 main(int argc, char *argv[])
77 int (*fcn)(FILE *, const char *);
81 setlocale(LC_ALL, "");
84 dchar = '\t'; /* default delimiter is \t */
85 strcpy(dcharmb, "\t");
87 while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
98 n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL);
99 if (dchar == '\0' || n != strlen(optarg))
100 errx(1, "bad delimiter");
101 strcpy(dcharmb, optarg);
122 if (bflag || cflag || nflag)
124 } else if (!(bflag || cflag) || dflag || sflag)
126 else if (!bflag && nflag)
132 fcn = MB_CUR_MAX > 1 ? c_cut : b_cut;
134 fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut;
138 for (; *argv; ++argv) {
139 if (strcmp(*argv, "-") == 0)
140 rval |= fcn(stdin, "stdin");
142 if (!(fp = fopen(*argv, "r"))) {
152 rval = fcn(stdin, "stdin");
159 size_t setautostart, start, stop;
164 * set a byte in the positions array to indicate if a field or
165 * column is to be selected; use +1, it's 1-based, not 0-based.
166 * Numbers and number ranges may be overlapping, repeated, and in
167 * any order. We handle "-3-5" although there's no real reason too.
169 for (; (p = strsep(&list, ", \t")) != NULL;) {
170 setautostart = start = stop = 0;
175 if (isdigit((unsigned char)*p)) {
176 start = stop = strtol(p, &p, 10);
177 if (setautostart && start > autostart)
181 if (isdigit((unsigned char)p[1]))
182 stop = strtol(p + 1, &p, 10);
185 if (!autostop || autostop > stop)
190 errx(1, "[-cf] list: illegal list value");
192 errx(1, "[-cf] list: values may not include zero");
197 for (pos = positions + start; start++ <= stop; *pos++ = 1);
200 /* overlapping ranges */
201 if (autostop && maxval > autostop) {
208 memset(positions + 1, '1', autostart);
217 /* Grow the positions array to at least the specified size. */
224 if ((positions = realloc(positions, npos)) == NULL)
226 memset((char *)positions + oldnpos, 0, npos - oldnpos);
231 b_cut(FILE *fp, const char *fname __unused)
239 for (col = maxval; col; --col) {
240 if ((ch = getc(fp)) == EOF)
249 while ((ch = getc(fp)) != EOF && ch != '\n')
252 while ((ch = getc(fp)) != EOF && ch != '\n');
260 * Cut based on byte positions, taking care not to split multibyte characters.
261 * Although this function also handles the case where -n is not specified,
262 * b_cut() ought to be much faster.
265 b_n_cut(FILE *fp, const char *fname)
267 size_t col, i, lbuflen;
269 int canwrite, clen, warned;
272 memset(&mbs, 0, sizeof(mbs));
274 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
275 for (col = 0; lbuflen > 0; col += clen) {
276 if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
281 memset(&mbs, 0, sizeof(mbs));
284 if (clen == 0 || *lbuf == '\n')
286 if (col < maxval && !positions[1 + col]) {
288 * Print the character if (1) after an initial
289 * segment of un-selected bytes, the rest of
290 * it is selected, and (2) the last byte is
294 while (i < col + clen && i < maxval &&
297 canwrite = i < col + clen;
298 for (; i < col + clen && i < maxval; i++)
299 canwrite &= positions[1 + i];
301 fwrite(lbuf, 1, clen, stdout);
304 * Print the character if all of it has
308 for (i = col; i < col + clen; i++)
309 if ((i >= maxval && !autostop) ||
310 (i < maxval && !positions[1 + i])) {
315 fwrite(lbuf, 1, clen, stdout);
327 c_cut(FILE *fp, const char *fname)
336 for (col = maxval; col; --col) {
337 if ((ch = getwc(fp)) == WEOF)
346 while ((ch = getwc(fp)) != WEOF && ch != '\n')
349 while ((ch = getwc(fp)) != WEOF && ch != '\n');
351 (void)putwchar('\n');
362 f_cut(FILE *fp, const char *fname)
365 int field, i, isdelim;
370 size_t clen, lbuflen, reallen;
373 for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
375 /* Assert EOL has a newline. */
376 if (*(lbuf + lbuflen - 1) != '\n') {
377 /* Can't have > 1 line with no trailing newline. */
378 mlbuf = malloc(lbuflen + 1);
381 memcpy(mlbuf, lbuf, lbuflen);
382 *(mlbuf + lbuflen) = '\n';
387 for (isdelim = 0, p = lbuf;; p += clen) {
388 clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
389 if (clen == (size_t)-1 || clen == (size_t)-2) {
390 warnc(EILSEQ, "%s", fname);
396 /* this should work if newline is delimiter */
400 if (!isdelim && !sflag)
401 (void)fwrite(lbuf, lbuflen, 1, stdout);
409 for (field = maxval, p = lbuf; field; --field, ++pos) {
410 if (*pos && output++)
411 for (i = 0; dcharmb[i] != '\0'; i++)
414 clen = mbrtowc(&ch, p, lbuf + reallen - p,
416 if (clen == (size_t)-1 || clen == (size_t)-2) {
417 warnc(EILSEQ, "%s", fname);
424 if (ch == '\n' || ch == sep)
427 for (i = 0; i < (int)clen; i++)
428 putchar(p[i - clen]);
436 for (i = 0; dcharmb[i] != '\0'; i++)
438 for (; (ch = *p) != '\n'; ++p)
441 for (; (ch = *p) != '\n'; ++p);
452 (void)fprintf(stderr, "%s\n%s\n%s\n",
453 "usage: cut -b list [-n] [file ...]",
454 " cut -c list [file ...]",
455 " cut -f list [-s] [-d delim] [file ...]");