xref: /csrg-svn/usr.bin/rev/rev.c (revision 53038)
150428Sbostic /*-
2*53038Sbostic  * Copyright (c) 1987, 1992 The Regents of the University of California.
333044Sbostic  * All rights reserved.
433044Sbostic  *
550428Sbostic  * %sccs.include.redist.c%
633044Sbostic  */
71079Sbill 
833044Sbostic #ifndef lint
933044Sbostic char copyright[] =
10*53038Sbostic "@(#) Copyright (c) 1987, 1992 The Regents of the University of California.\n\
1133044Sbostic  All rights reserved.\n";
1233044Sbostic #endif /* not lint */
131079Sbill 
1433044Sbostic #ifndef lint
15*53038Sbostic static char sccsid[] = "@(#)rev.c	5.1 (Berkeley) 03/21/92";
1633044Sbostic #endif /* not lint */
171079Sbill 
18*53038Sbostic #include <sys/types.h>
19*53038Sbostic #include <errno.h>
2033044Sbostic #include <stdio.h>
21*53038Sbostic #include <stdlib.h>
22*53038Sbostic #include <string.h>
2333044Sbostic 
24*53038Sbostic void usage __P((void));
25*53038Sbostic void warn __P((const char *, ...));
26*53038Sbostic 
27*53038Sbostic int
2833044Sbostic main(argc, argv)
2933044Sbostic 	int argc;
30*53038Sbostic 	char *argv[];
311079Sbill {
32*53038Sbostic 	register char *filename, *p, *t;
33*53038Sbostic 	FILE *fp;
34*53038Sbostic 	size_t len;
35*53038Sbostic 	int ch, rval;
3633044Sbostic 
37*53038Sbostic 	while ((ch = getopt(argc, argv, "")) != EOF)
38*53038Sbostic 		switch(ch) {
39*53038Sbostic 		case '?':
40*53038Sbostic 		default:
41*53038Sbostic 			usage();
42*53038Sbostic 		}
43*53038Sbostic 
44*53038Sbostic 	argc -= optind;
45*53038Sbostic 	argv += optind;
46*53038Sbostic 
47*53038Sbostic 	fp = stdin;
48*53038Sbostic 	filename = "stdin";
49*53038Sbostic 	rval = 0;
501079Sbill 	do {
51*53038Sbostic 		if (*argv) {
52*53038Sbostic 			if ((fp = fopen(*argv, "r")) == NULL) {
53*53038Sbostic 				warn("%s: %s", *argv, strerror(errno));
54*53038Sbostic 				rval = 1;
55*53038Sbostic 				++argv;
56*53038Sbostic 				continue;
57*53038Sbostic 			}
58*53038Sbostic 			filename = *argv++;
591079Sbill 		}
60*53038Sbostic 		while (p = fgetline(fp, &len)) {
61*53038Sbostic 			t = p + len - 1;
62*53038Sbostic 			for (t = p + len - 1; t >= p; --t)
6333044Sbostic 				putchar(*t);
6433044Sbostic 			putchar('\n');
651079Sbill 		}
66*53038Sbostic 		if (ferror(fp)) {
67*53038Sbostic 			warn("%s: %s", filename, strerror(errno));
68*53038Sbostic 			rval = 1;
69*53038Sbostic 		}
70*53038Sbostic 		(void)fclose(fp);
71*53038Sbostic 	} while(*argv);
7232745Sbostic 	exit(0);
731079Sbill }
74*53038Sbostic 
75*53038Sbostic #if __STDC__
76*53038Sbostic #include <stdarg.h>
77*53038Sbostic #else
78*53038Sbostic #include <varargs.h>
79*53038Sbostic #endif
80*53038Sbostic 
81*53038Sbostic void
82*53038Sbostic #if __STDC__
83*53038Sbostic warn(const char *fmt, ...)
84*53038Sbostic #else
85*53038Sbostic warn(fmt, va_alist)
86*53038Sbostic 	char *fmt;
87*53038Sbostic         va_dcl
88*53038Sbostic #endif
89*53038Sbostic {
90*53038Sbostic 	va_list ap;
91*53038Sbostic #if __STDC__
92*53038Sbostic 	va_start(ap, fmt);
93*53038Sbostic #else
94*53038Sbostic 	va_start(ap);
95*53038Sbostic #endif
96*53038Sbostic 	(void)fprintf(stderr, "rev: ");
97*53038Sbostic 	(void)vfprintf(stderr, fmt, ap);
98*53038Sbostic 	va_end(ap);
99*53038Sbostic 	(void)fprintf(stderr, "\n");
100*53038Sbostic }
101*53038Sbostic 
102*53038Sbostic void
103*53038Sbostic usage()
104*53038Sbostic {
105*53038Sbostic 	(void)fprintf(stderr, "usage: rev [file ...]\n");
106*53038Sbostic 	exit(1);
107*53038Sbostic }
108