]> cygwin.com Git - cygwin-apps/cygutils.git/blob - src/rev/rev.c
Fixes for cygstart; bump version number and documentation
[cygwin-apps/cygutils.git] / src / rev / 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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * Modified for Linux by Charles Hannum (mycroft@gnu.ai.mit.edu)
30 * and Brian Koehmstedt (bpk@gnu.ai.mit.edu)
31 *
32 * Wed Sep 14 22:26:00 1994: Patch from bjdouma <bjdouma@xs4all.nl> to handle
33 * last line that has no newline correctly.
34 * 3-Jun-1998: Patched by Nicolai Langfeldt to work better on Linux:
35 * Handle any-length-lines. Code copied from util-linux' setpwnam.c
36 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
37 * added Native Language Support
38 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
39 * modified to work correctly in multi-byte locales
40 * 2002-07-12 Charles Wilson <cwilson@ece.gatech.edu>
41 * modified to work on cygwin; integrated into cygutils package
42 *
43 */
44
45 #if HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48 #include "common.h"
49
50 /* included by common.h *//*
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 */
59 #ifndef LOCALEDIR
60 #define LOCALEDIR "/usr/share/locale"
61 #endif
62
63 #ifdef HAVE_LOCALE_H
64 # include <locale.h>
65 #endif
66 #if !HAVE_SETLOCALE
67 # undef setlocale(a, b)
68 # define setlocale(a, b) /* empty */
69 #endif
70
71 #ifdef ENABLE_NLS
72 # include <libintl.h>
73 # define _(Text) gettext (Text)
74 # ifdef gettext_noop
75 # define N_(String) gettext_noop (String)
76 # else
77 # define N_(String) (String)
78 # endif
79 #else
80 # undef bindtextdomain
81 # define bindtextdomain(Domain, Directory) /* empty */
82 # undef textdomain
83 # define textdomain(Domain) /* empty */
84 # define _(Text) (Text)
85 # define N_(Text) (Text)
86 #endif
87
88 #include "widechar.h"
89
90 void usage(void);
91 void warn(const char *, ...);
92
93 int
94 main(int argc, char *argv[])
95 {
96 register char *filename;
97 register wchar_t *t;
98 size_t buflen = 512;
99 wchar_t *p = malloc(buflen*sizeof(wchar_t));
100 size_t len;
101 FILE *fp;
102 int ch, rval;
103
104 setlocale(LC_ALL, "");
105 bindtextdomain(PACKAGE, LOCALEDIR);
106 textdomain(PACKAGE);
107
108 while ((ch = getopt(argc, argv, "")) != EOF)
109 switch(ch) {
110 case '?':
111 default:
112 usage();
113 }
114
115 argc -= optind;
116 argv += optind;
117
118 fp = stdin;
119 filename = "stdin";
120 rval = 0;
121 do {
122 if (*argv) {
123 if ((fp = fopen(*argv, "r")) == NULL) {
124 warn("%s: %s", *argv, strerror(errno));
125 rval = 1;
126 ++argv;
127 continue;
128 }
129 filename = *argv++;
130 }
131
132 while (fgetws(p, buflen, fp)) {
133
134 len = wcslen(p);
135
136 /* This is my hack from setpwnam.c -janl */
137 while (p[len-1] != '\n' && !feof(fp)) {
138 /* Extend input buffer if it failed getting the whole line */
139
140 /* So now we double the buffer size */
141 buflen *= 2;
142
143 p = realloc(p, buflen*sizeof(wchar_t));
144 if (p == NULL) {
145 fprintf(stderr,_("Unable to allocate bufferspace\n"));
146 exit(1);
147 }
148
149 /* And fill the rest of the buffer */
150 if (fgetws(&p[len], buflen/2, fp) == NULL) break;
151
152 len = wcslen(p);
153
154 /* That was a lot of work for nothing. Gimme perl! */
155 }
156
157 t = p + len - 1 - (*(p+len-1)=='\r' || *(p+len-1)=='\n');
158 for ( ; t >= p; --t)
159 if (*t != 0)
160 putwchar(*t);
161 putwchar('\n');
162 }
163 fflush(fp);
164 if (ferror(fp)) {
165 warn("%s: %s", filename, strerror(errno));
166 rval = 1;
167 }
168 if (fclose(fp))
169 rval = 1;
170 } while(*argv);
171 exit(rval);
172 }
173
174 void
175 warn(const char *fmt, ...)
176 {
177 va_list ap;
178 va_start(ap, fmt);
179 (void)fprintf(stderr, "rev: ");
180 (void)vfprintf(stderr, fmt, ap);
181 va_end(ap);
182 (void)fprintf(stderr, "\n");
183 }
184
185 void
186 usage(void)
187 {
188 (void)fprintf(stderr, _("usage: rev [file ...]\n"));
189 exit(1);
190 }
This page took 0.041877 seconds and 5 git commands to generate.