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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)cut.c 8.3 (Berkeley) 5/4/95
34 * $FreeBSD: src/usr.bin/cut/cut.c,v 1.33 2012/06/11 03:02:40 kevlo Exp $
51 static char dcharmb
[MB_LEN_MAX
+ 1];
58 static size_t autostart
, autostop
, maxval
;
59 static char * positions
;
61 static int b_cut(FILE *, const char *);
62 static int b_n_cut(FILE *, const char *);
63 static int c_cut(FILE *, const char *);
64 static int f_cut(FILE *, const char *);
65 static void get_list(char *);
66 static int is_delim(int);
67 static void needpos(size_t);
68 static void usage(void);
71 main(int argc
, char *argv
[])
74 int (*fcn
)(FILE *, const char *);
78 setlocale (LC_ALL
, "");
81 dchar
= '\t'; /* default delimiter is \t */
82 strcpy(dcharmb
, "\t");
84 while ((ch
= getopt(argc
, argv
, "b:c:d:f:snw")) != -1)
95 n
= mbrtowc(&dchar
, optarg
, MB_LEN_MAX
, NULL
);
96 if (dchar
== '\0' || n
!= strlen(optarg
))
97 errx(1, "bad delimiter");
98 strcpy(dcharmb
, optarg
);
122 if (bflag
|| cflag
|| nflag
|| (wflag
&& dflag
))
124 } else if (!(bflag
|| cflag
) || dflag
|| sflag
|| wflag
)
126 else if (!bflag
&& nflag
)
132 fcn
= MB_CUR_MAX
> 1 ? c_cut
: b_cut
;
134 fcn
= nflag
&& MB_CUR_MAX
> 1 ? b_n_cut
: b_cut
;
138 for (; *argv
; ++argv
) {
139 if (strcmp(*argv
, "-") == 0)
140 rval
|= fcn(stdin
, "stdin");
142 if (!(fp
= fopen(*argv
, "r"))) {
152 rval
= fcn(stdin
, "stdin");
159 size_t setautostart
, start
, stop
;
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 * Numbers and number ranges may be overlapping, repeated, and in
167 * any order. We handle "-3-5" although there's no real reason to.
169 for (; (p
= strsep(&list
, ", \t")) != NULL
;) {
170 setautostart
= start
= stop
= 0;
175 if (isdigit((unsigned char)*p
)) {
176 start
= stop
= strtol(p
, &p
, 10);
177 if (setautostart
&& start
> autostart
)
181 if (isdigit((unsigned char)p
[1]))
182 stop
= strtol(p
+ 1, &p
, 10);
185 if (!autostop
|| autostop
> stop
)
190 errx(1, "[-bcf] list: illegal list value");
192 errx(1, "[-bcf] list: values may not include zero");
197 for (pos
= positions
+ start
; start
++ <= stop
; *pos
++ = 1);
200 /* overlapping ranges */
201 if (autostop
&& maxval
> autostop
) {
208 memset(positions
+ 1, '1', autostart
);
217 /* Grow the positions array to at least the specified size. */
224 if ((positions
= realloc(positions
, npos
)) == NULL
)
226 memset((char *)positions
+ oldnpos
, 0, npos
- oldnpos
);
231 b_cut(FILE *fp
, const char *fname __unused
)
239 for (col
= maxval
; col
; --col
) {
240 if ((ch
= getc(fp
)) == EOF
)
249 while ((ch
= getc(fp
)) != EOF
&& ch
!= '\n')
252 while ((ch
= getc(fp
)) != EOF
&& ch
!= '\n');
260 * Cut based on byte positions, taking care not to split multibyte characters.
261 * Although this function also handles the case where -n is not specified,
262 * b_cut() ought to be much faster.
265 b_n_cut(FILE *fp
, const char *fname
)
267 size_t col
, i
, lbuflen
;
269 int canwrite
, clen
, warned
;
272 memset(&mbs
, 0, sizeof(mbs
));
274 while ((lbuf
= fgetln(fp
, &lbuflen
)) != NULL
) {
275 for (col
= 0; lbuflen
> 0; col
+= clen
) {
276 if ((clen
= mbrlen(lbuf
, lbuflen
, &mbs
)) < 0) {
281 memset(&mbs
, 0, sizeof(mbs
));
284 if (clen
== 0 || *lbuf
== '\n')
286 if (col
< maxval
&& !positions
[1 + col
]) {
288 * Print the character if (1) after an initial
289 * segment of un-selected bytes, the rest of
290 * it is selected, and (2) the last byte is
294 while (i
< col
+ clen
&& i
< maxval
&&
297 canwrite
= i
< col
+ clen
;
298 for (; i
< col
+ clen
&& i
< maxval
; i
++)
299 canwrite
&= positions
[1 + i
];
301 fwrite(lbuf
, 1, clen
, stdout
);
304 * Print the character if all of it has
308 for (i
= col
; i
< col
+ clen
; i
++)
309 if ((i
>= maxval
&& !autostop
) ||
310 (i
< maxval
&& !positions
[1 + i
])) {
315 fwrite(lbuf
, 1, clen
, stdout
);
327 c_cut(FILE *fp
, const char *fname
)
336 for (col
= maxval
; col
; --col
) {
337 if ((ch
= getwc(fp
)) == WEOF
)
346 while ((ch
= getwc(fp
)) != WEOF
&& ch
!= '\n')
349 while ((ch
= getwc(fp
)) != WEOF
&& ch
!= '\n');
351 (void)putwchar('\n');
365 if (ch
== ' ' || ch
== '\t')
375 f_cut(FILE *fp
, const char *fname
)
378 int field
, i
, isdelim
;
382 size_t clen
, lbuflen
, reallen
;
385 while ((lbuf
= fgetln(fp
, &lbuflen
)) != NULL
) {
387 /* Assert EOL has a newline. */
388 if (*(lbuf
+ lbuflen
- 1) != '\n') {
389 /* Can't have > 1 line with no trailing newline. */
390 mlbuf
= malloc(lbuflen
+ 1);
393 memcpy(mlbuf
, lbuf
, lbuflen
);
394 *(mlbuf
+ lbuflen
) = '\n';
399 for (isdelim
= 0, p
= lbuf
;; p
+= clen
) {
400 clen
= mbrtowc(&ch
, p
, lbuf
+ reallen
- p
, NULL
);
401 if (clen
== (size_t)-1 || clen
== (size_t)-2) {
402 warnc(EILSEQ
, "%s", fname
);
408 /* this should work if newline is delimiter */
412 if (!isdelim
&& !sflag
)
413 (void)fwrite(lbuf
, lbuflen
, 1, stdout
);
421 for (field
= maxval
, p
= lbuf
; field
; --field
, ++pos
) {
422 if (*pos
&& output
++)
423 for (i
= 0; dcharmb
[i
] != '\0'; i
++)
426 clen
= mbrtowc(&ch
, p
, lbuf
+ reallen
- p
,
428 if (clen
== (size_t)-1 || clen
== (size_t)-2) {
429 warnc(EILSEQ
, "%s", fname
);
436 if (ch
== '\n' || is_delim(ch
)) {
437 /* compress whitespace */
438 if (wflag
&& ch
!= '\n')
444 for (i
= 0; i
< (int)clen
; i
++)
445 putchar(p
[i
- clen
]);
453 for (i
= 0; dcharmb
[i
] != '\0'; i
++)
455 for (; (ch
= *p
) != '\n'; ++p
)
458 for (; (ch
= *p
) != '\n'; ++p
);
469 (void)fprintf(stderr
, "%s\n%s\n%s\n",
470 "usage: cut -b list [-n] [file ...]",
471 " cut -c list [file ...]",
472 " cut -f list [-s] [-w | -d delim] [file ...]");