aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-08-16 14:14:22 -0500
committerRob Landley <rob@landley.net>2016-08-16 14:14:22 -0500
commit05e1582ec435932a25db21fd3ae71fbf62a4b45d (patch)
treeb80417aaef80624cfef58d5cb170b8158c63b54a
parent1a429a722a504f0cdf3ffb90a331eb3f55deee5a (diff)
downloadtoybox-05e1582ec435932a25db21fd3ae71fbf62a4b45d.tar.gz
Add length modifier to date escapes, and length sanity check.
-rw-r--r--toys/other/stat.c26
1 files changed, 14 insertions, 12 deletions
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);