xref: /freebsd/usr.bin/cut/cut.c (revision d900c38411bbd8b75d877f29e751949f934cb127)
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 
64393cf508STim J. Robbins void	b_n_cut(FILE *, const char *);
6599557a79SWill Andrews void	c_cut(FILE *, const char *);
6699557a79SWill Andrews void	f_cut(FILE *, const char *);
6799557a79SWill Andrews void	get_list(char *);
68a8522a9bSTim J. Robbins void	needpos(size_t);
6999557a79SWill Andrews static 	void usage(void);
709b50d902SRodney W. Grimes 
719b50d902SRodney W. Grimes int
72f4ac32deSDavid Malone main(int argc, char *argv[])
739b50d902SRodney W. Grimes {
749b50d902SRodney W. Grimes 	FILE *fp;
7593738f50STim J. Robbins 	void (*fcn)(FILE *, const char *);
76a6ea32c3STim J. Robbins 	int ch, rval;
779b50d902SRodney W. Grimes 
78d51c6625SEivind Eklund 	setlocale(LC_ALL, "");
79d51c6625SEivind Eklund 
8093738f50STim J. Robbins 	fcn = NULL;
819b50d902SRodney W. Grimes 	dchar = '\t';			/* default delimiter is \t */
829b50d902SRodney W. Grimes 
83393cf508STim J. Robbins 	/*
84393cf508STim J. Robbins 	 * Since we don't support multi-byte characters, the -c and -b
85393cf508STim J. Robbins 	 * options are equivalent.
86393cf508STim J. Robbins 	 */
875183fb53SEivind Eklund 	while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
889b50d902SRodney W. Grimes 		switch(ch) {
89d51c6625SEivind Eklund 		case 'b':
90393cf508STim J. Robbins 			fcn = c_cut;
91393cf508STim J. Robbins 			get_list(optarg);
92393cf508STim J. Robbins 			bflag = 1;
93393cf508STim J. Robbins 			break;
949b50d902SRodney W. Grimes 		case 'c':
959b50d902SRodney W. Grimes 			fcn = c_cut;
969b50d902SRodney W. Grimes 			get_list(optarg);
979b50d902SRodney W. Grimes 			cflag = 1;
989b50d902SRodney W. Grimes 			break;
999b50d902SRodney W. Grimes 		case 'd':
1009b50d902SRodney W. Grimes 			dchar = *optarg;
1019b50d902SRodney W. Grimes 			dflag = 1;
1029b50d902SRodney W. Grimes 			break;
1039b50d902SRodney W. Grimes 		case 'f':
1049b50d902SRodney W. Grimes 			get_list(optarg);
1059b50d902SRodney W. Grimes 			fcn = f_cut;
1069b50d902SRodney W. Grimes 			fflag = 1;
1079b50d902SRodney W. Grimes 			break;
1089b50d902SRodney W. Grimes 		case 's':
1099b50d902SRodney W. Grimes 			sflag = 1;
1109b50d902SRodney W. Grimes 			break;
111d51c6625SEivind Eklund 		case 'n':
112393cf508STim J. Robbins 			nflag = 1;
113d51c6625SEivind Eklund 			break;
1149b50d902SRodney W. Grimes 		case '?':
1159b50d902SRodney W. Grimes 		default:
1169b50d902SRodney W. Grimes 			usage();
1179b50d902SRodney W. Grimes 		}
1189b50d902SRodney W. Grimes 	argc -= optind;
1199b50d902SRodney W. Grimes 	argv += optind;
1209b50d902SRodney W. Grimes 
1219b50d902SRodney W. Grimes 	if (fflag) {
122393cf508STim J. Robbins 		if (bflag || cflag || nflag)
1239b50d902SRodney W. Grimes 			usage();
124393cf508STim J. Robbins 	} else if (!(bflag || cflag) || dflag || sflag)
1259b50d902SRodney W. Grimes 		usage();
126393cf508STim J. Robbins 	else if (!bflag && nflag)
127393cf508STim J. Robbins 		usage();
128393cf508STim J. Robbins 
129393cf508STim J. Robbins 	if (nflag)
130393cf508STim J. Robbins 		fcn = b_n_cut;
1319b50d902SRodney W. Grimes 
132a6ea32c3STim J. Robbins 	rval = 0;
1339b50d902SRodney W. Grimes 	if (*argv)
1349b50d902SRodney W. Grimes 		for (; *argv; ++argv) {
135204c78a1STim J. Robbins 			if (strcmp(*argv, "-") == 0)
136204c78a1STim J. Robbins 				fcn(stdin, "stdin");
137204c78a1STim J. Robbins 			else {
138a6ea32c3STim J. Robbins 				if (!(fp = fopen(*argv, "r"))) {
139a6ea32c3STim J. Robbins 					warn("%s", *argv);
140a6ea32c3STim J. Robbins 					rval = 1;
141a6ea32c3STim J. Robbins 					continue;
142a6ea32c3STim J. Robbins 				}
1439b50d902SRodney W. Grimes 				fcn(fp, *argv);
1449b50d902SRodney W. Grimes 				(void)fclose(fp);
1459b50d902SRodney W. Grimes 			}
146204c78a1STim J. Robbins 		}
1479b50d902SRodney W. Grimes 	else
1489b50d902SRodney W. Grimes 		fcn(stdin, "stdin");
149a6ea32c3STim J. Robbins 	exit(rval);
1509b50d902SRodney W. Grimes }
1519b50d902SRodney W. Grimes 
152d8a3fbd5SWill Andrews size_t autostart, autostop, maxval;
1539b50d902SRodney W. Grimes 
154a8522a9bSTim J. Robbins char *positions;
1559b50d902SRodney W. Grimes 
1569b50d902SRodney W. Grimes void
157f4ac32deSDavid Malone get_list(char *list)
1589b50d902SRodney W. Grimes {
159d8a3fbd5SWill Andrews 	size_t setautostart, start, stop;
1602c39ae65SEivind Eklund 	char *pos;
1619b50d902SRodney W. Grimes 	char *p;
1629b50d902SRodney W. Grimes 
1639b50d902SRodney W. Grimes 	/*
1649b50d902SRodney W. Grimes 	 * set a byte in the positions array to indicate if a field or
1659b50d902SRodney W. Grimes 	 * column is to be selected; use +1, it's 1-based, not 0-based.
1669b50d902SRodney W. Grimes 	 * This parser is less restrictive than the Draft 9 POSIX spec.
1679b50d902SRodney W. Grimes 	 * POSIX doesn't allow lists that aren't in increasing order or
1689b50d902SRodney W. Grimes 	 * overlapping lists.  We also handle "-3-5" although there's no
1699b50d902SRodney W. Grimes 	 * real reason too.
1709b50d902SRodney W. Grimes 	 */
1712c39ae65SEivind Eklund 	for (; (p = strsep(&list, ", \t")) != NULL;) {
1729b50d902SRodney W. Grimes 		setautostart = start = stop = 0;
1739b50d902SRodney W. Grimes 		if (*p == '-') {
1749b50d902SRodney W. Grimes 			++p;
1759b50d902SRodney W. Grimes 			setautostart = 1;
1769b50d902SRodney W. Grimes 		}
1772c39ae65SEivind Eklund 		if (isdigit((unsigned char)*p)) {
1789b50d902SRodney W. Grimes 			start = stop = strtol(p, &p, 10);
1799b50d902SRodney W. Grimes 			if (setautostart && start > autostart)
1809b50d902SRodney W. Grimes 				autostart = start;
1819b50d902SRodney W. Grimes 		}
1829b50d902SRodney W. Grimes 		if (*p == '-') {
1832c39ae65SEivind Eklund 			if (isdigit((unsigned char)p[1]))
1849b50d902SRodney W. Grimes 				stop = strtol(p + 1, &p, 10);
1859b50d902SRodney W. Grimes 			if (*p == '-') {
1869b50d902SRodney W. Grimes 				++p;
1879b50d902SRodney W. Grimes 				if (!autostop || autostop > stop)
1889b50d902SRodney W. Grimes 					autostop = stop;
1899b50d902SRodney W. Grimes 			}
1909b50d902SRodney W. Grimes 		}
1919b50d902SRodney W. Grimes 		if (*p)
192812bff99SPhilippe Charnier 			errx(1, "[-cf] list: illegal list value");
1939b50d902SRodney W. Grimes 		if (!stop || !start)
194812bff99SPhilippe Charnier 			errx(1, "[-cf] list: values may not include zero");
195a8522a9bSTim J. Robbins 		if (maxval < stop) {
1969b50d902SRodney W. Grimes 			maxval = stop;
197a8522a9bSTim J. Robbins 			needpos(maxval + 1);
198a8522a9bSTim J. Robbins 		}
1999b50d902SRodney W. Grimes 		for (pos = positions + start; start++ <= stop; *pos++ = 1);
2009b50d902SRodney W. Grimes 	}
2019b50d902SRodney W. Grimes 
2029b50d902SRodney W. Grimes 	/* overlapping ranges */
203a8522a9bSTim J. Robbins 	if (autostop && maxval > autostop) {
2049b50d902SRodney W. Grimes 		maxval = autostop;
205a8522a9bSTim J. Robbins 		needpos(maxval + 1);
206a8522a9bSTim J. Robbins 	}
2079b50d902SRodney W. Grimes 
2089b50d902SRodney W. Grimes 	/* set autostart */
2099b50d902SRodney W. Grimes 	if (autostart)
2109b50d902SRodney W. Grimes 		memset(positions + 1, '1', autostart);
2119b50d902SRodney W. Grimes }
2129b50d902SRodney W. Grimes 
213a8522a9bSTim J. Robbins void
214a8522a9bSTim J. Robbins needpos(size_t n)
215a8522a9bSTim J. Robbins {
216a8522a9bSTim J. Robbins 	static size_t npos;
217f457179aSTim J. Robbins 	size_t oldnpos;
218a8522a9bSTim J. Robbins 
219a8522a9bSTim J. Robbins 	/* Grow the positions array to at least the specified size. */
220a8522a9bSTim J. Robbins 	if (n > npos) {
221f457179aSTim J. Robbins 		oldnpos = npos;
222a8522a9bSTim J. Robbins 		if (npos == 0)
223a8522a9bSTim J. Robbins 			npos = n;
224a8522a9bSTim J. Robbins 		while (n > npos)
225a8522a9bSTim J. Robbins 			npos *= 2;
226a8522a9bSTim J. Robbins 		if ((positions = realloc(positions, npos)) == NULL)
227a8522a9bSTim J. Robbins 			err(1, "realloc");
228f457179aSTim J. Robbins 		memset((char *)positions + oldnpos, 0, npos - oldnpos);
229a8522a9bSTim J. Robbins 	}
230a8522a9bSTim J. Robbins }
231a8522a9bSTim J. Robbins 
232393cf508STim J. Robbins /*
233393cf508STim J. Robbins  * Cut based on byte positions, taking care not to split multibyte characters.
234393cf508STim J. Robbins  * Although this function also handles the case where -n is not specified,
235393cf508STim J. Robbins  * c_cut() ought to be much faster.
236393cf508STim J. Robbins  */
237393cf508STim J. Robbins void
238f4ac32deSDavid Malone b_n_cut(FILE *fp, const char *fname)
239393cf508STim J. Robbins {
240393cf508STim J. Robbins 	size_t col, i, lbuflen;
241393cf508STim J. Robbins 	char *lbuf;
242393cf508STim J. Robbins 	int canwrite, clen, warned;
243d900c384STim J. Robbins 	mbstate_t mbs;
244393cf508STim J. Robbins 
245d900c384STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
246393cf508STim J. Robbins 	warned = 0;
247393cf508STim J. Robbins 	while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
248393cf508STim J. Robbins 		for (col = 0; lbuflen > 0; col += clen) {
249d900c384STim J. Robbins 			if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
250393cf508STim J. Robbins 				if (!warned) {
251393cf508STim J. Robbins 					warn("%s", fname);
252393cf508STim J. Robbins 					warned = 1;
253393cf508STim J. Robbins 				}
254d900c384STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
255393cf508STim J. Robbins 				clen = 1;
256393cf508STim J. Robbins 			}
257393cf508STim J. Robbins 			if (clen == 0 || *lbuf == '\n')
258393cf508STim J. Robbins 				break;
259393cf508STim J. Robbins 			if (col < maxval && !positions[1 + col]) {
260393cf508STim J. Robbins 				/*
261393cf508STim J. Robbins 				 * Print the character if (1) after an initial
262393cf508STim J. Robbins 				 * segment of un-selected bytes, the rest of
263393cf508STim J. Robbins 				 * it is selected, and (2) the last byte is
264393cf508STim J. Robbins 				 * selected.
265393cf508STim J. Robbins 				 */
266393cf508STim J. Robbins 				i = col;
267393cf508STim J. Robbins 				while (i < col + clen && i < maxval &&
268393cf508STim J. Robbins 				    !positions[1 + i])
269393cf508STim J. Robbins 					i++;
270393cf508STim J. Robbins 				canwrite = i < col + clen;
271393cf508STim J. Robbins 				for (; i < col + clen && i < maxval; i++)
272393cf508STim J. Robbins 					canwrite &= positions[1 + i];
273393cf508STim J. Robbins 				if (canwrite)
274393cf508STim J. Robbins 					fwrite(lbuf, 1, clen, stdout);
275393cf508STim J. Robbins 			} else {
276393cf508STim J. Robbins 				/*
277393cf508STim J. Robbins 				 * Print the character if all of it has
278393cf508STim J. Robbins 				 * been selected.
279393cf508STim J. Robbins 				 */
280393cf508STim J. Robbins 				canwrite = 1;
281393cf508STim J. Robbins 				for (i = col; i < col + clen; i++)
282393cf508STim J. Robbins 					if ((i >= maxval && !autostop) ||
283393cf508STim J. Robbins 					    (i < maxval && !positions[1 + i])) {
284393cf508STim J. Robbins 						canwrite = 0;
285393cf508STim J. Robbins 						break;
286393cf508STim J. Robbins 					}
287393cf508STim J. Robbins 				if (canwrite)
288393cf508STim J. Robbins 					fwrite(lbuf, 1, clen, stdout);
289393cf508STim J. Robbins 			}
290393cf508STim J. Robbins 			lbuf += clen;
291393cf508STim J. Robbins 			lbuflen -= clen;
292393cf508STim J. Robbins 		}
293393cf508STim J. Robbins 		if (lbuflen > 0)
294393cf508STim J. Robbins 			putchar('\n');
295393cf508STim J. Robbins 	}
296393cf508STim J. Robbins }
297393cf508STim J. Robbins 
2989b50d902SRodney W. Grimes void
299f4ac32deSDavid Malone c_cut(FILE *fp, const char *fname __unused)
3009b50d902SRodney W. Grimes {
3012c39ae65SEivind Eklund 	int ch, col;
3022c39ae65SEivind Eklund 	char *pos;
3039b50d902SRodney W. Grimes 
3042c39ae65SEivind Eklund 	ch = 0;
3059b50d902SRodney W. Grimes 	for (;;) {
3069b50d902SRodney W. Grimes 		pos = positions + 1;
3079b50d902SRodney W. Grimes 		for (col = maxval; col; --col) {
3089b50d902SRodney W. Grimes 			if ((ch = getc(fp)) == EOF)
3099b50d902SRodney W. Grimes 				return;
3109b50d902SRodney W. Grimes 			if (ch == '\n')
3119b50d902SRodney W. Grimes 				break;
3129b50d902SRodney W. Grimes 			if (*pos++)
3139b50d902SRodney W. Grimes 				(void)putchar(ch);
3149b50d902SRodney W. Grimes 		}
3152c39ae65SEivind Eklund 		if (ch != '\n') {
3169b50d902SRodney W. Grimes 			if (autostop)
3179b50d902SRodney W. Grimes 				while ((ch = getc(fp)) != EOF && ch != '\n')
3189b50d902SRodney W. Grimes 					(void)putchar(ch);
3199b50d902SRodney W. Grimes 			else
3209b50d902SRodney W. Grimes 				while ((ch = getc(fp)) != EOF && ch != '\n');
3212c39ae65SEivind Eklund 		}
3229b50d902SRodney W. Grimes 		(void)putchar('\n');
3239b50d902SRodney W. Grimes 	}
3249b50d902SRodney W. Grimes }
3259b50d902SRodney W. Grimes 
3269b50d902SRodney W. Grimes void
327f4ac32deSDavid Malone f_cut(FILE *fp, const char *fname __unused)
3289b50d902SRodney W. Grimes {
3292c39ae65SEivind Eklund 	int ch, field, isdelim;
3302c39ae65SEivind Eklund 	char *pos, *p, sep;
3319b50d902SRodney W. Grimes 	int output;
33293738f50STim J. Robbins 	char *lbuf, *mlbuf;
3331928e20eSDima Dorfman 	size_t lbuflen;
3349b50d902SRodney W. Grimes 
33593738f50STim J. Robbins 	mlbuf = NULL;
3361928e20eSDima Dorfman 	for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
3371928e20eSDima Dorfman 		/* Assert EOL has a newline. */
3381928e20eSDima Dorfman 		if (*(lbuf + lbuflen - 1) != '\n') {
3391928e20eSDima Dorfman 			/* Can't have > 1 line with no trailing newline. */
3401928e20eSDima Dorfman 			mlbuf = malloc(lbuflen + 1);
3411928e20eSDima Dorfman 			if (mlbuf == NULL)
3421928e20eSDima Dorfman 				err(1, "malloc");
3431928e20eSDima Dorfman 			memcpy(mlbuf, lbuf, lbuflen);
3441928e20eSDima Dorfman 			*(mlbuf + lbuflen) = '\n';
3451928e20eSDima Dorfman 			lbuf = mlbuf;
3461928e20eSDima Dorfman 		}
347eaf92380SAndrey A. Chernov 		output = 0;
3489b50d902SRodney W. Grimes 		for (isdelim = 0, p = lbuf;; ++p) {
3491928e20eSDima Dorfman 			ch = *p;
3509b50d902SRodney W. Grimes 			/* this should work if newline is delimiter */
3519b50d902SRodney W. Grimes 			if (ch == sep)
3529b50d902SRodney W. Grimes 				isdelim = 1;
3539b50d902SRodney W. Grimes 			if (ch == '\n') {
3549b50d902SRodney W. Grimes 				if (!isdelim && !sflag)
3551928e20eSDima Dorfman 					(void)fwrite(lbuf, lbuflen, 1, stdout);
3569b50d902SRodney W. Grimes 				break;
3579b50d902SRodney W. Grimes 			}
3589b50d902SRodney W. Grimes 		}
3599b50d902SRodney W. Grimes 		if (!isdelim)
3609b50d902SRodney W. Grimes 			continue;
3619b50d902SRodney W. Grimes 
3629b50d902SRodney W. Grimes 		pos = positions + 1;
3639b50d902SRodney W. Grimes 		for (field = maxval, p = lbuf; field; --field, ++pos) {
3649b50d902SRodney W. Grimes 			if (*pos) {
3659b50d902SRodney W. Grimes 				if (output++)
3669b50d902SRodney W. Grimes 					(void)putchar(sep);
3679b50d902SRodney W. Grimes 				while ((ch = *p++) != '\n' && ch != sep)
3689b50d902SRodney W. Grimes 					(void)putchar(ch);
3692c39ae65SEivind Eklund 			} else {
3702c39ae65SEivind Eklund 				while ((ch = *p++) != '\n' && ch != sep)
3712c39ae65SEivind Eklund 					continue;
3722c39ae65SEivind Eklund 			}
3739b50d902SRodney W. Grimes 			if (ch == '\n')
3749b50d902SRodney W. Grimes 				break;
3759b50d902SRodney W. Grimes 		}
3762c39ae65SEivind Eklund 		if (ch != '\n') {
3779b50d902SRodney W. Grimes 			if (autostop) {
3789b50d902SRodney W. Grimes 				if (output)
3799b50d902SRodney W. Grimes 					(void)putchar(sep);
3809b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p)
3819b50d902SRodney W. Grimes 					(void)putchar(ch);
3829b50d902SRodney W. Grimes 			} else
3839b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p);
3842c39ae65SEivind Eklund 		}
3859b50d902SRodney W. Grimes 		(void)putchar('\n');
3869b50d902SRodney W. Grimes 	}
3871928e20eSDima Dorfman 	if (mlbuf != NULL)
3881928e20eSDima Dorfman 		free(mlbuf);
3899b50d902SRodney W. Grimes }
3909b50d902SRodney W. Grimes 
391812bff99SPhilippe Charnier static void
392f4ac32deSDavid Malone usage(void)
3939b50d902SRodney W. Grimes {
394d51c6625SEivind Eklund 	(void)fprintf(stderr, "%s\n%s\n%s\n",
395d51c6625SEivind Eklund 		"usage: cut -b list [-n] [file ...]",
396d51c6625SEivind Eklund 		"       cut -c list [file ...]",
397812bff99SPhilippe Charnier 		"       cut -f list [-s] [-d delim] [file ...]");
3989b50d902SRodney W. Grimes 	exit(1);
3999b50d902SRodney W. Grimes }
400