xref: /csrg-svn/usr.bin/rev/rev.c (revision 53039)
150428Sbostic /*-
253038Sbostic  * Copyright (c) 1987, 1992 The Regents of the University of California.
333044Sbostic  * All rights reserved.
433044Sbostic  *
550428Sbostic  * %sccs.include.redist.c%
633044Sbostic  */
71079Sbill 
833044Sbostic #ifndef lint
933044Sbostic char copyright[] =
1053038Sbostic "@(#) Copyright (c) 1987, 1992 The Regents of the University of California.\n\
1133044Sbostic  All rights reserved.\n";
1233044Sbostic #endif /* not lint */
131079Sbill 
1433044Sbostic #ifndef lint
15*53039Sbostic static char sccsid[] = "@(#)rev.c	5.2 (Berkeley) 03/21/92";
1633044Sbostic #endif /* not lint */
171079Sbill 
1853038Sbostic #include <sys/types.h>
1953038Sbostic #include <errno.h>
2033044Sbostic #include <stdio.h>
2153038Sbostic #include <stdlib.h>
2253038Sbostic #include <string.h>
2333044Sbostic 
2453038Sbostic void usage __P((void));
2553038Sbostic void warn __P((const char *, ...));
2653038Sbostic 
2753038Sbostic int
2833044Sbostic main(argc, argv)
2933044Sbostic 	int argc;
3053038Sbostic 	char *argv[];
311079Sbill {
3253038Sbostic 	register char *filename, *p, *t;
3353038Sbostic 	FILE *fp;
3453038Sbostic 	size_t len;
3553038Sbostic 	int ch, rval;
3633044Sbostic 
3753038Sbostic 	while ((ch = getopt(argc, argv, "")) != EOF)
3853038Sbostic 		switch(ch) {
3953038Sbostic 		case '?':
4053038Sbostic 		default:
4153038Sbostic 			usage();
4253038Sbostic 		}
4353038Sbostic 
4453038Sbostic 	argc -= optind;
4553038Sbostic 	argv += optind;
4653038Sbostic 
4753038Sbostic 	fp = stdin;
4853038Sbostic 	filename = "stdin";
4953038Sbostic 	rval = 0;
501079Sbill 	do {
5153038Sbostic 		if (*argv) {
5253038Sbostic 			if ((fp = fopen(*argv, "r")) == NULL) {
5353038Sbostic 				warn("%s: %s", *argv, strerror(errno));
5453038Sbostic 				rval = 1;
5553038Sbostic 				++argv;
5653038Sbostic 				continue;
5753038Sbostic 			}
5853038Sbostic 			filename = *argv++;
591079Sbill 		}
6053038Sbostic 		while (p = fgetline(fp, &len)) {
6153038Sbostic 			t = p + len - 1;
6253038Sbostic 			for (t = p + len - 1; t >= p; --t)
6333044Sbostic 				putchar(*t);
6433044Sbostic 			putchar('\n');
651079Sbill 		}
6653038Sbostic 		if (ferror(fp)) {
6753038Sbostic 			warn("%s: %s", filename, strerror(errno));
6853038Sbostic 			rval = 1;
6953038Sbostic 		}
7053038Sbostic 		(void)fclose(fp);
7153038Sbostic 	} while(*argv);
72*53039Sbostic 	exit(rval);
731079Sbill }
7453038Sbostic 
7553038Sbostic #if __STDC__
7653038Sbostic #include <stdarg.h>
7753038Sbostic #else
7853038Sbostic #include <varargs.h>
7953038Sbostic #endif
8053038Sbostic 
8153038Sbostic void
8253038Sbostic #if __STDC__
8353038Sbostic warn(const char *fmt, ...)
8453038Sbostic #else
8553038Sbostic warn(fmt, va_alist)
8653038Sbostic 	char *fmt;
8753038Sbostic         va_dcl
8853038Sbostic #endif
8953038Sbostic {
9053038Sbostic 	va_list ap;
9153038Sbostic #if __STDC__
9253038Sbostic 	va_start(ap, fmt);
9353038Sbostic #else
9453038Sbostic 	va_start(ap);
9553038Sbostic #endif
9653038Sbostic 	(void)fprintf(stderr, "rev: ");
9753038Sbostic 	(void)vfprintf(stderr, fmt, ap);
9853038Sbostic 	va_end(ap);
9953038Sbostic 	(void)fprintf(stderr, "\n");
10053038Sbostic }
10153038Sbostic 
10253038Sbostic void
10353038Sbostic usage()
10453038Sbostic {
10553038Sbostic 	(void)fprintf(stderr, "usage: rev [file ...]\n");
10653038Sbostic 	exit(1);
10753038Sbostic }
108