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 | int bflag, eflag, nflag, sflag, tflag, vflag;
|
---|
84 | /*int rval;*/
|
---|
85 | const char *filename;
|
---|
86 |
|
---|
87 | static struct option long_options[] =
|
---|
88 | {
|
---|
89 | { "help", no_argument, 0, 261 },
|
---|
90 | { "version", no_argument, 0, 262 },
|
---|
91 | { 0, 0, 0, 0 },
|
---|
92 | };
|
---|
93 |
|
---|
94 |
|
---|
95 | static int usage(PKMKBUILTINCTX pCtx, int fIsErr);
|
---|
96 | static int scanfiles(PKMKBUILTINCTX pCtx, char *argv[], int cooked);
|
---|
97 | static int cook_cat(PKMKBUILTINCTX pCtx, FILE *);
|
---|
98 | static int raw_cat(PKMKBUILTINCTX pCtx, int);
|
---|
99 |
|
---|
100 | #ifndef NO_UDOM_SUPPORT
|
---|
101 | static int udom_open(PKMKBUILTINCTX pCtx, const char *path, int flags);
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | int
|
---|
105 | kmk_builtin_cat(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
|
---|
106 | {
|
---|
107 | int ch, rc;
|
---|
108 |
|
---|
109 | /* kmk: reinitialize globals */
|
---|
110 | bflag = eflag = nflag = sflag = tflag = vflag = 0;
|
---|
111 | filename = NULL;
|
---|
112 |
|
---|
113 | /* kmk: reset getopt and set progname */
|
---|
114 | opterr = 1;
|
---|
115 | optarg = NULL;
|
---|
116 | optopt = 0;
|
---|
117 | optind = 0; /* init */
|
---|
118 |
|
---|
119 | #ifdef KMK_BUILTIN_STANDALONE /* kmk did this already. */
|
---|
120 | setlocale(LC_CTYPE, "");
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | while ((ch = getopt_long(argc, argv, "benstuv", long_options, NULL)) != -1)
|
---|
124 | switch (ch) {
|
---|
125 | case 'b':
|
---|
126 | bflag = nflag = 1; /* -b implies -n */
|
---|
127 | break;
|
---|
128 | case 'e':
|
---|
129 | eflag = vflag = 1; /* -e implies -v */
|
---|
130 | break;
|
---|
131 | case 'n':
|
---|
132 | nflag = 1;
|
---|
133 | break;
|
---|
134 | case 's':
|
---|
135 | sflag = 1;
|
---|
136 | break;
|
---|
137 | case 't':
|
---|
138 | tflag = vflag = 1; /* -t implies -v */
|
---|
139 | break;
|
---|
140 | case 'u':
|
---|
141 | #ifdef KMK_BUILTIN_STANDALONE /* don't allow messing with stdout */
|
---|
142 | setbuf(stdout, NULL);
|
---|
143 | #endif
|
---|
144 | break;
|
---|
145 | case 'v':
|
---|
146 | vflag = 1;
|
---|
147 | break;
|
---|
148 | case 261:
|
---|
149 | usage(pCtx, 0);
|
---|
150 | return 0;
|
---|
151 | case 262:
|
---|
152 | return kbuild_version(argv[0]);
|
---|
153 | default:
|
---|
154 | return usage(pCtx, 1);
|
---|
155 | }
|
---|
156 | argv += optind;
|
---|
157 |
|
---|
158 | if (bflag || eflag || nflag || sflag || tflag || vflag)
|
---|
159 | rc = scanfiles(pCtx, argv, 1);
|
---|
160 | else
|
---|
161 | rc = scanfiles(pCtx, argv, 0);
|
---|
162 | #ifdef KMK_BUILTIN_STANDALONE /* don't allow messing with stdout */
|
---|
163 | if (fclose(stdout))
|
---|
164 | return err(pCtx, 1, "stdout");
|
---|
165 | #endif
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 |
|
---|
169 | #ifdef KMK_BUILTIN_STANDALONE
|
---|
170 | int main(int argc, char **argv, char **envp)
|
---|
171 | {
|
---|
172 | KMKBUILTINCTX Ctx = { "kmk_cat", NULL };
|
---|
173 | return kmk_builtin_cat(argc, argv, envp, &Ctx);
|
---|
174 | }
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | static int
|
---|
178 | usage(PKMKBUILTINCTX pCtx, int fIsErr)
|
---|
179 | {
|
---|
180 | kmk_builtin_ctx_printf(pCtx, fIsErr,
|
---|
181 | "usage: %s [-benstuv] [file ...]\n"
|
---|
182 | " or: %s --help\n"
|
---|
183 | " or: %s --version\n",
|
---|
184 | pCtx->pszProgName, pCtx->pszProgName,
|
---|
185 | pCtx->pszProgName);
|
---|
186 | return 1;
|
---|
187 | }
|
---|
188 |
|
---|
189 | static int
|
---|
190 | scanfiles(PKMKBUILTINCTX pCtx, char *argv[], int cooked)
|
---|
191 | {
|
---|
192 | int i = 0;
|
---|
193 | char *path;
|
---|
194 | FILE *fp;
|
---|
195 | int rc2 = 0;
|
---|
196 | int rc = 0;
|
---|
197 |
|
---|
198 | while ((path = argv[i]) != NULL || i == 0) {
|
---|
199 | int fd;
|
---|
200 |
|
---|
201 | if (path == NULL || strcmp(path, "-") == 0) {
|
---|
202 | filename = "stdin";
|
---|
203 | fd = STDIN_FILENO;
|
---|
204 | } else {
|
---|
205 | filename = path;
|
---|
206 | fd = open(path, O_RDONLY);
|
---|
207 | #ifndef NO_UDOM_SUPPORT
|
---|
208 | if (fd < 0 && errno == EOPNOTSUPP)
|
---|
209 | fd = udom_open(pCtx, path, O_RDONLY);
|
---|
210 | #endif
|
---|
211 | }
|
---|
212 | if (fd < 0) {
|
---|
213 | warn(pCtx, "%s", path);
|
---|
214 | rc2 = 1; /* non fatal */
|
---|
215 | } else if (cooked) {
|
---|
216 | if (fd == STDIN_FILENO)
|
---|
217 | rc = cook_cat(pCtx, stdin);
|
---|
218 | else {
|
---|
219 | fp = fdopen(fd, "r");
|
---|
220 | rc = cook_cat(pCtx, fp);
|
---|
221 | fclose(fp);
|
---|
222 | }
|
---|
223 | } else {
|
---|
224 | rc = raw_cat(pCtx, fd);
|
---|
225 | if (fd != STDIN_FILENO)
|
---|
226 | close(fd);
|
---|
227 | }
|
---|
228 | if (rc || path == NULL)
|
---|
229 | break;
|
---|
230 | ++i;
|
---|
231 | }
|
---|
232 | return !rc ? rc2 : rc;
|
---|
233 | }
|
---|
234 |
|
---|
235 | static int cat_putchar(PKMKBUILTINCTX pCtx, char ch)
|
---|
236 | {
|
---|
237 | #ifndef KMK_BUILTIN_STANDALONE
|
---|
238 | if (pCtx->pOut) {
|
---|
239 | output_write_text(pCtx->pOut, 0, &ch, 1);
|
---|
240 | return 0;
|
---|
241 | }
|
---|
242 | #endif
|
---|
243 | return putchar(ch);
|
---|
244 | }
|
---|
245 |
|
---|
246 | static int
|
---|
247 | cook_cat(PKMKBUILTINCTX pCtx, FILE *fp)
|
---|
248 | {
|
---|
249 | int ch, gobble, line, prev;
|
---|
250 | int rc = 0;
|
---|
251 |
|
---|
252 | /* Reset EOF condition on stdin. */
|
---|
253 | if (fp == stdin && feof(stdin))
|
---|
254 | clearerr(stdin);
|
---|
255 |
|
---|
256 | line = gobble = 0;
|
---|
257 | for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
|
---|
258 | if (prev == '\n') {
|
---|
259 | if (sflag) {
|
---|
260 | if (ch == '\n') {
|
---|
261 | if (gobble)
|
---|
262 | continue;
|
---|
263 | gobble = 1;
|
---|
264 | } else
|
---|
265 | gobble = 0;
|
---|
266 | }
|
---|
267 | if (nflag && (!bflag || ch != '\n')) {
|
---|
268 | kmk_builtin_ctx_printf(pCtx, 0, "%6d\t", ++line);
|
---|
269 | if (ferror(stdout))
|
---|
270 | break;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | if (ch == '\n') {
|
---|
274 | if (eflag && cat_putchar(pCtx, '$') == EOF)
|
---|
275 | break;
|
---|
276 | } else if (ch == '\t') {
|
---|
277 | if (tflag) {
|
---|
278 | if (cat_putchar(pCtx, '^') == EOF || cat_putchar(pCtx, 'I') == EOF)
|
---|
279 | break;
|
---|
280 | continue;
|
---|
281 | }
|
---|
282 | } else if (vflag) {
|
---|
283 | if (!isascii(ch) && !isprint(ch)) {
|
---|
284 | if (cat_putchar(pCtx, 'M') == EOF || cat_putchar(pCtx, '-') == EOF)
|
---|
285 | break;
|
---|
286 | ch = toascii(ch);
|
---|
287 | }
|
---|
288 | if (iscntrl(ch)) {
|
---|
289 | if (cat_putchar(pCtx, '^') == EOF ||
|
---|
290 | cat_putchar(pCtx, ch == '\177' ? '?' :
|
---|
291 | ch | 0100) == EOF)
|
---|
292 | break;
|
---|
293 | continue;
|
---|
294 | }
|
---|
295 | }
|
---|
296 | if (cat_putchar(pCtx, ch) == EOF)
|
---|
297 | break;
|
---|
298 | }
|
---|
299 | if (ferror(fp)) {
|
---|
300 | warn(pCtx, "%s", filename);
|
---|
301 | rc = 1;
|
---|
302 | clearerr(fp);
|
---|
303 | }
|
---|
304 | if (ferror(stdout))
|
---|
305 | return err(pCtx, 1, "stdout");
|
---|
306 | return rc;
|
---|
307 | }
|
---|
308 |
|
---|
309 | static int
|
---|
310 | raw_cat(PKMKBUILTINCTX pCtx, int rfd)
|
---|
311 | {
|
---|
312 | int off, wfd = fileno(stdout);
|
---|
313 | ssize_t nr, nw;
|
---|
314 | static size_t bsize;
|
---|
315 | static char *buf = NULL;
|
---|
316 | struct stat sbuf;
|
---|
317 |
|
---|
318 | wfd = fileno(stdout);
|
---|
319 | if (buf == NULL) {
|
---|
320 | if (fstat(wfd, &sbuf))
|
---|
321 | return err(pCtx, 1, "%s", filename);
|
---|
322 | #ifdef KBUILD_OS_WINDOWS
|
---|
323 | bsize = 16384;
|
---|
324 | #else
|
---|
325 | bsize = MAX(sbuf.st_blksize, 1024);
|
---|
326 | #endif
|
---|
327 | if ((buf = malloc(bsize)) == NULL)
|
---|
328 | return err(pCtx, 1, "buffer");
|
---|
329 | }
|
---|
330 | while ((nr = read(rfd, buf, bsize)) > 0)
|
---|
331 | for (off = 0; nr; nr -= nw, off += nw) {
|
---|
332 | #ifndef KMK_BUILTIN_STANDALONE
|
---|
333 | if (pCtx->pOut)
|
---|
334 | nw = output_write_text(pCtx->pOut, 0, buf, nr);
|
---|
335 | else
|
---|
336 | #endif
|
---|
337 | nw = write(wfd, buf + off, (size_t)nr);
|
---|
338 | if (nw < 0)
|
---|
339 | return err(pCtx, 1, "stdout");
|
---|
340 | }
|
---|
341 | if (nr < 0) {
|
---|
342 | warn(pCtx, "%s", filename);
|
---|
343 | return 1;
|
---|
344 | }
|
---|
345 | return 0;
|
---|
346 | }
|
---|
347 |
|
---|
348 | #ifndef NO_UDOM_SUPPORT
|
---|
349 |
|
---|
350 | static int
|
---|
351 | udom_open(PKMKBUILTINCTX pCtx, const char *path, int flags)
|
---|
352 | {
|
---|
353 | struct sockaddr_un sou;
|
---|
354 | int fd;
|
---|
355 | unsigned int len;
|
---|
356 |
|
---|
357 | bzero(&sou, sizeof(sou));
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * Construct the unix domain socket address and attempt to connect
|
---|
361 | */
|
---|
362 | fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
---|
363 | if (fd >= 0) {
|
---|
364 | sou.sun_family = AF_UNIX;
|
---|
365 | if ((len = strlcpy(sou.sun_path, path,
|
---|
366 | sizeof(sou.sun_path))) >= sizeof(sou.sun_path)) {
|
---|
367 | errno = ENAMETOOLONG;
|
---|
368 | return (-1);
|
---|
369 | }
|
---|
370 | len = offsetof(struct sockaddr_un, sun_path[len+1]);
|
---|
371 |
|
---|
372 | if (connect(fd, (void *)&sou, len) < 0) {
|
---|
373 | close(fd);
|
---|
374 | fd = -1;
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | /*
|
---|
379 | * handle the open flags by shutting down appropriate directions
|
---|
380 | */
|
---|
381 | if (fd >= 0) {
|
---|
382 | switch(flags & O_ACCMODE) {
|
---|
383 | case O_RDONLY:
|
---|
384 | if (shutdown(fd, SHUT_WR) == -1)
|
---|
385 | warn(pCtx, NULL);
|
---|
386 | break;
|
---|
387 | case O_WRONLY:
|
---|
388 | if (shutdown(fd, SHUT_RD) == -1)
|
---|
389 | warn(pCtx, NULL);
|
---|
390 | break;
|
---|
391 | default:
|
---|
392 | break;
|
---|
393 | }
|
---|
394 | }
|
---|
395 | return(fd);
|
---|
396 | }
|
---|
397 |
|
---|
398 | #endif
|
---|
399 |
|
---|