From 8a2ddac50e5c60815cb67405d3998cb2977b72e8 Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Tue, 12 Sep 2023 23:42:23 +0100 Subject: [PATCH] Type consistency corrections - use booleans for boolean operations, use size_t for byte counts. --- src/include/options.h | 14 ++++++++---- src/include/pv-internal.h | 33 +++++++++++++-------------- src/include/pv.h | 14 ++++++------ src/main/options.c | 10 ++++----- src/main/remote.c | 6 ++--- src/pv/display.c | 2 +- src/pv/file.c | 18 +++++++-------- src/pv/loop.c | 18 +++++++-------- src/pv/number.c | 18 +++++++-------- src/pv/state.c | 8 +++---- src/pv/transfer.c | 47 ++++++++++++++++++++------------------- 11 files changed, 97 insertions(+), 91 deletions(-) diff --git a/src/include/options.h b/src/include/options.h index b277d1a..64bee3c 100644 --- a/src/include/options.h +++ b/src/include/options.h @@ -9,6 +9,12 @@ #ifndef _OPTIONS_H #define _OPTIONS_H 1 +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include + #ifdef __cplusplus extern "C" { #endif @@ -36,13 +42,13 @@ struct opts_s { /* structure describing run-time options */ bool linemode; /* count lines instead of bytes */ bool null_terminated_lines; /* lines are null-terminated */ bool no_display; /* do nothing other than pipe data */ - unsigned long long rate_limit; /* rate limit, in bytes per second */ - unsigned long long buffer_size;/* buffer size, in bytes (0=default) */ + size_t rate_limit; /* rate limit, in bytes per second */ + size_t buffer_size; /* buffer size, in bytes (0=default) */ unsigned int remote; /* PID of pv to update settings of */ - unsigned long long size; /* total size of data */ + size_t size; /* total size of data */ bool no_splice; /* flag set if never to use splice */ unsigned int skip_errors; /* skip read errors counter */ - unsigned long long error_skip_block; /* skip block size, 0 for adaptive */ + size_t error_skip_block; /* skip block size, 0 for adaptive */ bool stop_at_size; /* set if we stop at "size" bytes */ bool sync_after_write; /* set if we sync after every write */ bool direct_io; /* set if O_DIRECT is to be used */ diff --git a/src/include/pv-internal.h b/src/include/pv-internal.h index 59fcd46..3bf141f 100644 --- a/src/include/pv-internal.h +++ b/src/include/pv-internal.h @@ -45,7 +45,7 @@ extern "C" { typedef struct pvhistory { - long long total_bytes; + size_t total_bytes; long double elapsed_sec; } pvhistory_t; @@ -102,16 +102,16 @@ struct pvstate_s { bool null_terminated_lines; /* lines are null-terminated */ bool no_display; /* do nothing other than pipe data */ unsigned int skip_errors; /* skip read errors counter */ - unsigned long long error_skip_block; /* skip block size, 0 for adaptive */ + size_t error_skip_block; /* skip block size, 0 for adaptive */ bool stop_at_size; /* set if we stop at "size" bytes */ bool sync_after_write; /* set if we sync after every write */ bool direct_io; /* set if O_DIRECT is to be used */ bool direct_io_changed; /* set when direct_io is changed */ bool no_splice; /* never use splice() */ bool discard_input; /* write nothing to stdout */ - unsigned long long rate_limit; /* rate limit, in bytes per second */ - unsigned long long target_buffer_size; /* buffer size (0=default) */ - unsigned long long size; /* total size of data */ + size_t rate_limit; /* rate limit, in bytes per second */ + size_t target_buffer_size; /* buffer size (0=default) */ + size_t size; /* total size of data */ double interval; /* interval between updates */ double delay_start; /* delay before first display */ unsigned int watch_pid; /* process to watch fds of */ @@ -167,7 +167,7 @@ struct pvstate_s { int history_last; long double current_avg_rate; /* current average rate over last history intervals */ - unsigned long long initial_offset; + size_t initial_offset; /*@only@*/ char *display_buffer; long display_buffer_size; int lastoutput_length; /* number of last-output bytes to show */ @@ -227,9 +227,9 @@ struct pvstate_s { * will always be less than or equal to read_position. */ unsigned char *transfer_buffer; /* data transfer buffer */ - unsigned long long buffer_size; /* size of buffer */ - unsigned long read_position; /* amount of data in buffer */ - unsigned long write_position; /* buffered data written */ + size_t buffer_size; /* size of buffer */ + size_t read_position; /* amount of data in buffer */ + size_t write_position; /* buffered data written */ /* * While reading from a file descriptor we keep track of how many @@ -245,7 +245,7 @@ struct pvstate_s { * This way, we're treating each input file separately. */ int last_read_skip_fd; - unsigned long read_errors_in_a_row; + size_t read_errors_in_a_row; int read_error_warning_shown; #ifdef HAVE_SPLICE /* @@ -258,8 +258,8 @@ struct pvstate_s { int splice_failed_fd; int splice_used; #endif - long to_write; /* max to write this time around */ - long written; /* bytes sent to stdout this time */ + ssize_t to_write; /* max to write this time around */ + ssize_t written; /* bytes sent to stdout this time */ }; @@ -275,8 +275,8 @@ struct pvwatchfd_s { char display_name[PV_SIZEOF_DISPLAY_NAME]; /* name to show on progress bar */ struct stat sb_fd; /* stat of fd symlink */ struct stat sb_fd_link; /* lstat of fd symlink */ - unsigned long long size; /* size of whole file, 0 if unknown */ - long long position; /* position last seen at */ + size_t size; /* size of whole file, 0 if unknown */ + ssize_t position; /* position last seen at */ struct timespec start_time; /* time we started watching the fd */ }; typedef struct pvwatchfd_s *pvwatchfd_t; @@ -284,9 +284,8 @@ typedef struct pvwatchfd_s *pvwatchfd_t; void pv_error(pvstate_t, char *, ...); int pv_main_loop(pvstate_t); -void pv_display(pvstate_t, long double, long long, long long); -long pv_transfer(pvstate_t, int, int *, int *, unsigned long long, long *); -void pv_set_buffer_size(unsigned long long, int); +void pv_display(pvstate_t, long double, ssize_t, ssize_t); +ssize_t pv_transfer(pvstate_t, int, bool *, bool *, size_t, long *); int pv_next_file(pvstate_t, unsigned int, int); /*@out@*/ const char *pv_current_file_name(pvstate_t); diff --git a/src/include/pv.h b/src/include/pv.h index eaabf5c..5450444 100644 --- a/src/include/pv.h +++ b/src/include/pv.h @@ -51,9 +51,9 @@ extern double pv_getnum_d(const char *); extern unsigned int pv_getnum_ui(const char *); /* - * Return the given string converted to an unsigned long long. + * Return the given string converted to a size_t. */ -extern unsigned long long pv_getnum_ull(const char *); +extern size_t pv_getnum_size(const char *); /* * Return zero if the given string is a number of the given type. NB an @@ -154,15 +154,15 @@ extern void pv_state_bits_set(pvstate_t, bool); extern void pv_state_null_terminated_lines_set(pvstate_t, bool); extern void pv_state_no_display_set(pvstate_t, bool); extern void pv_state_skip_errors_set(pvstate_t, unsigned int); -extern void pv_state_error_skip_block_set(pvstate_t, unsigned long long); +extern void pv_state_error_skip_block_set(pvstate_t, size_t); extern void pv_state_stop_at_size_set(pvstate_t, bool); extern void pv_state_sync_after_write_set(pvstate_t, bool); extern void pv_state_direct_io_set(pvstate_t, bool); -extern void pv_state_rate_limit_set(pvstate_t, unsigned long long); -extern void pv_state_target_buffer_size_set(pvstate_t, unsigned long long); +extern void pv_state_rate_limit_set(pvstate_t, size_t); +extern void pv_state_target_buffer_size_set(pvstate_t, size_t); extern void pv_state_no_splice_set(pvstate_t, bool); extern void pv_state_discard_input_set(pvstate_t, bool); -extern void pv_state_size_set(pvstate_t, unsigned long long); +extern void pv_state_size_set(pvstate_t, size_t); extern void pv_state_interval_set(pvstate_t, double); extern void pv_state_width_set(pvstate_t, unsigned int, bool); extern void pv_state_height_set(pvstate_t, unsigned int, bool); @@ -187,7 +187,7 @@ extern void pv_screensize(unsigned int *width, unsigned int *height); /* * Calculate the total size of all input files. */ -extern unsigned long long pv_calc_total_size(pvstate_t); +extern size_t pv_calc_total_size(pvstate_t); /* * Set up signal handlers ready for running the main loop. diff --git a/src/main/options.c b/src/main/options.c index 401b0bf..27d77bd 100644 --- a/src/main/options.c +++ b/src/main/options.c @@ -376,7 +376,7 @@ opts_t opts_parse(unsigned int argc, char **argv) memset(&sb, 0, sizeof(sb)); rc = stat(size_file, &sb); if (0 == rc) { - opts->size = (unsigned long long) (sb.st_size); + opts->size = (size_t) (sb.st_size); } else { /*@-mustfreefresh@ *//* see above */ fprintf(stderr, "%s: %s %s: %s\n", @@ -387,7 +387,7 @@ opts_t opts_parse(unsigned int argc, char **argv) /*@+mustfreefresh@ */ } } else { - opts->size = pv_getnum_ull(optarg); + opts->size = pv_getnum_size(optarg); } break; case 'l': @@ -417,10 +417,10 @@ opts_t opts_parse(unsigned int argc, char **argv) } break; case 'L': - opts->rate_limit = pv_getnum_ull(optarg); + opts->rate_limit = pv_getnum_size(optarg); break; case 'B': - opts->buffer_size = pv_getnum_ull(optarg); + opts->buffer_size = pv_getnum_size(optarg); opts->no_splice = true; break; case 'C': @@ -430,7 +430,7 @@ opts_t opts_parse(unsigned int argc, char **argv) opts->skip_errors++; break; case 'Z': - opts->error_skip_block = pv_getnum_ull(optarg); + opts->error_skip_block = pv_getnum_size(optarg); break; case 'S': opts->stop_at_size = true; diff --git a/src/main/remote.c b/src/main/remote.c index 53548aa..8cde960 100644 --- a/src/main/remote.c +++ b/src/main/remote.c @@ -36,9 +36,9 @@ struct remote_msg { bool bytes; /* bytes transferred flag */ bool bufpercent; /* transfer buffer percentage flag */ unsigned int lastwritten; /* last-written bytes count */ - unsigned long long rate_limit; /* rate limit, in bytes per second */ - unsigned long long buffer_size; /* buffer size, in bytes (0=default) */ - unsigned long long size; /* total size of data */ + size_t rate_limit; /* rate limit, in bytes per second */ + size_t buffer_size; /* buffer size, in bytes (0=default) */ + size_t size; /* total size of data */ double interval; /* interval between updates */ unsigned int width; /* screen width */ unsigned int height; /* screen height */ diff --git a/src/pv/display.c b/src/pv/display.c index f771edc..f9685a5 100644 --- a/src/pv/display.c +++ b/src/pv/display.c @@ -1107,7 +1107,7 @@ static const char *pv__format(pvstate_t state, * * In line mode, "sl" and "tot" are in lines, not bytes. */ -void pv_display(pvstate_t state, long double esec, long long sl, long long tot) +void pv_display(pvstate_t state, long double esec, ssize_t sl, ssize_t tot) { const char *display; diff --git a/src/pv/file.c b/src/pv/file.c index 3f8a687..bfdbdc6 100644 --- a/src/pv/file.c +++ b/src/pv/file.c @@ -33,9 +33,9 @@ * * Returns the total size, or 0 if it is unknown. */ -static unsigned long long pv_calc_total_bytes(pvstate_t state) +static size_t pv_calc_total_bytes(pvstate_t state) { - unsigned long long total; + size_t total; struct stat sb; unsigned int file_idx; @@ -47,7 +47,7 @@ static unsigned long long pv_calc_total_bytes(pvstate_t state) */ if ((state->input_file_count < 1) || (NULL == state->input_files)) { if (0 == fstat(STDIN_FILENO, &sb)) - total = (unsigned long long) (sb.st_size); + total = sb.st_size; return total; } @@ -108,7 +108,7 @@ static unsigned long long pv_calc_total_bytes(pvstate_t state) off_t end_position; end_position = lseek(fd, 0, SEEK_END); if (end_position > 0) { - total += (unsigned long long) end_position; + total += (size_t) end_position; } (void) close(fd); } else { @@ -116,7 +116,7 @@ static unsigned long long pv_calc_total_bytes(pvstate_t state) return total; } } else if (S_ISREG(sb.st_mode)) { - total += (unsigned long long) (sb.st_size); + total += sb.st_size; } else { total = 0; } @@ -141,7 +141,7 @@ static unsigned long long pv_calc_total_bytes(pvstate_t state) end_position = lseek(STDOUT_FILENO, 0, SEEK_END); total = 0; if (end_position > 0) { - total = (unsigned long long) end_position; + total = (size_t) end_position; } if (lseek(STDOUT_FILENO, 0, SEEK_SET) != 0) { pv_error(state, "%s: %s: %s", "(stdout)", @@ -174,9 +174,9 @@ static unsigned long long pv_calc_total_bytes(pvstate_t state) * * Returns the total size, or 0 if it is unknown. */ -static unsigned long long pv_calc_total_lines(pvstate_t state) +static size_t pv_calc_total_lines(pvstate_t state) { - unsigned long long total; + size_t total; struct stat sb; unsigned int file_idx; @@ -263,7 +263,7 @@ static unsigned long long pv_calc_total_lines(pvstate_t state) * * Returns the total size, or 0 if it is unknown. */ -unsigned long long pv_calc_total_size(pvstate_t state) +size_t pv_calc_total_size(pvstate_t state) { if (state->linemode) { return pv_calc_total_lines(state); diff --git a/src/pv/loop.c b/src/pv/loop.c index c9f7a1c..9b3ae5a 100644 --- a/src/pv/loop.c +++ b/src/pv/loop.c @@ -36,7 +36,7 @@ int pv_main_loop(pvstate_t state) long written, lineswritten; long long total_written, transferred_since_last, cansend; long double target; - int eof_in, eof_out, final_update; + bool eof_in, eof_out, final_update; struct timespec start_time, next_update, next_ratecheck, cur_time; struct timespec init_time, next_remotecheck, transfer_elapsed; long double elapsed_seconds; @@ -64,8 +64,8 @@ int pv_main_loop(pvstate_t state) pv_crs_init(state); - eof_in = 0; - eof_out = 0; + eof_in = false; + eof_out = false; total_written = 0; transferred_since_last = 0; state->initial_offset = 0; @@ -84,7 +84,7 @@ int pv_main_loop(pvstate_t state) } target = 0; - final_update = 0; + final_update = false; file_idx = 0; /* @@ -175,8 +175,8 @@ int pv_main_loop(pvstate_t state) && (0 == state->rate_limit))) { cansend = state->size - total_written; if (0 >= cansend) { - eof_in = 1; - eof_out = 1; + eof_in = true; + eof_out = true; } } } @@ -213,8 +213,8 @@ int pv_main_loop(pvstate_t state) file_idx++; fd = pv_next_file(state, file_idx, fd); if (fd >= 0) { - eof_in = 0; - eof_out = 0; + eof_in = false; + eof_out = false; } } @@ -223,7 +223,7 @@ int pv_main_loop(pvstate_t state) /* If full EOF, final update, and force a display updaate. */ if (eof_in && eof_out) { - final_update = 1; + final_update = true; if ((state->display_visible) || (0 == state->delay_start)) { pv_elapsedtime_copy(&next_update, &cur_time); diff --git a/src/pv/number.c b/src/pv/number.c index 2643b20..e613545 100644 --- a/src/pv/number.c +++ b/src/pv/number.c @@ -24,12 +24,12 @@ static bool pv__isdigit(char c) /* - * Return the numeric value of "str", as an unsigned long long. + * Return the numeric value of "str", as a size_t. */ -unsigned long long pv_getnum_ull(const char *str) +size_t pv_getnum_size(const char *str) { - unsigned long long n = 0; - unsigned long long decimal = 0; + size_t n = 0; + size_t decimal = 0; unsigned int decdivisor = 1; unsigned int shift = 0; @@ -41,7 +41,7 @@ unsigned long long pv_getnum_ull(const char *str) for (; pv__isdigit(str[0]); str++) { n = n * 10; - n += (unsigned long long) (str[0] - '0'); + n += (size_t) (str[0] - '0'); } /* @@ -52,7 +52,7 @@ unsigned long long pv_getnum_ull(const char *str) for (; pv__isdigit(str[0]); str++) { if (decdivisor < 10000) { decimal = decimal * 10; - decimal += (unsigned long long) (str[0] - '0'); + decimal += (size_t) (str[0] - '0'); decdivisor = decdivisor * 10; } } @@ -99,8 +99,8 @@ unsigned long long pv_getnum_ull(const char *str) if (shiftby > 30) shiftby = 30; - n = n << shiftby; - decimal = decimal << shiftby; + n = (size_t) (n << shiftby); + decimal = (size_t) (decimal << shiftby); shift -= shiftby; } @@ -152,7 +152,7 @@ double pv_getnum_d(const char *str) */ unsigned int pv_getnum_ui(const char *str) { - return (unsigned int) pv_getnum_ull(str); + return (unsigned int) pv_getnum_size(str); } diff --git a/src/pv/state.c b/src/pv/state.c index 45d839b..40b8436 100644 --- a/src/pv/state.c +++ b/src/pv/state.c @@ -246,7 +246,7 @@ void pv_state_skip_errors_set(pvstate_t state, unsigned int val) state->skip_errors = val; } -void pv_state_error_skip_block_set(pvstate_t state, unsigned long long val) +void pv_state_error_skip_block_set(pvstate_t state, size_t val) { state->error_skip_block = val; } @@ -272,12 +272,12 @@ void pv_state_discard_input_set(pvstate_t state, bool val) state->discard_input = val; } -void pv_state_rate_limit_set(pvstate_t state, unsigned long long val) +void pv_state_rate_limit_set(pvstate_t state, size_t val) { state->rate_limit = val; } -void pv_state_target_buffer_size_set(pvstate_t state, unsigned long long val) +void pv_state_target_buffer_size_set(pvstate_t state, size_t val) { state->target_buffer_size = val; } @@ -287,7 +287,7 @@ void pv_state_no_splice_set(pvstate_t state, bool val) state->no_splice = val; } -void pv_state_size_set(pvstate_t state, unsigned long long val) +void pv_state_size_set(pvstate_t state, size_t val) { state->size = val; } diff --git a/src/pv/transfer.c b/src/pv/transfer.c index 158873b..3c7dad3 100644 --- a/src/pv/transfer.c +++ b/src/pv/transfer.c @@ -303,10 +303,10 @@ static ssize_t pv__transfer_write_repeated(int fd, void *buf, size_t count, bool * state->skip_errors, tries to skip past the problem. * * If the end of the input file is reached or the error is unrecoverable, - * sets *eof_in to 1. If all data in the buffer has been written at this - * point, then also sets *eof_out. + * sets *eof_in to true. If all data in the buffer has been written at this + * point, then also sets *eof_out to true. */ -static int pv__transfer_read(pvstate_t state, int fd, int *eof_in, int *eof_out, unsigned long long allowed) +static int pv__transfer_read(pvstate_t state, int fd, bool *eof_in, bool *eof_out, size_t allowed) { bool do_not_skip_errors; size_t bytes_can_read; @@ -387,9 +387,9 @@ static int pv__transfer_read(pvstate_t state, int fd, int *eof_in, int *eof_out, * buffer, we set eof_out as well, so that the main loop can * move on to the next input file. */ - *eof_in = 1; + *eof_in = true; if (state->write_position >= state->read_position) - *eof_out = 1; + *eof_out = true; return 1; } else if (nread > 0) { /* @@ -439,9 +439,9 @@ static int pv__transfer_read(pvstate_t state, int fd, int *eof_in, int *eof_out, */ if (do_not_skip_errors) { pv_error(state, "%s: %s: %s", pv_current_file_name(state), _("read failed"), strerror(errno)); - *eof_in = 1; + *eof_in = true; if (state->write_position >= state->read_position) { - *eof_out = 1; + *eof_out = true; } return 1; } @@ -467,9 +467,9 @@ static int pv__transfer_read(pvstate_t state, int fd, int *eof_in, int *eof_out, */ if (0 > orig_offset) { pv_error(state, "%s: %s: %s", pv_current_file_name(state), _("file is not seekable"), strerror(errno)); - *eof_in = 1; + *eof_in = true; if (state->write_position >= state->read_position) { - *eof_out = 1; + *eof_out = true; } return 1; } @@ -526,7 +526,7 @@ static int pv__transfer_read(pvstate_t state, int fd, int *eof_in, int *eof_out, * Failed to skip - lseek() returned an error, so mark the * file as having ended. */ - *eof_in = 1; + *eof_in = true; /* * EINVAL means the file has ended since we've tried to go * past the end of it, so we don't bother with a warning @@ -557,9 +557,9 @@ static int pv__transfer_read(pvstate_t state, int fd, int *eof_in, int *eof_out, /* * Failed to skip - mark file as ended. */ - *eof_in = 1; + *eof_in = true; if (state->write_position >= state->read_position) { - *eof_out = 1; + *eof_out = true; } } @@ -574,15 +574,16 @@ static int pv__transfer_read(pvstate_t state, int fd, int *eof_in, int *eof_out, * * Updates state->write_position by moving it on by the number of bytes * written; adds the number of bytes written to state->written; sets - * *eof_out on stdout EOF or when the write position catches up with the - * read position AND *eof_in is 1 (meaning we've reached the end of data). + * *eof_out to true, on stdout EOF, or when the write position catches up + * with the read position AND *eof_in is true (meaning we've reached the end + * of data). * - * On error, sets *eof_out to 1, sets state->written to -1, and updates + * On error, sets *eof_out to true, sets state->written to -1, and updates * state->exit_status. * * If state->discard_input is true, does not actually write anything. */ -static int pv__transfer_write(pvstate_t state, int *eof_in, int *eof_out, long *lineswritten) +static int pv__transfer_write(pvstate_t state, bool *eof_in, bool *eof_out, long *lineswritten) { ssize_t nwritten; @@ -601,7 +602,7 @@ static int pv__transfer_write(pvstate_t state, int *eof_in, int *eof_out, long * /* * Write returned 0 - EOF on stdout. */ - *eof_out = 1; + *eof_out = true; return 1; } else if (nwritten > 0) { /* @@ -679,7 +680,7 @@ static int pv__transfer_write(pvstate_t state, int *eof_in, int *eof_out, long * state->write_position = 0; state->read_position = 0; if (*eof_in) - *eof_out = 1; + *eof_out = true; } return 1; @@ -704,14 +705,14 @@ static int pv__transfer_write(pvstate_t state, int *eof_in, int *eof_out, long * * not really our error to report. */ if (EPIPE == errno) { - *eof_in = 1; - *eof_out = 1; + *eof_in = true; + *eof_out = true; return 0; } pv_error(state, "%s: %s", _("write failed"), strerror(errno)); state->exit_status |= 16; - *eof_out = 1; + *eof_out = true; state->written = -1; return 1; @@ -783,7 +784,7 @@ static unsigned char *pv__allocate_aligned_buffer(int fd, size_t target_size) * state->exit_status is updated). In line mode, the number of lines written * will be put into *lineswritten. */ -long pv_transfer(pvstate_t state, int fd, int *eof_in, int *eof_out, unsigned long long allowed, long *lineswritten) +ssize_t pv_transfer(pvstate_t state, int fd, bool *eof_in, bool *eof_out, size_t allowed, long *lineswritten) { bool ready_to_read, ready_to_write; int check_read_fd, check_write_fd; @@ -889,7 +890,7 @@ long pv_transfer(pvstate_t state, int fd, int *eof_in, int *eof_out, unsigned lo */ state->to_write = state->read_position - state->write_position; if ((state->rate_limit > 0) || (allowed > 0)) { - if ((unsigned long long) (state->to_write) > allowed) { + if (state->to_write > allowed) { state->to_write = allowed; } }