From: Paul Eggert Date: Thu, 7 Nov 2024 23:04:50 +0000 (-0800) Subject: fold: prefer xpalloc to x2realloc X-Git-Tag: v9.6~101 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=901ba87ed54b8a733cf6f7bc744eb7143200f20b;p=thirdparty%2Fcoreutils.git fold: prefer xpalloc to x2realloc * src/fold.c (fold_file): Prefer idx_t to size_t for sizes. Use xpalloc, not x2realloc. --- diff --git a/src/fold.c b/src/fold.c index 7231e87742..545a1e9b5d 100644 --- a/src/fold.c +++ b/src/fold.c @@ -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);