1 /* $NetBSD: cut.c,v 1.25 2008/07/21 14:19:22 lukem 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>
48 #include <sys/param.h>
50 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
59 static void b_cut(FILE *, const char *);
61 static void c_cut(FILE *, const char *);
63 static void f_cut(FILE *, const char *);
64 static void get_list(char *);
65 static void usage(void) __dead
;
68 main(int argc
, char *argv
[])
71 void (*fcn
)(FILE *, const char *);
75 (void)setlocale(LC_ALL
, "");
77 dchar
= '\t'; /* default delimiter is \t */
79 /* Since we don't support multi-byte characters, the -c and -b
80 options are equivalent, and the -n option is meaningless. */
81 while ((ch
= getopt(argc
, argv
, "b:c:d:f:sn")) != -1)
120 } else if ((!cflag
&& !bflag
) || dflag
|| sflag
)
122 else if (bflag
&& cflag
)
126 for (; *argv
; ++argv
) {
127 if (strcmp(*argv
, "-") == 0)
130 if ((fp
= fopen(*argv
, "r")) == NULL
)
141 static size_t autostart
, autostop
, maxval
;
143 static char *positions
= NULL
;
144 static size_t numpositions
= 0;
145 #define ALLOC_CHUNK 4096 /* malloc granularity */
150 size_t setautostart
, start
, stop
;
154 if (positions
== NULL
) {
155 numpositions
= ALLOC_CHUNK
;
156 positions
= ecalloc(numpositions
, sizeof(*positions
));
160 * set a byte in the positions array to indicate if a field or
161 * column is to be selected; use +1, it's 1-based, not 0-based.
162 * This parser is less restrictive than the Draft 9 POSIX spec.
163 * POSIX doesn't allow lists that aren't in increasing order or
164 * overlapping lists. We also handle "-3-5" although there's no
167 for (; (p
= strtok(list
, ", \t")) != NULL
; list
= NULL
) {
168 setautostart
= start
= stop
= 0;
173 if (isdigit((unsigned char)*p
)) {
174 start
= stop
= strtol(p
, &p
, 10);
175 if (setautostart
&& start
> autostart
)
179 if (isdigit((unsigned char)p
[1]))
180 stop
= strtol(p
+ 1, &p
, 10);
183 if (!autostop
|| autostop
> stop
)
188 errx(1, "[-cf] list: illegal list value");
190 errx(1, "[-cf] list: values may not include zero");
191 if (stop
+ 1 > numpositions
) {
193 newsize
= roundup(stop
+ 1, ALLOC_CHUNK
);
194 positions
= erealloc(positions
, newsize
);
195 (void)memset(positions
+ numpositions
, 0,
196 newsize
- numpositions
);
197 numpositions
= newsize
;
201 for (pos
= positions
+ start
; start
++ <= stop
; pos
++)
205 /* overlapping ranges */
206 if (autostop
&& maxval
> autostop
)
211 (void)memset(positions
+ 1, '1', autostart
);
216 f_cut(FILE *fp
, const char *fname __unused
)
218 int ch
, field
, isdelim
;
224 for (sep
= dchar
, tbuf
= NULL
; (lbuf
= fgetln(fp
, &len
)) != NULL
;) {
226 if (lbuf
[len
- 1] != '\n') {
227 /* no newline at the end of the last line so add one */
228 if ((tbuf
= (char *)malloc(len
+ 1)) == NULL
)
230 (void)memcpy(tbuf
, lbuf
, len
);
234 for (isdelim
= 0, p
= lbuf
;; ++p
) {
236 /* this should work if newline is delimiter */
240 if (!isdelim
&& !sflag
)
241 (void)fwrite(lbuf
, len
, 1, stdout
);
249 for (field
= maxval
, p
= lbuf
; field
; --field
, ++pos
) {
253 while ((ch
= *p
++) != '\n' && ch
!= sep
)
256 while ((ch
= *p
++) != '\n' && ch
!= sep
)
266 for (; (ch
= *p
) != '\n'; ++p
)
269 for (; (ch
= *p
) != '\n'; ++p
);
284 (void)fprintf(stderr
, "Usage:\tcut -b list [-n] [file ...]\n"
285 "\tcut -c list [file1 ...]\n"
286 "\tcut -f list [-d delim] [-s] [file ...]\n");