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
38 static const char copyright
[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid
[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
60 char dcharmb
[MB_LEN_MAX
+ 1];
66 size_t autostart
, autostop
, maxval
;
69 int b_cut(FILE *, const char *);
70 int b_n_cut(FILE *, const char *);
71 int c_cut(FILE *, const char *);
72 int f_cut(FILE *, const char *);
73 void get_list(char *);
75 static void usage(void);
78 main(int argc
, char *argv
[])
81 int (*fcn
)(FILE *, const char *);
85 setlocale(LC_ALL
, "");
88 dchar
= '\t'; /* default delimiter is \t */
89 strcpy(dcharmb
, "\t");
91 while ((ch
= getopt(argc
, argv
, "b:c:d:f:sn")) != -1)
102 n
= mbrtowc(&dchar
, optarg
, MB_LEN_MAX
, NULL
);
103 if (dchar
== '\0' || n
!= strlen(optarg
))
104 errx(1, "bad delimiter");
105 strcpy(dcharmb
, optarg
);
126 if (bflag
|| cflag
|| nflag
)
128 } else if (!(bflag
|| cflag
) || dflag
|| sflag
)
130 else if (!bflag
&& nflag
)
136 fcn
= MB_CUR_MAX
> 1 ? c_cut
: b_cut
;
138 fcn
= nflag
&& MB_CUR_MAX
> 1 ? b_n_cut
: b_cut
;
142 for (; *argv
; ++argv
) {
143 if (strcmp(*argv
, "-") == 0)
144 rval
|= fcn(stdin
, "stdin");
146 if (!(fp
= fopen(*argv
, "r"))) {
156 rval
= fcn(stdin
, "stdin");
163 size_t setautostart
, start
, stop
;
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 * Numbers and number ranges may be overlapping, repeated, and in
171 * any order. We handle "-3-5" although there's no real reason too.
173 for (; (p
= strsep(&list
, ", \t")) != NULL
;) {
174 setautostart
= start
= stop
= 0;
179 if (isdigit((unsigned char)*p
)) {
180 start
= stop
= strtol(p
, &p
, 10);
181 if (setautostart
&& start
> autostart
)
185 if (isdigit((unsigned char)p
[1]))
186 stop
= strtol(p
+ 1, &p
, 10);
189 if (!autostop
|| autostop
> stop
)
194 errx(1, "[-cf] list: illegal list value");
196 errx(1, "[-cf] list: values may not include zero");
201 for (pos
= positions
+ start
; start
++ <= stop
; *pos
++ = 1);
204 /* overlapping ranges */
205 if (autostop
&& maxval
> autostop
) {
212 memset(positions
+ 1, '1', autostart
);
221 /* Grow the positions array to at least the specified size. */
228 if ((positions
= realloc(positions
, npos
)) == NULL
)
230 memset((char *)positions
+ oldnpos
, 0, npos
- oldnpos
);
235 b_cut(FILE *fp
, const char *fname __unused
)
243 for (col
= maxval
; col
; --col
) {
244 if ((ch
= getc(fp
)) == EOF
)
253 while ((ch
= getc(fp
)) != EOF
&& ch
!= '\n')
256 while ((ch
= getc(fp
)) != EOF
&& ch
!= '\n');
264 * Cut based on byte positions, taking care not to split multibyte characters.
265 * Although this function also handles the case where -n is not specified,
266 * b_cut() ought to be much faster.
269 b_n_cut(FILE *fp
, const char *fname
)
271 size_t col
, i
, lbuflen
;
273 int canwrite
, clen
, warned
;
276 memset(&mbs
, 0, sizeof(mbs
));
278 while ((lbuf
= fgetln(fp
, &lbuflen
)) != NULL
) {
279 for (col
= 0; lbuflen
> 0; col
+= clen
) {
280 if ((clen
= mbrlen(lbuf
, lbuflen
, &mbs
)) < 0) {
285 memset(&mbs
, 0, sizeof(mbs
));
288 if (clen
== 0 || *lbuf
== '\n')
290 if (col
< maxval
&& !positions
[1 + col
]) {
292 * Print the character if (1) after an initial
293 * segment of un-selected bytes, the rest of
294 * it is selected, and (2) the last byte is
298 while (i
< col
+ clen
&& i
< maxval
&&
301 canwrite
= i
< col
+ clen
;
302 for (; i
< col
+ clen
&& i
< maxval
; i
++)
303 canwrite
&= positions
[1 + i
];
305 fwrite(lbuf
, 1, clen
, stdout
);
308 * Print the character if all of it has
312 for (i
= col
; i
< col
+ clen
; i
++)
313 if ((i
>= maxval
&& !autostop
) ||
314 (i
< maxval
&& !positions
[1 + i
])) {
319 fwrite(lbuf
, 1, clen
, stdout
);
331 c_cut(FILE *fp
, const char *fname
)
340 for (col
= maxval
; col
; --col
) {
341 if ((ch
= getwc(fp
)) == WEOF
)
350 while ((ch
= getwc(fp
)) != WEOF
&& ch
!= '\n')
353 while ((ch
= getwc(fp
)) != WEOF
&& ch
!= '\n');
355 (void)putwchar('\n');
366 f_cut(FILE *fp
, const char *fname
)
369 int field
, i
, isdelim
;
374 size_t clen
, lbuflen
, reallen
;
377 for (sep
= dchar
; (lbuf
= fgetln(fp
, &lbuflen
)) != NULL
;) {
379 /* Assert EOL has a newline. */
380 if (*(lbuf
+ lbuflen
- 1) != '\n') {
381 /* Can't have > 1 line with no trailing newline. */
382 mlbuf
= malloc(lbuflen
+ 1);
385 memcpy(mlbuf
, lbuf
, lbuflen
);
386 *(mlbuf
+ lbuflen
) = '\n';
391 for (isdelim
= 0, p
= lbuf
;; p
+= clen
) {
392 clen
= mbrtowc(&ch
, p
, lbuf
+ reallen
- p
, NULL
);
393 if (clen
== (size_t)-1 || clen
== (size_t)-2) {
394 warnc(EILSEQ
, "%s", fname
);
400 /* this should work if newline is delimiter */
404 if (!isdelim
&& !sflag
)
405 (void)fwrite(lbuf
, lbuflen
, 1, stdout
);
413 for (field
= maxval
, p
= lbuf
; field
; --field
, ++pos
) {
414 if (*pos
&& output
++)
415 for (i
= 0; dcharmb
[i
] != '\0'; i
++)
418 clen
= mbrtowc(&ch
, p
, lbuf
+ reallen
- p
,
420 if (clen
== (size_t)-1 || clen
== (size_t)-2) {
421 warnc(EILSEQ
, "%s", fname
);
428 if (ch
== '\n' || ch
== sep
)
431 for (i
= 0; i
< (int)clen
; i
++)
432 putchar(p
[i
- clen
]);
440 for (i
= 0; dcharmb
[i
] != '\0'; i
++)
442 for (; (ch
= *p
) != '\n'; ++p
)
445 for (; (ch
= *p
) != '\n'; ++p
);
456 (void)fprintf(stderr
, "%s\n%s\n%s\n",
457 "usage: cut -b list [-n] [file ...]",
458 " cut -c list [file ...]",
459 " cut -f list [-s] [-d delim] [file ...]");