1*6818646aSjoerg /* $NetBSD: rev.c,v 1.12 2011/09/16 15:39:28 joerg 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*6818646aSjoerg __RCSID("$NetBSD: rev.c,v 1.12 2011/09/16 15:39:28 joerg 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*6818646aSjoerg __dead static void usage(void);
57fd5ef5c7Scgd
58fd5ef5c7Scgd int
main(int argc,char * argv[])59d5fb12b5Sahoka main(int argc, char *argv[])
60fd5ef5c7Scgd {
6140df2046Slukem const char *filename;
62e48fb46aSahoka wchar_t *p, *t;
63fd5ef5c7Scgd FILE *fp;
64fd5ef5c7Scgd size_t len;
65fd5ef5c7Scgd int ch, rval;
66fd5ef5c7Scgd
67e48fb46aSahoka setlocale(LC_ALL, "");
68d5fb12b5Sahoka setprogname(argv[0]);
69e48fb46aSahoka
70dcc8df4fSlukem while ((ch = getopt(argc, argv, "")) != -1)
71fd5ef5c7Scgd switch(ch) {
72fd5ef5c7Scgd case '?':
73fd5ef5c7Scgd default:
74fd5ef5c7Scgd usage();
75fd5ef5c7Scgd }
76fd5ef5c7Scgd
77fd5ef5c7Scgd argc -= optind;
78fd5ef5c7Scgd argv += optind;
79fd5ef5c7Scgd
80fd5ef5c7Scgd fp = stdin;
81fd5ef5c7Scgd filename = "stdin";
82fd5ef5c7Scgd rval = 0;
83fd5ef5c7Scgd do {
84fd5ef5c7Scgd if (*argv) {
85fd5ef5c7Scgd if ((fp = fopen(*argv, "r")) == NULL) {
86bd186630Stls warn("%s", *argv);
87fd5ef5c7Scgd rval = 1;
88fd5ef5c7Scgd ++argv;
89fd5ef5c7Scgd continue;
90fd5ef5c7Scgd }
91fd5ef5c7Scgd filename = *argv++;
92fd5ef5c7Scgd }
93e48fb46aSahoka while ((p = fgetwln(fp, &len)) != NULL) {
94e48fb46aSahoka if (p[len - 1] == L'\n')
9587815db7Scgd --len;
96fd5ef5c7Scgd t = p + len - 1;
97fd5ef5c7Scgd for (t = p + len - 1; t >= p; --t)
98e48fb46aSahoka putwchar(*t);
99e48fb46aSahoka putwchar(L'\n');
100fd5ef5c7Scgd }
101fd5ef5c7Scgd if (ferror(fp)) {
102bd186630Stls warn("%s", filename);
103fd5ef5c7Scgd rval = 1;
104fd5ef5c7Scgd }
105fd5ef5c7Scgd (void)fclose(fp);
106fd5ef5c7Scgd } while(*argv);
107fd5ef5c7Scgd exit(rval);
108fd5ef5c7Scgd }
109fd5ef5c7Scgd
110d5fb12b5Sahoka static void
usage(void)111d5fb12b5Sahoka usage(void)
112fd5ef5c7Scgd {
113d5fb12b5Sahoka (void)fprintf(stderr, "usage: %s [file ...]\n", getprogname());
114d5fb12b5Sahoka exit(EXIT_FAILURE);
115fd5ef5c7Scgd }
116