* last line that has no newline correctly.
* 3-Jun-1998: Patched by Nicolai Langfeldt to work better on Linux:
* Handle any-length-lines. Code copied from util-linux' setpwnam.c
- * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
- * - added Native Language Support
+ * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
+ * added Native Language Support
+ * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
+ * modified to work correctly in multi-byte locales
*
*/
+#include <stdarg.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include "nls.h"
-void usage __P((void));
-void warn __P((const char *, ...));
+#include "widechar.h"
+
+void usage(void);
+void warn(const char *, ...);
int
-main(argc, argv)
- int argc;
- char *argv[];
+main(int argc, char *argv[])
{
- register char *filename, *t;
- size_t buflen=512;
- char *p=malloc(buflen);
+ register char *filename;
+ register wchar_t *t;
+ size_t buflen = 512;
+ wchar_t *p = malloc(buflen*sizeof(wchar_t));
size_t len;
FILE *fp;
int ch, rval;
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
- while ((ch = getopt(argc, argv, "")) != EOF)
+ while ((ch = getopt(argc, argv, "")) != -1)
switch(ch) {
case '?':
default:
filename = *argv++;
}
- while (fgets(p, buflen, fp)) {
+ while (fgetws(p, buflen, fp)) {
- len = strlen(p);
+ len = wcslen(p);
/* This is my hack from setpwnam.c -janl */
while (p[len-1] != '\n' && !feof(fp)) {
/* So now we double the buffer size */
buflen *= 2;
- p = realloc(p, buflen);
+ p = realloc(p, buflen*sizeof(wchar_t));
if (p == NULL) {
fprintf(stderr,_("Unable to allocate bufferspace\n"));
exit(1);
}
/* And fill the rest of the buffer */
- if (fgets(&p[len], buflen/2, fp) == NULL) break;
+ if (fgetws(&p[len], buflen/2, fp) == NULL) break;
- len = strlen(p);
+ len = wcslen(p);
/* That was a lot of work for nothing. Gimme perl! */
}
- t = p + len - 1 - (*(p+len-1)=='\r'
- || *(p+len-1)=='\n');
+ t = p + len - 1 - (*(p+len-1)=='\r' || *(p+len-1)=='\n');
for ( ; t >= p; --t)
- if(strcmp(t, "\0") != 0)
- putchar(*t);
- putchar('\n');
+ if (*t != 0)
+ putwchar(*t);
+ putwchar('\n');
}
+ fflush(fp);
if (ferror(fp)) {
warn("%s: %s", filename, strerror(errno));
rval = 1;
}
- (void)fclose(fp);
+ if (fclose(fp))
+ rval = 1;
} while(*argv);
exit(rval);
}
-#if __STDC__
-#include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
-
void
-#if __STDC__
warn(const char *fmt, ...)
-#else
-warn(fmt, va_alist)
- char *fmt;
- va_dcl
-#endif
{
va_list ap;
-#if __STDC__
va_start(ap, fmt);
-#else
- va_start(ap);
-#endif
(void)fprintf(stderr, "rev: ");
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
}
void
-usage()
+usage(void)
{
(void)fprintf(stderr, _("usage: rev [file ...]\n"));
exit(1);