VirtualBox

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

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

Always use the GNU make getopt stuff.

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