]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
fold: prefer xpalloc to x2realloc
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 7 Nov 2024 23:04:50 +0000 (15:04 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 9 Nov 2024 07:41:18 +0000 (23:41 -0800)
* src/fold.c (fold_file): Prefer idx_t to size_t for sizes.
Use xpalloc, not x2realloc.

src/fold.c

index 7231e877420abf987bfab9ebbf43652b5480a8fd..545a1e9b5d072671aed7d181b93b0b841fe8ff9f 100644 (file)
@@ -121,9 +121,9 @@ fold_file (char const *filename, size_t width)
   FILE *istream;
   int c;
   size_t column = 0;           /* Screen column where next char will go. */
-  size_t offset_out = 0;       /* Index in 'line_out' for next char. */
+  idx_t offset_out = 0;                /* Index in 'line_out' for next char. */
   static char *line_out = nullptr;
-  static size_t allocated_out = 0;
+  static idx_t allocated_out = 0;
   int saved_errno;
 
   if (STREQ (filename, "-"))
@@ -144,8 +144,8 @@ fold_file (char const *filename, size_t width)
 
   while ((c = getc (istream)) != EOF)
     {
-      if (offset_out + 1 >= allocated_out)
-        line_out = X2REALLOC (line_out, &allocated_out);
+      if (allocated_out - offset_out <= 1)
+        line_out = xpalloc (line_out, &allocated_out, 1, -1, sizeof *line_out);
 
       if (c == '\n')
         {
@@ -166,7 +166,7 @@ fold_file (char const *filename, size_t width)
           if (break_spaces)
             {
               bool found_blank = false;
-              size_t logical_end = offset_out;
+              idx_t logical_end = offset_out;
 
               /* Look for the last blank. */
               while (logical_end)
@@ -181,19 +181,17 @@ fold_file (char const *filename, size_t width)
 
               if (found_blank)
                 {
-                  size_t i;
-
                   /* Found a blank.  Don't output the part after it. */
                   logical_end++;
-                  fwrite (line_out, sizeof (char), (size_t) logical_end,
-                          stdout);
+                  fwrite (line_out, sizeof (char), logical_end, stdout);
                   putchar ('\n');
                   /* Move the remainder to the beginning of the next line.
                      The areas being copied here might overlap. */
                   memmove (line_out, line_out + logical_end,
                            offset_out - logical_end);
                   offset_out -= logical_end;
-                  for (column = i = 0; i < offset_out; i++)
+                  column = 0;
+                  for (idx_t i = 0; i < offset_out; i++)
                     column = adjust_column (column, line_out[i]);
                   goto rescan;
                 }
@@ -206,7 +204,7 @@ fold_file (char const *filename, size_t width)
             }
 
           line_out[offset_out++] = '\n';
-          fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
+          fwrite (line_out, sizeof (char), offset_out, stdout);
           column = offset_out = 0;
           goto rescan;
         }
@@ -219,7 +217,7 @@ fold_file (char const *filename, size_t width)
     saved_errno = 0;
 
   if (offset_out)
-    fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
+    fwrite (line_out, sizeof (char), offset_out, stdout);
 
   if (STREQ (filename, "-"))
     clearerr (istream);