VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/cat.c@ 3140

Last change on this file since 3140 was 3117, checked in by bird, 8 years ago

mscfakes.c,cat.c: msc_write: Do the console optimizations. Make the ENOSPC workaround more reliable (looks like _write may actually return a count that is higher than the input, or something else to that effect is going tits up). Fixed a bug if we ever should write more than 1GB in one go, it would've spun forever.

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

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