diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-04-11 17:18:46 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-04-11 17:18:46 -0400 |
commit | 7e6af0686393dd9d46cc501f7f5a07eaa35663ad (patch) | |
tree | 2e744ccfca857cb5b85bd6d08e023977ef77b240 | |
parent | a169e4baf8936576059d3ca42cfa656c0191e592 (diff) |
show dates in -l
-rw-r--r-- | ls.c | 30 |
1 files changed, 28 insertions, 2 deletions
@@ -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"); |