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);
85 case 'a': /* octal mode */
86 printf("%o", (int)buf->st_mode & 07777);
89 if (S_ISBLK(buf->st_mode)) {
91 } else if (S_ISCHR(buf->st_mode)) {
93 } else if (S_ISDIR(buf->st_mode)) {
95 } else if (S_ISFIFO(buf->st_mode)) {
97 } else if (S_ISREG(buf->st_mode)) {
99 } else if (S_ISLNK(buf->st_mode)) {
101 } else if (S_ISSOCK(buf->st_mode)) {
107 case 'u': /* handle uid */
108 printf("%0d", buf->st_uid);
110 case 'U': /* handle username */
111 pw = getpwuid(buf->st_uid);
113 printf("%s", pw->pw_name);
115 case 'g': /* handle gid */
116 printf("%0d", buf->st_gid);
118 case 'G': /* handle groupname */
119 gr = getgrgid(buf->st_gid);
121 printf("%s", gr->gr_name);
124 printf("%s", filename);
129 default: /* handle unknown formatting character */
130 printf("%c", f.found);
137 int main(int ac, char *av[]) {
138 char *fmt = "%y"; /* default stat is mtime */
139 char *timefmt = "%s"; /* default time fmt is unix ts */
144 while ((c = getopt(ac, av, "lf:t:")) != -1) {
152 case ':': /* -f or -t without operand */
154 "Option -%c requires an operand\n", optopt);
162 "Unrecognized option: '-%c'\n", optopt);
167 fprintf(stderr, "zpm-stat [-f <format>] [-t <timefmt>]\n");
170 for ( ; optind < ac; optind++) {
173 lstat(av[optind], &buf);
175 stat(av[optind], &buf);
177 stat_one(fmt, timefmt, av[optind], &buf);