xref: /minix3/usr.bin/rev/rev.c (revision 79cc8b683fa5cb1e8b1e3f8a3c3b09ff71ce0230)
1*79cc8b68SThomas Cort /*	$NetBSD: rev.c,v 1.12 2011/09/16 15:39:28 joerg Exp $	*/
2*79cc8b68SThomas Cort 
3*79cc8b68SThomas Cort /*-
4*79cc8b68SThomas Cort  * Copyright (c) 1987, 1992, 1993
5*79cc8b68SThomas Cort  *	The Regents of the University of California.  All rights reserved.
6*79cc8b68SThomas Cort  *
7*79cc8b68SThomas Cort  * Redistribution and use in source and binary forms, with or without
8*79cc8b68SThomas Cort  * modification, are permitted provided that the following conditions
9*79cc8b68SThomas Cort  * are met:
10*79cc8b68SThomas Cort  * 1. Redistributions of source code must retain the above copyright
11*79cc8b68SThomas Cort  *    notice, this list of conditions and the following disclaimer.
12*79cc8b68SThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
13*79cc8b68SThomas Cort  *    notice, this list of conditions and the following disclaimer in the
14*79cc8b68SThomas Cort  *    documentation and/or other materials provided with the distribution.
15*79cc8b68SThomas Cort  * 3. Neither the name of the University nor the names of its contributors
16*79cc8b68SThomas Cort  *    may be used to endorse or promote products derived from this software
17*79cc8b68SThomas Cort  *    without specific prior written permission.
18*79cc8b68SThomas Cort  *
19*79cc8b68SThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*79cc8b68SThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*79cc8b68SThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*79cc8b68SThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*79cc8b68SThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*79cc8b68SThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*79cc8b68SThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*79cc8b68SThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*79cc8b68SThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*79cc8b68SThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*79cc8b68SThomas Cort  * SUCH DAMAGE.
30*79cc8b68SThomas Cort  */
31*79cc8b68SThomas Cort 
32*79cc8b68SThomas Cort #include <sys/cdefs.h>
33*79cc8b68SThomas Cort #ifndef lint
34*79cc8b68SThomas Cort __COPYRIGHT("@(#) Copyright (c) 1987, 1992, 1993\
35*79cc8b68SThomas Cort  The Regents of the University of California.  All rights reserved.");
36*79cc8b68SThomas Cort #endif /* not lint */
37*79cc8b68SThomas Cort 
38*79cc8b68SThomas Cort #ifndef lint
39*79cc8b68SThomas Cort #if 0
40*79cc8b68SThomas Cort static char sccsid[] = "@(#)rev.c	8.3 (Berkeley) 5/4/95";
41*79cc8b68SThomas Cort #else
42*79cc8b68SThomas Cort __RCSID("$NetBSD: rev.c,v 1.12 2011/09/16 15:39:28 joerg Exp $");
43*79cc8b68SThomas Cort #endif
44*79cc8b68SThomas Cort #endif /* not lint */
45*79cc8b68SThomas Cort 
46*79cc8b68SThomas Cort #include <sys/types.h>
47*79cc8b68SThomas Cort 
48*79cc8b68SThomas Cort #include <err.h>
49*79cc8b68SThomas Cort #include <errno.h>
50*79cc8b68SThomas Cort #include <locale.h>
51*79cc8b68SThomas Cort #include <stdio.h>
52*79cc8b68SThomas Cort #include <stdlib.h>
53*79cc8b68SThomas Cort #include <unistd.h>
54*79cc8b68SThomas Cort #include <wchar.h>
55*79cc8b68SThomas Cort 
56*79cc8b68SThomas Cort __dead static void usage(void);
57*79cc8b68SThomas Cort 
58*79cc8b68SThomas Cort int
main(int argc,char * argv[])59*79cc8b68SThomas Cort main(int argc, char *argv[])
60*79cc8b68SThomas Cort {
61*79cc8b68SThomas Cort 	const char *filename;
62*79cc8b68SThomas Cort 	wchar_t *p, *t;
63*79cc8b68SThomas Cort 	FILE *fp;
64*79cc8b68SThomas Cort 	size_t len;
65*79cc8b68SThomas Cort 	int ch, rval;
66*79cc8b68SThomas Cort 
67*79cc8b68SThomas Cort 	setlocale(LC_ALL, "");
68*79cc8b68SThomas Cort 	setprogname(argv[0]);
69*79cc8b68SThomas Cort 
70*79cc8b68SThomas Cort 	while ((ch = getopt(argc, argv, "")) != -1)
71*79cc8b68SThomas Cort 		switch(ch) {
72*79cc8b68SThomas Cort 		case '?':
73*79cc8b68SThomas Cort 		default:
74*79cc8b68SThomas Cort 			usage();
75*79cc8b68SThomas Cort 		}
76*79cc8b68SThomas Cort 
77*79cc8b68SThomas Cort 	argc -= optind;
78*79cc8b68SThomas Cort 	argv += optind;
79*79cc8b68SThomas Cort 
80*79cc8b68SThomas Cort 	fp = stdin;
81*79cc8b68SThomas Cort 	filename = "stdin";
82*79cc8b68SThomas Cort 	rval = 0;
83*79cc8b68SThomas Cort 	do {
84*79cc8b68SThomas Cort 		if (*argv) {
85*79cc8b68SThomas Cort 			if ((fp = fopen(*argv, "r")) == NULL) {
86*79cc8b68SThomas Cort 				warn("%s", *argv);
87*79cc8b68SThomas Cort 				rval = 1;
88*79cc8b68SThomas Cort 				++argv;
89*79cc8b68SThomas Cort 				continue;
90*79cc8b68SThomas Cort 			}
91*79cc8b68SThomas Cort 			filename = *argv++;
92*79cc8b68SThomas Cort 		}
93*79cc8b68SThomas Cort 		while ((p = fgetwln(fp, &len)) != NULL) {
94*79cc8b68SThomas Cort 			if (p[len - 1] == L'\n')
95*79cc8b68SThomas Cort 				--len;
96*79cc8b68SThomas Cort 			t = p + len - 1;
97*79cc8b68SThomas Cort 			for (t = p + len - 1; t >= p; --t)
98*79cc8b68SThomas Cort 				putwchar(*t);
99*79cc8b68SThomas Cort 			putwchar(L'\n');
100*79cc8b68SThomas Cort 		}
101*79cc8b68SThomas Cort 		if (ferror(fp)) {
102*79cc8b68SThomas Cort 			warn("%s", filename);
103*79cc8b68SThomas Cort 			rval = 1;
104*79cc8b68SThomas Cort 		}
105*79cc8b68SThomas Cort 		(void)fclose(fp);
106*79cc8b68SThomas Cort 	} while(*argv);
107*79cc8b68SThomas Cort 	exit(rval);
108*79cc8b68SThomas Cort }
109*79cc8b68SThomas Cort 
110*79cc8b68SThomas Cort static void
usage(void)111*79cc8b68SThomas Cort usage(void)
112*79cc8b68SThomas Cort {
113*79cc8b68SThomas Cort 	(void)fprintf(stderr, "usage: %s [file ...]\n", getprogname());
114*79cc8b68SThomas Cort 	exit(EXIT_FAILURE);
115*79cc8b68SThomas Cort }
116