2 * Copyright (c) 1987, 1992 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * Modified for Linux by Charles Hannum (mycroft@gnu.ai.mit.edu)
34 * and Brian Koehmstedt (bpk@gnu.ai.mit.edu)
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
52 #include <sys/types.h>
67 static void sig_handler(int signo
)
73 static void __attribute__((__noreturn__
)) usage(FILE *out
)
75 fprintf(out
, _("Usage: %s [file ...]\n"),
76 program_invocation_short_name
);
78 fprintf(out
, _("\nFor more information see rev(1).\n"));
80 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
83 int main(int argc
, char *argv
[])
85 char *filename
= "stdin";
87 size_t len
, bufsiz
= BUFSIZ
;
89 int ch
, rval
= EXIT_SUCCESS
;
91 setlocale(LC_ALL
, "");
92 bindtextdomain(PACKAGE
, LOCALEDIR
);
95 signal(SIGINT
, sig_handler
);
96 signal(SIGTERM
, sig_handler
);
98 while ((ch
= getopt(argc
, argv
, "")) != -1)
112 if ((fp
= fopen(*argv
, "r")) == NULL
) {
113 warn(_("%s: open failed"), *argv
);
121 buf
= xmalloc(bufsiz
* sizeof(wchar_t));
123 while (fgetws(buf
, bufsiz
, fp
)) {
126 /* This is my hack from setpwnam.c -janl */
127 while (buf
[len
-1] != '\n' && !feof(fp
)) {
130 /* Extend input buffer if it failed getting the whole line */
132 /* So now we double the buffer size */
135 x
= realloc(buf
, bufsiz
* sizeof(wchar_t));
138 err(EXIT_FAILURE
, _("realloc failed"));
142 /* And fill the rest of the buffer */
143 if (!fgetws(&buf
[len
], bufsiz
/2, fp
))
149 t
= buf
+ len
- 1 - (*(buf
+len
-1)=='\r' || *(buf
+len
-1)=='\n');
150 for ( ; t
>= buf
; --t
) {
159 warn("%s", filename
);