From d3a435e53c94ec25b4ae5fa2614f49ef8884e08a Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 5 Jan 2016 22:26:58 -0600 Subject: Add error_msg_raw() and friends, replace error_msg("%s", s) uses, enable format checking, and fix up format checking complaints. Added out(type, value) function to stat to avoid a zillion printf typecasts. --- toys/other/stat.c | 61 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 27 deletions(-) (limited to 'toys/other/stat.c') diff --git a/toys/other/stat.c b/toys/other/stat.c index fe522b08..fb5922c6 100644 --- a/toys/other/stat.c +++ b/toys/other/stat.c @@ -2,7 +2,7 @@ * Copyright 2012 * Copyright 2013 -USE_STAT(NEWTOY(stat, "c:f", TOYFLAG_BIN)) +USE_STAT(NEWTOY(stat, "<1c:f", TOYFLAG_BIN)) config STAT bool stat @@ -55,24 +55,31 @@ static void date_stat_format(struct timespec *ts) { strftime(toybuf, sizeof(toybuf), "%Y-%m-%d %H:%M:%S", localtime(&(ts->tv_sec))); - xprintf("%s.%09d", toybuf, ts->tv_nsec); + xprintf("%s.%09ld", toybuf, ts->tv_nsec); +} + +// Force numeric output to long long instead of manually typecasting everything +static void out(char c, long long val) +{ + sprintf(toybuf, "%%ll%c", c); + printf(toybuf, val); } static void print_stat(char type) { struct stat *stat = (struct stat *)&TT.stat; - if (type == 'a') xprintf("%lo", stat->st_mode & ~S_IFMT); + if (type == 'a') out('o', stat->st_mode&~S_IFMT); else if (type == 'A') { char str[11]; mode_to_string(stat->st_mode, str); xprintf("%s", str); - } else if (type == 'b') xprintf("%llu", stat->st_blocks); - else if (type == 'B') xprintf("%lu", stat->st_blksize); - else if (type == 'd') xprintf("%ld", stat->st_dev); - else if (type == 'D') xprintf("%llx", stat->st_dev); - else if (type == 'f') xprintf("%lx", stat->st_mode); + } else if (type == 'b') out('u', stat->st_blocks); + else if (type == 'B') out('u', stat->st_blksize); + else if (type == 'd') out('d', stat->st_dev); + else if (type == 'D') out('x', stat->st_dev); + else if (type == 'f') out('x', stat->st_mode); else if (type == 'F') { char *t = "character device\0directory\0block device\0" \ "regular file\0symbolic link\0socket\0FIFO (named pipe)"; @@ -81,38 +88,38 @@ static void print_stat(char type) for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1; if (!stat->st_size && filetype == S_IFREG) t = "regular empty file"; xprintf("%s", t); - } else if (type == 'g') xprintf("%lu", stat->st_gid); + } else if (type == 'g') out('u', stat->st_gid); else if (type == 'G') xprintf("%8s", TT.group_name->gr_name); - else if (type == 'h') xprintf("%lu", stat->st_nlink); - else if (type == 'i') xprintf("%llu", stat->st_ino); + else if (type == 'h') out('u', stat->st_nlink); + else if (type == 'i') out('u', stat->st_ino); else if (type == 'N') { xprintf("`%s'", *toys.optargs); if (S_ISLNK(stat->st_mode)) if (0 `%s'", toybuf); - } else if (type == 'o') xprintf("%lu", stat->st_blksize); - else if (type == 's') xprintf("%llu", stat->st_size); - else if (type == 'u') xprintf("%lu", stat->st_uid); + } else if (type == 'o') out('u', stat->st_blksize); + else if (type == 's') out('u', stat->st_size); + else if (type == 'u') out('u', stat->st_uid); else if (type == 'U') xprintf("%8s", TT.user_name->pw_name); else if (type == 'x') date_stat_format((void *)&stat->st_atime); - else if (type == 'X') xprintf("%llu", (long long)stat->st_atime); + else if (type == 'X') out('u', stat->st_atime); else if (type == 'y') date_stat_format((void *)&stat->st_mtime); - else if (type == 'Y') xprintf("%llu", (long long)stat->st_mtime); + else if (type == 'Y') out('u', stat->st_mtime); else if (type == 'z') date_stat_format((void *)&stat->st_ctime); - else if (type == 'Z') xprintf("%llu", (long long)stat->st_ctime); + else if (type == 'Z') out('u', stat->st_ctime); else xprintf("?"); } static void print_statfs(char type) { struct statfs *statfs = (struct statfs *)&TT.stat; - if (type == 'a') xprintf("%llu", statfs->f_bavail); - else if (type == 'b') xprintf("%llu", statfs->f_blocks); - else if (type == 'c') xprintf("%llu", statfs->f_files); - else if (type == 'd') xprintf("%llu", statfs->f_ffree); - else if (type == 'f') xprintf("%llu", statfs->f_bfree); - else if (type == 'l') xprintf("%ld", statfs->f_namelen); - else if (type == 't') xprintf("%lx", statfs->f_type); + if (type == 'a') out('u', statfs->f_bavail); + else if (type == 'b') out('u', statfs->f_blocks); + else if (type == 'c') out('u', statfs->f_files); + else if (type == 'd') out('u', statfs->f_ffree); + else if (type == 'f') out('u', statfs->f_bfree); + else if (type == 'l') out('d', statfs->f_namelen); + else if (type == 't') out('x', statfs->f_type); else if (type == 'T') { char *s = "unknown"; struct {unsigned num; char *name;} nn[] = { @@ -129,11 +136,11 @@ static void print_statfs(char type) { for (i=0; if_type) s = nn[i].name; - xprintf(s); + fputs(s, stdout); } else if (type == 'i') xprintf("%08x%08x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]); - else if (type == 's') xprintf("%d", statfs->f_frsize); - else if (type == 'S') xprintf("%d", statfs->f_bsize); + else if (type == 's') out('d', statfs->f_frsize); + else if (type == 'S') out('d', statfs->f_bsize); else xprintf("?"); } -- cgit v1.2.3