* Copyright (c) 1989 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
* %sccs.include.redist.c%
"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)cut.c 5.4 (Berkeley) %G%";
extern int errno
, optind
;
int ch
, (*fcn
)(), c_cut(), f_cut();
dchar
= '\t'; /* default delimiter is \t */
while ((ch
= getopt(argc
, argv
, "c:d:f:s")) != EOF
)
} else if (!cflag
|| dflag
|| sflag
)
if (!(fp
= fopen(*argv
, "r"))) {
"cut: %s: %s\n", *argv
, strerror(errno
));
int autostart
, autostop
, maxval
;
char positions
[_POSIX2_LINE_MAX
+ 1];
register int setautostart
, start
, stop
;
* set a byte in the positions array to indicate if a field or
* column is to be selected; use +1, it's 1-based, not 0-based.
* This parser is less restrictive than the Draft 9 POSIX spec.
* POSIX doesn't allow lists that aren't in increasing order or
* overlapping lists. We also handle "-3-5" although there's no
for (; p
= strtok(list
, ", \t"); list
= NULL
) {
setautostart
= start
= stop
= 0;
start
= stop
= strtol(p
, &p
, 10);
if (setautostart
&& start
> autostart
)
stop
= strtol(p
+ 1, &p
, 10);
if (!autostop
|| autostop
> stop
)
badlist("illegal list value");
badlist("values may not include zero");
if (stop
> _POSIX2_LINE_MAX
) {
/* positions used rather than allocate a new buffer */
(void)sprintf(positions
, "%d too large (max %d)",
for (pos
= positions
+ start
; start
++ <= stop
; *pos
++ = 1);
if (autostop
&& maxval
> autostop
)
memset(positions
+ 1, '1', autostart
);
for (col
= maxval
; col
; --col
) {
if ((ch
= getc(fp
)) == EOF
)
while ((ch
= getc(fp
)) != EOF
&& ch
!= '\n')
while ((ch
= getc(fp
)) != EOF
&& ch
!= '\n');
register int ch
, field
, isdelim
;
register char *pos
, *p
, sep
;
char lbuf
[_POSIX2_LINE_MAX
+ 1];
for (sep
= dchar
, output
= 0; fgets(lbuf
, sizeof(lbuf
), fp
);) {
for (isdelim
= 0, p
= lbuf
;; ++p
) {
"cut: %s: line too long.\n", fname
);
/* this should work if newline is delimiter */
(void)printf("%s", lbuf
);
for (field
= maxval
, p
= lbuf
; field
; --field
, ++pos
) {
while ((ch
= *p
++) != '\n' && ch
!= sep
)
while ((ch
= *p
++) != '\n' && ch
!= sep
);
for (; (ch
= *p
) != '\n'; ++p
)
for (; (ch
= *p
) != '\n'; ++p
);
(void)fprintf(stderr
, "cut: [-cf] list: %s.\n", msg
);
"usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n");