xref: /freebsd/usr.bin/cut/cut.c (revision 6872fd3c9470e867df5ff4cdcbcfe758248b6b48)
19b50d902SRodney W. Grimes /*
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes  * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
99b50d902SRodney W. Grimes  *
109b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes  * are met:
139b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes  *    without specific prior written permission.
219b50d902SRodney W. Grimes  *
229b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes  * SUCH DAMAGE.
339b50d902SRodney W. Grimes  */
349b50d902SRodney W. Grimes 
359b50d902SRodney W. Grimes #ifndef lint
36fa146c53SArchie Cobbs static const char copyright[] =
379b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
389b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
39fa146c53SArchie Cobbs static const char sccsid[] = "@(#)cut.c	8.3 (Berkeley) 5/4/95";
409b50d902SRodney W. Grimes #endif /* not lint */
4177c8bf7cSDavid E. O'Brien #include <sys/cdefs.h>
4277c8bf7cSDavid E. O'Brien __FBSDID("$FreeBSD$");
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <ctype.h>
45812bff99SPhilippe Charnier #include <err.h>
46a5c4bafcSTim J. Robbins #include <errno.h>
479b50d902SRodney W. Grimes #include <limits.h>
48d51c6625SEivind Eklund #include <locale.h>
499b50d902SRodney W. Grimes #include <stdio.h>
509b50d902SRodney W. Grimes #include <stdlib.h>
519b50d902SRodney W. Grimes #include <string.h>
52df3f5d9dSPeter Wemm #include <unistd.h>
53d900c384STim J. Robbins #include <wchar.h>
549b50d902SRodney W. Grimes 
5541b662c5SEd Schouten static int	bflag;
5641b662c5SEd Schouten static int	cflag;
5741b662c5SEd Schouten static wchar_t	dchar;
5841b662c5SEd Schouten static char	dcharmb[MB_LEN_MAX + 1];
5941b662c5SEd Schouten static int	dflag;
6041b662c5SEd Schouten static int	fflag;
6141b662c5SEd Schouten static int	nflag;
6241b662c5SEd Schouten static int	sflag;
6318ba28c8SEitan Adler static int	wflag;
649b50d902SRodney W. Grimes 
6541b662c5SEd Schouten static size_t	autostart, autostop, maxval;
6641b662c5SEd Schouten static char *	positions;
673b90bf79STim J. Robbins 
6841b662c5SEd Schouten static int	b_cut(FILE *, const char *);
6941b662c5SEd Schouten static int	b_n_cut(FILE *, const char *);
7041b662c5SEd Schouten static int	c_cut(FILE *, const char *);
7141b662c5SEd Schouten static int	f_cut(FILE *, const char *);
7241b662c5SEd Schouten static void	get_list(char *);
73bd9dd0c6SAndrew Turner static int	is_delim(wchar_t);
7441b662c5SEd Schouten static void	needpos(size_t);
7599557a79SWill Andrews static void	usage(void);
769b50d902SRodney W. Grimes 
779b50d902SRodney W. Grimes int
78f4ac32deSDavid Malone main(int argc, char *argv[])
799b50d902SRodney W. Grimes {
809b50d902SRodney W. Grimes 	FILE *fp;
81364d0a91STim J. Robbins 	int (*fcn)(FILE *, const char *);
82a6ea32c3STim J. Robbins 	int ch, rval;
83a5c4bafcSTim J. Robbins 	size_t n;
849b50d902SRodney W. Grimes 
85d51c6625SEivind Eklund 	setlocale(LC_ALL, "");
86d51c6625SEivind Eklund 
8793738f50STim J. Robbins 	fcn = NULL;
889b50d902SRodney W. Grimes 	dchar = '\t';			/* default delimiter is \t */
89a5c4bafcSTim J. Robbins 	strcpy(dcharmb, "\t");
909b50d902SRodney W. Grimes 
9118ba28c8SEitan Adler 	while ((ch = getopt(argc, argv, "b:c:d:f:snw")) != -1)
929b50d902SRodney W. Grimes 		switch(ch) {
93d51c6625SEivind Eklund 		case 'b':
94393cf508STim J. Robbins 			get_list(optarg);
95393cf508STim J. Robbins 			bflag = 1;
96393cf508STim J. Robbins 			break;
979b50d902SRodney W. Grimes 		case 'c':
989b50d902SRodney W. Grimes 			get_list(optarg);
999b50d902SRodney W. Grimes 			cflag = 1;
1009b50d902SRodney W. Grimes 			break;
1019b50d902SRodney W. Grimes 		case 'd':
102a5c4bafcSTim J. Robbins 			n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL);
103a5c4bafcSTim J. Robbins 			if (dchar == '\0' || n != strlen(optarg))
104a5c4bafcSTim J. Robbins 				errx(1, "bad delimiter");
105a5c4bafcSTim J. Robbins 			strcpy(dcharmb, optarg);
1069b50d902SRodney W. Grimes 			dflag = 1;
1079b50d902SRodney W. Grimes 			break;
1089b50d902SRodney W. Grimes 		case 'f':
1099b50d902SRodney W. Grimes 			get_list(optarg);
1109b50d902SRodney W. Grimes 			fflag = 1;
1119b50d902SRodney W. Grimes 			break;
1129b50d902SRodney W. Grimes 		case 's':
1139b50d902SRodney W. Grimes 			sflag = 1;
1149b50d902SRodney W. Grimes 			break;
115d51c6625SEivind Eklund 		case 'n':
116393cf508STim J. Robbins 			nflag = 1;
117d51c6625SEivind Eklund 			break;
11818ba28c8SEitan Adler 		case 'w':
11918ba28c8SEitan Adler 			wflag = 1;
12018ba28c8SEitan Adler 			break;
1219b50d902SRodney W. Grimes 		case '?':
1229b50d902SRodney W. Grimes 		default:
1239b50d902SRodney W. Grimes 			usage();
1249b50d902SRodney W. Grimes 		}
1259b50d902SRodney W. Grimes 	argc -= optind;
1269b50d902SRodney W. Grimes 	argv += optind;
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes 	if (fflag) {
12918ba28c8SEitan Adler 		if (bflag || cflag || nflag || (wflag && dflag))
1309b50d902SRodney W. Grimes 			usage();
13118ba28c8SEitan Adler 	} else if (!(bflag || cflag) || dflag || sflag || wflag)
1329b50d902SRodney W. Grimes 		usage();
133393cf508STim J. Robbins 	else if (!bflag && nflag)
134393cf508STim J. Robbins 		usage();
135393cf508STim J. Robbins 
136364d0a91STim J. Robbins 	if (fflag)
137364d0a91STim J. Robbins 		fcn = f_cut;
138364d0a91STim J. Robbins 	else if (cflag)
139364d0a91STim J. Robbins 		fcn = MB_CUR_MAX > 1 ? c_cut : b_cut;
140364d0a91STim J. Robbins 	else if (bflag)
141364d0a91STim J. Robbins 		fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut;
1429b50d902SRodney W. Grimes 
143a6ea32c3STim J. Robbins 	rval = 0;
1449b50d902SRodney W. Grimes 	if (*argv)
1459b50d902SRodney W. Grimes 		for (; *argv; ++argv) {
146204c78a1STim J. Robbins 			if (strcmp(*argv, "-") == 0)
147364d0a91STim J. Robbins 				rval |= fcn(stdin, "stdin");
148204c78a1STim J. Robbins 			else {
149a6ea32c3STim J. Robbins 				if (!(fp = fopen(*argv, "r"))) {
150a6ea32c3STim J. Robbins 					warn("%s", *argv);
151a6ea32c3STim J. Robbins 					rval = 1;
152a6ea32c3STim J. Robbins 					continue;
153a6ea32c3STim J. Robbins 				}
1549b50d902SRodney W. Grimes 				fcn(fp, *argv);
1559b50d902SRodney W. Grimes 				(void)fclose(fp);
1569b50d902SRodney W. Grimes 			}
157204c78a1STim J. Robbins 		}
1589b50d902SRodney W. Grimes 	else
159364d0a91STim J. Robbins 		rval = fcn(stdin, "stdin");
160a6ea32c3STim J. Robbins 	exit(rval);
1619b50d902SRodney W. Grimes }
1629b50d902SRodney W. Grimes 
16341b662c5SEd Schouten static void
164f4ac32deSDavid Malone get_list(char *list)
1659b50d902SRodney W. Grimes {
166d8a3fbd5SWill Andrews 	size_t setautostart, start, stop;
1672c39ae65SEivind Eklund 	char *pos;
1689b50d902SRodney W. Grimes 	char *p;
1699b50d902SRodney W. Grimes 
1709b50d902SRodney W. Grimes 	/*
1719b50d902SRodney W. Grimes 	 * set a byte in the positions array to indicate if a field or
1729b50d902SRodney W. Grimes 	 * column is to be selected; use +1, it's 1-based, not 0-based.
1730dcb7b75STim J. Robbins 	 * Numbers and number ranges may be overlapping, repeated, and in
174d334b286SKevin Lo 	 * any order. We handle "-3-5" although there's no real reason to.
1759b50d902SRodney W. Grimes 	 */
1762c39ae65SEivind Eklund 	for (; (p = strsep(&list, ", \t")) != NULL;) {
1779b50d902SRodney W. Grimes 		setautostart = start = stop = 0;
1789b50d902SRodney W. Grimes 		if (*p == '-') {
1799b50d902SRodney W. Grimes 			++p;
1809b50d902SRodney W. Grimes 			setautostart = 1;
1819b50d902SRodney W. Grimes 		}
1822c39ae65SEivind Eklund 		if (isdigit((unsigned char)*p)) {
1839b50d902SRodney W. Grimes 			start = stop = strtol(p, &p, 10);
1849b50d902SRodney W. Grimes 			if (setautostart && start > autostart)
1859b50d902SRodney W. Grimes 				autostart = start;
1869b50d902SRodney W. Grimes 		}
1879b50d902SRodney W. Grimes 		if (*p == '-') {
1882c39ae65SEivind Eklund 			if (isdigit((unsigned char)p[1]))
1899b50d902SRodney W. Grimes 				stop = strtol(p + 1, &p, 10);
1909b50d902SRodney W. Grimes 			if (*p == '-') {
1919b50d902SRodney W. Grimes 				++p;
1929b50d902SRodney W. Grimes 				if (!autostop || autostop > stop)
1939b50d902SRodney W. Grimes 					autostop = stop;
1949b50d902SRodney W. Grimes 			}
1959b50d902SRodney W. Grimes 		}
1969b50d902SRodney W. Grimes 		if (*p)
197d334b286SKevin Lo 			errx(1, "[-bcf] list: illegal list value");
1989b50d902SRodney W. Grimes 		if (!stop || !start)
199d334b286SKevin Lo 			errx(1, "[-bcf] list: values may not include zero");
200a8522a9bSTim J. Robbins 		if (maxval < stop) {
2019b50d902SRodney W. Grimes 			maxval = stop;
202a8522a9bSTim J. Robbins 			needpos(maxval + 1);
203a8522a9bSTim J. Robbins 		}
2049b50d902SRodney W. Grimes 		for (pos = positions + start; start++ <= stop; *pos++ = 1);
2059b50d902SRodney W. Grimes 	}
2069b50d902SRodney W. Grimes 
2079b50d902SRodney W. Grimes 	/* overlapping ranges */
208a8522a9bSTim J. Robbins 	if (autostop && maxval > autostop) {
2099b50d902SRodney W. Grimes 		maxval = autostop;
210a8522a9bSTim J. Robbins 		needpos(maxval + 1);
211a8522a9bSTim J. Robbins 	}
2129b50d902SRodney W. Grimes 
213*6872fd3cSEitan Adler 	/* reversed range with autostart */
214*6872fd3cSEitan Adler 	if (maxval < autostart) {
215*6872fd3cSEitan Adler 		maxval = autostart;
216*6872fd3cSEitan Adler 		needpos(maxval + 1);
217*6872fd3cSEitan Adler 	}
218*6872fd3cSEitan Adler 
2199b50d902SRodney W. Grimes 	/* set autostart */
2209b50d902SRodney W. Grimes 	if (autostart)
2219b50d902SRodney W. Grimes 		memset(positions + 1, '1', autostart);
2229b50d902SRodney W. Grimes }
2239b50d902SRodney W. Grimes 
22441b662c5SEd Schouten static void
225a8522a9bSTim J. Robbins needpos(size_t n)
226a8522a9bSTim J. Robbins {
227a8522a9bSTim J. Robbins 	static size_t npos;
228f457179aSTim J. Robbins 	size_t oldnpos;
229a8522a9bSTim J. Robbins 
230a8522a9bSTim J. Robbins 	/* Grow the positions array to at least the specified size. */
231a8522a9bSTim J. Robbins 	if (n > npos) {
232f457179aSTim J. Robbins 		oldnpos = npos;
233a8522a9bSTim J. Robbins 		if (npos == 0)
234a8522a9bSTim J. Robbins 			npos = n;
235a8522a9bSTim J. Robbins 		while (n > npos)
236a8522a9bSTim J. Robbins 			npos *= 2;
237a8522a9bSTim J. Robbins 		if ((positions = realloc(positions, npos)) == NULL)
238a8522a9bSTim J. Robbins 			err(1, "realloc");
239f457179aSTim J. Robbins 		memset((char *)positions + oldnpos, 0, npos - oldnpos);
240a8522a9bSTim J. Robbins 	}
241a8522a9bSTim J. Robbins }
242a8522a9bSTim J. Robbins 
24341b662c5SEd Schouten static int
244f0b4606fSTim J. Robbins b_cut(FILE *fp, const char *fname __unused)
245364d0a91STim J. Robbins {
246364d0a91STim J. Robbins 	int ch, col;
247364d0a91STim J. Robbins 	char *pos;
248364d0a91STim J. Robbins 
249364d0a91STim J. Robbins 	ch = 0;
250364d0a91STim J. Robbins 	for (;;) {
251364d0a91STim J. Robbins 		pos = positions + 1;
252364d0a91STim J. Robbins 		for (col = maxval; col; --col) {
253364d0a91STim J. Robbins 			if ((ch = getc(fp)) == EOF)
254364d0a91STim J. Robbins 				return (0);
255364d0a91STim J. Robbins 			if (ch == '\n')
256364d0a91STim J. Robbins 				break;
257364d0a91STim J. Robbins 			if (*pos++)
258364d0a91STim J. Robbins 				(void)putchar(ch);
259364d0a91STim J. Robbins 		}
260364d0a91STim J. Robbins 		if (ch != '\n') {
261364d0a91STim J. Robbins 			if (autostop)
262364d0a91STim J. Robbins 				while ((ch = getc(fp)) != EOF && ch != '\n')
263364d0a91STim J. Robbins 					(void)putchar(ch);
264364d0a91STim J. Robbins 			else
265364d0a91STim J. Robbins 				while ((ch = getc(fp)) != EOF && ch != '\n');
266364d0a91STim J. Robbins 		}
267364d0a91STim J. Robbins 		(void)putchar('\n');
268364d0a91STim J. Robbins 	}
269364d0a91STim J. Robbins 	return (0);
270364d0a91STim J. Robbins }
271364d0a91STim J. Robbins 
272393cf508STim J. Robbins /*
273393cf508STim J. Robbins  * Cut based on byte positions, taking care not to split multibyte characters.
274393cf508STim J. Robbins  * Although this function also handles the case where -n is not specified,
275364d0a91STim J. Robbins  * b_cut() ought to be much faster.
276393cf508STim J. Robbins  */
27741b662c5SEd Schouten static int
278f4ac32deSDavid Malone b_n_cut(FILE *fp, const char *fname)
279393cf508STim J. Robbins {
280393cf508STim J. Robbins 	size_t col, i, lbuflen;
281393cf508STim J. Robbins 	char *lbuf;
282393cf508STim J. Robbins 	int canwrite, clen, warned;
283d900c384STim J. Robbins 	mbstate_t mbs;
284393cf508STim J. Robbins 
285d900c384STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
286393cf508STim J. Robbins 	warned = 0;
287393cf508STim J. Robbins 	while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
288393cf508STim J. Robbins 		for (col = 0; lbuflen > 0; col += clen) {
289d900c384STim J. Robbins 			if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
290393cf508STim J. Robbins 				if (!warned) {
291393cf508STim J. Robbins 					warn("%s", fname);
292393cf508STim J. Robbins 					warned = 1;
293393cf508STim J. Robbins 				}
294d900c384STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
295393cf508STim J. Robbins 				clen = 1;
296393cf508STim J. Robbins 			}
297393cf508STim J. Robbins 			if (clen == 0 || *lbuf == '\n')
298393cf508STim J. Robbins 				break;
299393cf508STim J. Robbins 			if (col < maxval && !positions[1 + col]) {
300393cf508STim J. Robbins 				/*
301393cf508STim J. Robbins 				 * Print the character if (1) after an initial
302393cf508STim J. Robbins 				 * segment of un-selected bytes, the rest of
303393cf508STim J. Robbins 				 * it is selected, and (2) the last byte is
304393cf508STim J. Robbins 				 * selected.
305393cf508STim J. Robbins 				 */
306393cf508STim J. Robbins 				i = col;
307393cf508STim J. Robbins 				while (i < col + clen && i < maxval &&
308393cf508STim J. Robbins 				    !positions[1 + i])
309393cf508STim J. Robbins 					i++;
310393cf508STim J. Robbins 				canwrite = i < col + clen;
311393cf508STim J. Robbins 				for (; i < col + clen && i < maxval; i++)
312393cf508STim J. Robbins 					canwrite &= positions[1 + i];
313393cf508STim J. Robbins 				if (canwrite)
314393cf508STim J. Robbins 					fwrite(lbuf, 1, clen, stdout);
315393cf508STim J. Robbins 			} else {
316393cf508STim J. Robbins 				/*
317393cf508STim J. Robbins 				 * Print the character if all of it has
318393cf508STim J. Robbins 				 * been selected.
319393cf508STim J. Robbins 				 */
320393cf508STim J. Robbins 				canwrite = 1;
321393cf508STim J. Robbins 				for (i = col; i < col + clen; i++)
322393cf508STim J. Robbins 					if ((i >= maxval && !autostop) ||
323393cf508STim J. Robbins 					    (i < maxval && !positions[1 + i])) {
324393cf508STim J. Robbins 						canwrite = 0;
325393cf508STim J. Robbins 						break;
326393cf508STim J. Robbins 					}
327393cf508STim J. Robbins 				if (canwrite)
328393cf508STim J. Robbins 					fwrite(lbuf, 1, clen, stdout);
329393cf508STim J. Robbins 			}
330393cf508STim J. Robbins 			lbuf += clen;
331393cf508STim J. Robbins 			lbuflen -= clen;
332393cf508STim J. Robbins 		}
333393cf508STim J. Robbins 		if (lbuflen > 0)
334393cf508STim J. Robbins 			putchar('\n');
335393cf508STim J. Robbins 	}
336364d0a91STim J. Robbins 	return (warned);
337393cf508STim J. Robbins }
338393cf508STim J. Robbins 
33941b662c5SEd Schouten static int
340364d0a91STim J. Robbins c_cut(FILE *fp, const char *fname)
3419b50d902SRodney W. Grimes {
342364d0a91STim J. Robbins 	wint_t ch;
343364d0a91STim J. Robbins 	int col;
3442c39ae65SEivind Eklund 	char *pos;
3459b50d902SRodney W. Grimes 
3462c39ae65SEivind Eklund 	ch = 0;
3479b50d902SRodney W. Grimes 	for (;;) {
3489b50d902SRodney W. Grimes 		pos = positions + 1;
3499b50d902SRodney W. Grimes 		for (col = maxval; col; --col) {
350364d0a91STim J. Robbins 			if ((ch = getwc(fp)) == WEOF)
351364d0a91STim J. Robbins 				goto out;
3529b50d902SRodney W. Grimes 			if (ch == '\n')
3539b50d902SRodney W. Grimes 				break;
3549b50d902SRodney W. Grimes 			if (*pos++)
355364d0a91STim J. Robbins 				(void)putwchar(ch);
3569b50d902SRodney W. Grimes 		}
3572c39ae65SEivind Eklund 		if (ch != '\n') {
3589b50d902SRodney W. Grimes 			if (autostop)
359364d0a91STim J. Robbins 				while ((ch = getwc(fp)) != WEOF && ch != '\n')
360364d0a91STim J. Robbins 					(void)putwchar(ch);
3619b50d902SRodney W. Grimes 			else
362364d0a91STim J. Robbins 				while ((ch = getwc(fp)) != WEOF && ch != '\n');
3632c39ae65SEivind Eklund 		}
364364d0a91STim J. Robbins 		(void)putwchar('\n');
3659b50d902SRodney W. Grimes 	}
366364d0a91STim J. Robbins out:
367364d0a91STim J. Robbins 	if (ferror(fp)) {
368364d0a91STim J. Robbins 		warn("%s", fname);
369364d0a91STim J. Robbins 		return (1);
370364d0a91STim J. Robbins 	}
371364d0a91STim J. Robbins 	return (0);
3729b50d902SRodney W. Grimes }
3739b50d902SRodney W. Grimes 
37441b662c5SEd Schouten static int
375bd9dd0c6SAndrew Turner is_delim(wchar_t ch)
37618ba28c8SEitan Adler {
37718ba28c8SEitan Adler 	if (wflag) {
37818ba28c8SEitan Adler 		if (ch == ' ' || ch == '\t')
37918ba28c8SEitan Adler 			return 1;
38018ba28c8SEitan Adler 	} else {
38118ba28c8SEitan Adler 		if (ch == dchar)
38218ba28c8SEitan Adler 			return 1;
38318ba28c8SEitan Adler 	}
38418ba28c8SEitan Adler 	return 0;
38518ba28c8SEitan Adler }
38618ba28c8SEitan Adler 
38718ba28c8SEitan Adler static int
388a5c4bafcSTim J. Robbins f_cut(FILE *fp, const char *fname)
3899b50d902SRodney W. Grimes {
390a5c4bafcSTim J. Robbins 	wchar_t ch;
391a5c4bafcSTim J. Robbins 	int field, i, isdelim;
392a5c4bafcSTim J. Robbins 	char *pos, *p;
3939b50d902SRodney W. Grimes 	int output;
39493738f50STim J. Robbins 	char *lbuf, *mlbuf;
395a2222839STim J. Robbins 	size_t clen, lbuflen, reallen;
3969b50d902SRodney W. Grimes 
39793738f50STim J. Robbins 	mlbuf = NULL;
39818ba28c8SEitan Adler 	while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
399a2222839STim J. Robbins 		reallen = lbuflen;
4001928e20eSDima Dorfman 		/* Assert EOL has a newline. */
4011928e20eSDima Dorfman 		if (*(lbuf + lbuflen - 1) != '\n') {
4021928e20eSDima Dorfman 			/* Can't have > 1 line with no trailing newline. */
4031928e20eSDima Dorfman 			mlbuf = malloc(lbuflen + 1);
4041928e20eSDima Dorfman 			if (mlbuf == NULL)
4051928e20eSDima Dorfman 				err(1, "malloc");
4061928e20eSDima Dorfman 			memcpy(mlbuf, lbuf, lbuflen);
4071928e20eSDima Dorfman 			*(mlbuf + lbuflen) = '\n';
4081928e20eSDima Dorfman 			lbuf = mlbuf;
409a2222839STim J. Robbins 			reallen++;
4101928e20eSDima Dorfman 		}
411eaf92380SAndrey A. Chernov 		output = 0;
412a5c4bafcSTim J. Robbins 		for (isdelim = 0, p = lbuf;; p += clen) {
413a2222839STim J. Robbins 			clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
414a5c4bafcSTim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2) {
415a5c4bafcSTim J. Robbins 				warnc(EILSEQ, "%s", fname);
416a5c4bafcSTim J. Robbins 				free(mlbuf);
417a5c4bafcSTim J. Robbins 				return (1);
418a5c4bafcSTim J. Robbins 			}
419a5c4bafcSTim J. Robbins 			if (clen == 0)
420a5c4bafcSTim J. Robbins 				clen = 1;
4219b50d902SRodney W. Grimes 			/* this should work if newline is delimiter */
42218ba28c8SEitan Adler 			if (is_delim(ch))
4239b50d902SRodney W. Grimes 				isdelim = 1;
4249b50d902SRodney W. Grimes 			if (ch == '\n') {
4259b50d902SRodney W. Grimes 				if (!isdelim && !sflag)
4261928e20eSDima Dorfman 					(void)fwrite(lbuf, lbuflen, 1, stdout);
4279b50d902SRodney W. Grimes 				break;
4289b50d902SRodney W. Grimes 			}
4299b50d902SRodney W. Grimes 		}
4309b50d902SRodney W. Grimes 		if (!isdelim)
4319b50d902SRodney W. Grimes 			continue;
4329b50d902SRodney W. Grimes 
4339b50d902SRodney W. Grimes 		pos = positions + 1;
4349b50d902SRodney W. Grimes 		for (field = maxval, p = lbuf; field; --field, ++pos) {
435a5c4bafcSTim J. Robbins 			if (*pos && output++)
436a5c4bafcSTim J. Robbins 				for (i = 0; dcharmb[i] != '\0'; i++)
437a5c4bafcSTim J. Robbins 					putchar(dcharmb[i]);
438a5c4bafcSTim J. Robbins 			for (;;) {
439a2222839STim J. Robbins 				clen = mbrtowc(&ch, p, lbuf + reallen - p,
440a5c4bafcSTim J. Robbins 				    NULL);
441a5c4bafcSTim J. Robbins 				if (clen == (size_t)-1 || clen == (size_t)-2) {
442a5c4bafcSTim J. Robbins 					warnc(EILSEQ, "%s", fname);
443a5c4bafcSTim J. Robbins 					free(mlbuf);
444a5c4bafcSTim J. Robbins 					return (1);
445a5c4bafcSTim J. Robbins 				}
446a5c4bafcSTim J. Robbins 				if (clen == 0)
447a5c4bafcSTim J. Robbins 					clen = 1;
448a5c4bafcSTim J. Robbins 				p += clen;
44918ba28c8SEitan Adler 				if (ch == '\n' || is_delim(ch)) {
45018ba28c8SEitan Adler 					/* compress whitespace */
45118ba28c8SEitan Adler 					if (wflag && ch != '\n')
45218ba28c8SEitan Adler 						while (is_delim(*p))
45318ba28c8SEitan Adler 							p++;
454a5c4bafcSTim J. Robbins 					break;
45518ba28c8SEitan Adler 				}
456a5c4bafcSTim J. Robbins 				if (*pos)
457a5c4bafcSTim J. Robbins 					for (i = 0; i < (int)clen; i++)
458a5c4bafcSTim J. Robbins 						putchar(p[i - clen]);
4592c39ae65SEivind Eklund 			}
4609b50d902SRodney W. Grimes 			if (ch == '\n')
4619b50d902SRodney W. Grimes 				break;
4629b50d902SRodney W. Grimes 		}
4632c39ae65SEivind Eklund 		if (ch != '\n') {
4649b50d902SRodney W. Grimes 			if (autostop) {
4659b50d902SRodney W. Grimes 				if (output)
466a5c4bafcSTim J. Robbins 					for (i = 0; dcharmb[i] != '\0'; i++)
467a5c4bafcSTim J. Robbins 						putchar(dcharmb[i]);
4689b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p)
4699b50d902SRodney W. Grimes 					(void)putchar(ch);
4709b50d902SRodney W. Grimes 			} else
4719b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p);
4722c39ae65SEivind Eklund 		}
4739b50d902SRodney W. Grimes 		(void)putchar('\n');
4749b50d902SRodney W. Grimes 	}
4751928e20eSDima Dorfman 	free(mlbuf);
476364d0a91STim J. Robbins 	return (0);
4779b50d902SRodney W. Grimes }
4789b50d902SRodney W. Grimes 
479812bff99SPhilippe Charnier static void
480f4ac32deSDavid Malone usage(void)
4819b50d902SRodney W. Grimes {
482d51c6625SEivind Eklund 	(void)fprintf(stderr, "%s\n%s\n%s\n",
483d51c6625SEivind Eklund 		"usage: cut -b list [-n] [file ...]",
484d51c6625SEivind Eklund 		"       cut -c list [file ...]",
48518ba28c8SEitan Adler 		"       cut -f list [-s] [-w | -d delim] [file ...]");
4869b50d902SRodney W. Grimes 	exit(1);
4879b50d902SRodney W. Grimes }
488