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.3 2003/10/02 17:42:27 hmp Exp $
57 void c_cut (FILE *, const char *);
58 void f_cut (FILE *, const char *);
59 void get_list (char *);
60 int main (int, char **);
61 static void usage (void);
64 main(int argc
, char **argv
)
67 void (*fcn
) (FILE *, const char *) = NULL
;
71 setlocale (LC_ALL
, "");
73 dchar
= '\t'; /* default delimiter is \t */
75 /* Since we don't support multi-byte characters, the -c and -b
76 options are equivalent, and the -n option is meaningless. */
77 while ((ch
= getopt(argc
, argv
, "b:c:d:f:sn")) != -1)
109 } else if (!cflag
|| dflag
|| sflag
)
113 for (; *argv
; ++argv
) {
114 if (!(fp
= fopen(*argv
, "r")))
124 size_t autostart
, autostop
, maxval
;
126 char positions
[_POSIX2_LINE_MAX
+ 1];
131 size_t setautostart
, start
, stop
;
136 * set a byte in the positions array to indicate if a field or
137 * column is to be selected; use +1, it's 1-based, not 0-based.
138 * This parser is less restrictive than the Draft 9 POSIX spec.
139 * POSIX doesn't allow lists that aren't in increasing order or
140 * overlapping lists. We also handle "-3-5" although there's no
143 for (; (p
= strsep(&list
, ", \t")) != NULL
;) {
144 setautostart
= start
= stop
= 0;
149 if (isdigit((unsigned char)*p
)) {
150 start
= stop
= strtol(p
, &p
, 10);
151 if (setautostart
&& start
> autostart
)
155 if (isdigit((unsigned char)p
[1]))
156 stop
= strtol(p
+ 1, &p
, 10);
159 if (!autostop
|| autostop
> stop
)
164 errx(1, "[-cf] list: illegal list value");
166 errx(1, "[-cf] list: values may not include zero");
167 if (stop
> _POSIX2_LINE_MAX
)
168 errx(1, "[-cf] list: %ld too large (max %d)",
169 (long)stop
, _POSIX2_LINE_MAX
);
172 for (pos
= positions
+ start
; start
++ <= stop
; *pos
++ = 1);
175 /* overlapping ranges */
176 if (autostop
&& maxval
> autostop
)
181 memset(positions
+ 1, '1', autostart
);
186 c_cut(FILE *fp
, const char *fname
)
195 for (col
= maxval
; col
; --col
) {
196 if ((ch
= getc(fp
)) == EOF
)
205 while ((ch
= getc(fp
)) != EOF
&& ch
!= '\n')
208 while ((ch
= getc(fp
)) != EOF
&& ch
!= '\n');
215 f_cut(FILE *fp
, const char *fname __unused
)
217 int ch
, field
, isdelim
;
220 char *lbuf
, *mlbuf
= NULL
;
223 for (sep
= dchar
; (lbuf
= fgetln(fp
, &lbuflen
)) != NULL
;) {
224 /* Assert EOL has a newline. */
225 if (*(lbuf
+ lbuflen
- 1) != '\n') {
226 /* Can't have > 1 line with no trailing newline. */
227 mlbuf
= malloc(lbuflen
+ 1);
230 memcpy(mlbuf
, lbuf
, lbuflen
);
231 *(mlbuf
+ lbuflen
) = '\n';
235 for (isdelim
= 0, p
= lbuf
;; ++p
) {
237 /* this should work if newline is delimiter */
241 if (!isdelim
&& !sflag
)
242 (void)fwrite(lbuf
, lbuflen
, 1, stdout
);
250 for (field
= maxval
, p
= lbuf
; field
; --field
, ++pos
) {
254 while ((ch
= *p
++) != '\n' && ch
!= sep
)
257 while ((ch
= *p
++) != '\n' && ch
!= sep
)
267 for (; (ch
= *p
) != '\n'; ++p
)
270 for (; (ch
= *p
) != '\n'; ++p
);
281 (void)fprintf(stderr
, "%s\n%s\n%s\n",
282 "usage: cut -b list [-n] [file ...]",
283 " cut -c list [file ...]",
284 " cut -f list [-s] [-d delim] [file ...]");