1 /* $NetBSD: cut.c,v 1.29 2014/02/03 20:22:19 wiz Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95";
45 __RCSID("$NetBSD: cut.c,v 1.29 2014/02/03 20:22:19 wiz Exp $");
59 #include <sys/param.h>
68 static void b_cut(FILE *, const char *);
69 static void c_cut(FILE *, const char *);
70 static void f_cut(FILE *, const char *);
71 static void get_list(char *);
72 static void usage(void) __dead
;
75 main(int argc
, char *argv
[])
78 void (*fcn
)(FILE *, const char *);
82 (void)setlocale(LC_ALL
, "");
84 dchar
= '\t'; /* default delimiter is \t */
86 /* Since we don't support multi-byte characters, the -c and -b
87 options are equivalent, and the -n option is meaningless. */
88 while ((ch
= getopt(argc
, argv
, "b:c:d:f:sn")) != -1)
124 } else if ((!cflag
&& !bflag
) || dflag
|| sflag
)
126 else if (bflag
&& cflag
)
131 for (; *argv
; ++argv
) {
132 if (strcmp(*argv
, "-") == 0)
135 if ((fp
= fopen(*argv
, "r"))) {
149 static size_t autostart
, autostop
, maxval
;
151 static char *positions
= NULL
;
152 static size_t numpositions
= 0;
153 #define ALLOC_CHUNK _POSIX2_LINE_MAX /* malloc granularity */
158 size_t setautostart
, start
, stop
;
162 if (positions
== NULL
) {
163 numpositions
= ALLOC_CHUNK
;
164 positions
= ecalloc(numpositions
, sizeof(*positions
));
168 * set a byte in the positions array to indicate if a field or
169 * column is to be selected; use +1, it's 1-based, not 0-based.
170 * This parser is less restrictive than the Draft 9 POSIX spec.
171 * POSIX doesn't allow lists that aren't in increasing order or
172 * overlapping lists. We also handle "-3-5" although there's no
175 for (; (p
= strtok(list
, ", \t")) != NULL
; list
= NULL
) {
176 setautostart
= start
= stop
= 0;
181 if (isdigit((unsigned char)*p
)) {
182 start
= stop
= strtol(p
, &p
, 10);
183 if (setautostart
&& start
> autostart
)
187 if (isdigit((unsigned char)p
[1]))
188 stop
= strtol(p
+ 1, &p
, 10);
191 if (!autostop
|| autostop
> stop
)
196 errx(1, "[-bcf] list: illegal list value");
198 errx(1, "[-bcf] list: values may not include zero");
199 if (stop
+ 1 > numpositions
) {
201 newsize
= roundup(stop
+ 1, ALLOC_CHUNK
);
202 positions
= erealloc(positions
, newsize
);
203 (void)memset(positions
+ numpositions
, 0,
204 newsize
- numpositions
);
205 numpositions
= newsize
;
209 for (pos
= positions
+ start
; start
++ <= stop
; pos
++)
213 /* overlapping ranges */
214 if (autostop
&& maxval
> autostop
)
219 (void)memset(positions
+ 1, '1', autostart
);
224 f_cut(FILE *fp
, const char *fname __unused
)
226 int ch
, field
, isdelim
;
232 for (sep
= dchar
, tbuf
= NULL
; (lbuf
= fgetln(fp
, &len
)) != NULL
;) {
234 if (lbuf
[len
- 1] != '\n') {
235 /* no newline at the end of the last line so add one */
236 if ((tbuf
= (char *)malloc(len
+ 1)) == NULL
)
238 (void)memcpy(tbuf
, lbuf
, len
);
242 for (isdelim
= 0, p
= lbuf
;; ++p
) {
244 /* this should work if newline is delimiter */
248 if (!isdelim
&& !sflag
)
249 (void)fwrite(lbuf
, len
, 1, stdout
);
257 for (field
= maxval
, p
= lbuf
; field
; --field
, ++pos
) {
261 while ((ch
= *p
++) != '\n' && ch
!= sep
)
264 while ((ch
= *p
++) != '\n' && ch
!= sep
)
274 for (; (ch
= *p
) != '\n'; ++p
)
277 for (; (ch
= *p
) != '\n'; ++p
);
292 (void)fprintf(stderr
, "usage:\tcut -b list [-n] [file ...]\n"
293 "\tcut -c list [file ...]\n"
294 "\tcut -f list [-d string] [-s] [file ...]\n");