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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)cut.c 8.3 (Berkeley) 5/4/95
38 * $FreeBSD: src/usr.bin/cut/cut.c,v 1.9.2.3 2001/07/30 09:59:16 dd Exp $
39 * $DragonFly: src/usr.bin/cut/cut.c,v 1.6 2008/03/09 16:05:39 swildner Exp $
58 static void c_cut(FILE *, const char *);
59 static void f_cut(FILE *, const char *);
60 static void get_list(char *);
61 static int is_delim(int);
62 static void usage(void);
65 main(int argc
, char **argv
)
68 void (*fcn
) (FILE *, const char *) = NULL
;
72 setlocale (LC_ALL
, "");
74 dchar
= '\t'; /* default delimiter is \t */
76 /* Since we don't support multi-byte characters, the -c and -b
77 options are equivalent, and the -n option is meaningless. */
78 while ((ch
= getopt(argc
, argv
, "b:c:d:f:snw")) != -1)
111 if (cflag
|| (wflag
&& dflag
))
113 } else if (!cflag
|| dflag
|| sflag
|| wflag
)
117 for (; *argv
; ++argv
) {
118 if (!(fp
= fopen(*argv
, "r")))
128 size_t autostart
, autostop
, maxval
;
130 char positions
[_POSIX2_LINE_MAX
+ 1];
135 size_t setautostart
, start
, stop
;
140 * set a byte in the positions array to indicate if a field or
141 * column is to be selected; use +1, it's 1-based, not 0-based.
142 * This parser is less restrictive than the Draft 9 POSIX spec.
143 * POSIX doesn't allow lists that aren't in increasing order or
144 * overlapping lists. We also handle "-3-5" although there's no
147 for (; (p
= strsep(&list
, ", \t")) != NULL
;) {
148 setautostart
= start
= stop
= 0;
153 if (isdigit((unsigned char)*p
)) {
154 start
= stop
= strtol(p
, &p
, 10);
155 if (setautostart
&& start
> autostart
)
159 if (isdigit((unsigned char)p
[1]))
160 stop
= strtol(p
+ 1, &p
, 10);
163 if (!autostop
|| autostop
> stop
)
168 errx(1, "[-cf] list: illegal list value");
170 errx(1, "[-cf] list: values may not include zero");
171 if (stop
> _POSIX2_LINE_MAX
)
172 errx(1, "[-cf] list: %ld too large (max %d)",
173 (long)stop
, _POSIX2_LINE_MAX
);
176 for (pos
= positions
+ start
; start
++ <= stop
; *pos
++ = 1);
179 /* overlapping ranges */
180 if (autostop
&& maxval
> autostop
)
185 memset(positions
+ 1, '1', autostart
);
190 c_cut(FILE *fp
, const char *fname
)
199 for (col
= maxval
; col
; --col
) {
200 if ((ch
= getc(fp
)) == EOF
)
209 while ((ch
= getc(fp
)) != EOF
&& ch
!= '\n')
212 while ((ch
= getc(fp
)) != EOF
&& ch
!= '\n');
222 if (ch
== ' ' || ch
== '\t')
232 f_cut(FILE *fp
, const char *fname __unused
)
234 int ch
, field
, isdelim
;
237 char *lbuf
, *mlbuf
= NULL
;
240 sep
= wflag
? ' ' : dchar
;
241 while ((lbuf
= fgetln(fp
, &lbuflen
)) != NULL
) {
242 /* Assert EOL has a newline. */
243 if (*(lbuf
+ lbuflen
- 1) != '\n') {
244 /* Can't have > 1 line with no trailing newline. */
245 mlbuf
= malloc(lbuflen
+ 1);
248 memcpy(mlbuf
, lbuf
, lbuflen
);
249 *(mlbuf
+ lbuflen
) = '\n';
253 for (isdelim
= 0, p
= lbuf
;; ++p
) {
255 /* this should work if newline is delimiter */
259 if (!isdelim
&& !sflag
)
260 (void)fwrite(lbuf
, lbuflen
, 1, stdout
);
268 for (field
= maxval
, p
= lbuf
; field
; --field
, ++pos
) {
272 while ((ch
= *p
++) != '\n' && !is_delim(ch
))
274 /* compress whitespace */
275 if (wflag
&& ch
!= '\n')
276 while (is_delim(*p
)) p
++;
278 while ((ch
= *p
++) != '\n' && !is_delim(ch
))
280 /* compress whitespace */
281 if (wflag
&& ch
!= '\n')
282 while (is_delim(*p
)) p
++;
291 for (; (ch
= *p
) != '\n'; ++p
)
294 for (; (ch
= *p
) != '\n'; ++p
);
305 (void)fprintf(stderr
, "%s\n%s\n%s\n",
306 "usage: cut -b list [-n] [file ...]",
307 " cut -c list [file ...]",
308 " cut -f list [-s] [-w | -d delim] [file ...]");