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 | * 4. Neither the name of the University nor the names of its contributors
|
---|
17 | * may be used to endorse or promote products derived from this software
|
---|
18 | * without specific prior written permission.
|
---|
19 | *
|
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
30 | * SUCH DAMAGE.
|
---|
31 | */
|
---|
32 |
|
---|
33 | #if 0
|
---|
34 | #ifndef lint
|
---|
35 | static char const copyright[] =
|
---|
36 | "@(#) Copyright (c) 1989, 1993\n\
|
---|
37 | The Regents of the University of California. All rights reserved.\n";
|
---|
38 | #endif /* not lint */
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #ifndef lint
|
---|
42 | #if 0
|
---|
43 | static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95";
|
---|
44 | #endif
|
---|
45 | #endif /* not lint */
|
---|
46 | #if 0
|
---|
47 | #include <sys/cdefs.h>
|
---|
48 | __FBSDID("$FreeBSD: src/bin/cat/cat.c,v 1.32 2005/01/10 08:39:20 imp Exp $");
|
---|
49 | #else
|
---|
50 | #define NO_UDOM_SUPPORT /* kmk */
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #include "config.h"
|
---|
54 | #ifndef _MSC_VER
|
---|
55 | # include <sys/param.h>
|
---|
56 | #endif
|
---|
57 | #include <sys/stat.h>
|
---|
58 | #ifndef NO_UDOM_SUPPORT
|
---|
59 | # include <sys/socket.h>
|
---|
60 | # include <sys/un.h>
|
---|
61 | # include <errno.h>
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | #include <ctype.h>
|
---|
65 | #include "err.h"
|
---|
66 | #include <fcntl.h>
|
---|
67 | #include <locale.h>
|
---|
68 | #include <stdio.h>
|
---|
69 | #include <stdlib.h>
|
---|
70 | #include <string.h>
|
---|
71 | #include <unistd.h>
|
---|
72 | #include <stddef.h>
|
---|
73 | #include "getopt.h"
|
---|
74 | #ifdef __sun__
|
---|
75 | # include "solfakes.h"
|
---|
76 | #endif
|
---|
77 | #ifdef _MSC_VER
|
---|
78 | # include "mscfakes.h"
|
---|
79 | #endif
|
---|
80 | #include "kmkbuiltin.h"
|
---|
81 |
|
---|
82 |
|
---|
83 | #ifdef KBUILD_OS_WINDOWS
|
---|
84 | /* This is a trick to seriuosly speed up console output windows. */
|
---|
85 | # undef write
|
---|
86 | # define write maybe_con_write
|
---|
87 | extern ssize_t maybe_con_write(int, void const *, size_t);
|
---|
88 | #endif
|
---|
89 |
|
---|
90 |
|
---|
91 | int bflag, eflag, nflag, sflag, tflag, vflag;
|
---|
92 | /*int rval;*/
|
---|
93 | const char *filename;
|
---|
94 |
|
---|
95 | static struct option long_options[] =
|
---|
96 | {
|
---|
97 | { "help", no_argument, 0, 261 },
|
---|
98 | { "version", no_argument, 0, 262 },
|
---|
99 | { 0, 0, 0, 0 },
|
---|
100 | };
|
---|
101 |
|
---|
102 |
|
---|
103 | static int usage(FILE *);
|
---|
104 | static int scanfiles(char *argv[], int cooked);
|
---|
105 | static int cook_cat(FILE *);
|
---|
106 | static int raw_cat(int);
|
---|
107 |
|
---|
108 | #ifndef NO_UDOM_SUPPORT
|
---|
109 | static int udom_open(const char *path, int flags);
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | int
|
---|
113 | kmk_builtin_cat(int argc, char *argv[], char **envp)
|
---|
114 | {
|
---|
115 | int ch, rc;
|
---|
116 |
|
---|
117 | /* kmk: reinitialize globals */
|
---|
118 | bflag = eflag = nflag = sflag = tflag = vflag = 0;
|
---|
119 | filename = NULL;
|
---|
120 |
|
---|
121 | /* kmk: reset getopt and set progname */
|
---|
122 | g_progname = argv[0];
|
---|
123 | opterr = 1;
|
---|
124 | optarg = NULL;
|
---|
125 | optopt = 0;
|
---|
126 | optind = 0; /* init */
|
---|
127 |
|
---|
128 | #ifdef kmk_builtin_cat /* kmk did this already. */
|
---|
129 | setlocale(LC_CTYPE, "");
|
---|
130 | #else
|
---|
131 | fflush(stdout);
|
---|
132 | #endif
|
---|
133 |
|
---|
134 | while ((ch = getopt_long(argc, argv, "benstuv", long_options, NULL)) != -1)
|
---|
135 | switch (ch) {
|
---|
136 | case 'b':
|
---|
137 | bflag = nflag = 1; /* -b implies -n */
|
---|
138 | break;
|
---|
139 | case 'e':
|
---|
140 | eflag = vflag = 1; /* -e implies -v */
|
---|
141 | break;
|
---|
142 | case 'n':
|
---|
143 | nflag = 1;
|
---|
144 | break;
|
---|
145 | case 's':
|
---|
146 | sflag = 1;
|
---|
147 | break;
|
---|
148 | case 't':
|
---|
149 | tflag = vflag = 1; /* -t implies -v */
|
---|
150 | break;
|
---|
151 | case 'u':
|
---|
152 | setbuf(stdout, NULL);
|
---|
153 | break;
|
---|
154 | case 'v':
|
---|
155 | vflag = 1;
|
---|
156 | break;
|
---|
157 | case 261:
|
---|
158 | usage(stdout);
|
---|
159 | return 0;
|
---|
160 | case 262:
|
---|
161 | return kbuild_version(argv[0]);
|
---|
162 | default:
|
---|
163 | return usage(stderr);
|
---|
164 | }
|
---|
165 | argv += optind;
|
---|
166 |
|
---|
167 | if (bflag || eflag || nflag || sflag || tflag || vflag)
|
---|
168 | rc = scanfiles(argv, 1);
|
---|
169 | else
|
---|
170 | rc = scanfiles(argv, 0);
|
---|
171 | #ifdef kmk_builtin_cat /* only in the external program. */
|
---|
172 | if (fclose(stdout))
|
---|
173 | return err(1, "stdout");
|
---|
174 | #endif
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 | static int
|
---|
179 | usage(FILE *fp)
|
---|
180 | {
|
---|
181 | fprintf(fp, "usage: %s [-benstuv] [file ...]\n"
|
---|
182 | " or: %s --help\n"
|
---|
183 | " or: %s --version\n",
|
---|
184 | g_progname, g_progname, g_progname);
|
---|
185 | return 1;
|
---|
186 | }
|
---|
187 |
|
---|
188 | static int
|
---|
189 | scanfiles(char *argv[], int cooked)
|
---|
190 | {
|
---|
191 | int i = 0;
|
---|
192 | char *path;
|
---|
193 | FILE *fp;
|
---|
194 | int rc2 = 0;
|
---|
195 | int rc = 0;
|
---|
196 |
|
---|
197 | while ((path = argv[i]) != NULL || i == 0) {
|
---|
198 | int fd;
|
---|
199 |
|
---|
200 | if (path == NULL || strcmp(path, "-") == 0) {
|
---|
201 | filename = "stdin";
|
---|
202 | fd = STDIN_FILENO;
|
---|
203 | } else {
|
---|
204 | filename = path;
|
---|
205 | fd = open(path, O_RDONLY);
|
---|
206 | #ifndef NO_UDOM_SUPPORT
|
---|
207 | if (fd < 0 && errno == EOPNOTSUPP)
|
---|
208 | fd = udom_open(path, O_RDONLY);
|
---|
209 | #endif
|
---|
210 | }
|
---|
211 | if (fd < 0) {
|
---|
212 | warn("%s", path);
|
---|
213 | rc2 = 1; /* non fatal */
|
---|
214 | } else if (cooked) {
|
---|
215 | if (fd == STDIN_FILENO)
|
---|
216 | rc = cook_cat(stdin);
|
---|
217 | else {
|
---|
218 | fp = fdopen(fd, "r");
|
---|
219 | rc = cook_cat(fp);
|
---|
220 | fclose(fp);
|
---|
221 | }
|
---|
222 | } else {
|
---|
223 | rc = raw_cat(fd);
|
---|
224 | if (fd != STDIN_FILENO)
|
---|
225 | close(fd);
|
---|
226 | }
|
---|
227 | if (rc || path == NULL)
|
---|
228 | break;
|
---|
229 | ++i;
|
---|
230 | }
|
---|
231 | return !rc ? rc2 : rc;
|
---|
232 | }
|
---|
233 |
|
---|
234 | static int
|
---|
235 | cook_cat(FILE *fp)
|
---|
236 | {
|
---|
237 | int ch, gobble, line, prev;
|
---|
238 | int rc = 0;
|
---|
239 |
|
---|
240 | /* Reset EOF condition on stdin. */
|
---|
241 | if (fp == stdin && feof(stdin))
|
---|
242 | clearerr(stdin);
|
---|
243 |
|
---|
244 | line = gobble = 0;
|
---|
245 | for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
|
---|
246 | if (prev == '\n') {
|
---|
247 | if (sflag) {
|
---|
248 | if (ch == '\n') {
|
---|
249 | if (gobble)
|
---|
250 | continue;
|
---|
251 | gobble = 1;
|
---|
252 | } else
|
---|
253 | gobble = 0;
|
---|
254 | }
|
---|
255 | if (nflag && (!bflag || ch != '\n')) {
|
---|
256 | (void)fprintf(stdout, "%6d\t", ++line);
|
---|
257 | if (ferror(stdout))
|
---|
258 | break;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | if (ch == '\n') {
|
---|
262 | if (eflag && putchar('$') == EOF)
|
---|
263 | break;
|
---|
264 | } else if (ch == '\t') {
|
---|
265 | if (tflag) {
|
---|
266 | if (putchar('^') == EOF || putchar('I') == EOF)
|
---|
267 | break;
|
---|
268 | continue;
|
---|
269 | }
|
---|
270 | } else if (vflag) {
|
---|
271 | if (!isascii(ch) && !isprint(ch)) {
|
---|
272 | if (putchar('M') == EOF || putchar('-') == EOF)
|
---|
273 | break;
|
---|
274 | ch = toascii(ch);
|
---|
275 | }
|
---|
276 | if (iscntrl(ch)) {
|
---|
277 | if (putchar('^') == EOF ||
|
---|
278 | putchar(ch == '\177' ? '?' :
|
---|
279 | ch | 0100) == EOF)
|
---|
280 | break;
|
---|
281 | continue;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | if (putchar(ch) == EOF)
|
---|
285 | break;
|
---|
286 | }
|
---|
287 | if (ferror(fp)) {
|
---|
288 | warn("%s", filename);
|
---|
289 | rc = 1;
|
---|
290 | clearerr(fp);
|
---|
291 | }
|
---|
292 | if (ferror(stdout))
|
---|
293 | return err(1, "stdout");
|
---|
294 | return rc;
|
---|
295 | }
|
---|
296 |
|
---|
297 | static int
|
---|
298 | raw_cat(int rfd)
|
---|
299 | {
|
---|
300 | int off, wfd = fileno(stdout);
|
---|
301 | ssize_t nr, nw;
|
---|
302 | static size_t bsize;
|
---|
303 | static char *buf = NULL;
|
---|
304 | struct stat sbuf;
|
---|
305 |
|
---|
306 | wfd = fileno(stdout);
|
---|
307 | if (buf == NULL) {
|
---|
308 | if (fstat(wfd, &sbuf))
|
---|
309 | return err(1, "%s", filename);
|
---|
310 | #ifdef KBUILD_OS_WINDOWS
|
---|
311 | bsize = 16384;
|
---|
312 | #else
|
---|
313 | bsize = MAX(sbuf.st_blksize, 1024);
|
---|
314 | #endif
|
---|
315 | if ((buf = malloc(bsize)) == NULL)
|
---|
316 | return err(1, "buffer");
|
---|
317 | }
|
---|
318 | while ((nr = read(rfd, buf, bsize)) > 0)
|
---|
319 | for (off = 0; nr; nr -= nw, off += nw)
|
---|
320 | if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
|
---|
321 | return err(1, "stdout");
|
---|
322 | if (nr < 0) {
|
---|
323 | warn("%s", filename);
|
---|
324 | return 1;
|
---|
325 | }
|
---|
326 | return 0;
|
---|
327 | }
|
---|
328 |
|
---|
329 | #ifndef NO_UDOM_SUPPORT
|
---|
330 |
|
---|
331 | static int
|
---|
332 | udom_open(const char *path, int flags)
|
---|
333 | {
|
---|
334 | struct sockaddr_un sou;
|
---|
335 | int fd;
|
---|
336 | unsigned int len;
|
---|
337 |
|
---|
338 | bzero(&sou, sizeof(sou));
|
---|
339 |
|
---|
340 | /*
|
---|
341 | * Construct the unix domain socket address and attempt to connect
|
---|
342 | */
|
---|
343 | fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
---|
344 | if (fd >= 0) {
|
---|
345 | sou.sun_family = AF_UNIX;
|
---|
346 | if ((len = strlcpy(sou.sun_path, path,
|
---|
347 | sizeof(sou.sun_path))) >= sizeof(sou.sun_path)) {
|
---|
348 | errno = ENAMETOOLONG;
|
---|
349 | return (-1);
|
---|
350 | }
|
---|
351 | len = offsetof(struct sockaddr_un, sun_path[len+1]);
|
---|
352 |
|
---|
353 | if (connect(fd, (void *)&sou, len) < 0) {
|
---|
354 | close(fd);
|
---|
355 | fd = -1;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * handle the open flags by shutting down appropriate directions
|
---|
361 | */
|
---|
362 | if (fd >= 0) {
|
---|
363 | switch(flags & O_ACCMODE) {
|
---|
364 | case O_RDONLY:
|
---|
365 | if (shutdown(fd, SHUT_WR) == -1)
|
---|
366 | warn(NULL);
|
---|
367 | break;
|
---|
368 | case O_WRONLY:
|
---|
369 | if (shutdown(fd, SHUT_RD) == -1)
|
---|
370 | warn(NULL);
|
---|
371 | break;
|
---|
372 | default:
|
---|
373 | break;
|
---|
374 | }
|
---|
375 | }
|
---|
376 | return(fd);
|
---|
377 | }
|
---|
378 |
|
---|
379 | #endif
|
---|