From 7e6af0686393dd9d46cc501f7f5a07eaa35663ad Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 11 Apr 2022 17:18:46 -0400 Subject: show dates in -l --- ls.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/ls.c b/ls.c index d01c058..5575738 100644 --- a/ls.c +++ b/ls.c @@ -291,6 +291,32 @@ static char *ls_file_group(gid_t gid) return group; } +static char *ls_file_time(struct timespec mtime) +{ + static char date[16]; + time_t now = time(NULL); + struct tm mtm; + struct tm ntm; + + localtime_r(&now, &ntm); + localtime_r(&mtime.tv_sec, &mtm); + + if (mtm.tm_mon < 6) { + mtm.tm_year += 1; + mtm.tm_year += 6; + } + + if (ntm.tm_year > mtm.tm_year || + (ntm.tm_year == mtm.tm_year && ntm.tm_mon - mtm.tm_mon > 6) || + (now < mtime.tv_sec)) { + strftime(date, sizeof(date), "%b %e %Y", &mtm); + } else { + strftime(date, sizeof(date), "%b %e %H:%M", &mtm); + } + + return date; +} + static void ls_print_long(size_t n, struct file_info files[static n]) { for (size_t i = 0; i < n; i++) { @@ -309,12 +335,12 @@ static void ls_print_long(size_t n, struct file_info files[static n]) } if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { - printf("%s ", "DEVICE INFO"); + printf("%*jx ", SIZEWIDTH, (uintmax_t)st.st_rdev); } else { printf("%*ju ", SIZEWIDTH, (uintmax_t)st.st_size); } - printf("%s ", "DATE/TIME"); + printf("%s ", ls_file_time(st.st_mtim)); printf("%s", ls_filename(files + i)); if (S_ISLNK(st.st_mode)) { printf(" -> %s\n", "LINK_DESTINATION"); -- cgit v1.2.1