* 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.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)cut.c 5.4 (Berkeley) 10/30/90";
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");