Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / bin / cat / cat.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kevin Fall.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
37  * @(#)cat.c    8.2 (Berkeley) 4/27/95
38  * $FreeBSD: src/bin/cat/cat.c,v 1.14.2.8 2002/06/29 05:09:26 tjr Exp $
39  * $DragonFly: src/bin/cat/cat.c,v 1.2 2003/06/17 04:22:49 dillon Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #ifndef NO_UDOM_SUPPORT
45 #include <sys/socket.h>
46 #include <sys/un.h>
47 #include <errno.h>
48 #endif
49
50 #include <ctype.h>
51 #include <err.h>
52 #include <fcntl.h>
53 #include <locale.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <stddef.h>
59
60 int bflag, eflag, nflag, sflag, tflag, vflag;
61 int rval;
62 const char *filename;
63
64 int main __P((int argc, char *argv[]));
65
66 static void scanfiles __P((char **argv, int cooked));
67 static void cook_cat __P((FILE *));
68 static void raw_cat __P((int));
69
70 #ifndef NO_UDOM_SUPPORT
71 static int udom_open __P((const char *path, int flags));
72 #endif
73
74 int
75 main(argc, argv)
76         int argc;
77         char *argv[];
78 {
79         int ch;
80
81         setlocale(LC_CTYPE, "");
82
83         while ((ch = getopt(argc, argv, "benstuv")) != -1)
84                 switch (ch) {
85                 case 'b':
86                         bflag = nflag = 1;      /* -b implies -n */
87                         break;
88                 case 'e':
89                         eflag = vflag = 1;      /* -e implies -v */
90                         break;
91                 case 'n':
92                         nflag = 1;
93                         break;
94                 case 's':
95                         sflag = 1;
96                         break;
97                 case 't':
98                         tflag = vflag = 1;      /* -t implies -v */
99                         break;
100                 case 'u':
101                         setbuf(stdout, NULL);
102                         break;
103                 case 'v':
104                         vflag = 1;
105                         break;
106                 default:
107                         fprintf(stderr,
108                             "usage: cat [-benstuv] [-] [file ...]\n");
109                         exit(1);
110                 }
111         argv += optind;
112
113         if (bflag || eflag || nflag || sflag || tflag || vflag)
114                 scanfiles(argv, 1);
115         else
116                 scanfiles(argv, 0);
117         if (fclose(stdout))
118                 err(1, "stdout");
119         exit(rval);
120 }
121
122 void
123 scanfiles(argv, cooked)
124     char **argv;
125     int cooked;
126 {
127         int i = 0;
128         char *path;
129         FILE *fp;
130
131         while ((path = argv[i]) != NULL || i == 0) {
132                 int fd;
133
134                 if (path == NULL || strcmp(path, "-") == 0) {
135                         filename = "stdin";
136                         fd = STDIN_FILENO;
137                 } else {
138                         filename = path;
139                         fd = open(path, O_RDONLY);
140 #ifndef NO_UDOM_SUPPORT
141                         if (fd < 0 && errno == EOPNOTSUPP)
142                                 fd = udom_open(path, O_RDONLY);
143 #endif
144                 }
145                 if (fd < 0) {
146                         warn("%s", path);
147                         rval = 1;
148                 } else if (cooked) {
149                         if (fd == STDIN_FILENO)
150                                 cook_cat(stdin);
151                         else {
152                                 fp = fdopen(fd, "r");
153                                 cook_cat(fp);
154                                 fclose(fp);
155                         }
156                 } else {
157                         raw_cat(fd);
158                         if (fd != STDIN_FILENO)
159                                 close(fd);
160                 }
161                 if (path == NULL)
162                         break;
163                 ++i;
164         }
165 }
166
167 static void
168 cook_cat(fp)
169         register FILE *fp;
170 {
171         register int ch, gobble, line, prev;
172
173         /* Reset EOF condition on stdin. */
174         if (fp == stdin && feof(stdin))
175                 clearerr(stdin);
176
177         line = gobble = 0;
178         for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
179                 if (prev == '\n') {
180                         if (ch == '\n') {
181                                 if (sflag) {
182                                         if (!gobble && putchar(ch) == EOF)
183                                                 break;
184                                         gobble = 1;
185                                         continue;
186                                 }
187                                 if (nflag && !bflag) {
188                                         fprintf(stdout, "%6d\t", ++line);
189                                         if (ferror(stdout))
190                                                 break;
191                                 }
192                         } else if (nflag) {
193                                 fprintf(stdout, "%6d\t", ++line);
194                                 if (ferror(stdout))
195                                         break;
196                         }
197                 }
198                 gobble = 0;
199                 if (ch == '\n') {
200                         if (eflag)
201                                 if (putchar('$') == EOF)
202                                         break;
203                 } else if (ch == '\t') {
204                         if (tflag) {
205                                 if (putchar('^') == EOF || putchar('I') == EOF)
206                                         break;
207                                 continue;
208                         }
209                 } else if (vflag) {
210                         if (!isascii(ch) && !isprint(ch)) {
211                                 if (putchar('M') == EOF || putchar('-') == EOF)
212                                         break;
213                                 ch = toascii(ch);
214                         }
215                         if (iscntrl(ch)) {
216                                 if (putchar('^') == EOF ||
217                                     putchar(ch == '\177' ? '?' :
218                                     ch | 0100) == EOF)
219                                         break;
220                                 continue;
221                         }
222                 }
223                 if (putchar(ch) == EOF)
224                         break;
225         }
226         if (ferror(fp)) {
227                 warn("%s", filename);
228                 rval = 1;
229                 clearerr(fp);
230         }
231         if (ferror(stdout))
232                 err(1, "stdout");
233 }
234
235 static void
236 raw_cat(rfd)
237         register int rfd;
238 {
239         register int off, wfd;
240         ssize_t nr, nw;
241         static size_t bsize;
242         static char *buf = NULL;
243         struct stat sbuf;
244
245         wfd = fileno(stdout);
246         if (buf == NULL) {
247                 if (fstat(wfd, &sbuf))
248                         err(1, "%s", filename);
249                 bsize = MAX(sbuf.st_blksize, 1024);
250                 if ((buf = malloc(bsize)) == NULL)
251                         err(1, "buffer");
252         }
253         while ((nr = read(rfd, buf, bsize)) > 0)
254                 for (off = 0; nr; nr -= nw, off += nw)
255                         if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
256                                 err(1, "stdout");
257         if (nr < 0) {
258                 warn("%s", filename);
259                 rval = 1;
260         }
261 }
262
263 #ifndef NO_UDOM_SUPPORT
264
265 static int
266 udom_open(path, flags)
267     const char *path;
268     int flags;
269 {
270         struct sockaddr_un sou;
271         int fd;
272         unsigned int len;
273
274         bzero(&sou, sizeof(sou));
275
276         /*
277          * Construct the unix domain socket address and attempt to connect
278          */
279         fd = socket(AF_UNIX, SOCK_STREAM, 0);
280         if (fd >= 0) {
281                 sou.sun_family = AF_UNIX;
282                 snprintf(sou.sun_path, sizeof(sou.sun_path), "%s", path);
283                 len = strlen(sou.sun_path);
284                 len = offsetof(struct sockaddr_un, sun_path[len+1]);
285
286                 if (connect(fd, (void *)&sou, len) < 0) {
287                         close(fd);
288                         fd = -1;
289                 }
290         }
291
292         /*
293          * handle the open flags by shutting down appropriate directions
294          */
295         if (fd >= 0) {
296                 switch(flags & O_ACCMODE) {
297                 case O_RDONLY:
298                         if (shutdown(fd, SHUT_WR) == -1)
299                                 perror("cat");
300                         break;
301                 case O_WRONLY:
302                         if (shutdown(fd, SHUT_RD) == -1)
303                                 perror("cat");
304                         break;
305                 default:
306                         break;
307                 }
308         }
309         return(fd);
310 }
311
312 #endif