X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=zpm-stat.c;h=d9f5aa115a8985ab24beaffc8cc58f50f63254e8;hb=bd21f0a1265b43ad5f05353a39db31c16826f05c;hp=b85857a0b59eb0f54419cf1af1445c9f16d9ee96;hpb=aa882cfe7c8bb66b9f31d7f6581a94bc9744c04b;p=zpackage diff --git a/zpm-stat.c b/zpm-stat.c index b85857a..d9f5aa1 100644 --- a/zpm-stat.c +++ b/zpm-stat.c @@ -1,3 +1,5 @@ +#define _POSIX_C_SOURCE 200112L + #include #include #include @@ -58,56 +60,107 @@ char *format_string_parse(char *s, struct format_string *f) { return s; } -void stat_one(char *fmt, char *timefmt, char *filename) { - struct stat buf; +void stat_one(char *fmt, char *timefmt, int zeroterm, char *filename, struct + stat *buf) { struct tm *tm; char timestr[1024]; struct format_string f; struct passwd *pw; struct group *gr; - stat(filename, &buf); - while (*fmt) { fmt = format_string_parse(fmt, &f); if (!f.found) break; /* done */ if (!f.flags) { + if (f.found == '\\') { + switch (*fmt) { + case 0: + continue; break; + case 'n': + putchar('\n'); break; + case 't': + putchar('\t'); break; + default: + putchar(*fmt); + break; + } + fmt++; + continue; + } /* literal/noformat */ putchar(f.found); /* or whatever */ continue; } switch (f.found) { case 'y': /* handle mtime */ - tm = gmtime(&buf.st_mtime); + tm = gmtime(&buf->st_mtime); strftime(timestr, 1023, timefmt, tm); timestr[1023] = 0; printf("%s", timestr); break; + case 's': + printf("%jd", (intmax_t)buf->st_size); + break; case 'a': /* octal mode */ - printf("%o", (int)buf.st_mode & 07777); + printf("%o", (int)buf->st_mode & 07777); + break; + case 't': + if (S_ISBLK(buf->st_mode)) { + printf("block"); + } else if (S_ISCHR(buf->st_mode)) { + printf("character"); + } else if (S_ISDIR(buf->st_mode)) { + printf("directory"); + } else if (S_ISFIFO(buf->st_mode)) { + printf("fifo"); + } else if (S_ISREG(buf->st_mode)) { + printf("regular"); + } else if (S_ISLNK(buf->st_mode)) { + printf("symlink"); + } else if (S_ISSOCK(buf->st_mode)) { + printf("socket"); + } else { + printf("unknown"); + } break; case 'u': /* handle uid */ - printf("%0d", buf.st_uid); + printf("%0d", buf->st_uid); break; case 'U': /* handle username */ - pw = getpwuid(buf.st_uid); + pw = getpwuid(buf->st_uid); if (!pw) exit(1); printf("%s", pw->pw_name); break; case 'g': /* handle gid */ - printf("%0d", buf.st_gid); + printf("%0d", buf->st_gid); break; case 'G': /* handle groupname */ - gr = getgrgid(buf.st_gid); + gr = getgrgid(buf->st_gid); if (!gr) exit(1); printf("%s", gr->gr_name); break; + case 'n': + printf("%s", filename); + break; + case 'z': + putchar(0); break; + case '%': + putchar('%'); + break; default: /* handle unknown formatting character */ printf("%c", f.found); break; } } - putchar('\n'); + if (zeroterm & 2) { + return; + } + + if (zeroterm & 1 ) { + putchar(0); + } else { + putchar('\n'); + } } int main(int ac, char *av[]) { @@ -115,8 +168,10 @@ int main(int ac, char *av[]) { char *timefmt = "%s"; /* default time fmt is unix ts */ int errflg = 0; int c; + int uselstat = 0; + int zeroterm = 0; - while ((c = getopt(ac, av, "f:t:")) != -1) { + while ((c = getopt(ac, av, "lf:t:0")) != -1) { switch(c) { case 'f': fmt = optarg; @@ -129,6 +184,14 @@ int main(int ac, char *av[]) { "Option -%c requires an operand\n", optopt); errflg++; break; + case 'z': + zeroterm |= 2; + case '0': + zeroterm |= 1; + break; + case 'l': + uselstat = 1; + break; case '?': fprintf(stderr, "Unrecognized option: '-%c'\n", optopt); @@ -136,11 +199,17 @@ int main(int ac, char *av[]) { } } if (errflg) { - fprintf(stderr, "zpm-stat [-f ] [-t ]"); + fprintf(stderr, "zpm-stat [-f ] [-t ]\n"); exit(2); } for ( ; optind < ac; optind++) { - stat_one(fmt, timefmt, av[optind]); + struct stat buf; + if (uselstat) { + lstat(av[optind], &buf); + } else { + stat(av[optind], &buf); + } + stat_one(fmt, timefmt, zeroterm, av[optind], &buf); } return 0; }