VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/cat.c@ 684

Last change on this file since 684 was 611, checked in by bird, 18 years ago

Added cat as builtin command.

File size: 7.5 KB
Line 
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
35static 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
43static 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#ifndef _MSC_VER
54#include <sys/param.h>
55#endif
56#include <sys/stat.h>
57#ifndef NO_UDOM_SUPPORT
58#include <sys/socket.h>
59#include <sys/un.h>
60#include <errno.h>
61#endif
62
63#include <ctype.h>
64#include "err.h"
65#include <fcntl.h>
66#include <locale.h>
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70#ifndef _MSC_VER
71#include <unistd.h>
72#else
73#include "mscfakes.h"
74#endif
75#include <stddef.h>
76
77int bflag, eflag, nflag, sflag, tflag, vflag;
78/*int rval;*/
79const char *filename;
80
81static int usage(void);
82static int scanfiles(char *argv[], int cooked);
83static int cook_cat(FILE *);
84static int raw_cat(int);
85
86#ifndef NO_UDOM_SUPPORT
87static int udom_open(const char *path, int flags);
88#endif
89
90int
91kmk_builtin_cat(int argc, char *argv[])
92{
93 int ch, rc;
94
95 /* kmk: reinitialize globals */
96 bflag = eflag = nflag = sflag = tflag = vflag = 0;
97 filename = NULL;
98
99 /* kmk: reset getopt and set progname */
100 g_progname = argv[0];
101 opterr = 1;
102 optarg = NULL;
103 optopt = 0;
104#if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
105 optreset = 1;
106 optind = 1;
107#else
108 optind = 0; /* init */
109#endif
110
111#ifdef kmk_builtin_cat /* kmk did this already. */
112 setlocale(LC_CTYPE, "");
113#endif
114
115 while ((ch = getopt(argc, argv, "benstuv")) != -1)
116 switch (ch) {
117 case 'b':
118 bflag = nflag = 1; /* -b implies -n */
119 break;
120 case 'e':
121 eflag = vflag = 1; /* -e implies -v */
122 break;
123 case 'n':
124 nflag = 1;
125 break;
126 case 's':
127 sflag = 1;
128 break;
129 case 't':
130 tflag = vflag = 1; /* -t implies -v */
131 break;
132 case 'u':
133 setbuf(stdout, NULL);
134 break;
135 case 'v':
136 vflag = 1;
137 break;
138 default:
139 return usage();
140 }
141 argv += optind;
142
143 if (bflag || eflag || nflag || sflag || tflag || vflag)
144 rc = scanfiles(argv, 1);
145 else
146 rc = scanfiles(argv, 0);
147#ifdef kmk_builtin_cat /* only in the external program. */
148 if (fclose(stdout))
149 return err(1, "stdout");
150#endif
151 return rc;
152}
153
154static int
155usage(void)
156{
157 fprintf(stderr, "usage: cat [-benstuv] [file ...]\n");
158 return 1;
159}
160
161static int
162scanfiles(char *argv[], int cooked)
163{
164 int i = 0;
165 char *path;
166 FILE *fp;
167 int rc2 = 0;
168 int rc = 0;
169
170 while ((path = argv[i]) != NULL || i == 0) {
171 int fd;
172
173 if (path == NULL || strcmp(path, "-") == 0) {
174 filename = "stdin";
175 fd = STDIN_FILENO;
176 } else {
177 filename = path;
178 fd = open(path, O_RDONLY);
179#ifndef NO_UDOM_SUPPORT
180 if (fd < 0 && errno == EOPNOTSUPP)
181 fd = udom_open(path, O_RDONLY);
182#endif
183 }
184 if (fd < 0) {
185 warn("%s", path);
186 rc2 = 1; /* non fatal */
187 } else if (cooked) {
188 if (fd == STDIN_FILENO)
189 rc = cook_cat(stdin);
190 else {
191 fp = fdopen(fd, "r");
192 rc = cook_cat(fp);
193 fclose(fp);
194 }
195 } else {
196 rc = raw_cat(fd);
197 if (fd != STDIN_FILENO)
198 close(fd);
199 }
200 if (rc || path == NULL)
201 break;
202 ++i;
203 }
204 return !rc ? rc2 : rc;
205}
206
207static int
208cook_cat(FILE *fp)
209{
210 int ch, gobble, line, prev;
211 int rc = 0;
212
213 /* Reset EOF condition on stdin. */
214 if (fp == stdin && feof(stdin))
215 clearerr(stdin);
216
217 line = gobble = 0;
218 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
219 if (prev == '\n') {
220 if (sflag) {
221 if (ch == '\n') {
222 if (gobble)
223 continue;
224 gobble = 1;
225 } else
226 gobble = 0;
227 }
228 if (nflag && (!bflag || ch != '\n')) {
229 (void)fprintf(stdout, "%6d\t", ++line);
230 if (ferror(stdout))
231 break;
232 }
233 }
234 if (ch == '\n') {
235 if (eflag && putchar('$') == EOF)
236 break;
237 } else if (ch == '\t') {
238 if (tflag) {
239 if (putchar('^') == EOF || putchar('I') == EOF)
240 break;
241 continue;
242 }
243 } else if (vflag) {
244 if (!isascii(ch) && !isprint(ch)) {
245 if (putchar('M') == EOF || putchar('-') == EOF)
246 break;
247 ch = toascii(ch);
248 }
249 if (iscntrl(ch)) {
250 if (putchar('^') == EOF ||
251 putchar(ch == '\177' ? '?' :
252 ch | 0100) == EOF)
253 break;
254 continue;
255 }
256 }
257 if (putchar(ch) == EOF)
258 break;
259 }
260 if (ferror(fp)) {
261 warn("%s", filename);
262 rc = 1;
263 clearerr(fp);
264 }
265 if (ferror(stdout))
266 return err(1, "stdout");
267 return rc;
268}
269
270static int
271raw_cat(int rfd)
272{
273 int off, wfd;
274 ssize_t nr, nw;
275 static size_t bsize;
276 static char *buf = NULL;
277 struct stat sbuf;
278
279 wfd = fileno(stdout);
280 if (buf == NULL) {
281 if (fstat(wfd, &sbuf))
282 return err(1, "%s", filename);
283#ifdef _MSC_VER
284 bsize = 1024;
285#else
286 bsize = MAX(sbuf.st_blksize, 1024);
287#endif
288 if ((buf = malloc(bsize)) == NULL)
289 return err(1, "buffer");
290 }
291 while ((nr = read(rfd, buf, bsize)) > 0)
292 for (off = 0; nr; nr -= nw, off += nw)
293 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
294 return err(1, "stdout");
295 if (nr < 0) {
296 warn("%s", filename);
297 return 1;
298 }
299 return 0;
300}
301
302#ifndef NO_UDOM_SUPPORT
303
304static int
305udom_open(const char *path, int flags)
306{
307 struct sockaddr_un sou;
308 int fd;
309 unsigned int len;
310
311 bzero(&sou, sizeof(sou));
312
313 /*
314 * Construct the unix domain socket address and attempt to connect
315 */
316 fd = socket(AF_UNIX, SOCK_STREAM, 0);
317 if (fd >= 0) {
318 sou.sun_family = AF_UNIX;
319 if ((len = strlcpy(sou.sun_path, path,
320 sizeof(sou.sun_path))) >= sizeof(sou.sun_path)) {
321 errno = ENAMETOOLONG;
322 return (-1);
323 }
324 len = offsetof(struct sockaddr_un, sun_path[len+1]);
325
326 if (connect(fd, (void *)&sou, len) < 0) {
327 close(fd);
328 fd = -1;
329 }
330 }
331
332 /*
333 * handle the open flags by shutting down appropriate directions
334 */
335 if (fd >= 0) {
336 switch(flags & O_ACCMODE) {
337 case O_RDONLY:
338 if (shutdown(fd, SHUT_WR) == -1)
339 warn(NULL);
340 break;
341 case O_WRONLY:
342 if (shutdown(fd, SHUT_RD) == -1)
343 warn(NULL);
344 break;
345 default:
346 break;
347 }
348 }
349 return(fd);
350}
351
352#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette