1 /* $NetBSD: cat.c,v 1.55 2015/07/25 16:17:01 sevan Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
39 #include <sys/cdefs.h>
42 "@(#) Copyright (c) 1989, 1993\
43 The Regents of the University of California. All rights reserved.");
45 static char sccsid
[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95";
47 __RCSID("$NetBSD: cat.c,v 1.55 2015/07/25 16:17:01 sevan Exp $");
51 #include <sys/param.h>
64 static int bflag
, eflag
, fflag
, lflag
, nflag
, sflag
, tflag
, vflag
;
67 static const char *filename
;
69 void cook_args(char *argv
[]);
70 void cook_buf(FILE *);
71 void raw_args(char *argv
[]);
75 main(int argc
, char *argv
[])
78 struct flock stdout_lock
;
81 (void)setlocale(LC_ALL
, "");
83 while ((ch
= getopt(argc
, argv
, "B:beflnstuv")) != -1)
86 bsize
= (size_t)strtol(optarg
, NULL
, 0);
89 bflag
= nflag
= 1; /* -b implies -n */
92 eflag
= vflag
= 1; /* -e implies -v */
107 tflag
= vflag
= 1; /* -t implies -v */
110 setbuf(stdout
, NULL
);
116 (void)fprintf(stderr
,
117 "Usage: %s [-beflnstuv] [-B bsize] [-] "
118 "[file ...]\n", getprogname());
124 stdout_lock
.l_len
= 0;
125 stdout_lock
.l_start
= 0;
126 stdout_lock
.l_type
= F_WRLCK
;
127 stdout_lock
.l_whence
= SEEK_SET
;
128 if (fcntl(STDOUT_FILENO
, F_SETLKW
, &stdout_lock
) == -1)
129 err(EXIT_FAILURE
, "stdout");
132 if (bflag
|| eflag
|| nflag
|| sflag
|| tflag
|| vflag
)
137 err(EXIT_FAILURE
, "stdout");
142 cook_args(char **argv
)
150 if (!strcmp(*argv
, "-"))
152 else if ((fp
= fopen(*argv
,
153 fflag
? "rf" : "r")) == NULL
) {
172 int ch
, gobble
, line
, prev
;
175 for (prev
= '\n'; (ch
= getc(fp
)) != EOF
; prev
= ch
) {
179 if (!gobble
&& nflag
&& !bflag
)
180 (void)fprintf(stdout
,
182 else if (!gobble
&& putchar(ch
) == EOF
)
189 (void)fprintf(stdout
,
194 (void)fprintf(stdout
,
201 (void)fprintf(stdout
, "%6d\t", ++line
);
209 if (putchar('$') == EOF
)
211 } else if (ch
== '\t') {
213 if (putchar('^') == EOF
|| putchar('I') == EOF
)
219 if (putchar('M') == EOF
|| putchar('-') == EOF
)
224 if (putchar('^') == EOF
||
225 putchar(ch
== '\177' ? '?' :
231 if (putchar(ch
) == EOF
)
235 warn("%s", filename
);
240 err(EXIT_FAILURE
, "stdout");
244 raw_args(char **argv
)
252 if (!strcmp(*argv
, "-")) {
258 fd
= open(*argv
, O_RDONLY
|O_NONBLOCK
, 0);
262 if (fstat(fd
, &st
) == -1) {
266 if (!S_ISREG(st
.st_mode
)) {
268 warnx("%s: not a regular file", *argv
);
272 else if ((fd
= open(*argv
, O_RDONLY
, 0)) < 0) {
282 err(EXIT_FAILURE
, "stdin");
285 if (fd
!= fileno(stdin
))
294 static char fb_buf
[BUFSIZ
];
299 wfd
= fileno(stdout
);
301 err(EXIT_FAILURE
, "stdout");
306 if (fstat(wfd
, &sbuf
) == 0 && sbuf
.st_blksize
> 0 &&
307 (size_t)sbuf
.st_blksize
> sizeof(fb_buf
))
308 bsize
= sbuf
.st_blksize
;
310 if (bsize
> sizeof(fb_buf
)) {
313 warnx("malloc, using %zu buffer", bsize
);
316 bsize
= sizeof(fb_buf
);
320 while ((nr
= read(rfd
, buf
, bsize
)) > 0)
321 for (off
= 0; nr
; nr
-= nw
, off
+= nw
)
322 if ((nw
= write(wfd
, buf
+ off
, (size_t)nr
)) < 0)
323 err(EXIT_FAILURE
, "stdout");
325 warn("%s", filename
);