From 05e1582ec435932a25db21fd3ae71fbf62a4b45d Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 16 Aug 2016 14:14:22 -0500 Subject: Add length modifier to date escapes, and length sanity check. --- toys/other/stat.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'toys/other/stat.c') diff --git a/toys/other/stat.c b/toys/other/stat.c index 4bdf407d..b998a0ed 100644 --- a/toys/other/stat.c +++ b/toys/other/stat.c @@ -50,18 +50,6 @@ GLOBALS( int patlen; ) - -// Note: the atime, mtime, and ctime fields in struct stat are the start -// of embedded struct timespec, but posix won't let them use that -// struct definition for legacy/namespace reasons. - -static void date_stat_format(struct timespec *ts) -{ - strftime(toybuf, sizeof(toybuf), "%Y-%m-%d %H:%M:%S", - localtime(&(ts->tv_sec))); - xprintf("%s.%09ld", toybuf, ts->tv_nsec); -} - // Force numeric output to long long instead of manually typecasting everything // and safely parse length prefix static void out(char c, long long val) @@ -77,6 +65,19 @@ static void strout(char *val) printf(toybuf, val); } +// Note: the atime, mtime, and ctime fields in struct stat are the start +// of embedded struct timespec, but posix won't let them use that +// struct definition for legacy/namespace reasons. + +static void date_stat_format(struct timespec *ts) +{ + char *s = toybuf+128; + strftime(s, sizeof(toybuf), "%Y-%m-%d %H:%M:%S", + localtime(&(ts->tv_sec))); + sprintf(s+strlen(s), ".%09ld", ts->tv_nsec); + strout(s); +} + static void print_stat(char type) { struct stat *stat = (struct stat *)&TT.stat; @@ -206,6 +207,7 @@ void stat_main(void) else { f = next_printf(f, &TT.pattern); TT.patlen = f-TT.pattern; + if (TT.patlen>99) error_exit("bad %s", TT.pattern); if (*f == 'n') strout(TT.file); else if (flagf) print_statfs(*f); else print_stat(*f); -- cgit v1.2.3