xref: /original-bsd/usr.bin/rev/rev.c (revision 0f81f0ee)
1 /*-
2  * Copyright (c) 1987, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1987, 1992, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)rev.c	8.3 (Berkeley) 05/04/95";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 
20 #include <err.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 void usage __P((void));
28 
29 int
30 main(argc, argv)
31 	int argc;
32 	char *argv[];
33 {
34 	register char *filename, *p, *t;
35 	FILE *fp;
36 	size_t len;
37 	int ch, rval;
38 
39 	while ((ch = getopt(argc, argv, "")) != EOF)
40 		switch(ch) {
41 		case '?':
42 		default:
43 			usage();
44 		}
45 
46 	argc -= optind;
47 	argv += optind;
48 
49 	fp = stdin;
50 	filename = "stdin";
51 	rval = 0;
52 	do {
53 		if (*argv) {
54 			if ((fp = fopen(*argv, "r")) == NULL) {
55 				warn("%s", *argv);
56 				rval = 1;
57 				++argv;
58 				continue;
59 			}
60 			filename = *argv++;
61 		}
62 		while ((p = fgetln(fp, &len)) != NULL) {
63 			if (p[len - 1] == '\n')
64 				--len;
65 			t = p + len - 1;
66 			for (t = p + len - 1; t >= p; --t)
67 				putchar(*t);
68 			putchar('\n');
69 		}
70 		if (ferror(fp)) {
71 			warn("%s", filename);
72 			rval = 1;
73 		}
74 		(void)fclose(fp);
75 	} while(*argv);
76 	exit(rval);
77 }
78 
79 void
80 usage()
81 {
82 	(void)fprintf(stderr, "usage: rev [file ...]\n");
83 	exit(1);
84 }
85