aboutsummaryrefslogtreecommitdiff
path: root/toys/posix
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2019-06-27 13:27:01 -0700
committerRob Landley <rob@landley.net>2019-06-28 09:33:31 -0500
commit50f27779b68fb36cdc2ed4d929c8d97a0f69fe9f (patch)
tree93141d5c491ddb66cad84bd94a282c28ed01a865 /toys/posix
parentccf574ecc4dd8af8fd045a9d6bbaf5e8bf66a8d2 (diff)
downloadtoybox-50f27779b68fb36cdc2ed4d929c8d97a0f69fe9f.tar.gz
file, stat: various small improvements.
file now shows the target of a symbolic link and calls out broken symbolic links. file now shows the device type for block/character special files. file now shows specific reason when it can't open. stat now includes the device type, plus a little more space between the number of blocks and the human-readable file type. Adjusted tests accordingly, which actually makes more of them pass on the host as a convenient side-effect, but I actually made these changes because I've been finding the desktop file and stat output more convenient in these cases.
Diffstat (limited to 'toys/posix')
-rw-r--r--toys/posix/file.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/toys/posix/file.c b/toys/posix/file.c
index 6b8c6779..2370be79 100644
--- a/toys/posix/file.c
+++ b/toys/posix/file.c
@@ -421,7 +421,7 @@ void file_main(void)
// Can't use loopfiles here because it doesn't call function when can't open
for (arg = toys.optargs; *arg; arg++) {
- char *name = *arg, *what = "cannot open";
+ char *name = *arg, *what = "unknown";
struct stat sb;
int fd = !strcmp(name, "-");
@@ -439,14 +439,20 @@ void file_main(void)
continue;
}
} else if (S_ISFIFO(sb.st_mode)) what = "fifo";
- else if (S_ISBLK(sb.st_mode)) what = "block special";
- else if (S_ISCHR(sb.st_mode)) what = "character special";
+ else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode))
+ sprintf(what = toybuf, "%s special (%u/%u)",
+ S_ISBLK(sb.st_mode) ? "block" : "character",
+ dev_major(sb.st_rdev), dev_minor(sb.st_rdev));
else if (S_ISDIR(sb.st_mode)) what = "directory";
else if (S_ISSOCK(sb.st_mode)) what = "socket";
- else if (S_ISLNK(sb.st_mode)) what = "symbolic link";
- else what = "unknown";
- }
+ else if (S_ISLNK(sb.st_mode)) {
+ char *lnk = xreadlink(name);
- xputs(what);
+ sprintf(what = toybuf, "%ssymbolic link to %s",
+ stat(lnk, &sb) ? "broken " : "", lnk);
+ free(lnk);
+ }
+ xputs(what);
+ } else xprintf("cannot open: %s\n", strerror(errno));
}
}