diff options
| author | atatat <atatat@NetBSD.org> | 2004-12-31 03:24:31 +0000 |
|---|---|---|
| committer | atatat <atatat@NetBSD.org> | 2004-12-31 03:24:31 +0000 |
| commit | fa01c2e835a49d117a7d53eba33a005ab759f07f (patch) | |
| tree | 3cc111646a2b1b589e439fbf9a7110a53b08b2e2 /usr.bin/stat | |
| parent | 1fce7b094940c9dc751ccf653b1fdd6df98a363a (diff) | |
Add "dirname"- and "basename"-like functionality to stat(1) by way of
the "high" and "low" selectors on the filename.
Diffstat (limited to 'usr.bin/stat')
| -rw-r--r-- | usr.bin/stat/stat.1 | 23 | ||||
| -rw-r--r-- | usr.bin/stat/stat.c | 43 |
2 files changed, 54 insertions, 12 deletions
diff --git a/usr.bin/stat/stat.1 b/usr.bin/stat/stat.1 index eb2025357af..4db61f83040 100644 --- a/usr.bin/stat/stat.1 +++ b/usr.bin/stat/stat.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: stat.1,v 1.13 2004/09/01 23:26:10 wiz Exp $ +.\" $NetBSD: stat.1,v 1.14 2004/12/31 03:24:31 atatat Exp $ .\" .\" Copyright (c) 2002 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -34,7 +34,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd September 2, 2004 +.Dd December 31, 2004 .Dt STAT 1 .Os .Sh NAME @@ -281,8 +281,13 @@ the file .Dq type bits from the numeric forms of .Cm p , -and the long output form of -.Cm T . +the long output form of +.Cm T , +and the directory path from the +.Cm N +output similar to what +.Xr dirname 1 +would show. .It Cm L .Dq Low -- specifies the minor number for devices from @@ -300,13 +305,17 @@ and .Dq other bits from the numeric forms of .Cm p , -and the +the .Ic ls -F style output character for file type when used with .Cm T (the use of .Cm L -for this is optional). +for this is optional), and the filename component of the +.Cm N +output form similar to what +.Xr basename 1 +would display. .It Cm M .Dq Middle -- specifies the @@ -492,6 +501,8 @@ Apr 24 16:47:35 2002 /tmp/foo .Ed .Sh SEE ALSO .Xr file 1 , +.Xr basename 1 , +.Xr dirname 1 , .Xr ls 1 , .Xr lstat 2 , .Xr readlink 2 , diff --git a/usr.bin/stat/stat.c b/usr.bin/stat/stat.c index 4abc79bc819..05d6a7000d7 100644 --- a/usr.bin/stat/stat.c +++ b/usr.bin/stat/stat.c @@ -1,4 +1,4 @@ -/* $NetBSD: stat.c,v 1.19 2004/06/20 22:20:16 jmc Exp $ */ +/* $NetBSD: stat.c,v 1.20 2004/12/31 03:24:31 atatat Exp $ */ /* * Copyright (c) 2002 The NetBSD Foundation, Inc. @@ -42,7 +42,7 @@ #include <sys/cdefs.h> #if !defined(lint) -__RCSID("$NetBSD: stat.c,v 1.19 2004/06/20 22:20:16 jmc Exp $"); +__RCSID("$NetBSD: stat.c,v 1.20 2004/12/31 03:24:31 atatat Exp $"); #endif #if ! HAVE_NBTOOL_CONFIG_H @@ -325,8 +325,9 @@ main(int argc, char *argv[]) errs = 1; linkfail = 1; if (!quiet) - warn("%s: stat", - argc == 0 ? "(stdin)" : argv[0]); + warn("%s: %s", + argc == 0 ? "(stdin)" : argv[0], + usestat ? "stat" : "lstat"); } else output(&st, argv[0], statfmt, fn, nonl, quiet); @@ -842,10 +843,40 @@ format1(const struct stat *st, case SHOW_filename: small = 0; data = 0; - if (file == NULL) + if (file == NULL) { (void)strncpy(path, "(stdin)", sizeof(path)); - else + if (hilo == HIGH_PIECE || hilo == LOW_PIECE) + hilo = 0; + } + else if (hilo == 0) + (void)strncpy(path, file, sizeof(path)); + else { + char *s; (void)strncpy(path, file, sizeof(path)); + s = strrchr(path, '/'); + if (s != NULL) { + /* trim off trailing /'s */ + while (s != path && + s[0] == '/' && s[1] == '\0') + *s-- = '\0'; + s = strrchr(path, '/'); + } + if (hilo == HIGH_PIECE) { + if (s == NULL) + (void)strncpy(path, ".", sizeof(path)); + else { + while (s != path && s[0] == '/') + *s-- = '\0'; + } + hilo = 0; + } + else if (hilo == LOW_PIECE) { + if (s != NULL && s[1] != '\0') + (void)strncpy(path, s + 1, + sizeof(path)); + hilo = 0; + } + } sdata = path; formats = FMTF_STRING; if (ofmt == 0) |
