From 586dfa995a7a3a65b629877ad58f7480805eff83 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 10 Aug 2024 19:30:46 -0700 Subject: [PATCH] maint: adjust to Gnulib safe_read etc. changes MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Although these patches don’t affect user-visible behavior, they do clean up the source code a bit, and the machine code should be a tiny bit more efficient. * src/cat.c (simple_cat, cat): * src/csplit.c (read_input): * src/head.c (copy_fd, elide_tail_bytes_pipe) (elide_tail_lines_pipe, elide_tail_lines_seekable, head_bytes) (head_lines): * src/install.c (have_same_content): * src/tac-pipe.c (buf_init_from_stdin): * src/tac.c (tac_seekable, copy_to_temp): * src/tail.c (dump_remainder, file_lines, pipe_lines) (pipe_bytes, start_bytes, start_lines, tail_forever_inotify): * src/tr.c (plain_read): Adjust to recent Gnulib changes by using new types for safe_read, safe_write, full_read, full_write. --- src/cat.c | 8 ++++---- src/csplit.c | 6 ++---- src/head.c | 38 ++++++++++++++++++-------------------- src/install.c | 2 +- src/tac-pipe.c | 7 +++---- src/tac.c | 12 ++++++------ src/tail.c | 41 ++++++++++++++++++++--------------------- src/tr.c | 4 ++-- 8 files changed, 56 insertions(+), 62 deletions(-) diff --git a/src/cat.c b/src/cat.c index b33faeb35e..b7aa853af3 100644 --- a/src/cat.c +++ b/src/cat.c @@ -161,8 +161,8 @@ simple_cat (char *buf, idx_t bufsize) { /* Read a block of input. */ - size_t n_read = safe_read (input_desc, buf, bufsize); - if (n_read == SAFE_READ_ERROR) + ptrdiff_t n_read = safe_read (input_desc, buf, bufsize); + if (n_read < 0) { error (0, errno, "%s", quotef (infile)); return false; @@ -310,8 +310,8 @@ cat (char *inbuf, idx_t insize, char *outbuf, idx_t outsize, /* Read more input into INBUF. */ - size_t n_read = safe_read (input_desc, inbuf, insize); - if (n_read == SAFE_READ_ERROR) + ptrdiff_t n_read = safe_read (input_desc, inbuf, insize); + if (n_read < 0) { error (0, errno, "%s", quotef (infile)); write_pending (outbuf, &bpout); diff --git a/src/csplit.c b/src/csplit.c index acde48bbdb..51bb38549a 100644 --- a/src/csplit.c +++ b/src/csplit.c @@ -255,17 +255,15 @@ save_to_hold_area (char *start, idx_t num) static idx_t read_input (char *dest, idx_t max_n_bytes) { - idx_t bytes_read; - if (max_n_bytes == 0) return 0; - bytes_read = safe_read (STDIN_FILENO, dest, max_n_bytes); + ptrdiff_t bytes_read = safe_read (STDIN_FILENO, dest, max_n_bytes); if (bytes_read == 0) have_read_eof = true; - if (bytes_read == SAFE_READ_ERROR) + if (bytes_read < 0) { error (0, errno, _("read error")); cleanup_fatal (); diff --git a/src/head.c b/src/head.c index 9715b7b73b..14f686e5f6 100644 --- a/src/head.c +++ b/src/head.c @@ -194,14 +194,13 @@ static enum Copy_fd_status copy_fd (int src_fd, uintmax_t n_bytes) { char buf[BUFSIZ]; - const size_t buf_size = sizeof (buf); /* Copy the file contents. */ while (0 < n_bytes) { - size_t n_to_read = MIN (buf_size, n_bytes); - size_t n_read = safe_read (src_fd, buf, n_to_read); - if (n_read == SAFE_READ_ERROR) + idx_t n_to_read = MIN (n_bytes, sizeof buf); + ptrdiff_t n_read = safe_read (src_fd, buf, n_to_read); + if (n_read < 0) return COPY_FD_READ_ERROR; n_bytes -= n_read; @@ -285,7 +284,7 @@ elide_tail_bytes_pipe (char const *filename, int fd, uintmax_t n_elide, idx_t in_elide = n_elide; bool first = true; bool eof = false; - size_t n_to_read = READ_BUFSIZE + in_elide; + idx_t n_to_read = READ_BUFSIZE + n_elide; bool i; char *b[2]; b[0] = xnmalloc (2, n_to_read); @@ -293,8 +292,8 @@ elide_tail_bytes_pipe (char const *filename, int fd, uintmax_t n_elide, for (i = false; ! eof ; i = !i) { - size_t n_read = full_read (fd, b[i], n_to_read); - size_t delta = 0; + idx_t n_read = full_read (fd, b[i], n_to_read); + idx_t delta = 0; if (n_read < n_to_read) { if (errno != 0) @@ -496,7 +495,7 @@ elide_tail_lines_pipe (char const *filename, int fd, uintmax_t n_elide, LBUFFER *first, *last, *tmp; size_t total_lines = 0; /* Total number of newlines in all buffers. */ bool ok = true; - size_t n_read; /* Size in bytes of most recent read */ + ptrdiff_t n_read; /* Size in bytes of most recent read */ first = last = xmalloc (sizeof (LBUFFER)); first->nbytes = first->nlines = 0; @@ -509,7 +508,7 @@ elide_tail_lines_pipe (char const *filename, int fd, uintmax_t n_elide, while (true) { n_read = safe_read (fd, tmp->buffer, BUFSIZ); - if (n_read == 0 || n_read == SAFE_READ_ERROR) + if (n_read <= 0) break; if (! n_elide) @@ -568,7 +567,7 @@ elide_tail_lines_pipe (char const *filename, int fd, uintmax_t n_elide, free (tmp); - if (n_read == SAFE_READ_ERROR) + if (n_read < 0) { error (0, errno, _("error reading %s"), quoteaf (filename)); ok = false; @@ -636,7 +635,7 @@ elide_tail_lines_seekable (char const *pretty_filename, int fd, off_t start_pos, off_t size) { char buffer[BUFSIZ]; - size_t bytes_read; + ptrdiff_t bytes_read; off_t pos = size; /* Set 'bytes_read' to the size of the last, probably partial, buffer; @@ -650,7 +649,7 @@ elide_tail_lines_seekable (char const *pretty_filename, int fd, if (elseek (fd, pos, SEEK_SET, pretty_filename) < 0) return false; bytes_read = safe_read (fd, buffer, bytes_read); - if (bytes_read == SAFE_READ_ERROR) + if (bytes_read < 0) { error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); return false; @@ -667,7 +666,7 @@ elide_tail_lines_seekable (char const *pretty_filename, int fd, { /* Scan backward, counting the newlines in this bufferfull. */ - size_t n = bytes_read; + idx_t n = bytes_read; while (n) { if (all_lines) @@ -719,7 +718,7 @@ elide_tail_lines_seekable (char const *pretty_filename, int fd, return false; bytes_read = safe_read (fd, buffer, BUFSIZ); - if (bytes_read == SAFE_READ_ERROR) + if (bytes_read < 0) { error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); return false; @@ -765,11 +764,10 @@ head_bytes (char const *filename, int fd, uintmax_t bytes_to_write) while (bytes_to_write) { - size_t bytes_read; if (bytes_to_write < bytes_to_read) bytes_to_read = bytes_to_write; - bytes_read = safe_read (fd, buffer, bytes_to_read); - if (bytes_read == SAFE_READ_ERROR) + ptrdiff_t bytes_read = safe_read (fd, buffer, bytes_to_read); + if (bytes_read < 0) { error (0, errno, _("error reading %s"), quoteaf (filename)); return false; @@ -789,10 +787,10 @@ head_lines (char const *filename, int fd, uintmax_t lines_to_write) while (lines_to_write) { - size_t bytes_read = safe_read (fd, buffer, BUFSIZ); - size_t bytes_to_write = 0; + ptrdiff_t bytes_read = safe_read (fd, buffer, BUFSIZ); + idx_t bytes_to_write = 0; - if (bytes_read == SAFE_READ_ERROR) + if (bytes_read < 0) { error (0, errno, _("error reading %s"), quoteaf (filename)); return false; diff --git a/src/install.c b/src/install.c index accd0fd4e5..939db9cd41 100644 --- a/src/install.c +++ b/src/install.c @@ -142,7 +142,7 @@ have_same_content (int a_fd, int b_fd) static char a_buff[CMP_BLOCK_SIZE]; static char b_buff[CMP_BLOCK_SIZE]; - size_t size; + idx_t size; while (0 < (size = full_read (a_fd, a_buff, sizeof a_buff))) { if (size != full_read (b_fd, b_buff, sizeof b_buff)) return false; diff --git a/src/tac-pipe.c b/src/tac-pipe.c index ac05080719..d36b785fbb 100644 --- a/src/tac-pipe.c +++ b/src/tac-pipe.c @@ -58,8 +58,7 @@ buf_init_from_stdin (Buf *x, char eol_byte) while (true) { - char *buf = (char *) malloc (BUFFER_SIZE); - size_t bytes_read; + char *buf = malloc (BUFFER_SIZE); if (buf == nullptr) { @@ -69,8 +68,8 @@ buf_init_from_stdin (Buf *x, char eol_byte) ok = false; break; } - bytes_read = full_read (STDIN_FILENO, buf, BUFFER_SIZE); - if (bytes_read != buffer_size && errno != 0) + idx_t bytes_read = full_read (STDIN_FILENO, buf, BUFFER_SIZE); + if (bytes_read != BUFFER_SIZE && errno != 0) error (EXIT_FAILURE, errno, _("read error")); { diff --git a/src/tac.c b/src/tac.c index 44ddaf85bc..e8d0dbe4ab 100644 --- a/src/tac.c +++ b/src/tac.c @@ -188,7 +188,7 @@ tac_seekable (int input_fd, char const *file, off_t file_pos) char *past_end; /* Length of the record growing in 'G_buffer'. */ - size_t saved_record_size; + ptrdiff_t saved_record_size; /* True if 'output' has not been called yet for any file. Only used when the separator is attached to the preceding record. */ @@ -224,16 +224,16 @@ tac_seekable (int input_fd, char const *file, off_t file_pos) /* Now scan forward, looking for end of file. */ while (saved_record_size == read_size) { - size_t nread = safe_read (input_fd, G_buffer, read_size); + ptrdiff_t nread = safe_read (input_fd, G_buffer, read_size); if (nread == 0) break; saved_record_size = nread; - if (saved_record_size == SAFE_READ_ERROR) + if (saved_record_size < 0) break; file_pos += nread; } - if (saved_record_size == SAFE_READ_ERROR) + if (saved_record_size < 0) { error (0, errno, _("%s: read error"), quotef (file)); return false; @@ -385,10 +385,10 @@ copy_to_temp (FILE **g_tmp, char **g_tempfile, int input_fd, char const *file) while (true) { - size_t bytes_read = safe_read (input_fd, G_buffer, read_size); + ptrdiff_t bytes_read = safe_read (input_fd, G_buffer, read_size); if (bytes_read == 0) break; - if (bytes_read == SAFE_READ_ERROR) + if (bytes_read < 0) { error (0, errno, _("%s: read error"), quotef (file)); return -1; diff --git a/src/tail.c b/src/tail.c index df13b2c90f..33acf9ab31 100644 --- a/src/tail.c +++ b/src/tail.c @@ -443,9 +443,9 @@ dump_remainder (bool want_header, char const *pretty_filename, int fd, while (true) { char buffer[BUFSIZ]; - size_t n = MIN (n_remaining, BUFSIZ); - size_t bytes_read = safe_read (fd, buffer, n); - if (bytes_read == SAFE_READ_ERROR) + idx_t n = MIN (n_remaining, BUFSIZ); + ptrdiff_t bytes_read = safe_read (fd, buffer, n); + if (bytes_read < 0) { if (errno != EAGAIN) error (EXIT_FAILURE, errno, _("error reading %s"), @@ -520,7 +520,6 @@ file_lines (char const *pretty_filename, int fd, struct stat const *sb, uintmax_t *read_pos) { char *buffer; - size_t bytes_read; blksize_t bufsize = BUFSIZ; off_t pos = end_pos; bool ok = true; @@ -545,7 +544,7 @@ file_lines (char const *pretty_filename, int fd, struct stat const *sb, /* Set 'bytes_read' to the size of the last, probably partial, buffer; 0 < 'bytes_read' <= 'bufsize'. */ - bytes_read = (pos - start_pos) % bufsize; + ptrdiff_t bytes_read = (pos - start_pos) % bufsize; if (bytes_read == 0) bytes_read = bufsize; /* Make 'pos' a multiple of 'bufsize' (0 if the file is short), so that all @@ -553,7 +552,7 @@ file_lines (char const *pretty_filename, int fd, struct stat const *sb, pos -= bytes_read; xlseek (fd, pos, SEEK_SET, pretty_filename); bytes_read = safe_read (fd, buffer, bytes_read); - if (bytes_read == SAFE_READ_ERROR) + if (bytes_read < 0) { error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); ok = false; @@ -569,7 +568,7 @@ file_lines (char const *pretty_filename, int fd, struct stat const *sb, { /* Scan backward, counting the newlines in this bufferfull. */ - size_t n = bytes_read; + idx_t n = bytes_read; while (n) { char const *nl; @@ -602,7 +601,7 @@ file_lines (char const *pretty_filename, int fd, struct stat const *sb, xlseek (fd, pos, SEEK_SET, pretty_filename); bytes_read = safe_read (fd, buffer, bufsize); - if (bytes_read == SAFE_READ_ERROR) + if (bytes_read < 0) { error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); ok = false; @@ -638,7 +637,7 @@ pipe_lines (char const *pretty_filename, int fd, uintmax_t n_lines, LBUFFER *first, *last, *tmp; size_t total_lines = 0; /* Total number of newlines in all buffers. */ bool ok = true; - size_t n_read; /* Size in bytes of most recent read */ + ptrdiff_t n_read; /* Size in bytes of most recent read */ first = last = xmalloc (sizeof (LBUFFER)); first->nbytes = first->nlines = 0; @@ -649,7 +648,7 @@ pipe_lines (char const *pretty_filename, int fd, uintmax_t n_lines, while (true) { n_read = safe_read (fd, tmp->buffer, BUFSIZ); - if (n_read == 0 || n_read == SAFE_READ_ERROR) + if (n_read <= 0) break; tmp->nbytes = n_read; *read_pos += n_read; @@ -698,7 +697,7 @@ pipe_lines (char const *pretty_filename, int fd, uintmax_t n_lines, free (tmp); - if (n_read == SAFE_READ_ERROR) + if (n_read < 0) { error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); ok = false; @@ -777,7 +776,7 @@ pipe_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes, size_t i; /* Index into buffers. */ size_t total_bytes = 0; /* Total characters in all buffers. */ bool ok = true; - size_t n_read; + ptrdiff_t n_read; first = last = xmalloc (sizeof (CBUFFER)); first->nbytes = 0; @@ -788,7 +787,7 @@ pipe_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes, while (true) { n_read = safe_read (fd, tmp->buffer, BUFSIZ); - if (n_read == 0 || n_read == SAFE_READ_ERROR) + if (n_read <= 0) break; *read_pos += n_read; tmp->nbytes = n_read; @@ -826,7 +825,7 @@ pipe_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes, free (tmp); - if (n_read == SAFE_READ_ERROR) + if (n_read < 0) { error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); ok = false; @@ -871,10 +870,10 @@ start_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes, while (0 < n_bytes) { - size_t bytes_read = safe_read (fd, buffer, BUFSIZ); + ptrdiff_t bytes_read = safe_read (fd, buffer, BUFSIZ); if (bytes_read == 0) return -1; - if (bytes_read == SAFE_READ_ERROR) + if (bytes_read < 0) { error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); return 1; @@ -908,10 +907,10 @@ start_lines (char const *pretty_filename, int fd, uintmax_t n_lines, while (true) { char buffer[BUFSIZ]; - size_t bytes_read = safe_read (fd, buffer, BUFSIZ); + ptrdiff_t bytes_read = safe_read (fd, buffer, BUFSIZ); if (bytes_read == 0) /* EOF */ return -1; - if (bytes_read == SAFE_READ_ERROR) /* error */ + if (bytes_read < 0) /* error */ { error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); return 1; @@ -1490,7 +1489,6 @@ tail_forever_inotify (int wd, struct File_spec *f, size_t n_files, size_t evlen = 0; char *evbuf; size_t evbuf_off = 0; - size_t len = 0; wd_to_name = hash_initialize (n_files, nullptr, wd_hasher, wd_comparator, nullptr); @@ -1625,6 +1623,7 @@ tail_forever_inotify (int wd, struct File_spec *f, size_t n_files, ensure that watched files can be re-added when following by name. This loop blocks on the 'safe_read' call until a new event is notified. But when --pid=P is specified, tail usually waits via poll. */ + ptrdiff_t len = 0; while (true) { struct File_spec *fspec; @@ -1688,7 +1687,7 @@ tail_forever_inotify (int wd, struct File_spec *f, size_t n_files, /* For kernels prior to 2.6.21, read returns 0 when the buffer is too small. */ - if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL)) + if ((len == 0 || (len < 0 && errno == EINVAL)) && max_realloc--) { len = 0; @@ -1697,7 +1696,7 @@ tail_forever_inotify (int wd, struct File_spec *f, size_t n_files, continue; } - if (len == 0 || len == SAFE_READ_ERROR) + if (len <= 0) error (EXIT_FAILURE, errno, _("error reading inotify event")); } diff --git a/src/tr.c b/src/tr.c index 11498ba801..94869169ce 100644 --- a/src/tr.c +++ b/src/tr.c @@ -1594,8 +1594,8 @@ squeeze_filter (char *buf, size_t size, size_t (*reader) (char *, size_t)) static size_t plain_read (char *buf, size_t size) { - size_t nr = safe_read (STDIN_FILENO, buf, size); - if (nr == SAFE_READ_ERROR) + ptrdiff_t nr = safe_read (STDIN_FILENO, buf, size); + if (nr < 0) error (EXIT_FAILURE, errno, _("read error")); return nr; } -- 2.47.2