From b2b97e301892d89b9909bed17f4299e09af1e33b Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 20 Jan 2018 11:53:36 -0500 Subject: printf: Add %w and %Wk for file birth times %w and %W were chosen to match the format specifiers for file birth times from stat(1) --- bfs.1 | 16 ++++++++++++++++ parse.c | 7 ++++++- printf.c | 9 +++++++++ tests.sh | 6 ++++++ tests/test_printf_w.out | 0 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/test_printf_w.out diff --git a/bfs.1 b/bfs.1 index a6ff153..15a998f 100644 --- a/bfs.1 +++ b/bfs.1 @@ -627,6 +627,22 @@ Actions: .TP .B \-nohidden Filter out hidden files and directories. +.LP +\fB\-printf \fIFORMAT\fR +.br +\fB\-fprintf \fIFORMAT FILE\fR +.RS +These additional format directives are supported: +.TP +%w +The file's birth time, in the same format as %a/%c/%t. +.TP +.RI %W k +Field +.I k +of the file's birth time, in the same format as +.RI %A k /%C k /%T k . +.RE .SH EXAMPLES .TP .B bfs diff --git a/parse.c b/parse.c index e71aa47..2811004 100644 --- a/parse.c +++ b/parse.c @@ -2406,7 +2406,7 @@ static struct expr *parse_help(struct parser_state *state, int arg1, int arg2) { cfprintf(cout, " %{blu}-Btime%{rs} %{bld}[-+]N%{rs}\n"); cfprintf(cout, " Find files Birthed %{bld}N%{rs} minutes/days ago\n"); cfprintf(cout, " %{blu}-Bnewer%{rs} %{bld}FILE%{rs}\n"); - cfprintf(cout, " Find files Birthed more recently than %{bld}FILEa%{rs} was modified\n"); + cfprintf(cout, " Find files Birthed more recently than %{bld}FILE%{rs} was modified\n"); cfprintf(cout, " %{blu}-depth%{rs} %{bld}[-+]N%{rs}\n"); cfprintf(cout, " Find files with depth %{bld}N%{rs}\n"); cfprintf(cout, " %{blu}-gid%{rs} %{bld}NAME%{rs}\n"); @@ -2448,6 +2448,11 @@ static struct expr *parse_help(struct parser_state *state, int arg1, int arg2) { cfprintf(cout, " %{blu}-nohidden%{rs}\n"); cfprintf(cout, " Match hidden files, or filter them out\n\n"); + cfprintf(cout, " %{blu}-printf%{rs} %{bld}FORMAT%{rs}\n"); + cfprintf(cout, " %{blu}-fprintf%{rs} %{bld}FORMAT%{rs} %{bld}FILE%{rs}\n"); + cfprintf(cout, " The additional format directives %%w and %%W%{bld}k%{rs} for printing file birth times\n"); + cfprintf(cout, " are supported.\n\n"); + cfprintf(cout, "%s\n", BFS_HOMEPAGE); state->just_info = true; diff --git a/printf.c b/printf.c index 27d9521..86804b8 100644 --- a/printf.c +++ b/printf.c @@ -81,6 +81,7 @@ static int bfs_printf_flush(FILE *file, const struct bfs_printf_directive *direc */ static const struct timespec *get_time_field(const struct bfs_stat *statbuf, enum bfs_stat_field stat_field) { if (!(statbuf->mask & stat_field)) { + errno = ENOTSUP; return NULL; } @@ -700,6 +701,11 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct cmdline *cmdline) directive->fn = bfs_printf_U; command->needs_stat = true; break; + case 'w': + directive->fn = bfs_printf_ctime; + directive->stat_field = BFS_STAT_BTIME; + command->needs_stat = true; + break; case 'y': directive->fn = bfs_printf_y; break; @@ -716,6 +722,9 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct cmdline *cmdline) case 'T': directive->stat_field = BFS_STAT_MTIME; goto directive_strftime; + case 'W': + directive->stat_field = BFS_STAT_BTIME; + goto directive_strftime; directive_strftime: directive->fn = bfs_printf_strftime; diff --git a/tests.sh b/tests.sh index 0585ff4..de0afb7 100755 --- a/tests.sh +++ b/tests.sh @@ -394,6 +394,7 @@ bfs_tests=( test_execdir_plus test_hidden test_nohidden + test_printf_w test_path_flag_expr test_path_expr_flag test_flag_expr_path @@ -1290,6 +1291,11 @@ function test_printf_nul() { fi } +function test_printf_w() { + # Birth times may not be supported, so just check that %w/%W can be parsed + bfs_diff times -false -printf '%w %WY\n' +} + function test_fstype() { fstype="$(invoke_bfs -printf '%F\n' | head -n1)" bfs_diff basic -fstype "$fstype" diff --git a/tests/test_printf_w.out b/tests/test_printf_w.out new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3