xref: /original-bsd/usr.bin/rev/rev.c (revision 431be598)
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.1 (Berkeley) 06/09/93";
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 = fgetline(fp, &len)) != NULL) {
62 			t = p + len - 1;
63 			for (t = p + len - 1; t >= p; --t)
64 				putchar(*t);
65 			putchar('\n');
66 		}
67 		if (ferror(fp)) {
68 			warn("%s", filename);
69 			rval = 1;
70 		}
71 		(void)fclose(fp);
72 	} while(*argv);
73 	exit(rval);
74 }
75 
76 void
77 usage()
78 {
79 	(void)fprintf(stderr, "usage: rev [file ...]\n");
80 	exit(1);
81 }
82