]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /*- |
2 | * Copyright (c) 1987, 1992 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | * | |
33 | * Modified for Linux by Charles Hannum (mycroft@gnu.ai.mit.edu) | |
34 | * and Brian Koehmstedt (bpk@gnu.ai.mit.edu) | |
35 | * | |
36 | * Wed Sep 14 22:26:00 1994: Patch from bjdouma <bjdouma@xs4all.nl> to handle | |
37 | * last line that has no newline correctly. | |
2b6fc908 | 38 | * 3-Jun-1998: Patched by Nicolai Langfeldt to work better on Linux: |
06eee0d8 | 39 | * Handle any-length-lines. Code copied from util-linux' setpwnam.c |
b50945d4 | 40 | * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL> |
06eee0d8 | 41 | * added Native Language Support |
eb63b9b8 | 42 | * 1999-09-19 Bruno Haible <haible@clisp.cons.org> |
06eee0d8 DB |
43 | * modified to work correctly in multi-byte locales |
44 | * July 2010 - Davidlohr Bueso <dave@gnu.org> | |
45 | * Fixed memory leaks (including Linux signal handling) | |
46 | * Added some memory allocation error handling | |
47 | * Lowered the default buffer size to 256, instead of 512 bytes | |
48 | * Changed tab indentation to 8 chars for better reading the code | |
6dbe3af9 KZ |
49 | */ |
50 | ||
22853e4a | 51 | #include <stdarg.h> |
6dbe3af9 KZ |
52 | #include <sys/types.h> |
53 | #include <errno.h> | |
54 | #include <stdio.h> | |
55 | #include <stdlib.h> | |
56 | #include <string.h> | |
6dbe3af9 | 57 | #include <unistd.h> |
06eee0d8 | 58 | #include <signal.h> |
ca96c6ac | 59 | #include <getopt.h> |
6dbe3af9 | 60 | |
fffe3905 | 61 | #include "nls.h" |
3924d0f1 | 62 | #include "xalloc.h" |
eb63b9b8 | 63 | #include "widechar.h" |
eb76ca98 | 64 | #include "c.h" |
b87cbe84 | 65 | #include "closestream.h" |
eb63b9b8 | 66 | |
06eee0d8 | 67 | wchar_t *buf; |
6dbe3af9 | 68 | |
66b0b03e | 69 | static void sig_handler(int signo __attribute__ ((__unused__))) |
6dbe3af9 | 70 | { |
06eee0d8 | 71 | free(buf); |
50644ff4 | 72 | _exit(EXIT_SUCCESS); |
6dbe3af9 KZ |
73 | } |
74 | ||
ca96c6ac | 75 | static void __attribute__ ((__noreturn__)) usage(FILE * out) |
6dbe3af9 | 76 | { |
ca96c6ac SK |
77 | fprintf(out, _("Usage: %s [options] [file ...]\n"), |
78 | program_invocation_short_name); | |
79 | ||
80 | fprintf(out, _("\nOptions:\n" | |
81 | " -V, --version output version information and exit\n" | |
82 | " -h, --help display this help and exit\n")); | |
06eee0d8 DB |
83 | |
84 | fprintf(out, _("\nFor more information see rev(1).\n")); | |
85 | ||
86 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
6dbe3af9 | 87 | } |
06eee0d8 | 88 | |
4b4eb340 SK |
89 | static void reverse_str(wchar_t *str, size_t n) |
90 | { | |
91 | size_t i; | |
92 | ||
93 | for (i = 0; i < n / 2; ++i) { | |
94 | wchar_t tmp = str[i]; | |
95 | str[i] = str[n - 1 - i]; | |
96 | str[n - 1 - i] = tmp; | |
97 | } | |
98 | } | |
99 | ||
06eee0d8 DB |
100 | int main(int argc, char *argv[]) |
101 | { | |
102 | char *filename = "stdin"; | |
06eee0d8 DB |
103 | size_t len, bufsiz = BUFSIZ; |
104 | FILE *fp = stdin; | |
105 | int ch, rval = EXIT_SUCCESS; | |
106 | ||
3acc206d SK |
107 | static const struct option longopts[] = { |
108 | { "version", no_argument, 0, 'V' }, | |
109 | { "help", no_argument, 0, 'h' }, | |
110 | { NULL, 0, 0, 0 } | |
111 | }; | |
112 | ||
06eee0d8 DB |
113 | setlocale(LC_ALL, ""); |
114 | bindtextdomain(PACKAGE, LOCALEDIR); | |
115 | textdomain(PACKAGE); | |
b87cbe84 | 116 | atexit(close_stdout); |
06eee0d8 DB |
117 | |
118 | signal(SIGINT, sig_handler); | |
119 | signal(SIGTERM, sig_handler); | |
120 | ||
ca96c6ac | 121 | while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) |
06eee0d8 | 122 | switch(ch) { |
ca96c6ac SK |
123 | case 'V': |
124 | printf(_("%s from %s\n"), program_invocation_short_name, | |
125 | PACKAGE_STRING); | |
126 | exit(EXIT_SUCCESS); | |
06eee0d8 DB |
127 | case 'h': |
128 | usage(stdout); | |
129 | default: | |
130 | usage(stderr); | |
131 | } | |
132 | ||
133 | argc -= optind; | |
134 | argv += optind; | |
135 | ||
2226064c SK |
136 | buf = xmalloc(bufsiz * sizeof(wchar_t)); |
137 | ||
06eee0d8 DB |
138 | do { |
139 | if (*argv) { | |
140 | if ((fp = fopen(*argv, "r")) == NULL) { | |
289dcc90 | 141 | warn(_("cannot open %s"), *argv ); |
06eee0d8 DB |
142 | rval = EXIT_FAILURE; |
143 | ++argv; | |
144 | continue; | |
145 | } | |
146 | filename = *argv++; | |
147 | } | |
148 | ||
06eee0d8 DB |
149 | while (fgetws(buf, bufsiz, fp)) { |
150 | len = wcslen(buf); | |
151 | ||
152 | /* This is my hack from setpwnam.c -janl */ | |
153 | while (buf[len-1] != '\n' && !feof(fp)) { | |
06eee0d8 | 154 | /* Extend input buffer if it failed getting the whole line */ |
06eee0d8 DB |
155 | /* So now we double the buffer size */ |
156 | bufsiz *= 2; | |
157 | ||
099bce7f | 158 | buf = xrealloc(buf, bufsiz * sizeof(wchar_t)); |
06eee0d8 DB |
159 | |
160 | /* And fill the rest of the buffer */ | |
161 | if (!fgetws(&buf[len], bufsiz/2, fp)) | |
162 | break; | |
163 | ||
164 | len = wcslen(buf); | |
165 | } | |
d267fdb8 SK |
166 | if (buf[len - 1] == '\n') |
167 | buf[len--] = '\0'; | |
168 | reverse_str(buf, len); | |
4b4eb340 | 169 | fputws(buf, stdout); |
06eee0d8 | 170 | } |
06eee0d8 DB |
171 | if (ferror(fp)) { |
172 | warn("%s", filename); | |
173 | rval = EXIT_FAILURE; | |
174 | } | |
67c47dff | 175 | fclose(fp); |
06eee0d8 DB |
176 | } while(*argv); |
177 | ||
178 | free(buf); | |
179 | return rval; | |
180 | } | |
181 |