* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
* %sccs.include.redist.c%
static char copyright
[] =
"@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)cut.c 8.2 (Berkeley) %G%";
void c_cut
__P((FILE *, char *));
void err
__P((const char *, ...));
void f_cut
__P((FILE *, char *));
void get_list
__P((char *));
void (*fcn
) __P((FILE *, char *));
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")))
err("%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
)
err("[-cf] list: illegal list value\n");
err("[-cf] list: values may not include zero\n");
if (stop
> _POSIX2_LINE_MAX
)
err("[-cf] list: %d too large (max %d)\n",
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
; fgets(lbuf
, sizeof(lbuf
), fp
);) {
for (isdelim
= 0, p
= lbuf
;; ++p
) {
err("%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
);
"usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n");
err(const char *fmt
, ...)
(void)fprintf(stderr
, "cut: ");
(void)vfprintf(stderr
, fmt
, ap
);
(void)fprintf(stderr
, "\n");