From 71d894419d5f0ed07d4fa52c3fd2c59cca374660 Mon Sep 17 00:00:00 2001 From: Thomas Koutcher Date: Sun, 14 Feb 2021 14:05:15 +0100 Subject: [PATCH] Format time_t values portably and fix other values too Fixes #1084 --- src/argv.c | 2 +- src/blame.c | 2 +- src/diff.c | 2 +- src/display.c | 4 ++-- src/draw.c | 2 +- src/status.c | 2 +- src/ui.c | 2 +- src/util.c | 10 +++++----- src/view.c | 8 ++++---- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/argv.c b/src/argv.c index 0e68358c5..52d178d90 100644 --- a/src/argv.c +++ b/src/argv.c @@ -389,7 +389,7 @@ argv_number_formatter(struct format_context *format, struct format_var *var) { unsigned long value = *(unsigned long *) var->value_ref; - return string_format_from(format->buf, &format->bufpos, "%ld", value); + return string_format_from(format->buf, &format->bufpos, "%lu", value); } static bool diff --git a/src/blame.c b/src/blame.c index d35444de7..335c251a7 100644 --- a/src/blame.c +++ b/src/blame.c @@ -293,7 +293,7 @@ blame_read(struct view *view, struct buffer *buf, bool force_stop) if (!state->commit) { state->commit = read_blame_commit(view, buf->data, state); - string_format(view->ref, "%s %2zd%%", view->vid, + string_format(view->ref, "%s %2zu%%", view->vid, view->lines ? 5 * (size_t) (state->blamed * 20 / view->lines) : 0); } else if (parse_blame_info(state->commit, state->author, buf->data)) { diff --git a/src/diff.c b/src/diff.c index 94b060432..732fc3f4a 100644 --- a/src/diff.c +++ b/src/diff.c @@ -576,7 +576,7 @@ diff_blame_line(const char *ref, const char *file, unsigned long lineno, bool ok = false; struct buffer buf; - if (!string_format(line_arg, "-L%ld,+1", lineno)) + if (!string_format(line_arg, "-L%lu,+1", lineno)) return false; if (!io_run(&io, IO_RD, repo.exec_dir, NULL, blame_argv)) diff --git a/src/display.c b/src/display.c index 3712a5961..e42b28d40 100644 --- a/src/display.c +++ b/src/display.c @@ -416,7 +416,7 @@ save_view(struct view *view, const char *path) fprintf(file, "Parent: %s\n", view->parent->name); fprintf(file, "Ref: %s\n", view->ref); fprintf(file, "Dimensions: height=%d width=%d\n", view->height, view->width); - fprintf(file, "Position: offset=%ld column=%ld lineno=%ld\n", + fprintf(file, "Position: offset=%lu column=%lu lineno=%lu\n", view->pos.offset, view->pos.col, view->pos.lineno); @@ -424,7 +424,7 @@ save_view(struct view *view, const char *path) for (i = 0; i < view->lines; i++) { struct line *line = &view->line[i]; - fprintf(file, "line[%3zu] type=%s selected=%d\n", + fprintf(file, "line[%3zu] type=%s selected=%u\n", i, enum_name(get_line_type_name(line->type)), line->selected); diff --git a/src/draw.c b/src/draw.c index 6021356ae..afab85f2b 100644 --- a/src/draw.c +++ b/src/draw.c @@ -322,7 +322,7 @@ draw_lineno_custom(struct view *view, struct view_column *column, unsigned int l return false; if (lineno == 1 || (lineno % interval) == 0) { - static char fmt[] = "%ld"; + static char fmt[] = "%3u"; fmt[1] = '0' + digits3; if (string_format(number, fmt, lineno)) diff --git a/src/status.c b/src/status.c index da312ec2b..18a166e23 100644 --- a/src/status.c +++ b/src/status.c @@ -603,7 +603,7 @@ status_update_files(struct view *view, struct line *line) if (almost_done > done && view_is_displayed(view)) { done = almost_done; - string_format(view->ref, "updating file %u of %u (%d%% done)", + string_format(view->ref, "updating file %d of %d (%d%% done)", file, files, done); update_view_title(view); set_cursor_pos(cursor_y, cursor_x); diff --git a/src/ui.c b/src/ui.c index 29935b093..07a869e3f 100644 --- a/src/ui.c +++ b/src/ui.c @@ -170,7 +170,7 @@ file_finder_draw(struct file_finder *finder) wmove(finder->win, finder->height - 1, 0); wbkgdset(finder->win, get_line_attr(NULL, LINE_TITLE_FOCUS)); - wprintw(finder->win, "[finder] file %d of %d", pos->lineno + 1, finder->lines); + wprintw(finder->win, "[finder] file %lu of %zu", pos->lineno + 1, finder->lines); wclrtoeol(finder->win); wrefresh(finder->win); } diff --git a/src/util.c b/src/util.c index 83753f7e0..ef775149b 100644 --- a/src/util.c +++ b/src/util.c @@ -146,13 +146,13 @@ get_relative_date(const struct time *time, char *buf, size_t buflen, bool compac { struct timeval now; time_t timestamp = time->sec + time->tz; - time_t seconds; + long long seconds; int i; if (time_now(&now, NULL)) return ""; - seconds = now.tv_sec < timestamp ? timestamp - now.tv_sec : now.tv_sec - timestamp; + seconds = now.tv_sec < timestamp ? (long long) difftime(timestamp, now.tv_sec) : (long long) difftime(now.tv_sec, timestamp); for (i = 0; i < ARRAY_SIZE(reldate); i++) { if (seconds >= reldate[i].interval && reldate[i].interval) @@ -160,12 +160,12 @@ get_relative_date(const struct time *time, char *buf, size_t buflen, bool compac seconds /= reldate[i].in_seconds; if (compact) { - if (!string_nformat(buf, buflen, NULL, "%s%ld%c", + if (!string_nformat(buf, buflen, NULL, "%s%lld%c", now.tv_sec >= timestamp ? "" : "-", seconds, reldate[i].compact_symbol)) return ""; - } else if (!string_nformat(buf, buflen, NULL, "%ld %s%s %s", + } else if (!string_nformat(buf, buflen, NULL, "%lld %s%s %s", seconds, reldate[i].name, seconds > 1 ? "s" : "", now.tv_sec >= timestamp ? "ago" : "ahead")) @@ -293,7 +293,7 @@ mkfilesize(unsigned long size, enum file_size format) } } - return string_format(buf, "%ld", size) ? buf : NULL; + return string_format(buf, "%lu", size) ? buf : NULL; } const struct ident unknown_ident = { diff --git a/src/view.c b/src/view.c index 31af02d98..9980e3a2f 100644 --- a/src/view.c +++ b/src/view.c @@ -690,7 +690,7 @@ update_view_title(struct view *view) if (!view_has_flags(view, VIEW_CUSTOM_STATUS) && view_has_line(view, line) && line->lineno) { - wprintw(window, " - %s %d of %zd", + wprintw(window, " - %s %u of %zu", view->ops->type, line->lineno, MAX(line->lineno, @@ -701,16 +701,16 @@ update_view_title(struct view *view) } if (view->pipe) { - time_t secs = time(NULL) - view->start_time; + long long secs = (long long) difftime(time(NULL), view->start_time); /* Three git seconds are a long time ... */ if (secs > 2) - wprintw(window, " loading %lds", secs); + wprintw(window, " loading %llds", secs); } view_lines = view->pos.offset + view->height; lines = view->lines ? MIN(view_lines, view->lines) * 100 / view->lines : 0; - mvwprintw(window, 0, view->width - count_digits(lines) - 1, "%d%%", lines); + mvwprintw(window, 0, view->width - count_digits(lines) - 1, "%u%%", lines); wnoutrefresh(window); }