xref: /freebsd/usr.bin/cut/cut.c (revision 364d0a915ce4bf999c0772f9558394a4d841f515)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
38fa146c53SArchie Cobbs static const char copyright[] =
399b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
409b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
41fa146c53SArchie Cobbs static const char sccsid[] = "@(#)cut.c	8.3 (Berkeley) 5/4/95";
429b50d902SRodney W. Grimes #endif /* not lint */
4377c8bf7cSDavid E. O'Brien #include <sys/cdefs.h>
4477c8bf7cSDavid E. O'Brien __FBSDID("$FreeBSD$");
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <ctype.h>
47812bff99SPhilippe Charnier #include <err.h>
489b50d902SRodney W. Grimes #include <limits.h>
49d51c6625SEivind Eklund #include <locale.h>
509b50d902SRodney W. Grimes #include <stdio.h>
519b50d902SRodney W. Grimes #include <stdlib.h>
529b50d902SRodney W. Grimes #include <string.h>
53df3f5d9dSPeter Wemm #include <unistd.h>
54d900c384STim J. Robbins #include <wchar.h>
559b50d902SRodney W. Grimes 
56393cf508STim J. Robbins int	bflag;
579b50d902SRodney W. Grimes int	cflag;
589b50d902SRodney W. Grimes char	dchar;
599b50d902SRodney W. Grimes int	dflag;
609b50d902SRodney W. Grimes int	fflag;
61393cf508STim J. Robbins int	nflag;
629b50d902SRodney W. Grimes int	sflag;
639b50d902SRodney W. Grimes 
64364d0a91STim J. Robbins int	b_cut(FILE *, const char *);
65364d0a91STim J. Robbins int	b_n_cut(FILE *, const char *);
66364d0a91STim J. Robbins int	c_cut(FILE *, const char *);
67364d0a91STim J. Robbins int	f_cut(FILE *, const char *);
6899557a79SWill Andrews void	get_list(char *);
69a8522a9bSTim J. Robbins void	needpos(size_t);
7099557a79SWill Andrews static 	void usage(void);
719b50d902SRodney W. Grimes 
729b50d902SRodney W. Grimes int
73f4ac32deSDavid Malone main(int argc, char *argv[])
749b50d902SRodney W. Grimes {
759b50d902SRodney W. Grimes 	FILE *fp;
76364d0a91STim J. Robbins 	int (*fcn)(FILE *, const char *);
77a6ea32c3STim J. Robbins 	int ch, rval;
789b50d902SRodney W. Grimes 
79d51c6625SEivind Eklund 	setlocale(LC_ALL, "");
80d51c6625SEivind Eklund 
8193738f50STim J. Robbins 	fcn = NULL;
829b50d902SRodney W. Grimes 	dchar = '\t';			/* default delimiter is \t */
839b50d902SRodney W. Grimes 
845183fb53SEivind Eklund 	while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
859b50d902SRodney W. Grimes 		switch(ch) {
86d51c6625SEivind Eklund 		case 'b':
87393cf508STim J. Robbins 			get_list(optarg);
88393cf508STim J. Robbins 			bflag = 1;
89393cf508STim J. Robbins 			break;
909b50d902SRodney W. Grimes 		case 'c':
919b50d902SRodney W. Grimes 			get_list(optarg);
929b50d902SRodney W. Grimes 			cflag = 1;
939b50d902SRodney W. Grimes 			break;
949b50d902SRodney W. Grimes 		case 'd':
959b50d902SRodney W. Grimes 			dchar = *optarg;
969b50d902SRodney W. Grimes 			dflag = 1;
979b50d902SRodney W. Grimes 			break;
989b50d902SRodney W. Grimes 		case 'f':
999b50d902SRodney W. Grimes 			get_list(optarg);
1009b50d902SRodney W. Grimes 			fflag = 1;
1019b50d902SRodney W. Grimes 			break;
1029b50d902SRodney W. Grimes 		case 's':
1039b50d902SRodney W. Grimes 			sflag = 1;
1049b50d902SRodney W. Grimes 			break;
105d51c6625SEivind Eklund 		case 'n':
106393cf508STim J. Robbins 			nflag = 1;
107d51c6625SEivind Eklund 			break;
1089b50d902SRodney W. Grimes 		case '?':
1099b50d902SRodney W. Grimes 		default:
1109b50d902SRodney W. Grimes 			usage();
1119b50d902SRodney W. Grimes 		}
1129b50d902SRodney W. Grimes 	argc -= optind;
1139b50d902SRodney W. Grimes 	argv += optind;
1149b50d902SRodney W. Grimes 
1159b50d902SRodney W. Grimes 	if (fflag) {
116393cf508STim J. Robbins 		if (bflag || cflag || nflag)
1179b50d902SRodney W. Grimes 			usage();
118393cf508STim J. Robbins 	} else if (!(bflag || cflag) || dflag || sflag)
1199b50d902SRodney W. Grimes 		usage();
120393cf508STim J. Robbins 	else if (!bflag && nflag)
121393cf508STim J. Robbins 		usage();
122393cf508STim J. Robbins 
123364d0a91STim J. Robbins 	if (fflag)
124364d0a91STim J. Robbins 		fcn = f_cut;
125364d0a91STim J. Robbins 	else if (cflag)
126364d0a91STim J. Robbins 		fcn = MB_CUR_MAX > 1 ? c_cut : b_cut;
127364d0a91STim J. Robbins 	else if (bflag)
128364d0a91STim J. Robbins 		fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut;
1299b50d902SRodney W. Grimes 
130a6ea32c3STim J. Robbins 	rval = 0;
1319b50d902SRodney W. Grimes 	if (*argv)
1329b50d902SRodney W. Grimes 		for (; *argv; ++argv) {
133204c78a1STim J. Robbins 			if (strcmp(*argv, "-") == 0)
134364d0a91STim J. Robbins 				rval |= fcn(stdin, "stdin");
135204c78a1STim J. Robbins 			else {
136a6ea32c3STim J. Robbins 				if (!(fp = fopen(*argv, "r"))) {
137a6ea32c3STim J. Robbins 					warn("%s", *argv);
138a6ea32c3STim J. Robbins 					rval = 1;
139a6ea32c3STim J. Robbins 					continue;
140a6ea32c3STim J. Robbins 				}
1419b50d902SRodney W. Grimes 				fcn(fp, *argv);
1429b50d902SRodney W. Grimes 				(void)fclose(fp);
1439b50d902SRodney W. Grimes 			}
144204c78a1STim J. Robbins 		}
1459b50d902SRodney W. Grimes 	else
146364d0a91STim J. Robbins 		rval = fcn(stdin, "stdin");
147a6ea32c3STim J. Robbins 	exit(rval);
1489b50d902SRodney W. Grimes }
1499b50d902SRodney W. Grimes 
150d8a3fbd5SWill Andrews size_t autostart, autostop, maxval;
1519b50d902SRodney W. Grimes 
152a8522a9bSTim J. Robbins char *positions;
1539b50d902SRodney W. Grimes 
1549b50d902SRodney W. Grimes void
155f4ac32deSDavid Malone get_list(char *list)
1569b50d902SRodney W. Grimes {
157d8a3fbd5SWill Andrews 	size_t setautostart, start, stop;
1582c39ae65SEivind Eklund 	char *pos;
1599b50d902SRodney W. Grimes 	char *p;
1609b50d902SRodney W. Grimes 
1619b50d902SRodney W. Grimes 	/*
1629b50d902SRodney W. Grimes 	 * set a byte in the positions array to indicate if a field or
1639b50d902SRodney W. Grimes 	 * column is to be selected; use +1, it's 1-based, not 0-based.
1649b50d902SRodney W. Grimes 	 * This parser is less restrictive than the Draft 9 POSIX spec.
1659b50d902SRodney W. Grimes 	 * POSIX doesn't allow lists that aren't in increasing order or
1669b50d902SRodney W. Grimes 	 * overlapping lists.  We also handle "-3-5" although there's no
1679b50d902SRodney W. Grimes 	 * real reason too.
1689b50d902SRodney W. Grimes 	 */
1692c39ae65SEivind Eklund 	for (; (p = strsep(&list, ", \t")) != NULL;) {
1709b50d902SRodney W. Grimes 		setautostart = start = stop = 0;
1719b50d902SRodney W. Grimes 		if (*p == '-') {
1729b50d902SRodney W. Grimes 			++p;
1739b50d902SRodney W. Grimes 			setautostart = 1;
1749b50d902SRodney W. Grimes 		}
1752c39ae65SEivind Eklund 		if (isdigit((unsigned char)*p)) {
1769b50d902SRodney W. Grimes 			start = stop = strtol(p, &p, 10);
1779b50d902SRodney W. Grimes 			if (setautostart && start > autostart)
1789b50d902SRodney W. Grimes 				autostart = start;
1799b50d902SRodney W. Grimes 		}
1809b50d902SRodney W. Grimes 		if (*p == '-') {
1812c39ae65SEivind Eklund 			if (isdigit((unsigned char)p[1]))
1829b50d902SRodney W. Grimes 				stop = strtol(p + 1, &p, 10);
1839b50d902SRodney W. Grimes 			if (*p == '-') {
1849b50d902SRodney W. Grimes 				++p;
1859b50d902SRodney W. Grimes 				if (!autostop || autostop > stop)
1869b50d902SRodney W. Grimes 					autostop = stop;
1879b50d902SRodney W. Grimes 			}
1889b50d902SRodney W. Grimes 		}
1899b50d902SRodney W. Grimes 		if (*p)
190812bff99SPhilippe Charnier 			errx(1, "[-cf] list: illegal list value");
1919b50d902SRodney W. Grimes 		if (!stop || !start)
192812bff99SPhilippe Charnier 			errx(1, "[-cf] list: values may not include zero");
193a8522a9bSTim J. Robbins 		if (maxval < stop) {
1949b50d902SRodney W. Grimes 			maxval = stop;
195a8522a9bSTim J. Robbins 			needpos(maxval + 1);
196a8522a9bSTim J. Robbins 		}
1979b50d902SRodney W. Grimes 		for (pos = positions + start; start++ <= stop; *pos++ = 1);
1989b50d902SRodney W. Grimes 	}
1999b50d902SRodney W. Grimes 
2009b50d902SRodney W. Grimes 	/* overlapping ranges */
201a8522a9bSTim J. Robbins 	if (autostop && maxval > autostop) {
2029b50d902SRodney W. Grimes 		maxval = autostop;
203a8522a9bSTim J. Robbins 		needpos(maxval + 1);
204a8522a9bSTim J. Robbins 	}
2059b50d902SRodney W. Grimes 
2069b50d902SRodney W. Grimes 	/* set autostart */
2079b50d902SRodney W. Grimes 	if (autostart)
2089b50d902SRodney W. Grimes 		memset(positions + 1, '1', autostart);
2099b50d902SRodney W. Grimes }
2109b50d902SRodney W. Grimes 
211a8522a9bSTim J. Robbins void
212a8522a9bSTim J. Robbins needpos(size_t n)
213a8522a9bSTim J. Robbins {
214a8522a9bSTim J. Robbins 	static size_t npos;
215f457179aSTim J. Robbins 	size_t oldnpos;
216a8522a9bSTim J. Robbins 
217a8522a9bSTim J. Robbins 	/* Grow the positions array to at least the specified size. */
218a8522a9bSTim J. Robbins 	if (n > npos) {
219f457179aSTim J. Robbins 		oldnpos = npos;
220a8522a9bSTim J. Robbins 		if (npos == 0)
221a8522a9bSTim J. Robbins 			npos = n;
222a8522a9bSTim J. Robbins 		while (n > npos)
223a8522a9bSTim J. Robbins 			npos *= 2;
224a8522a9bSTim J. Robbins 		if ((positions = realloc(positions, npos)) == NULL)
225a8522a9bSTim J. Robbins 			err(1, "realloc");
226f457179aSTim J. Robbins 		memset((char *)positions + oldnpos, 0, npos - oldnpos);
227a8522a9bSTim J. Robbins 	}
228a8522a9bSTim J. Robbins }
229a8522a9bSTim J. Robbins 
230364d0a91STim J. Robbins int
231364d0a91STim J. Robbins b_cut(FILE *fp, const char *fname)
232364d0a91STim J. Robbins {
233364d0a91STim J. Robbins 	int ch, col;
234364d0a91STim J. Robbins 	char *pos;
235364d0a91STim J. Robbins 
236364d0a91STim J. Robbins 	ch = 0;
237364d0a91STim J. Robbins 	for (;;) {
238364d0a91STim J. Robbins 		pos = positions + 1;
239364d0a91STim J. Robbins 		for (col = maxval; col; --col) {
240364d0a91STim J. Robbins 			if ((ch = getc(fp)) == EOF)
241364d0a91STim J. Robbins 				return (0);
242364d0a91STim J. Robbins 			if (ch == '\n')
243364d0a91STim J. Robbins 				break;
244364d0a91STim J. Robbins 			if (*pos++)
245364d0a91STim J. Robbins 				(void)putchar(ch);
246364d0a91STim J. Robbins 		}
247364d0a91STim J. Robbins 		if (ch != '\n') {
248364d0a91STim J. Robbins 			if (autostop)
249364d0a91STim J. Robbins 				while ((ch = getc(fp)) != EOF && ch != '\n')
250364d0a91STim J. Robbins 					(void)putchar(ch);
251364d0a91STim J. Robbins 			else
252364d0a91STim J. Robbins 				while ((ch = getc(fp)) != EOF && ch != '\n');
253364d0a91STim J. Robbins 		}
254364d0a91STim J. Robbins 		(void)putchar('\n');
255364d0a91STim J. Robbins 	}
256364d0a91STim J. Robbins 	return (0);
257364d0a91STim J. Robbins }
258364d0a91STim J. Robbins 
259393cf508STim J. Robbins /*
260393cf508STim J. Robbins  * Cut based on byte positions, taking care not to split multibyte characters.
261393cf508STim J. Robbins  * Although this function also handles the case where -n is not specified,
262364d0a91STim J. Robbins  * b_cut() ought to be much faster.
263393cf508STim J. Robbins  */
264364d0a91STim J. Robbins int
265f4ac32deSDavid Malone b_n_cut(FILE *fp, const char *fname)
266393cf508STim J. Robbins {
267393cf508STim J. Robbins 	size_t col, i, lbuflen;
268393cf508STim J. Robbins 	char *lbuf;
269393cf508STim J. Robbins 	int canwrite, clen, warned;
270d900c384STim J. Robbins 	mbstate_t mbs;
271393cf508STim J. Robbins 
272d900c384STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
273393cf508STim J. Robbins 	warned = 0;
274393cf508STim J. Robbins 	while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
275393cf508STim J. Robbins 		for (col = 0; lbuflen > 0; col += clen) {
276d900c384STim J. Robbins 			if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
277393cf508STim J. Robbins 				if (!warned) {
278393cf508STim J. Robbins 					warn("%s", fname);
279393cf508STim J. Robbins 					warned = 1;
280393cf508STim J. Robbins 				}
281d900c384STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
282393cf508STim J. Robbins 				clen = 1;
283393cf508STim J. Robbins 			}
284393cf508STim J. Robbins 			if (clen == 0 || *lbuf == '\n')
285393cf508STim J. Robbins 				break;
286393cf508STim J. Robbins 			if (col < maxval && !positions[1 + col]) {
287393cf508STim J. Robbins 				/*
288393cf508STim J. Robbins 				 * Print the character if (1) after an initial
289393cf508STim J. Robbins 				 * segment of un-selected bytes, the rest of
290393cf508STim J. Robbins 				 * it is selected, and (2) the last byte is
291393cf508STim J. Robbins 				 * selected.
292393cf508STim J. Robbins 				 */
293393cf508STim J. Robbins 				i = col;
294393cf508STim J. Robbins 				while (i < col + clen && i < maxval &&
295393cf508STim J. Robbins 				    !positions[1 + i])
296393cf508STim J. Robbins 					i++;
297393cf508STim J. Robbins 				canwrite = i < col + clen;
298393cf508STim J. Robbins 				for (; i < col + clen && i < maxval; i++)
299393cf508STim J. Robbins 					canwrite &= positions[1 + i];
300393cf508STim J. Robbins 				if (canwrite)
301393cf508STim J. Robbins 					fwrite(lbuf, 1, clen, stdout);
302393cf508STim J. Robbins 			} else {
303393cf508STim J. Robbins 				/*
304393cf508STim J. Robbins 				 * Print the character if all of it has
305393cf508STim J. Robbins 				 * been selected.
306393cf508STim J. Robbins 				 */
307393cf508STim J. Robbins 				canwrite = 1;
308393cf508STim J. Robbins 				for (i = col; i < col + clen; i++)
309393cf508STim J. Robbins 					if ((i >= maxval && !autostop) ||
310393cf508STim J. Robbins 					    (i < maxval && !positions[1 + i])) {
311393cf508STim J. Robbins 						canwrite = 0;
312393cf508STim J. Robbins 						break;
313393cf508STim J. Robbins 					}
314393cf508STim J. Robbins 				if (canwrite)
315393cf508STim J. Robbins 					fwrite(lbuf, 1, clen, stdout);
316393cf508STim J. Robbins 			}
317393cf508STim J. Robbins 			lbuf += clen;
318393cf508STim J. Robbins 			lbuflen -= clen;
319393cf508STim J. Robbins 		}
320393cf508STim J. Robbins 		if (lbuflen > 0)
321393cf508STim J. Robbins 			putchar('\n');
322393cf508STim J. Robbins 	}
323364d0a91STim J. Robbins 	return (warned);
324393cf508STim J. Robbins }
325393cf508STim J. Robbins 
326364d0a91STim J. Robbins int
327364d0a91STim J. Robbins c_cut(FILE *fp, const char *fname)
3289b50d902SRodney W. Grimes {
329364d0a91STim J. Robbins 	wint_t ch;
330364d0a91STim J. Robbins 	int col;
3312c39ae65SEivind Eklund 	char *pos;
3329b50d902SRodney W. Grimes 
3332c39ae65SEivind Eklund 	ch = 0;
3349b50d902SRodney W. Grimes 	for (;;) {
3359b50d902SRodney W. Grimes 		pos = positions + 1;
3369b50d902SRodney W. Grimes 		for (col = maxval; col; --col) {
337364d0a91STim J. Robbins 			if ((ch = getwc(fp)) == WEOF)
338364d0a91STim J. Robbins 				goto out;
3399b50d902SRodney W. Grimes 			if (ch == '\n')
3409b50d902SRodney W. Grimes 				break;
3419b50d902SRodney W. Grimes 			if (*pos++)
342364d0a91STim J. Robbins 				(void)putwchar(ch);
3439b50d902SRodney W. Grimes 		}
3442c39ae65SEivind Eklund 		if (ch != '\n') {
3459b50d902SRodney W. Grimes 			if (autostop)
346364d0a91STim J. Robbins 				while ((ch = getwc(fp)) != WEOF && ch != '\n')
347364d0a91STim J. Robbins 					(void)putwchar(ch);
3489b50d902SRodney W. Grimes 			else
349364d0a91STim J. Robbins 				while ((ch = getwc(fp)) != WEOF && ch != '\n');
3502c39ae65SEivind Eklund 		}
351364d0a91STim J. Robbins 		(void)putwchar('\n');
3529b50d902SRodney W. Grimes 	}
353364d0a91STim J. Robbins out:
354364d0a91STim J. Robbins 	if (ferror(fp)) {
355364d0a91STim J. Robbins 		warn("%s", fname);
356364d0a91STim J. Robbins 		return (1);
357364d0a91STim J. Robbins 	}
358364d0a91STim J. Robbins 	return (0);
3599b50d902SRodney W. Grimes }
3609b50d902SRodney W. Grimes 
361364d0a91STim J. Robbins int
362f4ac32deSDavid Malone f_cut(FILE *fp, const char *fname __unused)
3639b50d902SRodney W. Grimes {
3642c39ae65SEivind Eklund 	int ch, field, isdelim;
3652c39ae65SEivind Eklund 	char *pos, *p, sep;
3669b50d902SRodney W. Grimes 	int output;
36793738f50STim J. Robbins 	char *lbuf, *mlbuf;
3681928e20eSDima Dorfman 	size_t lbuflen;
3699b50d902SRodney W. Grimes 
37093738f50STim J. Robbins 	mlbuf = NULL;
3711928e20eSDima Dorfman 	for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
3721928e20eSDima Dorfman 		/* Assert EOL has a newline. */
3731928e20eSDima Dorfman 		if (*(lbuf + lbuflen - 1) != '\n') {
3741928e20eSDima Dorfman 			/* Can't have > 1 line with no trailing newline. */
3751928e20eSDima Dorfman 			mlbuf = malloc(lbuflen + 1);
3761928e20eSDima Dorfman 			if (mlbuf == NULL)
3771928e20eSDima Dorfman 				err(1, "malloc");
3781928e20eSDima Dorfman 			memcpy(mlbuf, lbuf, lbuflen);
3791928e20eSDima Dorfman 			*(mlbuf + lbuflen) = '\n';
3801928e20eSDima Dorfman 			lbuf = mlbuf;
3811928e20eSDima Dorfman 		}
382eaf92380SAndrey A. Chernov 		output = 0;
3839b50d902SRodney W. Grimes 		for (isdelim = 0, p = lbuf;; ++p) {
3841928e20eSDima Dorfman 			ch = *p;
3859b50d902SRodney W. Grimes 			/* this should work if newline is delimiter */
3869b50d902SRodney W. Grimes 			if (ch == sep)
3879b50d902SRodney W. Grimes 				isdelim = 1;
3889b50d902SRodney W. Grimes 			if (ch == '\n') {
3899b50d902SRodney W. Grimes 				if (!isdelim && !sflag)
3901928e20eSDima Dorfman 					(void)fwrite(lbuf, lbuflen, 1, stdout);
3919b50d902SRodney W. Grimes 				break;
3929b50d902SRodney W. Grimes 			}
3939b50d902SRodney W. Grimes 		}
3949b50d902SRodney W. Grimes 		if (!isdelim)
3959b50d902SRodney W. Grimes 			continue;
3969b50d902SRodney W. Grimes 
3979b50d902SRodney W. Grimes 		pos = positions + 1;
3989b50d902SRodney W. Grimes 		for (field = maxval, p = lbuf; field; --field, ++pos) {
3999b50d902SRodney W. Grimes 			if (*pos) {
4009b50d902SRodney W. Grimes 				if (output++)
4019b50d902SRodney W. Grimes 					(void)putchar(sep);
4029b50d902SRodney W. Grimes 				while ((ch = *p++) != '\n' && ch != sep)
4039b50d902SRodney W. Grimes 					(void)putchar(ch);
4042c39ae65SEivind Eklund 			} else {
4052c39ae65SEivind Eklund 				while ((ch = *p++) != '\n' && ch != sep)
4062c39ae65SEivind Eklund 					continue;
4072c39ae65SEivind Eklund 			}
4089b50d902SRodney W. Grimes 			if (ch == '\n')
4099b50d902SRodney W. Grimes 				break;
4109b50d902SRodney W. Grimes 		}
4112c39ae65SEivind Eklund 		if (ch != '\n') {
4129b50d902SRodney W. Grimes 			if (autostop) {
4139b50d902SRodney W. Grimes 				if (output)
4149b50d902SRodney W. Grimes 					(void)putchar(sep);
4159b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p)
4169b50d902SRodney W. Grimes 					(void)putchar(ch);
4179b50d902SRodney W. Grimes 			} else
4189b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p);
4192c39ae65SEivind Eklund 		}
4209b50d902SRodney W. Grimes 		(void)putchar('\n');
4219b50d902SRodney W. Grimes 	}
4221928e20eSDima Dorfman 	if (mlbuf != NULL)
4231928e20eSDima Dorfman 		free(mlbuf);
424364d0a91STim J. Robbins 	return (0);
4259b50d902SRodney W. Grimes }
4269b50d902SRodney W. Grimes 
427812bff99SPhilippe Charnier static void
428f4ac32deSDavid Malone usage(void)
4299b50d902SRodney W. Grimes {
430d51c6625SEivind Eklund 	(void)fprintf(stderr, "%s\n%s\n%s\n",
431d51c6625SEivind Eklund 		"usage: cut -b list [-n] [file ...]",
432d51c6625SEivind Eklund 		"       cut -c list [file ...]",
433812bff99SPhilippe Charnier 		"       cut -f list [-s] [-d delim] [file ...]");
4349b50d902SRodney W. Grimes 	exit(1);
4359b50d902SRodney W. Grimes }
436