* Copyright (c) 1989 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* 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
[] = "@(#)cat.c 5.15 (Berkeley) 5/23/91";
int bflag
, eflag
, nflag
, sflag
, tflag
, vflag
;
void cook_args(), cook_buf(), raw_args(), raw_cat();
void err
__P((int, const char *, ...));
while ((ch
= getopt(argc
, argv
, "benstuv")) != EOF
)
bflag
= nflag
= 1; /* -b implies -n */
eflag
= vflag
= 1; /* -e implies -v */
tflag
= vflag
= 1; /* -t implies -v */
setbuf(stdout
, (char *)NULL
);
"usage: cat [-benstuv] [-] [file ...]\n");
if (bflag
|| eflag
|| nflag
|| sflag
|| tflag
|| vflag
)
err(1, "stdout: %s", strerror(errno
));
else if (!(fp
= fopen(*argv
, "r"))) {
err(0, "%s: %s", *argv
, strerror(errno
));
register int ch
, gobble
, line
, prev
;
for (prev
= '\n'; (ch
= getc(fp
)) != EOF
; prev
= ch
) {
if (!gobble
&& putchar(ch
) == EOF
)
(void)fprintf(stdout
, "%6d\t", ++line
);
(void)fprintf(stdout
, "%6d\t", ++line
);
if (putchar('^') == EOF
|| putchar('I') == EOF
)
if (putchar('M') == EOF
|| putchar('-') == EOF
)
if (putchar('^') == EOF
||
putchar(ch
== '\177' ? '?' :
err(0, "%s: %s", strerror(errno
));
err(1, "stdout: %s", strerror(errno
));
else if ((fd
= open(*argv
, O_RDONLY
, 0)) < 0) {
err(0, "%s: %s", *argv
, strerror(errno
));
register int nr
, nw
, off
, wfd
;
err(1, "%s: %s", filename
, strerror(errno
));
bsize
= MAX(sbuf
.st_blksize
, 1024);
if (!(buf
= malloc((u_int
)bsize
)))
err(1, "%s", strerror(errno
));
while ((nr
= read(rfd
, buf
, bsize
)) > 0)
for (off
= 0; off
< nr
; nr
-= nw
, off
+= nw
)
if ((nw
= write(wfd
, buf
+ off
, nr
)) < 0)
err(0, "%s: %s", filename
, strerror(errno
));
err(int ex
, const char *fmt
, ...)
(void)fprintf(stderr
, "cat: ");
(void)vfprintf(stderr
, fmt
, ap
);
(void)fprintf(stderr
, "\n");