xref: /csrg-svn/usr.bin/rev/rev.c (revision 69247)
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*69247Sbostic static char sccsid[] = "@(#)rev.c	8.3 (Berkeley) 05/04/95";
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>
25*69247Sbostic #include <unistd.h>
2633044Sbostic 
2753038Sbostic void usage __P((void));
2853038Sbostic 
2953038Sbostic int
main(argc,argv)3033044Sbostic main(argc, argv)
3133044Sbostic 	int argc;
3253038Sbostic 	char *argv[];
331079Sbill {
3453038Sbostic 	register char *filename, *p, *t;
3553038Sbostic 	FILE *fp;
3653038Sbostic 	size_t len;
3753038Sbostic 	int ch, rval;
3833044Sbostic 
3953038Sbostic 	while ((ch = getopt(argc, argv, "")) != EOF)
4053038Sbostic 		switch(ch) {
4153038Sbostic 		case '?':
4253038Sbostic 		default:
4353038Sbostic 			usage();
4453038Sbostic 		}
4553038Sbostic 
4653038Sbostic 	argc -= optind;
4753038Sbostic 	argv += optind;
4853038Sbostic 
4953038Sbostic 	fp = stdin;
5053038Sbostic 	filename = "stdin";
5153038Sbostic 	rval = 0;
521079Sbill 	do {
5353038Sbostic 		if (*argv) {
5453038Sbostic 			if ((fp = fopen(*argv, "r")) == NULL) {
5560164Sbostic 				warn("%s", *argv);
5653038Sbostic 				rval = 1;
5753038Sbostic 				++argv;
5853038Sbostic 				continue;
5953038Sbostic 			}
6053038Sbostic 			filename = *argv++;
611079Sbill 		}
6265331Sbostic 		while ((p = fgetln(fp, &len)) != NULL) {
6365331Sbostic 			if (p[len - 1] == '\n')
6465331Sbostic 				--len;
6553038Sbostic 			t = p + len - 1;
6653038Sbostic 			for (t = p + len - 1; t >= p; --t)
6733044Sbostic 				putchar(*t);
6833044Sbostic 			putchar('\n');
691079Sbill 		}
7053038Sbostic 		if (ferror(fp)) {
7160164Sbostic 			warn("%s", filename);
7253038Sbostic 			rval = 1;
7353038Sbostic 		}
7453038Sbostic 		(void)fclose(fp);
7553038Sbostic 	} while(*argv);
7653039Sbostic 	exit(rval);
771079Sbill }
7853038Sbostic 
7953038Sbostic void
usage()8053038Sbostic usage()
8153038Sbostic {
8253038Sbostic 	(void)fprintf(stderr, "usage: rev [file ...]\n");
8353038Sbostic 	exit(1);
8453038Sbostic }
85