]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - text-utils/rev.c
Imported from util-linux-2.11o tarball.
[thirdparty/util-linux.git] / text-utils / rev.c
index dbc25cd8020185daac05dc27024b204fe39e14aa..e3ac017a0ad339bea09b32e28a97a62b6817271f 100644 (file)
  *                           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;
@@ -69,7 +73,7 @@ main(argc, argv)
   bindtextdomain(PACKAGE, LOCALEDIR);
   textdomain(PACKAGE);
 
-  while ((ch = getopt(argc, argv, "")) != EOF)
+  while ((ch = getopt(argc, argv, "")) != -1)
     switch(ch) {
     case '?':
     default:
@@ -93,9 +97,9 @@ main(argc, argv)
       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)) {
@@ -104,57 +108,42 @@ main(argc, argv)
        /* 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);
@@ -162,7 +151,7 @@ warn(fmt, va_alist)
 }
 
 void
-usage()
+usage(void)
 {
        (void)fprintf(stderr, _("usage: rev [file ...]\n"));
        exit(1);