From 50f27779b68fb36cdc2ed4d929c8d97a0f69fe9f Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 27 Jun 2019 13:27:01 -0700 Subject: 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. --- toys/posix/file.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'toys/posix') 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)); } } -- cgit v1.2.3