xref: /freebsd/usr.bin/cut/cut.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
19b50d902SRodney W. Grimes /*
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro 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 
2139b50d902SRodney W. Grimes 	/* set autostart */
2149b50d902SRodney W. Grimes 	if (autostart)
2159b50d902SRodney W. Grimes 		memset(positions + 1, '1', autostart);
2169b50d902SRodney W. Grimes }
2179b50d902SRodney W. Grimes 
21841b662c5SEd Schouten static void
219a8522a9bSTim J. Robbins needpos(size_t n)
220a8522a9bSTim J. Robbins {
221a8522a9bSTim J. Robbins 	static size_t npos;
222f457179aSTim J. Robbins 	size_t oldnpos;
223a8522a9bSTim J. Robbins 
224a8522a9bSTim J. Robbins 	/* Grow the positions array to at least the specified size. */
225a8522a9bSTim J. Robbins 	if (n > npos) {
226f457179aSTim J. Robbins 		oldnpos = npos;
227a8522a9bSTim J. Robbins 		if (npos == 0)
228a8522a9bSTim J. Robbins 			npos = n;
229a8522a9bSTim J. Robbins 		while (n > npos)
230a8522a9bSTim J. Robbins 			npos *= 2;
231a8522a9bSTim J. Robbins 		if ((positions = realloc(positions, npos)) == NULL)
232a8522a9bSTim J. Robbins 			err(1, "realloc");
233f457179aSTim J. Robbins 		memset((char *)positions + oldnpos, 0, npos - oldnpos);
234a8522a9bSTim J. Robbins 	}
235a8522a9bSTim J. Robbins }
236a8522a9bSTim J. Robbins 
23741b662c5SEd Schouten static int
238f0b4606fSTim J. Robbins b_cut(FILE *fp, const char *fname __unused)
239364d0a91STim J. Robbins {
240364d0a91STim J. Robbins 	int ch, col;
241364d0a91STim J. Robbins 	char *pos;
242364d0a91STim J. Robbins 
243364d0a91STim J. Robbins 	ch = 0;
244364d0a91STim J. Robbins 	for (;;) {
245364d0a91STim J. Robbins 		pos = positions + 1;
246364d0a91STim J. Robbins 		for (col = maxval; col; --col) {
247364d0a91STim J. Robbins 			if ((ch = getc(fp)) == EOF)
248364d0a91STim J. Robbins 				return (0);
249364d0a91STim J. Robbins 			if (ch == '\n')
250364d0a91STim J. Robbins 				break;
251364d0a91STim J. Robbins 			if (*pos++)
252364d0a91STim J. Robbins 				(void)putchar(ch);
253364d0a91STim J. Robbins 		}
254364d0a91STim J. Robbins 		if (ch != '\n') {
255364d0a91STim J. Robbins 			if (autostop)
256364d0a91STim J. Robbins 				while ((ch = getc(fp)) != EOF && ch != '\n')
257364d0a91STim J. Robbins 					(void)putchar(ch);
258364d0a91STim J. Robbins 			else
259364d0a91STim J. Robbins 				while ((ch = getc(fp)) != EOF && ch != '\n');
260364d0a91STim J. Robbins 		}
261364d0a91STim J. Robbins 		(void)putchar('\n');
262364d0a91STim J. Robbins 	}
263364d0a91STim J. Robbins 	return (0);
264364d0a91STim J. Robbins }
265364d0a91STim J. Robbins 
266393cf508STim J. Robbins /*
267393cf508STim J. Robbins  * Cut based on byte positions, taking care not to split multibyte characters.
268393cf508STim J. Robbins  * Although this function also handles the case where -n is not specified,
269364d0a91STim J. Robbins  * b_cut() ought to be much faster.
270393cf508STim J. Robbins  */
27141b662c5SEd Schouten static int
272f4ac32deSDavid Malone b_n_cut(FILE *fp, const char *fname)
273393cf508STim J. Robbins {
274393cf508STim J. Robbins 	size_t col, i, lbuflen;
275393cf508STim J. Robbins 	char *lbuf;
276393cf508STim J. Robbins 	int canwrite, clen, warned;
277d900c384STim J. Robbins 	mbstate_t mbs;
278393cf508STim J. Robbins 
279d900c384STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
280393cf508STim J. Robbins 	warned = 0;
281393cf508STim J. Robbins 	while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
282393cf508STim J. Robbins 		for (col = 0; lbuflen > 0; col += clen) {
283d900c384STim J. Robbins 			if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
284393cf508STim J. Robbins 				if (!warned) {
285393cf508STim J. Robbins 					warn("%s", fname);
286393cf508STim J. Robbins 					warned = 1;
287393cf508STim J. Robbins 				}
288d900c384STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
289393cf508STim J. Robbins 				clen = 1;
290393cf508STim J. Robbins 			}
291393cf508STim J. Robbins 			if (clen == 0 || *lbuf == '\n')
292393cf508STim J. Robbins 				break;
293393cf508STim J. Robbins 			if (col < maxval && !positions[1 + col]) {
294393cf508STim J. Robbins 				/*
295393cf508STim J. Robbins 				 * Print the character if (1) after an initial
296393cf508STim J. Robbins 				 * segment of un-selected bytes, the rest of
297393cf508STim J. Robbins 				 * it is selected, and (2) the last byte is
298393cf508STim J. Robbins 				 * selected.
299393cf508STim J. Robbins 				 */
300393cf508STim J. Robbins 				i = col;
301393cf508STim J. Robbins 				while (i < col + clen && i < maxval &&
302393cf508STim J. Robbins 				    !positions[1 + i])
303393cf508STim J. Robbins 					i++;
304393cf508STim J. Robbins 				canwrite = i < col + clen;
305393cf508STim J. Robbins 				for (; i < col + clen && i < maxval; i++)
306393cf508STim J. Robbins 					canwrite &= positions[1 + i];
307393cf508STim J. Robbins 				if (canwrite)
308393cf508STim J. Robbins 					fwrite(lbuf, 1, clen, stdout);
309393cf508STim J. Robbins 			} else {
310393cf508STim J. Robbins 				/*
311393cf508STim J. Robbins 				 * Print the character if all of it has
312393cf508STim J. Robbins 				 * been selected.
313393cf508STim J. Robbins 				 */
314393cf508STim J. Robbins 				canwrite = 1;
315393cf508STim J. Robbins 				for (i = col; i < col + clen; i++)
316393cf508STim J. Robbins 					if ((i >= maxval && !autostop) ||
317393cf508STim J. Robbins 					    (i < maxval && !positions[1 + i])) {
318393cf508STim J. Robbins 						canwrite = 0;
319393cf508STim J. Robbins 						break;
320393cf508STim J. Robbins 					}
321393cf508STim J. Robbins 				if (canwrite)
322393cf508STim J. Robbins 					fwrite(lbuf, 1, clen, stdout);
323393cf508STim J. Robbins 			}
324393cf508STim J. Robbins 			lbuf += clen;
325393cf508STim J. Robbins 			lbuflen -= clen;
326393cf508STim J. Robbins 		}
327393cf508STim J. Robbins 		if (lbuflen > 0)
328393cf508STim J. Robbins 			putchar('\n');
329393cf508STim J. Robbins 	}
330364d0a91STim J. Robbins 	return (warned);
331393cf508STim J. Robbins }
332393cf508STim J. Robbins 
33341b662c5SEd Schouten static int
334364d0a91STim J. Robbins c_cut(FILE *fp, const char *fname)
3359b50d902SRodney W. Grimes {
336364d0a91STim J. Robbins 	wint_t ch;
337364d0a91STim J. Robbins 	int col;
3382c39ae65SEivind Eklund 	char *pos;
3399b50d902SRodney W. Grimes 
3402c39ae65SEivind Eklund 	ch = 0;
3419b50d902SRodney W. Grimes 	for (;;) {
3429b50d902SRodney W. Grimes 		pos = positions + 1;
3439b50d902SRodney W. Grimes 		for (col = maxval; col; --col) {
344364d0a91STim J. Robbins 			if ((ch = getwc(fp)) == WEOF)
345364d0a91STim J. Robbins 				goto out;
3469b50d902SRodney W. Grimes 			if (ch == '\n')
3479b50d902SRodney W. Grimes 				break;
3489b50d902SRodney W. Grimes 			if (*pos++)
349364d0a91STim J. Robbins 				(void)putwchar(ch);
3509b50d902SRodney W. Grimes 		}
3512c39ae65SEivind Eklund 		if (ch != '\n') {
3529b50d902SRodney W. Grimes 			if (autostop)
353364d0a91STim J. Robbins 				while ((ch = getwc(fp)) != WEOF && ch != '\n')
354364d0a91STim J. Robbins 					(void)putwchar(ch);
3559b50d902SRodney W. Grimes 			else
356364d0a91STim J. Robbins 				while ((ch = getwc(fp)) != WEOF && ch != '\n');
3572c39ae65SEivind Eklund 		}
358364d0a91STim J. Robbins 		(void)putwchar('\n');
3599b50d902SRodney W. Grimes 	}
360364d0a91STim J. Robbins out:
361364d0a91STim J. Robbins 	if (ferror(fp)) {
362364d0a91STim J. Robbins 		warn("%s", fname);
363364d0a91STim J. Robbins 		return (1);
364364d0a91STim J. Robbins 	}
365364d0a91STim J. Robbins 	return (0);
3669b50d902SRodney W. Grimes }
3679b50d902SRodney W. Grimes 
36841b662c5SEd Schouten static int
369bd9dd0c6SAndrew Turner is_delim(wchar_t ch)
37018ba28c8SEitan Adler {
37118ba28c8SEitan Adler 	if (wflag) {
37218ba28c8SEitan Adler 		if (ch == ' ' || ch == '\t')
37318ba28c8SEitan Adler 			return 1;
37418ba28c8SEitan Adler 	} else {
37518ba28c8SEitan Adler 		if (ch == dchar)
37618ba28c8SEitan Adler 			return 1;
37718ba28c8SEitan Adler 	}
37818ba28c8SEitan Adler 	return 0;
37918ba28c8SEitan Adler }
38018ba28c8SEitan Adler 
38118ba28c8SEitan Adler static int
382a5c4bafcSTim J. Robbins f_cut(FILE *fp, const char *fname)
3839b50d902SRodney W. Grimes {
384a5c4bafcSTim J. Robbins 	wchar_t ch;
385a5c4bafcSTim J. Robbins 	int field, i, isdelim;
386a5c4bafcSTim J. Robbins 	char *pos, *p;
3879b50d902SRodney W. Grimes 	int output;
38893738f50STim J. Robbins 	char *lbuf, *mlbuf;
389a2222839STim J. Robbins 	size_t clen, lbuflen, reallen;
3909b50d902SRodney W. Grimes 
39193738f50STim J. Robbins 	mlbuf = NULL;
39218ba28c8SEitan Adler 	while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
393a2222839STim J. Robbins 		reallen = lbuflen;
3941928e20eSDima Dorfman 		/* Assert EOL has a newline. */
3951928e20eSDima Dorfman 		if (*(lbuf + lbuflen - 1) != '\n') {
3961928e20eSDima Dorfman 			/* Can't have > 1 line with no trailing newline. */
3971928e20eSDima Dorfman 			mlbuf = malloc(lbuflen + 1);
3981928e20eSDima Dorfman 			if (mlbuf == NULL)
3991928e20eSDima Dorfman 				err(1, "malloc");
4001928e20eSDima Dorfman 			memcpy(mlbuf, lbuf, lbuflen);
4011928e20eSDima Dorfman 			*(mlbuf + lbuflen) = '\n';
4021928e20eSDima Dorfman 			lbuf = mlbuf;
403a2222839STim J. Robbins 			reallen++;
4041928e20eSDima Dorfman 		}
405eaf92380SAndrey A. Chernov 		output = 0;
406a5c4bafcSTim J. Robbins 		for (isdelim = 0, p = lbuf;; p += clen) {
407a2222839STim J. Robbins 			clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
408a5c4bafcSTim J. Robbins 			if (clen == (size_t)-1 || clen == (size_t)-2) {
409a5c4bafcSTim J. Robbins 				warnc(EILSEQ, "%s", fname);
410a5c4bafcSTim J. Robbins 				free(mlbuf);
411a5c4bafcSTim J. Robbins 				return (1);
412a5c4bafcSTim J. Robbins 			}
413a5c4bafcSTim J. Robbins 			if (clen == 0)
414a5c4bafcSTim J. Robbins 				clen = 1;
4159b50d902SRodney W. Grimes 			/* this should work if newline is delimiter */
41618ba28c8SEitan Adler 			if (is_delim(ch))
4179b50d902SRodney W. Grimes 				isdelim = 1;
4189b50d902SRodney W. Grimes 			if (ch == '\n') {
4199b50d902SRodney W. Grimes 				if (!isdelim && !sflag)
4201928e20eSDima Dorfman 					(void)fwrite(lbuf, lbuflen, 1, stdout);
4219b50d902SRodney W. Grimes 				break;
4229b50d902SRodney W. Grimes 			}
4239b50d902SRodney W. Grimes 		}
4249b50d902SRodney W. Grimes 		if (!isdelim)
4259b50d902SRodney W. Grimes 			continue;
4269b50d902SRodney W. Grimes 
4279b50d902SRodney W. Grimes 		pos = positions + 1;
4289b50d902SRodney W. Grimes 		for (field = maxval, p = lbuf; field; --field, ++pos) {
429a5c4bafcSTim J. Robbins 			if (*pos && output++)
430a5c4bafcSTim J. Robbins 				for (i = 0; dcharmb[i] != '\0'; i++)
431a5c4bafcSTim J. Robbins 					putchar(dcharmb[i]);
432a5c4bafcSTim J. Robbins 			for (;;) {
433a2222839STim J. Robbins 				clen = mbrtowc(&ch, p, lbuf + reallen - p,
434a5c4bafcSTim J. Robbins 				    NULL);
435a5c4bafcSTim J. Robbins 				if (clen == (size_t)-1 || clen == (size_t)-2) {
436a5c4bafcSTim J. Robbins 					warnc(EILSEQ, "%s", fname);
437a5c4bafcSTim J. Robbins 					free(mlbuf);
438a5c4bafcSTim J. Robbins 					return (1);
439a5c4bafcSTim J. Robbins 				}
440a5c4bafcSTim J. Robbins 				if (clen == 0)
441a5c4bafcSTim J. Robbins 					clen = 1;
442a5c4bafcSTim J. Robbins 				p += clen;
44318ba28c8SEitan Adler 				if (ch == '\n' || is_delim(ch)) {
44418ba28c8SEitan Adler 					/* compress whitespace */
44518ba28c8SEitan Adler 					if (wflag && ch != '\n')
44618ba28c8SEitan Adler 						while (is_delim(*p))
44718ba28c8SEitan Adler 							p++;
448a5c4bafcSTim J. Robbins 					break;
44918ba28c8SEitan Adler 				}
450a5c4bafcSTim J. Robbins 				if (*pos)
451a5c4bafcSTim J. Robbins 					for (i = 0; i < (int)clen; i++)
452a5c4bafcSTim J. Robbins 						putchar(p[i - clen]);
4532c39ae65SEivind Eklund 			}
4549b50d902SRodney W. Grimes 			if (ch == '\n')
4559b50d902SRodney W. Grimes 				break;
4569b50d902SRodney W. Grimes 		}
4572c39ae65SEivind Eklund 		if (ch != '\n') {
4589b50d902SRodney W. Grimes 			if (autostop) {
4599b50d902SRodney W. Grimes 				if (output)
460a5c4bafcSTim J. Robbins 					for (i = 0; dcharmb[i] != '\0'; i++)
461a5c4bafcSTim J. Robbins 						putchar(dcharmb[i]);
4629b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p)
4639b50d902SRodney W. Grimes 					(void)putchar(ch);
4649b50d902SRodney W. Grimes 			} else
4659b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p);
4662c39ae65SEivind Eklund 		}
4679b50d902SRodney W. Grimes 		(void)putchar('\n');
4689b50d902SRodney W. Grimes 	}
4691928e20eSDima Dorfman 	free(mlbuf);
470364d0a91STim J. Robbins 	return (0);
4719b50d902SRodney W. Grimes }
4729b50d902SRodney W. Grimes 
473812bff99SPhilippe Charnier static void
474f4ac32deSDavid Malone usage(void)
4759b50d902SRodney W. Grimes {
476d51c6625SEivind Eklund 	(void)fprintf(stderr, "%s\n%s\n%s\n",
477d51c6625SEivind Eklund 		"usage: cut -b list [-n] [file ...]",
478d51c6625SEivind Eklund 		"       cut -c list [file ...]",
47918ba28c8SEitan Adler 		"       cut -f list [-s] [-w | -d delim] [file ...]");
4809b50d902SRodney W. Grimes 	exit(1);
4819b50d902SRodney W. Grimes }
482