xref: /csrg-svn/usr.bin/rev/rev.c (revision 65331)
150428Sbostic /*-
263075Sbostic  * Copyright (c) 1987, 1992, 1993
363075Sbostic  *	The Regents of the University of California.  All rights reserved.
433044Sbostic  *
550428Sbostic  * %sccs.include.redist.c%
633044Sbostic  */
71079Sbill 
833044Sbostic #ifndef lint
963075Sbostic static char copyright[] =
1063075Sbostic "@(#) Copyright (c) 1987, 1992, 1993\n\
1163075Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1233044Sbostic #endif /* not lint */
131079Sbill 
1433044Sbostic #ifndef lint
15*65331Sbostic static char sccsid[] = "@(#)rev.c	8.2 (Berkeley) 01/02/94";
1633044Sbostic #endif /* not lint */
171079Sbill 
1853038Sbostic #include <sys/types.h>
1960164Sbostic 
2060164Sbostic #include <err.h>
2153038Sbostic #include <errno.h>
2233044Sbostic #include <stdio.h>
2353038Sbostic #include <stdlib.h>
2453038Sbostic #include <string.h>
2533044Sbostic 
2653038Sbostic void usage __P((void));
2753038Sbostic 
2853038Sbostic int
2933044Sbostic main(argc, argv)
3033044Sbostic 	int argc;
3153038Sbostic 	char *argv[];
321079Sbill {
3353038Sbostic 	register char *filename, *p, *t;
3453038Sbostic 	FILE *fp;
3553038Sbostic 	size_t len;
3653038Sbostic 	int ch, rval;
3733044Sbostic 
3853038Sbostic 	while ((ch = getopt(argc, argv, "")) != EOF)
3953038Sbostic 		switch(ch) {
4053038Sbostic 		case '?':
4153038Sbostic 		default:
4253038Sbostic 			usage();
4353038Sbostic 		}
4453038Sbostic 
4553038Sbostic 	argc -= optind;
4653038Sbostic 	argv += optind;
4753038Sbostic 
4853038Sbostic 	fp = stdin;
4953038Sbostic 	filename = "stdin";
5053038Sbostic 	rval = 0;
511079Sbill 	do {
5253038Sbostic 		if (*argv) {
5353038Sbostic 			if ((fp = fopen(*argv, "r")) == NULL) {
5460164Sbostic 				warn("%s", *argv);
5553038Sbostic 				rval = 1;
5653038Sbostic 				++argv;
5753038Sbostic 				continue;
5853038Sbostic 			}
5953038Sbostic 			filename = *argv++;
601079Sbill 		}
61*65331Sbostic 		while ((p = fgetln(fp, &len)) != NULL) {
62*65331Sbostic 			if (p[len - 1] == '\n')
63*65331Sbostic 				--len;
6453038Sbostic 			t = p + len - 1;
6553038Sbostic 			for (t = p + len - 1; t >= p; --t)
6633044Sbostic 				putchar(*t);
6733044Sbostic 			putchar('\n');
681079Sbill 		}
6953038Sbostic 		if (ferror(fp)) {
7060164Sbostic 			warn("%s", filename);
7153038Sbostic 			rval = 1;
7253038Sbostic 		}
7353038Sbostic 		(void)fclose(fp);
7453038Sbostic 	} while(*argv);
7553039Sbostic 	exit(rval);
761079Sbill }
7753038Sbostic 
7853038Sbostic void
7953038Sbostic usage()
8053038Sbostic {
8153038Sbostic 	(void)fprintf(stderr, "usage: rev [file ...]\n");
8253038Sbostic 	exit(1);
8353038Sbostic }
84