1 #define _POSIX_C_SOURCE 200112L
13 * a generic printf style format string parser
14 * takes a character pointer, returns pointer to next non
15 * parsed character, next token, optionally
16 * does a callback, fills in a format struct
24 fmt = parse_format_str(fmt, &f);
25 if (!f.found) break; /* done */
27 /* literal/noformat */
28 putchar(f.found); /* or whatever */
32 case 'a': /* handle %a */
34 default: /* handle unknown formatting character */
40 struct format_string {
41 int found; /* the 'd' in '%d' */
42 unsigned flags; /* 1 = found, 0 = none found */
45 char *format_string_parse(char *s, struct format_string *f) {
49 if (!*s) return s; /* done */
51 /* literal non-format specifier */
63 void stat_one(char *fmt, char *timefmt, char *filename, struct stat *buf) {
66 struct format_string f;
71 fmt = format_string_parse(fmt, &f);
72 if (!f.found) break; /* done */
74 /* literal/noformat */
75 putchar(f.found); /* or whatever */
79 case 'y': /* handle mtime */
80 tm = gmtime(&buf->st_mtime);
81 strftime(timestr, 1023, timefmt, tm);
83 printf("%s", timestr);
86 printf("%jd", (intmax_t)buf->st_size);
88 case 'a': /* octal mode */
89 printf("%o", (int)buf->st_mode & 07777);
92 if (S_ISBLK(buf->st_mode)) {
94 } else if (S_ISCHR(buf->st_mode)) {
96 } else if (S_ISDIR(buf->st_mode)) {
98 } else if (S_ISFIFO(buf->st_mode)) {
100 } else if (S_ISREG(buf->st_mode)) {
102 } else if (S_ISLNK(buf->st_mode)) {
104 } else if (S_ISSOCK(buf->st_mode)) {
110 case 'u': /* handle uid */
111 printf("%0d", buf->st_uid);
113 case 'U': /* handle username */
114 pw = getpwuid(buf->st_uid);
116 printf("%s", pw->pw_name);
118 case 'g': /* handle gid */
119 printf("%0d", buf->st_gid);
121 case 'G': /* handle groupname */
122 gr = getgrgid(buf->st_gid);
124 printf("%s", gr->gr_name);
127 printf("%s", filename);
132 default: /* handle unknown formatting character */
133 printf("%c", f.found);
140 int main(int ac, char *av[]) {
141 char *fmt = "%y"; /* default stat is mtime */
142 char *timefmt = "%s"; /* default time fmt is unix ts */
147 while ((c = getopt(ac, av, "lf:t:")) != -1) {
155 case ':': /* -f or -t without operand */
157 "Option -%c requires an operand\n", optopt);
165 "Unrecognized option: '-%c'\n", optopt);
170 fprintf(stderr, "zpm-stat [-f <format>] [-t <timefmt>]\n");
173 for ( ; optind < ac; optind++) {
176 lstat(av[optind], &buf);
178 stat(av[optind], &buf);
180 stat_one(fmt, timefmt, av[optind], &buf);