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
32 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)cut.c 8.3 (Berkeley) 5/4/95
34 * $FreeBSD: src/usr.bin/cut/cut.c,v 1.9.2.3 2001/07/30 09:59:16 dd Exp $
51 static char dcharmb[MB_LEN_MAX + 1];
57 static size_t autostart, autostop, maxval;
58 static char * positions;
60 static int b_cut(FILE *, const char *);
61 static int b_n_cut(FILE *, const char *);
62 static int c_cut(FILE *, const char *);
63 static int f_cut(FILE *, const char *);
64 static void get_list(char *);
65 static void needpos(size_t);
66 static void usage(void);
69 main(int argc, char *argv[])
72 int (*fcn)(FILE *, const char *);
76 setlocale (LC_ALL, "");
79 dchar = '\t'; /* default delimiter is \t */
80 strcpy(dcharmb, "\t");
82 while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
93 n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL);
94 if (dchar == '\0' || n != strlen(optarg))
95 errx(1, "bad delimiter");
96 strcpy(dcharmb, optarg);
117 if (bflag || cflag || nflag)
119 } else if (!(bflag || cflag) || dflag || sflag)
121 else if (!bflag && nflag)
127 fcn = MB_CUR_MAX > 1 ? c_cut : b_cut;
129 fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut;
133 for (; *argv; ++argv) {
134 if (strcmp(*argv, "-") == 0)
135 rval |= fcn(stdin, "stdin");
137 if (!(fp = fopen(*argv, "r"))) {
147 rval = fcn(stdin, "stdin");
154 size_t setautostart, start, stop;
159 * set a byte in the positions array to indicate if a field or
160 * column is to be selected; use +1, it's 1-based, not 0-based.
161 * Numbers and number ranges may be overlapping, repeated, and in
162 * any order. We handle "-3-5" although there's no real reason to.
164 for (; (p = strsep(&list, ", \t")) != NULL;) {
165 setautostart = start = stop = 0;
170 if (isdigit((unsigned char)*p)) {
171 start = stop = strtol(p, &p, 10);
172 if (setautostart && start > autostart)
176 if (isdigit((unsigned char)p[1]))
177 stop = strtol(p + 1, &p, 10);
180 if (!autostop || autostop > stop)
185 errx(1, "[-bcf] list: illegal list value");
187 errx(1, "[-bcf] list: values may not include zero");
192 for (pos = positions + start; start++ <= stop; *pos++ = 1);
195 /* overlapping ranges */
196 if (autostop && maxval > autostop) {
203 memset(positions + 1, '1', autostart);
212 /* Grow the positions array to at least the specified size. */
219 if ((positions = realloc(positions, npos)) == NULL)
221 memset((char *)positions + oldnpos, 0, npos - oldnpos);
226 b_cut(FILE *fp, const char *fname __unused)
234 for (col = maxval; col; --col) {
235 if ((ch = getc(fp)) == EOF)
244 while ((ch = getc(fp)) != EOF && ch != '\n')
247 while ((ch = getc(fp)) != EOF && ch != '\n');
255 * Cut based on byte positions, taking care not to split multibyte characters.
256 * Although this function also handles the case where -n is not specified,
257 * b_cut() ought to be much faster.
260 b_n_cut(FILE *fp, const char *fname)
262 size_t col, i, lbuflen;
264 int canwrite, clen, warned;
267 memset(&mbs, 0, sizeof(mbs));
269 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
270 for (col = 0; lbuflen > 0; col += clen) {
271 if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
276 memset(&mbs, 0, sizeof(mbs));
279 if (clen == 0 || *lbuf == '\n')
281 if (col < maxval && !positions[1 + col]) {
283 * Print the character if (1) after an initial
284 * segment of un-selected bytes, the rest of
285 * it is selected, and (2) the last byte is
289 while (i < col + clen && i < maxval &&
292 canwrite = i < col + clen;
293 for (; i < col + clen && i < maxval; i++)
294 canwrite &= positions[1 + i];
296 fwrite(lbuf, 1, clen, stdout);
299 * Print the character if all of it has
303 for (i = col; i < col + clen; i++)
304 if ((i >= maxval && !autostop) ||
305 (i < maxval && !positions[1 + i])) {
310 fwrite(lbuf, 1, clen, stdout);
322 c_cut(FILE *fp, const char *fname)
331 for (col = maxval; col; --col) {
332 if ((ch = getwc(fp)) == WEOF)
341 while ((ch = getwc(fp)) != WEOF && ch != '\n')
344 while ((ch = getwc(fp)) != WEOF && ch != '\n');
346 (void)putwchar('\n');
357 f_cut(FILE *fp, const char *fname)
360 int field, i, isdelim;
365 size_t clen, lbuflen, reallen;
368 for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
370 /* Assert EOL has a newline. */
371 if (*(lbuf + lbuflen - 1) != '\n') {
372 /* Can't have > 1 line with no trailing newline. */
373 mlbuf = malloc(lbuflen + 1);
376 memcpy(mlbuf, lbuf, lbuflen);
377 *(mlbuf + lbuflen) = '\n';
382 for (isdelim = 0, p = lbuf;; p += clen) {
383 clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
384 if (clen == (size_t)-1 || clen == (size_t)-2) {
385 warnc(EILSEQ, "%s", fname);
391 /* this should work if newline is delimiter */
395 if (!isdelim && !sflag)
396 (void)fwrite(lbuf, lbuflen, 1, stdout);
404 for (field = maxval, p = lbuf; field; --field, ++pos) {
405 if (*pos && output++)
406 for (i = 0; dcharmb[i] != '\0'; i++)
409 clen = mbrtowc(&ch, p, lbuf + reallen - p,
411 if (clen == (size_t)-1 || clen == (size_t)-2) {
412 warnc(EILSEQ, "%s", fname);
419 if (ch == '\n' || ch == sep)
422 for (i = 0; i < (int)clen; i++)
423 putchar(p[i - clen]);
431 for (i = 0; dcharmb[i] != '\0'; i++)
433 for (; (ch = *p) != '\n'; ++p)
436 for (; (ch = *p) != '\n'; ++p);
447 (void)fprintf(stderr, "%s\n%s\n%s\n",
448 "usage: cut -b list [-n] [file ...]",
449 " cut -c list [file ...]",
450 " cut -f list [-s] [-d delim] [file ...]");