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