Skip to content

Commit dd5df1f

Browse files
committed
eval: Don't error out in -ls if the time overflows
1 parent e1d46eb commit dd5df1f

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/eval.c

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,34 @@ static int print_owner(FILE *file, const char *name, uintmax_t id, int *width) {
700700
}
701701
}
702702

703+
/** Print a file's modification time. */
704+
static int print_time(FILE *file, time_t time, time_t now) {
705+
struct tm tm;
706+
if (!localtime_r(&time, &tm)) {
707+
goto error;
708+
}
709+
710+
char time_str[256];
711+
size_t time_ret;
712+
713+
time_t six_months_ago = now - 6 * 30 * 24 * 60 * 60;
714+
time_t tomorrow = now + 24 * 60 * 60;
715+
if (time <= six_months_ago || time >= tomorrow) {
716+
time_ret = strftime(time_str, sizeof(time_str), "%b %e %Y", &tm);
717+
} else {
718+
time_ret = strftime(time_str, sizeof(time_str), "%b %e %H:%M", &tm);
719+
}
720+
721+
if (time_ret == 0) {
722+
goto error;
723+
}
724+
725+
return fprintf(file, " %s", time_str);
726+
727+
error:
728+
return fprintf(file, " %jd", (intmax_t)time);
729+
}
730+
703731
/**
704732
* -f?ls action.
705733
*/
@@ -756,28 +784,11 @@ bool eval_fls(const struct bfs_expr *expr, struct bfs_eval *state) {
756784

757785
time_t time = statbuf->mtime.tv_sec;
758786
time_t now = ctx->now.tv_sec;
759-
time_t six_months_ago = now - 6 * 30 * 24 * 60 * 60;
760-
time_t tomorrow = now + 24 * 60 * 60;
761-
struct tm tm;
762-
if (!localtime_r(&time, &tm)) {
763-
goto error;
764-
}
765-
char time_str[256];
766-
size_t time_ret;
767-
if (time <= six_months_ago || time >= tomorrow) {
768-
time_ret = strftime(time_str, sizeof(time_str), "%b %e %Y", &tm);
769-
} else {
770-
time_ret = strftime(time_str, sizeof(time_str), "%b %e %H:%M", &tm);
771-
}
772-
if (time_ret == 0) {
773-
errno = EOVERFLOW;
774-
goto error;
775-
}
776-
if (cfprintf(cfile, " %s${rs}", time_str) < 0) {
787+
if (print_time(file, time, now) < 0) {
777788
goto error;
778789
}
779790

780-
if (cfprintf(cfile, " %pP", ftwbuf) < 0) {
791+
if (cfprintf(cfile, "${rs} %pP", ftwbuf) < 0) {
781792
goto error;
782793
}
783794

tests/gnu/fls_overflow.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Regression test: times that overflow localtime() should still print
2+
cd "$TEST"
3+
"$XTOUCH" -t "@1111111111111111111" overflow
4+
invoke_bfs . -fls "$OUT"

0 commit comments

Comments
 (0)