Don't include version.h.
[platform/upstream/coreutils.git] / src / fold.c
index 114924e..747f786 100644 (file)
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
+
+#if HAVE_LIMITS_H
+# include <limits.h>
+#endif
+
+#ifndef UINT_MAX
+# define UINT_MAX ((unsigned int) ~(unsigned int) 0)
+#endif
+
+#ifndef INT_MAX
+# define INT_MAX ((int) (UINT_MAX >> 1))
+#endif
+
 #include "system.h"
-#include "version.h"
+#include "xstrtol.h"
 #include "error.h"
 
 char *xrealloc ();
@@ -44,10 +57,10 @@ static int count_bytes;
 /* If nonzero, at least one of the files we read was standard input. */
 static int have_read_stdin;
 
-/* If non-zero, display usage information and exit.  */
+/* If nonzero, display usage information and exit.  */
 static int show_help;
 
-/* If non-zero, print the version on standard output then exit.  */
+/* If nonzero, print the version on standard output then exit.  */
 static int show_version;
 
 static struct option const longopts[] =
@@ -120,9 +133,9 @@ fold_file (char *filename, int width)
   FILE *istream;
   register int c;
   int column = 0;              /* Screen column where next char will go. */
-  size_t offset_out = 0;       /* Index in `line_out' for next char. */
+  int offset_out = 0;  /* Index in `line_out' for next char. */
   static char *line_out = NULL;
-  static size_t allocated_out = 0;
+  static int allocated_out = 0;
 
   if (!strcmp (filename, "-"))
     {
@@ -149,7 +162,7 @@ fold_file (char *filename, int width)
       if (c == '\n')
        {
          line_out[offset_out++] = c;
-         fwrite (line_out, sizeof (char), offset_out, stdout);
+         fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
          column = offset_out = 0;
          continue;
        }
@@ -177,7 +190,8 @@ fold_file (char *filename, int width)
 
                  /* Found a blank.  Don't output the part after it. */
                  logical_end++;
-                 fwrite (line_out, sizeof (char), logical_end, stdout);
+                 fwrite (line_out, sizeof (char), (size_t) logical_end,
+                         stdout);
                  putchar ('\n');
                  /* Move the remainder to the beginning of the next line.
                     The areas being copied here might overlap. */
@@ -198,7 +212,7 @@ fold_file (char *filename, int width)
                }
            }
          line_out[offset_out++] = '\n';
-         fwrite (line_out, sizeof (char), offset_out, stdout);
+         fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
          column = offset_out = 0;
          goto rescan;
        }
@@ -207,7 +221,7 @@ fold_file (char *filename, int width)
     }
 
   if (offset_out)
-    fwrite (line_out, sizeof (char), offset_out, stdout);
+    fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
 
   if (ferror (istream))
     {
@@ -240,6 +254,10 @@ main (int argc, char **argv)
   int errs = 0;
 
   program_name = argv[0];
+  setlocale (LC_ALL, "");
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
+
   break_spaces = count_bytes = have_read_stdin = 0;
 
   /* Turn any numeric options into -w options.  */
@@ -274,9 +292,13 @@ main (int argc, char **argv)
          break;
 
        case 'w':               /* Line width. */
-         width = atoi (optarg);
-         if (width < 1)
-           error (1, 0, _("%s: invalid line width"), optarg);
+         {
+           long int tmp_long;
+           if (xstrtol (optarg, NULL, 10, &tmp_long, NULL) != LONGINT_OK
+               || tmp_long <= 0 || tmp_long > INT_MAX)
+             error (1, 0, _("invalid number of columns: `%s'"), optarg);
+           width = (int) tmp_long;
+         }
          break;
 
        default:
@@ -286,7 +308,7 @@ main (int argc, char **argv)
 
   if (show_version)
     {
-      printf ("fold - %s\n", version_string);
+      printf ("fold - %s\n", PACKAGE_VERSION);
       exit (0);
     }