xref: /original-bsd/usr.bin/rev/rev.c (revision 72f3481b)
1 /*-
2  * Copyright (c) 1987, 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1987, 1992 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)rev.c	5.2 (Berkeley) 03/21/92";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 void usage __P((void));
25 void warn __P((const char *, ...));
26 
27 int
28 main(argc, argv)
29 	int argc;
30 	char *argv[];
31 {
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 		}
43 
44 	argc -= optind;
45 	argv += optind;
46 
47 	fp = stdin;
48 	filename = "stdin";
49 	rval = 0;
50 	do {
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++;
59 		}
60 		while (p = fgetline(fp, &len)) {
61 			t = p + len - 1;
62 			for (t = p + len - 1; t >= p; --t)
63 				putchar(*t);
64 			putchar('\n');
65 		}
66 		if (ferror(fp)) {
67 			warn("%s: %s", filename, strerror(errno));
68 			rval = 1;
69 		}
70 		(void)fclose(fp);
71 	} while(*argv);
72 	exit(rval);
73 }
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 }
108