* de- __P()
[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.4 2003/09/21 04:13:50 drhodus 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 (int argc, char *argv[]);
65
66 static void scanfiles (char **argv, int cooked);
67 static void cook_cat (FILE *);
68 static void raw_cat (int);
69
70 #ifndef NO_UDOM_SUPPORT
71 static int udom_open (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                         /* NOTREACHED */
111                 }
112         argv += optind;
113
114         if (bflag || eflag || nflag || sflag || tflag || vflag)
115                 scanfiles(argv, 1);
116         else
117                 scanfiles(argv, 0);
118         if (fclose(stdout))
119                 err(1, "stdout");
120         exit(rval);
121         /* NOTREACHED */
122 }
123
124 void
125 scanfiles(argv, cooked)
126     char **argv;
127     int cooked;
128 {
129         int i = 0;
130         char *path;
131         FILE *fp;
132
133         while ((path = argv[i]) != NULL || i == 0) {
134                 int fd;
135
136                 if (path == NULL || strcmp(path, "-") == 0) {
137                         filename = "stdin";
138                         fd = STDIN_FILENO;
139                 } else {
140                         filename = path;
141                         fd = open(path, O_RDONLY);
142 #ifndef NO_UDOM_SUPPORT
143                         if (fd < 0 && errno == EOPNOTSUPP)
144                                 fd = udom_open(path, O_RDONLY);
145 #endif
146                 }
147                 if (fd < 0) {
148                         warn("%s", path);
149                         rval = 1;
150                 } else if (cooked) {
151                         if (fd == STDIN_FILENO)
152                                 cook_cat(stdin);
153                         else {
154                                 fp = fdopen(fd, "r");
155                                 cook_cat(fp);
156                                 fclose(fp);
157                         }
158                 } else {
159                         raw_cat(fd);
160                         if (fd != STDIN_FILENO)
161                                 close(fd);
162                 }
163                 if (path == NULL)
164                         break;
165                 ++i;
166         }
167 }
168
169 static void
170 cook_cat(fp)
171         register FILE *fp;
172 {
173         register int ch, gobble, line, prev;
174
175         /* Reset EOF condition on stdin. */
176         if (fp == stdin && feof(stdin))
177                 clearerr(stdin);
178
179         line = gobble = 0;
180         for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
181                 if (prev == '\n') {
182                         if (ch == '\n') {
183                                 if (sflag) {
184                                         if (!gobble && putchar(ch) == EOF)
185                                                 break;
186                                         gobble = 1;
187                                         continue;
188                                 }
189                                 if (nflag && !bflag) {
190                                         fprintf(stdout, "%6d\t", ++line);
191                                         if (ferror(stdout))
192                                                 break;
193                                 }
194                         } else if (nflag) {
195                                 fprintf(stdout, "%6d\t", ++line);
196                                 if (ferror(stdout))
197                                         break;
198                         }
199                 }
200                 gobble = 0;
201                 if (ch == '\n') {
202                         if (eflag)
203                                 if (putchar('$') == EOF)
204                                         break;
205                 } else if (ch == '\t') {
206                         if (tflag) {
207                                 if (putchar('^') == EOF || putchar('I') == EOF)
208                                         break;
209                                 continue;
210                         }
211                 } else if (vflag) {
212                         if (!isascii(ch) && !isprint(ch)) {
213                                 if (putchar('M') == EOF || putchar('-') == EOF)
214                                         break;
215                                 ch = toascii(ch);
216                         }
217                         if (iscntrl(ch)) {
218                                 if (putchar('^') == EOF ||
219                                     putchar(ch == '\177' ? '?' :
220                                     ch | 0100) == EOF)
221                                         break;
222                                 continue;
223                         }
224                 }
225                 if (putchar(ch) == EOF)
226                         break;
227         }
228         if (ferror(fp)) {
229                 warn("%s", filename);
230                 rval = 1;
231                 clearerr(fp);
232         }
233         if (ferror(stdout))
234                 err(1, "stdout");
235 }
236
237 static void
238 raw_cat(rfd)
239         register int rfd;
240 {
241         register int off, wfd;
242         ssize_t nr, nw;
243         static size_t bsize;
244         static char *buf = NULL;
245         struct stat sbuf;
246
247         wfd = fileno(stdout);
248         if (buf == NULL) {
249                 if (fstat(wfd, &sbuf))
250                         err(1, "%s", filename);
251                 bsize = MAX(sbuf.st_blksize, 1024);
252                 if ((buf = malloc(bsize)) == NULL)
253                         err(1, "buffer");
254         }
255         while ((nr = read(rfd, buf, bsize)) > 0)
256                 for (off = 0; nr; nr -= nw, off += nw)
257                         if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
258                                 err(1, "stdout");
259         if (nr < 0) {
260                 warn("%s", filename);
261                 rval = 1;
262         }
263 }
264
265 #ifndef NO_UDOM_SUPPORT
266
267 static int
268 udom_open(path, flags)
269     const char *path;
270     int flags;
271 {
272         struct sockaddr_un sou;
273         int fd;
274         unsigned int len;
275
276         bzero(&sou, sizeof(sou));
277
278         /*
279          * Construct the unix domain socket address and attempt to connect
280          */
281         fd = socket(AF_UNIX, SOCK_STREAM, 0);
282         if (fd >= 0) {
283                 sou.sun_family = AF_UNIX;
284                 snprintf(sou.sun_path, sizeof(sou.sun_path), "%s", path);
285                 len = strlen(sou.sun_path);
286                 len = offsetof(struct sockaddr_un, sun_path[len+1]);
287
288                 if (connect(fd, (void *)&sou, len) < 0) {
289                         close(fd);
290                         fd = -1;
291                 }
292         }
293
294         /*
295          * handle the open flags by shutting down appropriate directions
296          */
297         if (fd >= 0) {
298                 switch(flags & O_ACCMODE) {
299                 case O_RDONLY:
300                         if (shutdown(fd, SHUT_WR) == -1)
301                                 perror("cat");
302                         break;
303                 case O_WRONLY:
304                         if (shutdown(fd, SHUT_RD) == -1)
305                                 perror("cat");
306                         break;
307                 default:
308                         break;
309                 }
310         }
311         return(fd);
312 }
313
314 #endif