xref: /original-bsd/usr.bin/rev/rev.c (revision ddd31891)
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.2 (Berkeley) 01/02/94";
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 
26 void usage __P((void));
27 
28 int
29 main(argc, argv)
30 	int argc;
31 	char *argv[];
32 {
33 	register char *filename, *p, *t;
34 	FILE *fp;
35 	size_t len;
36 	int ch, rval;
37 
38 	while ((ch = getopt(argc, argv, "")) != EOF)
39 		switch(ch) {
40 		case '?':
41 		default:
42 			usage();
43 		}
44 
45 	argc -= optind;
46 	argv += optind;
47 
48 	fp = stdin;
49 	filename = "stdin";
50 	rval = 0;
51 	do {
52 		if (*argv) {
53 			if ((fp = fopen(*argv, "r")) == NULL) {
54 				warn("%s", *argv);
55 				rval = 1;
56 				++argv;
57 				continue;
58 			}
59 			filename = *argv++;
60 		}
61 		while ((p = fgetln(fp, &len)) != NULL) {
62 			if (p[len - 1] == '\n')
63 				--len;
64 			t = p + len - 1;
65 			for (t = p + len - 1; t >= p; --t)
66 				putchar(*t);
67 			putchar('\n');
68 		}
69 		if (ferror(fp)) {
70 			warn("%s", filename);
71 			rval = 1;
72 		}
73 		(void)fclose(fp);
74 	} while(*argv);
75 	exit(rval);
76 }
77 
78 void
79 usage()
80 {
81 	(void)fprintf(stderr, "usage: rev [file ...]\n");
82 	exit(1);
83 }
84