Contents of /trunk/mkinitrd-magellan/klibc/usr/utils/cat.c
Parent Directory | Revision Log
Revision 1297 -
(show annotations)
(download)
Fri May 27 15:12:11 2011 UTC (12 years, 11 months ago) by niro
File MIME type: text/plain
File size: 6740 byte(s)
Fri May 27 15:12:11 2011 UTC (12 years, 11 months ago) by niro
File MIME type: text/plain
File size: 6740 byte(s)
-updated to klibc-1.5.22 with mntproc definitions patch included
1 | /* $NetBSD: cat.c,v 1.43 2004/01/04 03:31:28 jschauma Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 1989, 1993 |
5 | * The Regents of the University of California. All rights reserved. |
6 | * |
7 | * This code is derived from software contributed to Berkeley by |
8 | * Kevin Fall. |
9 | * |
10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions |
12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. |
18 | * 3. Neither the name of the University nor the names of its contributors |
19 | * may be used to endorse or promote products derived from this software |
20 | * without specific prior written permission. |
21 | * |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * SUCH DAMAGE. |
33 | */ |
34 | |
35 | #ifndef __COPYRIGHT |
36 | #define __COPYRIGHT(arg) |
37 | #endif |
38 | #ifndef __RCSID |
39 | #define __RCSID(arg) |
40 | #endif |
41 | |
42 | #if !defined(lint) |
43 | __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\ |
44 | The Regents of the University of California. All rights reserved.\n"); |
45 | #if 0 |
46 | static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95"; |
47 | #else |
48 | __RCSID("$NetBSD: cat.c,v 1.43 2004/01/04 03:31:28 jschauma Exp $"); |
49 | #endif |
50 | #endif /* not lint */ |
51 | |
52 | #include <sys/param.h> |
53 | #include <sys/stat.h> |
54 | |
55 | #include <ctype.h> |
56 | #include <errno.h> |
57 | #include <fcntl.h> |
58 | #include <stdio.h> |
59 | #include <stdlib.h> |
60 | #include <string.h> |
61 | #include <unistd.h> |
62 | |
63 | int bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag; |
64 | int rval; |
65 | const char *filename; |
66 | |
67 | int main(int, char *[]); |
68 | void cook_args(char *argv[]); |
69 | void cook_buf(FILE *); |
70 | void raw_args(char *argv[]); |
71 | void raw_cat(int); |
72 | |
73 | int main(int argc, char *argv[]) |
74 | { |
75 | int ch; |
76 | struct flock stdout_lock; |
77 | |
78 | while ((ch = getopt(argc, argv, "beflnstuv")) != -1) |
79 | switch (ch) { |
80 | case 'b': |
81 | bflag = nflag = 1; /* -b implies -n */ |
82 | break; |
83 | case 'e': |
84 | eflag = vflag = 1; /* -e implies -v */ |
85 | break; |
86 | case 'f': |
87 | fflag = 1; |
88 | break; |
89 | case 'l': |
90 | lflag = 1; |
91 | break; |
92 | case 'n': |
93 | nflag = 1; |
94 | break; |
95 | case 's': |
96 | sflag = 1; |
97 | break; |
98 | case 't': |
99 | tflag = vflag = 1; /* -t implies -v */ |
100 | break; |
101 | case 'u': |
102 | /* unimplemented */ |
103 | break; |
104 | case 'v': |
105 | vflag = 1; |
106 | break; |
107 | default: |
108 | case '?': |
109 | (void)fprintf(stderr, |
110 | "usage: cat [-beflnstuv] [-] [file ...]\n"); |
111 | exit(1); |
112 | /* NOTREACHED */ |
113 | } |
114 | argv += optind; |
115 | |
116 | if (lflag) { |
117 | stdout_lock.l_len = 0; |
118 | stdout_lock.l_start = 0; |
119 | stdout_lock.l_type = F_WRLCK; |
120 | stdout_lock.l_whence = SEEK_SET; |
121 | if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1) { |
122 | perror("fcntl"); |
123 | exit(1); |
124 | } |
125 | } |
126 | |
127 | if (bflag || eflag || nflag || sflag || tflag || vflag) |
128 | cook_args(argv); |
129 | else |
130 | raw_args(argv); |
131 | if (fclose(stdout)) { |
132 | perror("fclose"); |
133 | exit(1); |
134 | } |
135 | exit(rval); |
136 | /* NOTREACHED */ |
137 | } |
138 | |
139 | void cook_args(char **argv) |
140 | { |
141 | FILE *fp; |
142 | |
143 | fp = stdin; |
144 | filename = "stdin"; |
145 | do { |
146 | if (*argv) { |
147 | if (!strcmp(*argv, "-")) |
148 | fp = stdin; |
149 | else if ((fp = fopen(*argv, |
150 | fflag ? "rf" : "r")) == NULL) { |
151 | perror("fopen"); |
152 | rval = 1; |
153 | ++argv; |
154 | continue; |
155 | } |
156 | filename = *argv++; |
157 | } |
158 | cook_buf(fp); |
159 | if (fp != stdin) |
160 | (void)fclose(fp); |
161 | } while (*argv); |
162 | } |
163 | |
164 | void cook_buf(FILE * fp) |
165 | { |
166 | int ch, gobble, line, prev; |
167 | int stdout_err = 0; |
168 | |
169 | line = gobble = 0; |
170 | for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) { |
171 | if (prev == '\n') { |
172 | if (ch == '\n') { |
173 | if (sflag) { |
174 | if (!gobble && putchar(ch) == EOF) |
175 | break; |
176 | gobble = 1; |
177 | continue; |
178 | } |
179 | if (nflag) { |
180 | if (!bflag) { |
181 | if (fprintf(stdout, |
182 | "%6d\t", |
183 | ++line) < 0) { |
184 | stdout_err++; |
185 | break; |
186 | } |
187 | } else if (eflag) { |
188 | if (fprintf(stdout, |
189 | "%6s\t", "") < 0) { |
190 | stdout_err++; |
191 | break; |
192 | } |
193 | } |
194 | } |
195 | } else if (nflag) { |
196 | if (fprintf(stdout, "%6d\t", ++line) < 0) { |
197 | stdout_err++; |
198 | break; |
199 | } |
200 | } |
201 | } |
202 | gobble = 0; |
203 | if (ch == '\n') { |
204 | if (eflag) |
205 | if (putchar('$') == EOF) |
206 | break; |
207 | } else if (ch == '\t') { |
208 | if (tflag) { |
209 | if (putchar('^') == EOF || putchar('I') == EOF) |
210 | break; |
211 | continue; |
212 | } |
213 | } else if (vflag) { |
214 | if (!isascii(ch)) { |
215 | if (putchar('M') == EOF || putchar('-') == EOF) |
216 | break; |
217 | ch = (ch) & 0x7f; |
218 | } |
219 | if (iscntrl(ch)) { |
220 | if (putchar('^') == EOF || |
221 | putchar(ch == '\177' ? '?' : |
222 | ch | 0100) == EOF) |
223 | break; |
224 | continue; |
225 | } |
226 | } |
227 | if (putchar(ch) == EOF) |
228 | break; |
229 | } |
230 | if (stdout_err) { |
231 | perror(filename); |
232 | rval = 1; |
233 | } |
234 | } |
235 | |
236 | void raw_args(char **argv) |
237 | { |
238 | int fd; |
239 | |
240 | fd = fileno(stdin); |
241 | filename = "stdin"; |
242 | do { |
243 | if (*argv) { |
244 | if (!strcmp(*argv, "-")) |
245 | fd = fileno(stdin); |
246 | else if (fflag) { |
247 | struct stat st; |
248 | fd = open(*argv, O_RDONLY | O_NONBLOCK, 0); |
249 | if (fd < 0) |
250 | goto skip; |
251 | |
252 | if (fstat(fd, &st) == -1) { |
253 | close(fd); |
254 | goto skip; |
255 | } |
256 | if (!S_ISREG(st.st_mode)) { |
257 | close(fd); |
258 | errno = EINVAL; |
259 | goto skipnomsg; |
260 | } |
261 | } else if ((fd = open(*argv, O_RDONLY, 0)) < 0) { |
262 | skip: |
263 | perror(*argv); |
264 | skipnomsg: |
265 | rval = 1; |
266 | ++argv; |
267 | continue; |
268 | } |
269 | filename = *argv++; |
270 | } |
271 | raw_cat(fd); |
272 | if (fd != fileno(stdin)) |
273 | (void)close(fd); |
274 | } while (*argv); |
275 | } |
276 | |
277 | void raw_cat(int rfd) |
278 | { |
279 | static char *buf; |
280 | static char fb_buf[BUFSIZ]; |
281 | static size_t bsize; |
282 | |
283 | struct stat sbuf; |
284 | ssize_t nr, nw, off; |
285 | int wfd; |
286 | |
287 | wfd = fileno(stdout); |
288 | if (buf == NULL) { |
289 | if (fstat(wfd, &sbuf) == 0) { |
290 | bsize = sbuf.st_blksize > BUFSIZ ? |
291 | sbuf.st_blksize : BUFSIZ; |
292 | buf = malloc(bsize); |
293 | } |
294 | if (buf == NULL) { |
295 | buf = fb_buf; |
296 | bsize = BUFSIZ; |
297 | } |
298 | } |
299 | while ((nr = read(rfd, buf, bsize)) > 0) |
300 | for (off = 0; nr; nr -= nw, off += nw) |
301 | if ((nw = write(wfd, buf + off, (size_t) nr)) < 0) { |
302 | perror("write"); |
303 | exit(1); |
304 | } |
305 | if (nr < 0) { |
306 | fprintf(stderr, "%s: invalid length\n", filename); |
307 | rval = 1; |
308 | } |
309 | } |