]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/cat/cat.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ trunk r321545,
[FreeBSD/FreeBSD.git] / bin / cat / cat.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kevin Fall.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34
35 #if 0
36 #ifndef lint
37 static char const copyright[] =
38 "@(#) Copyright (c) 1989, 1993\n\
39         The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 #endif
42
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)cat.c       8.2 (Berkeley) 4/27/95";
46 #endif
47 #endif /* not lint */
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50
51 #include <sys/param.h>
52 #include <sys/stat.h>
53 #ifndef NO_UDOM_SUPPORT
54 #include <sys/socket.h>
55 #include <sys/un.h>
56 #include <netdb.h>
57 #endif
58
59 #include <ctype.h>
60 #include <err.h>
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <locale.h>
64 #include <stddef.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 #include <wchar.h>
70 #include <wctype.h>
71
72 static int bflag, eflag, lflag, nflag, sflag, tflag, vflag;
73 static int rval;
74 static const char *filename;
75
76 static void usage(void) __dead2;
77 static void scanfiles(char *argv[], int cooked);
78 static void cook_cat(FILE *);
79 static void raw_cat(int);
80
81 #ifndef NO_UDOM_SUPPORT
82 static int udom_open(const char *path, int flags);
83 #endif
84
85 /*
86  * Memory strategy threshold, in pages: if physmem is larger than this,
87  * use a large buffer.
88  */
89 #define PHYSPAGES_THRESHOLD (32 * 1024)
90
91 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
92 #define BUFSIZE_MAX (2 * 1024 * 1024)
93
94 /*
95  * Small (default) buffer size in bytes. It's inefficient for this to be
96  * smaller than MAXPHYS.
97  */
98 #define BUFSIZE_SMALL (MAXPHYS)
99
100 int
101 main(int argc, char *argv[])
102 {
103         int ch;
104         struct flock stdout_lock;
105
106         setlocale(LC_CTYPE, "");
107
108         while ((ch = getopt(argc, argv, "belnstuv")) != -1)
109                 switch (ch) {
110                 case 'b':
111                         bflag = nflag = 1;      /* -b implies -n */
112                         break;
113                 case 'e':
114                         eflag = vflag = 1;      /* -e implies -v */
115                         break;
116                 case 'l':
117                         lflag = 1;
118                         break;
119                 case 'n':
120                         nflag = 1;
121                         break;
122                 case 's':
123                         sflag = 1;
124                         break;
125                 case 't':
126                         tflag = vflag = 1;      /* -t implies -v */
127                         break;
128                 case 'u':
129                         setbuf(stdout, NULL);
130                         break;
131                 case 'v':
132                         vflag = 1;
133                         break;
134                 default:
135                         usage();
136                 }
137         argv += optind;
138
139         if (lflag) {
140                 stdout_lock.l_len = 0;
141                 stdout_lock.l_start = 0;
142                 stdout_lock.l_type = F_WRLCK;
143                 stdout_lock.l_whence = SEEK_SET;
144                 if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
145                         err(EXIT_FAILURE, "stdout");
146         }
147
148         if (bflag || eflag || nflag || sflag || tflag || vflag)
149                 scanfiles(argv, 1);
150         else
151                 scanfiles(argv, 0);
152         if (fclose(stdout))
153                 err(1, "stdout");
154         exit(rval);
155         /* NOTREACHED */
156 }
157
158 static void
159 usage(void)
160 {
161
162         fprintf(stderr, "usage: cat [-belnstuv] [file ...]\n");
163         exit(1);
164         /* NOTREACHED */
165 }
166
167 static void
168 scanfiles(char *argv[], int cooked)
169 {
170         int fd, i;
171         char *path;
172         FILE *fp;
173
174         i = 0;
175         fd = -1;
176         while ((path = argv[i]) != NULL || i == 0) {
177                 if (path == NULL || strcmp(path, "-") == 0) {
178                         filename = "stdin";
179                         fd = STDIN_FILENO;
180                 } else {
181                         filename = path;
182                         fd = open(path, O_RDONLY);
183 #ifndef NO_UDOM_SUPPORT
184                         if (fd < 0 && errno == EOPNOTSUPP)
185                                 fd = udom_open(path, O_RDONLY);
186 #endif
187                 }
188                 if (fd < 0) {
189                         warn("%s", path);
190                         rval = 1;
191                 } else if (cooked) {
192                         if (fd == STDIN_FILENO)
193                                 cook_cat(stdin);
194                         else {
195                                 fp = fdopen(fd, "r");
196                                 cook_cat(fp);
197                                 fclose(fp);
198                         }
199                 } else {
200                         raw_cat(fd);
201                         if (fd != STDIN_FILENO)
202                                 close(fd);
203                 }
204                 if (path == NULL)
205                         break;
206                 ++i;
207         }
208 }
209
210 static void
211 cook_cat(FILE *fp)
212 {
213         int ch, gobble, line, prev;
214         wint_t wch;
215
216         /* Reset EOF condition on stdin. */
217         if (fp == stdin && feof(stdin))
218                 clearerr(stdin);
219
220         line = gobble = 0;
221         for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
222                 if (prev == '\n') {
223                         if (sflag) {
224                                 if (ch == '\n') {
225                                         if (gobble)
226                                                 continue;
227                                         gobble = 1;
228                                 } else
229                                         gobble = 0;
230                         }
231                         if (nflag) {
232                                 if (!bflag || ch != '\n') {
233                                         (void)fprintf(stdout, "%6d\t", ++line);
234                                         if (ferror(stdout))
235                                                 break;
236                                 } else if (eflag) {
237                                         (void)fprintf(stdout, "%6s\t", "");
238                                         if (ferror(stdout))
239                                                 break;
240                                 }
241                         }
242                 }
243                 if (ch == '\n') {
244                         if (eflag && putchar('$') == EOF)
245                                 break;
246                 } else if (ch == '\t') {
247                         if (tflag) {
248                                 if (putchar('^') == EOF || putchar('I') == EOF)
249                                         break;
250                                 continue;
251                         }
252                 } else if (vflag) {
253                         (void)ungetc(ch, fp);
254                         /*
255                          * Our getwc(3) doesn't change file position
256                          * on error.
257                          */
258                         if ((wch = getwc(fp)) == WEOF) {
259                                 if (ferror(fp) && errno == EILSEQ) {
260                                         clearerr(fp);
261                                         /* Resync attempt. */
262                                         memset(&fp->_mbstate, 0, sizeof(mbstate_t));
263                                         if ((ch = getc(fp)) == EOF)
264                                                 break;
265                                         wch = ch;
266                                         goto ilseq;
267                                 } else
268                                         break;
269                         }
270                         if (!iswascii(wch) && !iswprint(wch)) {
271 ilseq:
272                                 if (putchar('M') == EOF || putchar('-') == EOF)
273                                         break;
274                                 wch = toascii(wch);
275                         }
276                         if (iswcntrl(wch)) {
277                                 ch = toascii(wch);
278                                 ch = (ch == '\177') ? '?' : (ch | 0100);
279                                 if (putchar('^') == EOF || putchar(ch) == EOF)
280                                         break;
281                                 continue;
282                         }
283                         if (putwchar(wch) == WEOF)
284                                 break;
285                         ch = -1;
286                         continue;
287                 }
288                 if (putchar(ch) == EOF)
289                         break;
290         }
291         if (ferror(fp)) {
292                 warn("%s", filename);
293                 rval = 1;
294                 clearerr(fp);
295         }
296         if (ferror(stdout))
297                 err(1, "stdout");
298 }
299
300 static void
301 raw_cat(int rfd)
302 {
303         int off, wfd;
304         ssize_t nr, nw;
305         static size_t bsize;
306         static char *buf = NULL;
307         struct stat sbuf;
308
309         wfd = fileno(stdout);
310         if (buf == NULL) {
311                 if (fstat(wfd, &sbuf))
312                         err(1, "stdout");
313                 if (S_ISREG(sbuf.st_mode)) {
314                         /* If there's plenty of RAM, use a large copy buffer */
315                         if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
316                                 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
317                         else
318                                 bsize = BUFSIZE_SMALL;
319                 } else
320                         bsize = MAX(sbuf.st_blksize,
321                             (blksize_t)sysconf(_SC_PAGESIZE));
322                 if ((buf = malloc(bsize)) == NULL)
323                         err(1, "malloc() failure of IO buffer");
324         }
325         while ((nr = read(rfd, buf, bsize)) > 0)
326                 for (off = 0; nr; nr -= nw, off += nw)
327                         if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
328                                 err(1, "stdout");
329         if (nr < 0) {
330                 warn("%s", filename);
331                 rval = 1;
332         }
333 }
334
335 #ifndef NO_UDOM_SUPPORT
336
337 static int
338 udom_open(const char *path, int flags)
339 {
340         struct addrinfo hints, *res, *res0;
341         char rpath[PATH_MAX];
342         int fd = -1;
343         int error;
344
345         /*
346          * Construct the unix domain socket address and attempt to connect.
347          */
348         bzero(&hints, sizeof(hints));
349         hints.ai_family = AF_LOCAL;
350         if (realpath(path, rpath) == NULL)
351                 return (-1);
352         error = getaddrinfo(rpath, NULL, &hints, &res0);
353         if (error) {
354                 warn("%s", gai_strerror(error));
355                 errno = EINVAL;
356                 return (-1);
357         }
358         for (res = res0; res != NULL; res = res->ai_next) {
359                 fd = socket(res->ai_family, res->ai_socktype,
360                     res->ai_protocol);
361                 if (fd < 0) {
362                         freeaddrinfo(res0);
363                         return (-1);
364                 }
365                 error = connect(fd, res->ai_addr, res->ai_addrlen);
366                 if (error == 0)
367                         break;
368                 else {
369                         close(fd);
370                         fd = -1;
371                 }
372         }
373         freeaddrinfo(res0);
374
375         /*
376          * handle the open flags by shutting down appropriate directions
377          */
378         if (fd >= 0) {
379                 switch(flags & O_ACCMODE) {
380                 case O_RDONLY:
381                         if (shutdown(fd, SHUT_WR) == -1)
382                                 warn(NULL);
383                         break;
384                 case O_WRONLY:
385                         if (shutdown(fd, SHUT_RD) == -1)
386                                 warn(NULL);
387                         break;
388                 default:
389                         break;
390                 }
391         }
392         return (fd);
393 }
394
395 #endif