Skip to content

Commit

Permalink
Format time_t values portably and fix other values too
Browse files Browse the repository at this point in the history
  • Loading branch information
koutcher committed Mar 5, 2021
1 parent 9fb0a2f commit 71d8944
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,15 @@ 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);

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);
Expand Down
2 changes: 1 addition & 1 deletion src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,26 @@ 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)
continue;

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"))
Expand Down Expand Up @@ -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 = {
Expand Down
8 changes: 4 additions & 4 deletions src/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down

0 comments on commit 71d8944

Please sign in to comment.