From 017f8027a18008ff7e60a1b2c5ba44d94671c37b Mon Sep 17 00:00:00 2001 From: ed Date: Wed, 11 Feb 2009 20:36:17 +0000 Subject: Let stat(1) use fdevname(3). Because we now have a reliable library function that converts file descriptors to character device names, let stat(1) use this. This means it can now do the following: $ stat -f %N /dev/pts/0 I've changed main() to set file properly, so output() is never called with file set to NULL. Approved by: dougb (older version, still used devname) --- usr.bin/stat/stat.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'usr.bin/stat') diff --git a/usr.bin/stat/stat.c b/usr.bin/stat/stat.c index 1f6d114..0b32f11 100644 --- a/usr.bin/stat/stat.c +++ b/usr.bin/stat/stat.c @@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$"); #define HAVE_DEVNAME 1 #endif /* HAVE_CONFIG_H */ +#include #include #include @@ -60,6 +61,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -204,6 +206,8 @@ main(int argc, char *argv[]) int ch, rc, errs, am_readlink; int lsF, fmtchar, usestat, fn, nonl, quiet; char *statfmt, *options, *synopsis; + char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV; + const char *file; am_readlink = 0; lsF = 0; @@ -304,22 +308,29 @@ main(int argc, char *argv[]) errs = 0; do { - if (argc == 0) + if (argc == 0) { + if (fdevname_r(STDIN_FILENO, dname + + sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL) + file = dname; + else + file = "(stdin)"; rc = fstat(STDIN_FILENO, &st); - else if (usestat) - rc = stat(argv[0], &st); - else - rc = lstat(argv[0], &st); + } else { + file = argv[0]; + if (usestat) + rc = stat(file, &st); + else + rc = lstat(file, &st); + } if (rc == -1) { errs = 1; linkfail = 1; if (!quiet) - warn("%s: stat", - argc == 0 ? "(stdin)" : argv[0]); + warn("%s: stat", file); } else - output(&st, argv[0], statfmt, fn, nonl, quiet); + output(&st, file, statfmt, fn, nonl, quiet); argv++; argc--; @@ -810,10 +821,7 @@ format1(const struct stat *st, case SHOW_filename: small = 0; data = 0; - if (file == NULL) - (void)strncpy(path, "(stdin)", sizeof(path)); - else - (void)strncpy(path, file, sizeof(path)); + (void)strncpy(path, file, sizeof(path)); sdata = path; formats = FMTF_STRING; if (ofmt == 0) -- cgit v1.1