]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
fold -s didn't work on e.g., alpha-based systems.
authorJim Meyering <jim@meyering.net>
Sat, 27 Dec 2003 09:55:11 +0000 (09:55 +0000)
committerJim Meyering <jim@meyering.net>
Sat, 27 Dec 2003 09:55:11 +0000 (09:55 +0000)
* src/fold.c (fold_file): Adjust types (int->size_t) so that using
x2nrealloc works properly on systems with differing sizes for int
and size_t.  Reported by Nelson Beebe.

* src/fold.c: Use `bool' (not int) as the type for a few
global variables.

src/fold.c

index e8a2f81c4682d3bb616be0000b42557783b95c41..5d3b3d8977b2e3a95f0a54341b2424f1f91e17de 100644 (file)
 char *program_name;
 
 /* If nonzero, try to break on whitespace. */
-static int break_spaces;
+static bool break_spaces;
 
 /* If nonzero, count bytes, not column positions. */
-static int count_bytes;
+static bool count_bytes;
 
 /* If nonzero, at least one of the files we read was standard input. */
-static int have_read_stdin;
+static bool have_read_stdin;
 
 static struct option const longopts[] =
 {
@@ -91,8 +91,8 @@ Mandatory arguments to long options are mandatory for short options too.\n\
    printing C will move the cursor to.
    The first column is 0. */
 
-static int
-adjust_column (int column, char c)
+static size_t
+adjust_column (size_t column, char c)
 {
   if (!count_bytes)
     {
@@ -122,16 +122,16 @@ fold_file (char *filename, int width)
 {
   FILE *istream;
   register int c;
-  int column = 0;              /* Screen column where next char will go. */
-  int offset_out = 0;  /* Index in `line_out' for next char. */
+  size_t column = 0;           /* Screen column where next char will go. */
+  size_t offset_out = 0;       /* Index in `line_out' for next char. */
   static char *line_out = NULL;
-  static int allocated_out = 0;
+  static size_t allocated_out = 0;
   int saved_errno;
 
   if (STREQ (filename, "-"))
     {
       istream = stdin;
-      have_read_stdin = 1;
+      have_read_stdin = true;
     }
   else
     istream = fopen (filename, "r");
@@ -150,7 +150,7 @@ fold_file (char *filename, int width)
       if (c == '\n')
        {
          line_out[offset_out++] = c;
-         fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
+         fwrite (line_out, sizeof (char), offset_out, stdout);
          column = offset_out = 0;
          continue;
        }
@@ -165,16 +165,23 @@ fold_file (char *filename, int width)
             start the next line. */
          if (break_spaces)
            {
+             bool found_blank = false;
+             size_t logical_end = offset_out;
+
              /* Look for the last blank. */
-             int logical_end;
+             while (logical_end)
+               {
+                 --logical_end;
+                 if (ISBLANK (line_out[logical_end]))
+                   {
+                     found_blank = true;
+                     break;
+                   }
+               }
 
-             for (logical_end = offset_out - 1; logical_end >= 0;
-                  logical_end--)
-               if (ISBLANK (line_out[logical_end]))
-                 break;
-             if (logical_end >= 0)
+             if (found_blank)
                {
-                 int i;
+                 size_t i;
 
                  /* Found a blank.  Don't output the part after it. */
                  logical_end++;
@@ -244,7 +251,7 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  break_spaces = count_bytes = have_read_stdin = 0;
+  break_spaces = count_bytes = have_read_stdin = false;
 
   /* Turn any numeric options into -w options.  */
   for (i = 1; i < argc; i++)
@@ -278,11 +285,11 @@ main (int argc, char **argv)
          break;
 
        case 'b':               /* Count bytes rather than columns. */
-         count_bytes = 1;
+         count_bytes = true;
          break;
 
        case 's':               /* Break at word boundaries. */
-         break_spaces = 1;
+         break_spaces = true;
          break;
 
        case 'w':               /* Line width. */