diff --git a/doc/NEWS.md b/doc/NEWS.md index ce0c6c1..daa758c 100644 --- a/doc/NEWS.md +++ b/doc/NEWS.md @@ -9,6 +9,7 @@ * cleanup: skip test 10 (for *SIGPIPE*) if GNU "`head`" is not available, so "`make test`" on stock OpenBSD 7.3 works * cleanup: replace all calls to `sprintf()` and `snprintf()` with a new wrapper function `pv_snprintf()` to centralise compatibility changes * cleanup: tidy up and fix compilation warning in "`--watchfd`" code + * cleanup: replace all `write()` calls to the terminal with a wrapper `pv_write_retry()` for consistency 1.7.0 - 17 July 2023 * dropped: support for Red Hat Enterprise Linux and its derivatives has been dropped; removed the RPM spec file, and will no longer build binaries diff --git a/src/include/pv-internal.h b/src/include/pv-internal.h index a7ad449..c36d072 100644 --- a/src/include/pv-internal.h +++ b/src/include/pv-internal.h @@ -9,6 +9,7 @@ #ifndef _PV_INTERNAL_H #define _PV_INTERNAL_H 1 +#include #include #include #include @@ -249,9 +250,11 @@ long pv_transfer(pvstate_t, int, int *, int *, unsigned long long, long *); void pv_set_buffer_size(unsigned long long, int); int pv_next_file(pvstate_t, int, int); +void pv_write_retry(int, const char *, size_t); + void pv_crs_fini(pvstate_t); void pv_crs_init(pvstate_t); -void pv_crs_update(pvstate_t, char *); +void pv_crs_update(pvstate_t, const char *); #ifdef HAVE_IPC void pv_crs_needreinit(pvstate_t); #endif diff --git a/src/pv/cursor.c b/src/pv/cursor.c index 4f99a54..4d6973c 100644 --- a/src/pv/cursor.c +++ b/src/pv/cursor.c @@ -45,7 +45,7 @@ * Write the given buffer to the given file descriptor, retrying until all * bytes have been written or an error has occurred. */ -static void write_retry(int fd, const char *buf, size_t count) +void pv_write_retry(int fd, const char *buf, size_t count) { while (count > 0) { ssize_t nwritten; @@ -251,7 +251,7 @@ static int pv_crs_get_ypos(int terminalfd) debug("%s: %s", "tcsetattr (1) failed", strerror(errno)); } - write_retry(terminalfd, "\033[6n", 4); + pv_write_retry(terminalfd, "\033[6n", 4); memset(cpr, 0, sizeof(cpr)); @@ -436,7 +436,7 @@ void pv_crs_init(pvstate_t state) * initial ypos. */ if (state->crs_y_start > 0) - write_retry(STDERR_FILENO, "\n", 1); + pv_write_retry(STDERR_FILENO, "\n", 1); pv_crs_unlock(state, fd); if (state->crs_y_start < 1) @@ -497,7 +497,7 @@ void pv_crs_reinit(pvstate_t state) * Output a single-line update, moving the cursor to the correct position to * do so. */ -void pv_crs_update(pvstate_t state, char *str) +void pv_crs_update(pvstate_t state, const char *str) { char pos[32]; int y; @@ -552,9 +552,9 @@ void pv_crs_update(pvstate_t state, char *str) memset(pos, 0, sizeof(pos)); (void) pv_snprintf(pos, sizeof(pos), "\033[%u;1H", state->height); - write_retry(STDERR_FILENO, pos, strlen(pos)); + pv_write_retry(STDERR_FILENO, pos, strlen(pos)); for (; offs > 0; offs--) { - write_retry(STDERR_FILENO, "\n", 1); + pv_write_retry(STDERR_FILENO, "\n", 1); } pv_crs_unlock(state, STDERR_FILENO); @@ -579,8 +579,8 @@ void pv_crs_update(pvstate_t state, char *str) pv_crs_lock(state, STDERR_FILENO); - write_retry(STDERR_FILENO, pos, strlen(pos)); - write_retry(STDERR_FILENO, str, strlen(str)); + pv_write_retry(STDERR_FILENO, pos, strlen(pos)); + pv_write_retry(STDERR_FILENO, str, strlen(str)); pv_crs_unlock(state, STDERR_FILENO); } @@ -617,7 +617,7 @@ void pv_crs_fini(pvstate_t state) pv_crs_lock(state, STDERR_FILENO); - write_retry(STDERR_FILENO, pos, strlen(pos)); + pv_write_retry(STDERR_FILENO, pos, strlen(pos)); #ifdef HAVE_IPC pv_crs_ipccount(state); diff --git a/src/pv/display.c b/src/pv/display.c index d4dbcd9..76f29c0 100644 --- a/src/pv/display.c +++ b/src/pv/display.c @@ -520,9 +520,10 @@ static void update_history_avg_rate(pvstate_t state, long long total_bytes, * If "total_bytes" is negative, then free all allocated memory and return * NULL. */ -static char *pv__format(pvstate_t state, - long double elapsed_sec, - long long bytes_since_last, long long total_bytes) +static const char *pv__format(pvstate_t state, + long double elapsed_sec, + long long bytes_since_last, + long long total_bytes) { long double time_since_last, rate, average_rate; long eta; @@ -672,7 +673,7 @@ static char *pv__format(pvstate_t state, } } else { (void) pv_snprintf(state->display_buffer, - sizeof(state->display_buffer), + state->display_buffer_size, "%.99s%ld\n", numericprefix, state->percentage); } @@ -1094,7 +1095,7 @@ static char *pv__format(pvstate_t state, void pv_display(pvstate_t state, long double esec, long long sl, long long tot) { - char *display; + const char *display; if (NULL == state) return; @@ -1115,7 +1116,7 @@ void pv_display(pvstate_t state, long double esec, long long sl, return; if (state->numeric) { - write(STDERR_FILENO, display, strlen(display)); + pv_write_retry(STDERR_FILENO, display, strlen(display)); } else if (state->cursor) { if (state->force || pv_in_foreground()) { pv_crs_update(state, display); @@ -1123,8 +1124,9 @@ void pv_display(pvstate_t state, long double esec, long long sl, } } else { if (state->force || pv_in_foreground()) { - write(STDERR_FILENO, display, strlen(display)); - write(STDERR_FILENO, "\r", 1); + pv_write_retry(STDERR_FILENO, display, + strlen(display)); + pv_write_retry(STDERR_FILENO, "\r", 1); state->display_visible = true; } } diff --git a/src/pv/loop.c b/src/pv/loop.c index b99a633..1ecbcfa 100644 --- a/src/pv/loop.c +++ b/src/pv/loop.c @@ -334,7 +334,7 @@ int pv_main_loop(pvstate_t state) } else { if ((!state->numeric) && (!state->no_op) && (state->display_visible)) - write(STDERR_FILENO, "\n", 1); + pv_write_retry(STDERR_FILENO, "\n", 1); } if (state->pv_sig_abort) @@ -494,7 +494,7 @@ int pv_watchfd_loop(pvstate_t state) } if (!state->numeric) - write(STDERR_FILENO, "\n", 1); + pv_write_retry(STDERR_FILENO, "\n", 1); if (state->pv_sig_abort) state->exit_status |= 32; @@ -727,7 +727,7 @@ int pv_watchpid_loop(pvstate_t state) if (displayed_lines > 0) { debug("%s", "adding newline"); - write(STDERR_FILENO, "\n", 1); + pv_write_retry(STDERR_FILENO, "\n", 1); } debug("%s %d [%d]: %Lf / %Ld / %Ld", "fd", fd, idx, @@ -751,10 +751,10 @@ int pv_watchpid_loop(pvstate_t state) while (blank_lines > 0) { unsigned int x; if (displayed_lines > 0) - write(STDERR_FILENO, "\n", 1); + pv_write_retry(STDERR_FILENO, "\n", 1); for (x = 0; x < state->width; x++) - write(STDERR_FILENO, " ", 1); - write(STDERR_FILENO, "\r", 1); + pv_write_retry(STDERR_FILENO, " ", 1); + pv_write_retry(STDERR_FILENO, "\r", 1); blank_lines--; displayed_lines++; } @@ -762,7 +762,7 @@ int pv_watchpid_loop(pvstate_t state) debug("%s: %d", "displayed lines", displayed_lines); while (displayed_lines > 1) { - write(STDERR_FILENO, "\033[A", 3); + pv_write_retry(STDERR_FILENO, "\033[A", 3); displayed_lines--; } } @@ -774,14 +774,14 @@ int pv_watchpid_loop(pvstate_t state) while (blank_lines > 0) { unsigned int x; for (x = 0; x < state->width; x++) - write(STDERR_FILENO, " ", 1); - write(STDERR_FILENO, "\r", 1); + pv_write_retry(STDERR_FILENO, " ", 1); + pv_write_retry(STDERR_FILENO, "\r", 1); blank_lines--; if (blank_lines > 0) - write(STDERR_FILENO, "\n", 1); + pv_write_retry(STDERR_FILENO, "\n", 1); } while (prev_displayed_lines > 1) { - write(STDERR_FILENO, "\033[A", 3); + pv_write_retry(STDERR_FILENO, "\033[A", 3); prev_displayed_lines--; }