1 /* $NetBSD: cut.c,v 1.24 2007/12/15 19:44:49 perry 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.24 2007/12/15 19:44:49 perry 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
)
130 for (; *argv
; ++argv
) {
131 if (strcmp(*argv
, "-") == 0)
134 if ((fp
= fopen(*argv
, "r")) == NULL
)
145 static size_t autostart
, autostop
, maxval
;
147 static char *positions
= NULL
;
148 static size_t numpositions
= 0;
149 #define ALLOC_CHUNK _POSIX2_LINE_MAX /* malloc granularity */
154 size_t setautostart
, start
, stop
;
158 if (positions
== NULL
) {
159 numpositions
= ALLOC_CHUNK
;
160 positions
= ecalloc(numpositions
, sizeof(*positions
));
164 * set a byte in the positions array to indicate if a field or
165 * column is to be selected; use +1, it's 1-based, not 0-based.
166 * This parser is less restrictive than the Draft 9 POSIX spec.
167 * POSIX doesn't allow lists that aren't in increasing order or
168 * overlapping lists. We also handle "-3-5" although there's no
171 for (; (p
= strtok(list
, ", \t")) != NULL
; list
= NULL
) {
172 setautostart
= start
= stop
= 0;
177 if (isdigit((unsigned char)*p
)) {
178 start
= stop
= strtol(p
, &p
, 10);
179 if (setautostart
&& start
> autostart
)
183 if (isdigit((unsigned char)p
[1]))
184 stop
= strtol(p
+ 1, &p
, 10);
187 if (!autostop
|| autostop
> stop
)
192 errx(1, "[-cf] list: illegal list value");
194 errx(1, "[-cf] list: values may not include zero");
195 if (stop
+ 1 > numpositions
) {
197 newsize
= roundup(stop
+ 1, ALLOC_CHUNK
);
198 positions
= erealloc(positions
, newsize
);
199 (void)memset(positions
+ numpositions
, 0,
200 newsize
- numpositions
);
201 numpositions
= newsize
;
205 for (pos
= positions
+ start
; start
++ <= stop
; pos
++)
209 /* overlapping ranges */
210 if (autostop
&& maxval
> autostop
)
215 (void)memset(positions
+ 1, '1', autostart
);
220 f_cut(FILE *fp
, const char *fname __unused
)
222 int ch
, field
, isdelim
;
228 for (sep
= dchar
, tbuf
= NULL
; (lbuf
= fgetln(fp
, &len
)) != NULL
;) {
230 if (lbuf
[len
- 1] != '\n') {
231 /* no newline at the end of the last line so add one */
232 if ((tbuf
= (char *)malloc(len
+ 1)) == NULL
)
234 (void)memcpy(tbuf
, lbuf
, len
);
238 for (isdelim
= 0, p
= lbuf
;; ++p
) {
240 /* this should work if newline is delimiter */
244 if (!isdelim
&& !sflag
)
245 (void)fwrite(lbuf
, len
, 1, stdout
);
253 for (field
= maxval
, p
= lbuf
; field
; --field
, ++pos
) {
257 while ((ch
= *p
++) != '\n' && ch
!= sep
)
260 while ((ch
= *p
++) != '\n' && ch
!= sep
)
270 for (; (ch
= *p
) != '\n'; ++p
)
273 for (; (ch
= *p
) != '\n'; ++p
);
288 (void)fprintf(stderr
, "Usage:\tcut -b list [-n] [file ...]\n"
289 "\tcut -c list [file1 ...]\n"
290 "\tcut -f list [-d delim] [-s] [file ...]\n");