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