]> git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/rev.c
bfc4e804b66a5bd3e4de1406dfba228ad037e848
[thirdparty/util-linux.git] / text-utils / rev.c
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.
38 * 3-Jun-1998: Patched by Nicolai Langfeldt to work better on Linux:
39 * Handle any-length-lines. Code copied from util-linux' setpwnam.c
40 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
41 * added Native Language Support
42 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
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
49 */
50
51 #include <stdarg.h>
52 #include <sys/types.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <signal.h>
59 #include <getopt.h>
60
61 #include "nls.h"
62 #include "xalloc.h"
63 #include "widechar.h"
64 #include "c.h"
65 #include "closestream.h"
66
67 static void sig_handler(int signo __attribute__ ((__unused__)))
68 {
69 _exit(EXIT_SUCCESS);
70 }
71
72 static void __attribute__((__noreturn__)) usage(void)
73 {
74 FILE *out = stdout;
75 fprintf(out, _("Usage: %s [options] [file ...]\n"),
76 program_invocation_short_name);
77
78 fputs(USAGE_SEPARATOR, out);
79 fputs(_("Reverse lines characterwise.\n"), out);
80
81 fputs(USAGE_OPTIONS, out);
82 printf(USAGE_HELP_OPTIONS(16));
83 printf(USAGE_MAN_TAIL("rev(1)"));
84
85 exit(EXIT_SUCCESS);
86 }
87
88 static void reverse_str(wchar_t *str, size_t n)
89 {
90 size_t i;
91
92 for (i = 0; i < n / 2; ++i) {
93 wchar_t tmp = str[i];
94 str[i] = str[n - 1 - i];
95 str[n - 1 - i] = tmp;
96 }
97 }
98
99 static size_t read_line(wchar_t sep, wchar_t *str, size_t n, FILE *stream)
100 {
101 size_t r = 0;
102 while (r < n) {
103 wint_t c = fgetwc(stream);
104 if (c == WEOF)
105 break;
106 str[r++] = c;
107 if ((wchar_t) c == sep)
108 break;
109 }
110 return r;
111 }
112
113 static void write_line(wchar_t *str, size_t n, FILE *stream)
114 {
115 for (size_t i = 0; i < n; i++)
116 fputwc(str[i], stream);
117 }
118
119 int main(int argc, char *argv[])
120 {
121 char const *filename = "stdin";
122 wchar_t *buf;
123 wchar_t sep = L'\n';
124 size_t len, bufsiz = BUFSIZ;
125 FILE *fp = stdin;
126 int ch, rval = EXIT_SUCCESS;
127 uintmax_t line;
128
129 static const struct option longopts[] = {
130 { "zero", no_argument, NULL, '0' },
131 { "version", no_argument, NULL, 'V' },
132 { "help", no_argument, NULL, 'h' },
133 { NULL, 0, NULL, 0 }
134 };
135
136 setlocale(LC_ALL, "");
137 bindtextdomain(PACKAGE, LOCALEDIR);
138 textdomain(PACKAGE);
139 close_stdout_atexit();
140
141 signal(SIGINT, sig_handler);
142 signal(SIGTERM, sig_handler);
143
144 while ((ch = getopt_long(argc, argv, "Vh0", longopts, NULL)) != -1)
145 switch(ch) {
146 case '0':
147 sep = L'\0';
148 break;
149 case 'V':
150 print_version(EXIT_SUCCESS);
151 case 'h':
152 usage();
153 default:
154 errtryhelp(EXIT_FAILURE);
155 }
156
157 argc -= optind;
158 argv += optind;
159
160 buf = xreallocarray(NULL, bufsiz, sizeof(wchar_t));
161
162 do {
163 if (*argv) {
164 if ((fp = fopen(*argv, "r")) == NULL) {
165 warn(_("cannot open %s"), *argv );
166 rval = EXIT_FAILURE;
167 ++argv;
168 continue;
169 }
170 filename = *argv++;
171 }
172
173 line = 0;
174 while (!feof(fp)) {
175 len = read_line(sep, buf, bufsiz, fp);
176 if (len == 0)
177 continue;
178
179 /* This is my hack from setpwnam.c -janl */
180 while (len == bufsiz && !feof(fp)) {
181 /* Extend input buffer if it failed getting the whole line */
182 /* So now we double the buffer size */
183 bufsiz *= 2;
184
185 buf = xreallocarray(buf, bufsiz, sizeof(wchar_t));
186
187 /* And fill the rest of the buffer */
188 len += read_line(sep, &buf[len], bufsiz/2, fp);
189 }
190 reverse_str(buf, buf[len - 1] == sep ? len - 1 : len);
191 write_line(buf, len, stdout);
192 line++;
193 }
194 if (ferror(fp)) {
195 warn("%s: %ju", filename, line);
196 rval = EXIT_FAILURE;
197 }
198 if (fp != stdin)
199 fclose(fp);
200 } while(*argv);
201
202 free(buf);
203 return rval;
204 }
205