VirtualBox

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

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

Solaris + cleanup.

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