Implement -L flag for stat(1)

This commit is contained in:
sin 2013-08-12 10:11:55 +01:00
parent a7dc139c35
commit afc831a836
1 changed files with 10 additions and 3 deletions

13
stat.c
View File

@ -13,7 +13,7 @@ static void show_stat(const char *file, struct stat *st);
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s filename...\n", argv0); eprintf("usage: %s [-L] filename...\n", argv0);
} }
int int
@ -21,8 +21,13 @@ main(int argc, char *argv[])
{ {
struct stat st; struct stat st;
int i, ret = 0; int i, ret = 0;
int Lflag = 0;
int (*fn)(const char *, struct stat *);
ARGBEGIN { ARGBEGIN {
case 'L':
Lflag = 1;
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -34,8 +39,10 @@ main(int argc, char *argv[])
} }
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if (stat(argv[i], &st) == -1) { fn = Lflag ? stat : lstat;
fprintf(stderr, "stat %s: ", argv[i]); if (fn(argv[i], &st) == -1) {
fprintf(stderr, "%s %s: ", Lflag ? "stat" : "lstat",
argv[i]);
perror(NULL); perror(NULL);
ret = 1; ret = 1;
continue; continue;