Commit | Line | Data |
---|---|---|
3665d343 | 1 | /*- |
aba01c45 | 2 | * Copyright (c) 1987, 1992 The Regents of the University of California. |
4391e8e5 KB |
3 | * All rights reserved. |
4 | * | |
3665d343 | 5 | * %sccs.include.redist.c% |
4391e8e5 KB |
6 | */ |
7 | ||
8 | #ifndef lint | |
9 | char copyright[] = | |
aba01c45 | 10 | "@(#) Copyright (c) 1987, 1992 The Regents of the University of California.\n\ |
4391e8e5 KB |
11 | All rights reserved.\n"; |
12 | #endif /* not lint */ | |
e04fe8e4 | 13 | |
4391e8e5 | 14 | #ifndef lint |
6337837e | 15 | static char sccsid[] = "@(#)rev.c 5.2 (Berkeley) %G%"; |
4391e8e5 | 16 | #endif /* not lint */ |
e04fe8e4 | 17 | |
aba01c45 KB |
18 | #include <sys/types.h> |
19 | #include <errno.h> | |
4391e8e5 | 20 | #include <stdio.h> |
aba01c45 KB |
21 | #include <stdlib.h> |
22 | #include <string.h> | |
e04fe8e4 | 23 | |
aba01c45 KB |
24 | void usage __P((void)); |
25 | void warn __P((const char *, ...)); | |
26 | ||
27 | int | |
4391e8e5 KB |
28 | main(argc, argv) |
29 | int argc; | |
aba01c45 | 30 | char *argv[]; |
e04fe8e4 | 31 | { |
aba01c45 KB |
32 | register char *filename, *p, *t; |
33 | FILE *fp; | |
34 | size_t len; | |
35 | int ch, rval; | |
36 | ||
37 | while ((ch = getopt(argc, argv, "")) != EOF) | |
38 | switch(ch) { | |
39 | case '?': | |
40 | default: | |
41 | usage(); | |
42 | } | |
4391e8e5 | 43 | |
aba01c45 KB |
44 | argc -= optind; |
45 | argv += optind; | |
46 | ||
47 | fp = stdin; | |
48 | filename = "stdin"; | |
49 | rval = 0; | |
e04fe8e4 | 50 | do { |
aba01c45 KB |
51 | if (*argv) { |
52 | if ((fp = fopen(*argv, "r")) == NULL) { | |
53 | warn("%s: %s", *argv, strerror(errno)); | |
54 | rval = 1; | |
55 | ++argv; | |
56 | continue; | |
57 | } | |
58 | filename = *argv++; | |
e04fe8e4 | 59 | } |
aba01c45 KB |
60 | while (p = fgetline(fp, &len)) { |
61 | t = p + len - 1; | |
62 | for (t = p + len - 1; t >= p; --t) | |
4391e8e5 KB |
63 | putchar(*t); |
64 | putchar('\n'); | |
e04fe8e4 | 65 | } |
aba01c45 KB |
66 | if (ferror(fp)) { |
67 | warn("%s: %s", filename, strerror(errno)); | |
68 | rval = 1; | |
69 | } | |
70 | (void)fclose(fp); | |
71 | } while(*argv); | |
6337837e | 72 | exit(rval); |
e04fe8e4 | 73 | } |
aba01c45 KB |
74 | |
75 | #if __STDC__ | |
76 | #include <stdarg.h> | |
77 | #else | |
78 | #include <varargs.h> | |
79 | #endif | |
80 | ||
81 | void | |
82 | #if __STDC__ | |
83 | warn(const char *fmt, ...) | |
84 | #else | |
85 | warn(fmt, va_alist) | |
86 | char *fmt; | |
87 | va_dcl | |
88 | #endif | |
89 | { | |
90 | va_list ap; | |
91 | #if __STDC__ | |
92 | va_start(ap, fmt); | |
93 | #else | |
94 | va_start(ap); | |
95 | #endif | |
96 | (void)fprintf(stderr, "rev: "); | |
97 | (void)vfprintf(stderr, fmt, ap); | |
98 | va_end(ap); | |
99 | (void)fprintf(stderr, "\n"); | |
100 | } | |
101 | ||
102 | void | |
103 | usage() | |
104 | { | |
105 | (void)fprintf(stderr, "usage: rev [file ...]\n"); | |
106 | exit(1); | |
107 | } |