11 * a generic printf style format string parser
12 * takes a character pointer, returns pointer to next non
13 * parsed character, next token, optionally
14 * does a callback, fills in a format struct
22 fmt = parse_format_str(fmt, &f);
23 if (!f.found) break; /* done */
25 /* literal/noformat */
26 putchar(f.found); /* or whatever */
30 case 'a': /* handle %a */
32 default: /* handle unknown formatting character */
38 struct format_string {
39 int found; /* the 'd' in '%d' */
40 unsigned flags; /* 1 = found, 0 = none found */
43 char *format_string_parse(char *s, struct format_string *f) {
47 if (!*s) return s; /* done */
49 /* literal non-format specifier */
61 void stat_one(char *fmt, char *timefmt, char *filename, struct stat *buf) {
64 struct format_string f;
69 fmt = format_string_parse(fmt, &f);
70 if (!f.found) break; /* done */
72 /* literal/noformat */
73 putchar(f.found); /* or whatever */
77 case 'y': /* handle mtime */
78 tm = gmtime(&buf->st_mtime);
79 strftime(timestr, 1023, timefmt, tm);
81 printf("%s", timestr);
83 case 'a': /* octal mode */
84 printf("%o", (int)buf->st_mode & 07777);
87 if (S_ISBLK(buf->st_mode)) {
89 } else if (S_ISCHR(buf->st_mode)) {
91 } else if (S_ISDIR(buf->st_mode)) {
93 } else if (S_ISFIFO(buf->st_mode)) {
95 } else if (S_ISREG(buf->st_mode)) {
97 } else if (S_ISLNK(buf->st_mode)) {
99 } else if (S_ISSOCK(buf->st_mode)) {
105 case 'u': /* handle uid */
106 printf("%0d", buf->st_uid);
108 case 'U': /* handle username */
109 pw = getpwuid(buf->st_uid);
111 printf("%s", pw->pw_name);
113 case 'g': /* handle gid */
114 printf("%0d", buf->st_gid);
116 case 'G': /* handle groupname */
117 gr = getgrgid(buf->st_gid);
119 printf("%s", gr->gr_name);
122 printf("%s", filename);
127 default: /* handle unknown formatting character */
128 printf("%c", f.found);
135 int main(int ac, char *av[]) {
136 char *fmt = "%y"; /* default stat is mtime */
137 char *timefmt = "%s"; /* default time fmt is unix ts */
142 while ((c = getopt(ac, av, "lf:t:")) != -1) {
150 case ':': /* -f or -t without operand */
152 "Option -%c requires an operand\n", optopt);
160 "Unrecognized option: '-%c'\n", optopt);
165 fprintf(stderr, "zpm-stat [-f <format>] [-t <timefmt>]");
168 for ( ; optind < ac; optind++) {
171 lstat(av[optind], &buf);
173 stat(av[optind], &buf);
175 stat_one(fmt, timefmt, av[optind], &buf);