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