source: trunk/essentials/app-shells/bash/examples/loadables/cat.c@ 3506

Last change on this file since 3506 was 3231, checked in by bird, 19 years ago

eol style.

  • Property svn:eol-style set to native
File size: 1.5 KB
Line 
1/*
2 * cat replacement
3 *
4 * no options - the way cat was intended
5 */
6
7#include <fcntl.h>
8#include <errno.h>
9
10#include "builtins.h"
11#include "shell.h"
12
13#ifndef errno
14extern int errno;
15#endif
16
17extern char *strerror ();
18extern char **make_builtin_argv ();
19
20static int
21fcopy(fd)
22int fd;
23{
24 char buf[1024], *s;
25 int n, w, e;
26
27 while (n = read(fd, buf, sizeof (buf))) {
28 w = write(1, buf, n);
29 if (w != n) {
30 e = errno;
31 write(2, "cat: write error: ", 18);
32 s = strerror(e);
33 write(2, s, strlen(s));
34 write(2, "\n", 1);
35 return 1;
36 }
37 }
38 return 0;
39}
40
41cat_main (argc, argv)
42int argc;
43char **argv;
44{
45 int i, fd, r;
46 char *s;
47
48 if (argc == 1)
49 return (fcopy(0));
50
51 for (i = r = 1; i < argc; i++) {
52 if (argv[i][0] == '-' && argv[i][1] == '\0')
53 fd = 0;
54 else {
55 fd = open(argv[i], O_RDONLY, 0666);
56 if (fd < 0) {
57 s = strerror(errno);
58 write(2, "cat: cannot open ", 17);
59 write(2, argv[i], strlen(argv[i]));
60 write(2, ": ", 2);
61 write(2, s, strlen(s));
62 write(2, "\n", 1);
63 continue;
64 }
65 }
66 r = fcopy(fd);
67 if (fd != 0)
68 close(fd);
69 }
70 return (r);
71}
72
73cat_builtin(list)