From: Vivek Prakash Date: Mon, 18 Jul 2011 13:49:37 +0000 (+0200) Subject: import NetBSD stat X-Git-Tag: v3.2.0~449 X-Git-Url: http://zhaoyanbai.com/repos/%22https:/www.google.com/jsapi/static/gitweb.js?a=commitdiff_plain;h=49cab1c73cb9020b90a02d49166d6760e0fa30da;p=minix.git import NetBSD stat --- diff --git a/commands/Makefile b/commands/Makefile index bfb12d4ef..edfa5be99 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -25,7 +25,7 @@ SUBDIR= aal add_route adduser advent arp ash at autil awk \ ramdisk rarpd rawspeed rcp rdate readall readclock \ readfs reboot remsync rev rget rlogin rlogind rmdir \ rotate rsh rshd sed service setup shar size \ - sleep slip sort spell split srccrc stat strings ackstrip \ + sleep slip sort spell split srccrc strings ackstrip \ stty su sum svclog swapfs swifi sync synctree sysenv \ syslogd tail talk talkd tar tcpd tcpdp tcpstat tee telnet \ telnetd term termcap tget time tinyhalt top touch tr \ diff --git a/commands/stat/Makefile b/commands/stat/Makefile deleted file mode 100644 index b36e14d75..000000000 --- a/commands/stat/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -PROG= stat -MAN= - -LINKS+= ${BINDIR}/stat ${BINDIR}/readlink -LINKS+= ${BINDIR}/stat ${BINDIR}/fstat -LINKS+= ${BINDIR}/stat ${BINDIR}/lstat - -.include diff --git a/commands/stat/stat.c b/commands/stat/stat.c deleted file mode 100644 index c872088d4..000000000 --- a/commands/stat/stat.c +++ /dev/null @@ -1,389 +0,0 @@ -/* stat.c Feb 1987 - main, printit, statit - * - * stat - a program to perform what the stat(2) call does. - * - * usage: stat [-] [-all] - [- ...] [file1 file2 file3 ...] - * - * where is one of the struct stat fields without the leading "st_". - * The three times can be printed out as human times by requesting - * -Ctime instead of -ctime (upper case 1st letter). - * - means take the file names from stdin. - * -0.. means fd0.. - * no files means all fds. - * - * output: if only one field is specified, that fields' contents are printed. - * if more than one field is specified, the output is - * file filed1: f1val, field2: f2val, etc - * - * written: Larry McVoy, (mcvoy@rsch.wisc.edu) - */ - -# define ALLDEF /* Make -all default. (kjb) */ - -# include -# include -# include -# include -# include -# include -# include -# include -# include -# define addr(x) ((void*) &sbuf.x) -# define size(x) sizeof(sbuf.x) -# define equal(s, t) (strcmp(s, t) == 0) -# ifndef PATH_MAX -# define PATH_MAX 1024 -# endif -# undef LS_ADDS_SPACE /* AT&T Unix PC, ls prints "file[* /]" */ - /* This makes stat fail. */ - -# ifndef _MINIX /* All but Minix have u_* and st_blocks */ -# define BSD -# endif - -# ifndef S_IREAD -# define S_IREAD S_IRUSR -# define S_IWRITE S_IWUSR -# define S_IEXEC S_IXUSR -# endif - -char * arg0; -struct stat sbuf; -extern int errno; -int first_file= 1; -#ifndef S_IFLNK -#define lstat stat -#endif - -struct field { - char* f_name; /* field name in stat */ - u_char* f_addr; /* address of the field in sbuf */ - u_short f_size; /* size of the object, needed for pointer arith */ - u_short f_print; /* show this field? */ -} fields[] = { - { "dev", addr(st_dev), size(st_dev), 0 }, - { "ino", addr(st_ino), size(st_ino), 0 }, - { "mode", addr(st_mode), size(st_mode), 0 }, - { "nlink", addr(st_nlink), size(st_nlink), 0 }, - { "uid", addr(st_uid), size(st_uid), 0 }, - { "gid", addr(st_gid), size(st_gid), 0 }, - { "rdev", addr(st_rdev), size(st_rdev), 0 }, - { "size", addr(st_size), size(st_size), 0 }, - { "Atime", addr(st_atime), size(st_atime), 0 }, - { "atime", addr(st_atime), size(st_atime), 0 }, - { "Mtime", addr(st_mtime), size(st_mtime), 0 }, - { "mtime", addr(st_mtime), size(st_mtime), 0 }, - { "Ctime", addr(st_ctime), size(st_ctime), 0 }, - { "ctime", addr(st_ctime), size(st_ctime), 0 }, - { "blksize", addr(st_blksize), size(st_blksize), 0 }, - { "blocks", addr(st_blocks), size(st_blocks), 0 }, - { NULL, 0, 0, 0 }, -}; - -void printstat(struct stat *sbuf, int nprint); -void printit(struct stat* sb, struct field* f, int n); -void rwx(mode_t mode, char *bit); -void usage(void); - -int do_readlink=0; - -int main(int ac, char** av) -{ - int i, j, nprint = 0, files = 0; - char buf[PATH_MAX], *check; - int sym=0, ret=0, from_stdin = 0; - int err; - u_long fd; - - if ((arg0 = strrchr(av[0], '/')) == NULL) arg0 = av[0]; else arg0++; -#ifdef S_IFLNK - if (equal(arg0, "lstat")) sym = 1; - if (equal(arg0, "readlink")) do_readlink = 1; -#endif - - if (ac > 1 && equal(av[i = 1], "-")) - i++, from_stdin++; - - for (i= 1; i= INT_MAX) { - err= -1; - errno= EBADF; - } - else { - err= fstat((int) fd, &sbuf); - } - if (err != -1) { - if (!first_file) fputc('\n', stdout); - if (files != 1) printf("fd %lu:\n", fd); - printstat(&sbuf, nprint); - } - else { - fprintf(stderr, "fd %lu: %s\n", fd, strerror(errno)); - ret= 1; - } - continue; - } - if(do_readlink) { - char sbuf[300]; - int n; - if((n=err=readlink(av[i], sbuf, sizeof(sbuf)-1)) < 0) { - perror(av[i]); - continue; - } - sbuf[n] = '\0'; - printf("%s: %s\n", av[i], sbuf); - continue; - } - if (!sym) err= stat(av[i], &sbuf); - if (sym || (err != 0 && errno == ENOENT)) err= lstat(av[i], &sbuf); - if (err != -1) { - if (!first_file) fputc('\n', stdout); - if (files != 1) printf("%s:\n", av[i]); - printstat(&sbuf, nprint); - } - else { - fprintf(stderr, "%s: %s: %s\n", arg0, av[i], strerror(errno)); - ret= 1; - } - } - exit(ret); -} - -/*------------------------------------------------------------------30/Jan/87-* - * printstat(file, nprint) - do the work - *----------------------------------------------------------------larry mcvoy-*/ -void printstat(struct stat *sbuf, int nprint) -{ - int j; - int first_field= 1; - - for (j=0; fields[j].f_name; j++) { - if (fields[j].f_print) { - if (!first_field) fputc('\n', stdout); - printit(sbuf, &fields[j], nprint); - first_field= 0; - } - } - fputc('\n', stdout); - first_file= 0; -} - -/*------------------------------------------------------------------30/Jan/87-* - * printit(sb, f, n) - print the field - * - * Inputs -> (struct stat*), (struct field*), (int) - * - * Results -> Displays the field, with special handling of weird fields like - * mode and mtime. The mode field is dumped in octal, followed - * by one or more of the S_IF and/or S_I values. - *----------------------------------------------------------------larry mcvoy-*/ -void printit(struct stat* sb, struct field* f, int n) -{ - if (n > 1) - printf("%s: ", f->f_name); - if (equal(f->f_name, "mode")) { - /* This lot changed to my personal liking. (kjb) */ - char bit[11]; - - printf("%07lo, ", (u_long) sb->st_mode); - - strcpy(bit, "----------"); - - switch (sb->st_mode&S_IFMT) { - case S_IFDIR: bit[0]='d'; break; -# ifdef S_IFFIFO - case S_IFFIFO: bit[0]='p'; break; -# endif - case S_IFCHR: bit[0]='c'; break; - case S_IFBLK: bit[0]='b'; break; -# ifdef S_IFSOCK - case S_IFSOCK: bit[0]='S'; break; -# endif -# ifdef S_IFMPC - case S_IFMPC: bit[0]='C'; break; -# endif -# ifdef S_IFMPB - case S_IFMPB: bit[0]='B'; break; -# endif -# ifdef S_IFLNK - case S_IFLNK: bit[0]='l'; break; -# endif - } - rwx(sb->st_mode, bit+1); - rwx(sb->st_mode<<3, bit+4); - rwx(sb->st_mode<<6, bit+7); - if (sb->st_mode&S_ISUID) bit[3]='s'; - if (sb->st_mode&S_ISGID) bit[6]='s'; - if (sb->st_mode&S_ISVTX) bit[9]='t'; - printf("\"%s\"", bit); - } - /* times in human form, uppercase first letter */ - else if (equal("Ctime", f->f_name)) { - printf("%.24s (%lu)", ctime(&sb->st_ctime), (u_long) sb->st_ctime); - f[1].f_print= 0; - } - else if (equal("Mtime", f->f_name)) { - printf("%.24s (%lu)", ctime(&sb->st_mtime), (u_long) sb->st_mtime); - f[1].f_print= 0; - } - else if (equal("Atime", f->f_name)) { - printf("%.24s (%lu)", ctime(&sb->st_atime), (u_long) sb->st_atime); - f[1].f_print= 0; - } - else if (equal("ctime", f->f_name)) { - printf("%lu", (u_long) sb->st_ctime); - } - else if (equal("mtime", f->f_name)) { - printf("%lu", (u_long) sb->st_mtime); - } - else if (equal("atime", f->f_name)) { - printf("%lu", (u_long) sb->st_atime); - } - else { - switch (f->f_size) { - case sizeof(char): - printf("%d", * (u_char *) f->f_addr); - break; - case sizeof(short): - printf("%u", (u_int) * (u_short *) f->f_addr); - break; -#if INT_MAX != SHRT_MAX - case sizeof(int): - printf("%u", * (u_int *) f->f_addr); - break; -#endif -#if LONG_MAX != INT_MAX && LONG_MAX != SHRT_MAX - case sizeof(long): - printf("%lu", * (u_long *) f->f_addr); - break; -#endif - default: - fprintf(stderr, "\nProgram error: bad '%s' field size %d\n", - f->f_name, f->f_size); - break; - } - } -} - -void rwx(mode_t mode, char *bit) -{ - if (mode&S_IREAD) bit[0]='r'; - if (mode&S_IWRITE) bit[1]='w'; - if (mode&S_IEXEC) bit[2]='x'; -} - -void usage(void) -{ - fprintf(stderr, - "Usage: %s [-] [-fd] [-all] [-s] [-field ...] [file1 ...]\n", - arg0); - exit(1); -} diff --git a/man/man1/Makefile b/man/man1/Makefile index 92184b405..557bd9aea 100644 --- a/man/man1/Makefile +++ b/man/man1/Makefile @@ -18,7 +18,7 @@ MAN= acd.1 anm.1 ar.1 ash.1 asize.1 at.1 banner.1 basename.1 \ profile.1 ps.1 pwd.1 rcp.1 readall.1 readfs.1 recwave.1 \ ref.1 remsync.1 rget.1 rlogin.1 rmdir.1 rsh.1 rz.1 \ shar.1 size.1 sleep.1 sort.1 soundoff.1 soundon.1 spell.1 \ - split.1 stat.1 strings.1 strip.1 stty.1 su.1 sum.1 svc.1 \ + split.1 strings.1 strip.1 stty.1 su.1 sum.1 svc.1 \ synctree.1 sysenv.1 sz.1 tail.1 tee.1 telnet.1 template.1 \ term.1 termcap.1 tget.1 time.1 top.1 tr.1 true.1 \ truncate.1 tsort.1 tty.1 umount.1 uname.1 unexpand.1 uniq.1 \ diff --git a/man/man1/stat.1 b/man/man1/stat.1 deleted file mode 100644 index 8e9c222be..000000000 --- a/man/man1/stat.1 +++ /dev/null @@ -1,111 +0,0 @@ -.TH STAT 1 -.SH NAME -stat, lstat, readlink \- provide a shell interface to the stat(2) system call -.SH SYNOPSIS -.B stat -.RB [ - ] -.RB [ -\fIfd ] -.RB [ -all ] -.RB [ -s ] -.RB [ -\fIfield " ...]" -.RI [ file1 " ...]" -.SH DESCRIPTION -.B Stat -does little more than provide access to the fields in the -.B struct stat -as defined in the -.BR stat (2) -manual page. Each field that is to be listed -is specified as the field name without the leading -.BR st_ . -This and the other two options are described below. All options are then -applied to the files listed. If -.B stat -is called as -.B lstat -then the -.BR lstat (2) -system call is used, if called as -.B stat -symbolic links are expanded with -.BR stat (2). -If called as -.B readlink -then the output is only the contents of the symbolic link. -.PP -If no fields are named then all fields are printed. If no files are listed -then all open filedescriptors are printed. -.SH OPTIONS -.TP -.B \- -If the first argument is ``\-'', the list of files is assumed to come from stdin. -This is useful for things like ``ls | stat \-uid \-mtime.'' -.B \-\fIfd -If an argument is a ``\-'' followed by a number then that number is used as -a file descriptor whose information is to be printed. -.TP -.B \-all -List all fields for each file. -.TP -.B \-s -Use -.BR lstat (2). -.TP -.B \-mode -List the -.B mode -field. Similarly for -.BR ino , -.BR dev , -.BR rdev , -.BR nlink , -.BR uid , -.BR gid , -.BR size , -.BR atime , -.BR mtime , -and -.BR ctime . -Under BSD derived systems you also have -.B blksize -and -.BR blocks . -.PP -.B \-Atime -.br -.B \-Mtime -.br -.B \-Ctime -.RS -The lower case versions of these three options display the time as an integer -that is the ``seconds since 00:00 Jan 1. 1970.'' -Listing the fields with the first letter -in caps causes the times to be printed in -.BR ctime (3) -format (i.e., human readable). -.RE -.SH EXAMPLES -.LP -# Find out the number of links to each file -.br -$ stat \-nlink *.c -.LP -# sort files by age (much like ls \-t) -.br -$ stat \-atime * | sort +1 -.LP -# Find out which file is older in sh(1) -.br -if test `stat -mtime $1` -lt `stat -mtime $2`; then -.br - echo $1 is older than $2 -.br -else -.br - echo $2 is older than $1 -.br -fi -.SH "SEE ALSO" -.BR stat (2). -.SH AUTHOR -Larry McVoy (mcvoy@rsch.wisc.edu) diff --git a/tools/nbsd_ports b/tools/nbsd_ports index e639d3098..564e990cb 100644 --- a/tools/nbsd_ports +++ b/tools/nbsd_ports @@ -5,3 +5,4 @@ lib/libcrypt src/lib/libcrypt nbsd_include src/include usr.bin/m4 src/usr.bin/m4 usr.bin/indent src/usr.bin/indent +usr.bin/stat src/usr.bin/stat diff --git a/usr.bin/Makefile b/usr.bin/Makefile index 7077a048c..94eb6410c 100644 --- a/usr.bin/Makefile +++ b/usr.bin/Makefile @@ -3,7 +3,7 @@ .include # NetBSD imports -SUBDIR= indent m4 mkimage +SUBDIR= indent m4 mkimage stat # Non-NetBSD imports SUBDIR+= ministat diff --git a/usr.bin/stat/Makefile b/usr.bin/stat/Makefile new file mode 100644 index 000000000..c2cd6e1cc --- /dev/null +++ b/usr.bin/stat/Makefile @@ -0,0 +1,15 @@ +# $NetBSD: Makefile,v 1.7 2003/07/25 03:21:17 atatat Exp $ + +PROG= stat + +.if !defined(HOSTPROG) +LINKS= ${BINDIR}/stat ${BINDIR}/readlink +.if defined(__MINIX) +# To prevent breaking of existing utilities in commands/ +LINKS+= ${BINDIR}/stat ${BINDIR}/fstat +LINKS+= ${BINDIR}/stat ${BINDIR}/lstat +.endif +MLINKS= stat.1 readlink.1 +.endif + +.include diff --git a/usr.bin/stat/minix-port.patch b/usr.bin/stat/minix-port.patch new file mode 100644 index 000000000..bb5dfec52 --- /dev/null +++ b/usr.bin/stat/minix-port.patch @@ -0,0 +1,31 @@ +diff -ru nbsdsrc/src/usr.bin/stat/Makefile usr.bin/stat/Makefile +--- nbsdsrc/src/usr.bin/stat/Makefile ++++ usr.bin/stat/Makefile +@@ -4,6 +4,11 @@ + + .if !defined(HOSTPROG) + LINKS= ${BINDIR}/stat ${BINDIR}/readlink ++.if defined(__MINIX) ++# To prevent breaking of existing utilities in commands/ ++LINKS+= ${BINDIR}/stat ${BINDIR}/fstat ++LINKS+= ${BINDIR}/stat ${BINDIR}/lstat ++.endif + MLINKS= stat.1 readlink.1 + .endif + +diff -ru nbsdsrc/src/usr.bin/stat/stat.c usr.bin/stat/stat.c +--- nbsdsrc/src/usr.bin/stat/stat.c ++++ usr.bin/stat/stat.c +@@ -44,7 +44,12 @@ + #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1 + #define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1 + #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1 ++#ifdef __minix ++/* Not supported in Minix. */ ++#define HAVE_DEVNAME 0 ++#else /* __minix */ + #define HAVE_DEVNAME 1 ++#endif /* __minx */ + #endif /* HAVE_NBTOOL_CONFIG_H */ + + #include diff --git a/usr.bin/stat/stat.1 b/usr.bin/stat/stat.1 new file mode 100644 index 000000000..98c279576 --- /dev/null +++ b/usr.bin/stat/stat.1 @@ -0,0 +1,569 @@ +.\" $NetBSD: stat.1,v 1.28 2010/04/05 21:25:01 joerg Exp $ +.\" +.\" Copyright (c) 2002-2005 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Andrew Brown and Jan Schaumann. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd November 7, 2008 +.Dt STAT 1 +.Os +.Sh NAME +.Nm stat , +.Nm readlink +.Nd display file status +.Sh SYNOPSIS +.Nm +.Op Fl FLnq +.Oo +.Fl f Ar format | +.Fl l | +.Fl r | +.Fl s | +.Fl x +.Oc +.Op Fl t Ar timefmt +.Op Ar +.Nm readlink +.Op Fl fn +.Op Ar +.Sh DESCRIPTION +The +.Nm +utility displays information about the file pointed to by +.Ar file . +Read, write, or execute permissions of the named file are not required, but +all directories listed in the pathname leading to the file must be +searchable. +If no argument is given, +.Nm +displays information about the file descriptor for standard input. +.Pp +When invoked as +.Nm readlink , +only the target of the symbolic link is printed. +If the given argument is not a symbolic link and the +.Fl f +option is not specified, +.Nm readlink +will print nothing and exit with an error. +If the +.Fl f +option is specified, the output is canonicalized by following every symlink +in every component of the given path recursively. +.Nm readlink +will resolve both absolute and relative paths, and return the absolute pathname +corresponding to +.Ar file . +In this case, the argument does not need to be a symbolic link. +.Pp +The information displayed is obtained by calling +.Xr lstat 2 +with the given argument and evaluating the returned structure. +The default format displays the +.Fa st_dev , +.Fa st_ino , +.Fa st_mode , +.Fa st_nlink , +.Fa st_uid , +.Fa st_gid , +.Fa st_rdev , +.Fa st_size , +.Fa st_atime , +.Fa st_mtime , +.Fa st_ctime , +.Fa st_birthtime , +.Fa st_blksize , +.Fa st_blocks , +and +.Fa st_flags +fields, in that order. +.Pp +The options are as follows: +.Bl -tag -width XFXformatXXX +.It Fl F +As in +.Xr ls 1 , +display a slash +.Pq Sq / +immediately after each pathname that is a directory, an +asterisk +.Pq Sq * +after each that is executable, an at sign +.Pq Sq @ +after each symbolic link, a percent sign +.Pq Sq % +after each whiteout, an equal sign +.Pq Sq = +after each socket, and a vertical bar +.Pq Sq \&| +after each that is a FIFO. +The use of +.Fl F +implies +.Fl l . +.It Fl f Ar format +Display information using the specified format. +See the +.Sx FORMATS +section for a description of valid formats. +.It Fl L +Use +.Xr stat 2 +instead of +.Xr lstat 2 . +The information reported by +.Nm +will refer to the target of +.Ar file , +if file is a symbolic link, and not to +.Ar file +itself. +.It Fl l +Display output in +.Ic ls Fl lT +format. +.It Fl n +Do not force a newline to appear at the end of each piece of output. +.It Fl q +Suppress failure messages if calls to +.Xr stat 2 +or +.Xr lstat 2 +fail. +When run as +.Nm readlink , +error messages are automatically suppressed. +.It Fl r +Display raw information. +That is, for all the fields in the stat-structure, +display the raw, numerical value (for example, times in seconds since the +epoch, etc.) +.It Fl s +Display information in +.Dq shell output , +suitable for initializing variables. +.It Fl t Ar timefmt +Display timestamps using the specified format. +This format is +passed directly to +.Xr strftime 3 . +.It Fl x +Display information in a more verbose way as known from some Linux +distributions. +.El +.Ss FORMATS +Format strings are similar to +.Xr printf 3 +formats in that they start with +.Cm % , +are then followed by a sequence of formatting characters, and end in +a character that selects the field of the struct stat which is to be +formatted. +If the +.Cm % +is immediately followed by one of +.Cm n , +.Cm t , +.Cm % , +or +.Cm @ , +then a newline character, a tab character, a percent character, +or the current file number is printed, otherwise the string is +examined for the following: +.Pp +Any of the following optional flags: +.Bl -tag -width Ds +.It Cm # +Selects an alternate output form for octal and hexadecimal output. +Non-zero octal output will have a leading zero, and non-zero +hexadecimal output will have +.Dq 0x +prepended to it. +.It Cm + +Asserts that a sign indicating whether a number is positive or negative +should always be printed. +Non-negative numbers are not usually printed with a sign. +.It Cm - +Aligns string output to the left of the field, instead of to the right. +.It Cm 0 +Sets the fill character for left padding to the 0 character, instead of +a space. +.It space +Reserves a space at the front of non-negative signed output fields. +A +.Sq Cm + +overrides a space if both are used. +.El +.Pp +Then the following fields: +.Bl -tag -width Ds +.It Cm size +An optional decimal digit string specifying the minimum field width. +.It Cm prec +An optional precision composed of a decimal point +.Sq Cm \&. +and a decimal digit string that indicates the maximum string length, +the number of digits to appear after the decimal point in floating point +output, or the minimum number of digits to appear in numeric output. +.It Cm fmt +An optional output format specifier which is one of +.Cm D , +.Cm O , +.Cm U , +.Cm X , +.Cm F , +or +.Cm S . +These represent signed decimal output, octal output, unsigned decimal +output, hexadecimal output, floating point output, and string output, +respectively. +Some output formats do not apply to all fields. +Floating point output only applies to timespec fields (the +.Cm a , +.Cm m , +and +.Cm c +fields). +.Pp +The special output specifier +.Cm S +may be used to indicate that the output, if +applicable, should be in string format. +May be used in combination with +.Bl -tag -width Ds +.It Cm amc +Display date in strftime(3) format. +.It Cm dr +Display actual device name. +.It Cm gu +Display group or user name. +.It Cm p +Display the mode of +.Ar file +as in +.Ic ls -lTd . +.It Cm N +Displays the name of +.Ar file . +.It Cm T +Displays the type of +.Ar file . +.It Cm Y +Insert a `` -\*[Gt] '' into the output. +Note that the default output format for +.Cm Y +is a string, but if specified explicitly, these four characters are +prepended. +.El +.It Cm sub +An optional sub field specifier (high, middle, or low). +Only applies to the +.Cm p , +.Cm d , +.Cm r , +.Cm T , +.Cm N , +and +.Cm z +output formats. +It can be one of the following: +.Bl -tag -width Ds +.It Cm H +.Dq High +-- depending on the +.Cm datum : +.Bl -tag -compact -width door +.It Cm d , r +Major number for devices +.It Cm p +.Dq User +bits from the string form of permissions or the file +.Dq type +bits from the numeric forms +.It Cm T +The long output form of file type +.It Cm N +Directory path of the file, similar to what +.Xr dirname 1 +would show +.It Cm z +File size, rounded to the nearest gigabyte +.El +.It Cm M +.Dq Middle +-- depending on the +.Cm datum : +.Bl -tag -compact -width door +.It Cm p +The +.Dq group +bits from the string form of permissions or the +.Dq suid , +.Dq sgid , +and +.Dq sticky +bits from the numeric forms +.It Cm z +File size, rounded to the nearest megabyte +.El +.It Cm L +.Dq Low +-- depending on the +.Cm datum : +.Bl -tag -compact -width door +.It Cm r , d +Minor number for devices +.It Cm p +The +.Dq other +bits from the string form of permissions or the +.Dq user , +.Dq group , +and +.Dq other +bits from the numeric forms +.It Cm T +The +.Ic ls -F +style output character for file type (the use of +.Cm L +here is optional) +.It Cm N +Base filename of the file, similar to what +.Xr basename 1 +would show +.It Cm z +File size, rounded to the nearest kilobyte +.El +.El +.It Cm datum +A required field specifier, being one of the following: +.Bl -tag -width 11n +.It Cm d +Device upon which +.Ar file +resides +.Pq Fa st_dev . +.It Cm i +.Ar file Ap s +inode number +.Pq Fa st_ino . +.It Cm p +File type and permissions +.Pq Fa st_mode . +.It Cm l +Number of hard links to +.Ar file +.Pq Fa st_nlink . +.It Cm u , g +User-id and group-id of +.Ar file Ap s +owner +.Pq Fa st_uid , st_gid . +.It Cm r +Device number for character and block device special files +.Pq Fa st_rdev . +.It Cm a , m , c , B +The time +.Ar file +was last accessed or modified, or when the inode was last changed, or +the birth time of the inode +.Pq Fa st_atime , st_mtime , st_ctime, st_birthtime . +.It Cm z +The size of +.Ar file +in bytes +.Pq Fa st_size . +.It Cm b +Number of blocks allocated for +.Ar file +.Pq Fa st_blocks . +.It Cm k +Optimal file system I/O operation block size +.Pq Fa st_blksize . +.It Cm f +User defined flags for +.Ar file +.Pq Fa st_flags . +.It Cm v +Inode generation number +.Pq Fa st_gen . +.El +.Pp +The following five field specifiers are not drawn directly from the +data in struct stat, but are: +.Bl -tag -width Ds +.It Cm N +The name of the file. +.It Cm R +The absolute pathname corresponding to the file. +.It Cm T +The file type, either as in +.Ic ls -F +or in a more descriptive form if the sub field specifier +.Cm H +is given. +.It Cm Y +The target of a symbolic link. +.It Cm Z +Expands to +.Dq major,minor +from the rdev field for character or block +special devices and gives size output for all others. +.El +.El +.Pp +Only the +.Cm % +and the field specifier are required. +Most field specifiers default to +.Cm U +as an output form, with the +exception of +.Cm p +which defaults to +.Cm O ; +.Cm a , m , +and +.Cm c +which default to +.Cm D ; +and +.Cm Y , T , +and +.Cm N , +which default to +.Cm S . +.Sh EXIT STATUS +.Nm +exits 0 on success, and \*[Gt]0 if an error occurred. +.Sh EXAMPLES +If no options are specified, the default format is +"%d %i %Sp %l %Su %Sg %r %z \e"%Sa\e" \e"%Sm\e" \e"%Sc\e" \e"%SB\e" %k %b %#Xf %N". +.Bd -literal -offset indent +\*[Gt] stat /tmp/bar +0 78852 -rw-r--r-- 1 root wheel 0 0 "Jul 8 10:26:03 2004" "Jul 8 10:26:03 2004" "Jul 8 10:28:13 2004" "Jan 1 09:00:00 1970" 16384 0 0 /tmp/bar +.Ed +.Pp +Given a symbolic link +.Dq foo +that points from +.Pa /tmp/foo +to +.Pa / , +you would use +.Nm +as follows: +.Bd -literal -offset indent +\*[Gt] stat -F /tmp/foo +lrwxrwxrwx 1 jschauma cs 1 Apr 24 16:37:28 2002 /tmp/foo@ -\*[Gt] / + +\*[Gt] stat -LF /tmp/foo +drwxr-xr-x 16 root wheel 512 Apr 19 10:57:54 2002 /tmp/foo/ +.Ed +.Pp +To initialize some shell-variables, you could use the +.Fl s +flag as follows: +.Bd -literal -offset indent +\*[Gt] csh +% eval set `stat -s .cshrc` +% echo $st_size $st_mtime +1148 1015432481 + +\*[Gt] sh +$ eval $(stat -s .profile) +$ echo $st_size $st_mtime +1148 1015432481 +.Ed +.Pp +In order to get a list of the kind of files including files pointed to if the +file is a symbolic link, you could use the following format: +.Bd -literal -offset indent +$ stat -f "%N: %HT%SY" /tmp/* +/tmp/bar: Symbolic Link -\*[Gt] /tmp/foo +/tmp/output25568: Regular File +/tmp/blah: Directory +/tmp/foo: Symbolic Link -\*[Gt] / +.Ed +.Pp +In order to get a list of the devices, their types and the major and minor +device numbers, formatted with tabs and linebreaks, you could use the +following format: +.Bd -literal -offset indent +stat -f "Name: %N%n%tType: %HT%n%tMajor: %Hr%n%tMinor: %Lr%n%n" /dev/* +[...] +Name: /dev/wt8 + Type: Block Device + Major: 3 + Minor: 8 + +Name: /dev/zero + Type: Character Device + Major: 2 + Minor: 12 +.Ed +.Pp +In order to determine the permissions set on a file separately, you could use +the following format: +.Bd -literal -offset indent +\*[Gt] stat -f "%Sp -\*[Gt] owner=%SHp group=%SMp other=%SLp" . +drwxr-xr-x -\*[Gt] owner=rwx group=r-x other=r-x +.Ed +.Pp +In order to determine the three files that have been modified most recently, +you could use the following format: +.Bd -literal -offset indent +\*[Gt] stat -f "%m%t%Sm %N" /tmp/* | sort -rn | head -3 | cut -f2- +Apr 25 11:47:00 2002 /tmp/blah +Apr 25 10:36:34 2002 /tmp/bar +Apr 24 16:47:35 2002 /tmp/foo +.Ed +.Sh SEE ALSO +.Xr basename 1 , +.Xr dirname 1 , +.Xr file 1 , +.Xr ls 1 , +.Xr lstat 2 , +.Xr readlink 2 , +.Xr stat 2 , +.Xr printf 3 , +.Xr strftime 3 +.Sh HISTORY +The +.Nm +utility appeared in +.Nx 1.6 . +.Sh AUTHORS +.An -nosplit +The +.Nm +utility was written by +.An Andrew Brown +.Aq atatat@NetBSD.org . +This man page was written by +.An Jan Schaumann +.Aq jschauma@NetBSD.org . diff --git a/usr.bin/stat/stat.c b/usr.bin/stat/stat.c new file mode 100644 index 000000000..8beadb3bf --- /dev/null +++ b/usr.bin/stat/stat.c @@ -0,0 +1,1111 @@ +/* $NetBSD: stat.c,v 1.33 2011/01/15 22:54:10 njoly Exp $ */ + +/* + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Andrew Brown. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if !defined(lint) +__RCSID("$NetBSD: stat.c,v 1.33 2011/01/15 22:54:10 njoly Exp $"); +#endif + +#if ! HAVE_NBTOOL_CONFIG_H +#define HAVE_STRUCT_STAT_ST_FLAGS 1 +#define HAVE_STRUCT_STAT_ST_GEN 1 +#define HAVE_STRUCT_STAT_ST_BIRTHTIME 1 +#define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1 +#define HAVE_STRUCT_STAT_ST_MTIMENSEC 1 +#ifdef __minix +/* Not supported in Minix. */ +#define HAVE_DEVNAME 0 +#else /* __minix */ +#define HAVE_DEVNAME 1 +#endif /* __minx */ +#endif /* HAVE_NBTOOL_CONFIG_H */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if HAVE_STRUCT_STAT_ST_FLAGS +#define DEF_F "%#Xf " +#define RAW_F "%f " +#define SHELL_F " st_flags=%f" +#else /* HAVE_STRUCT_STAT_ST_FLAGS */ +#define DEF_F +#define RAW_F +#define SHELL_F +#endif /* HAVE_STRUCT_STAT_ST_FLAGS */ + +#if HAVE_STRUCT_STAT_ST_BIRTHTIME +#define DEF_B "\"%SB\" " +#define RAW_B "%B " +#define SHELL_B "st_birthtime=%B " +#else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ +#define DEF_B +#define RAW_B +#define SHELL_B +#endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ + +#if HAVE_STRUCT_STAT_ST_ATIM +#define st_atimespec st_atim +#define st_ctimespec st_ctim +#define st_mtimespec st_mtim +#endif /* HAVE_STRUCT_STAT_ST_ATIM */ + +#define DEF_FORMAT \ + "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \ + "%k %b " DEF_F "%N" +#define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \ + "%k %b " RAW_F "%N" +#define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY" +#define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY" +#define SHELL_FORMAT \ + "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \ + "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \ + "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \ + "st_blksize=%k st_blocks=%b" SHELL_F +#define LINUX_FORMAT \ + " File: \"%N\"%n" \ + " Size: %-11z FileType: %HT%n" \ + " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \ + "Device: %Hd,%Ld Inode: %i Links: %l%n" \ + "Access: %Sa%n" \ + "Modify: %Sm%n" \ + "Change: %Sc" + +#define TIME_FORMAT "%b %e %T %Y" + +#define FLAG_POUND 0x01 +#define FLAG_SPACE 0x02 +#define FLAG_PLUS 0x04 +#define FLAG_ZERO 0x08 +#define FLAG_MINUS 0x10 + +/* + * These format characters must all be unique, except the magic one. + */ +#define FMT_MAGIC '%' +#define FMT_DOT '.' + +#define SIMPLE_NEWLINE 'n' +#define SIMPLE_TAB 't' +#define SIMPLE_PERCENT '%' +#define SIMPLE_NUMBER '@' + +#define FMT_POUND '#' +#define FMT_SPACE ' ' +#define FMT_PLUS '+' +#define FMT_ZERO '0' +#define FMT_MINUS '-' + +#define FMT_DECIMAL 'D' +#define FMT_OCTAL 'O' +#define FMT_UNSIGNED 'U' +#define FMT_HEX 'X' +#define FMT_FLOAT 'F' +#define FMT_STRING 'S' + +#define FMTF_DECIMAL 0x01 +#define FMTF_OCTAL 0x02 +#define FMTF_UNSIGNED 0x04 +#define FMTF_HEX 0x08 +#define FMTF_FLOAT 0x10 +#define FMTF_STRING 0x20 + +#define HIGH_PIECE 'H' +#define MIDDLE_PIECE 'M' +#define LOW_PIECE 'L' + +#define SHOW_realpath 'R' +#define SHOW_st_dev 'd' +#define SHOW_st_ino 'i' +#define SHOW_st_mode 'p' +#define SHOW_st_nlink 'l' +#define SHOW_st_uid 'u' +#define SHOW_st_gid 'g' +#define SHOW_st_rdev 'r' +#define SHOW_st_atime 'a' +#define SHOW_st_mtime 'm' +#define SHOW_st_ctime 'c' +#define SHOW_st_btime 'B' +#define SHOW_st_size 'z' +#define SHOW_st_blocks 'b' +#define SHOW_st_blksize 'k' +#define SHOW_st_flags 'f' +#define SHOW_st_gen 'v' +#define SHOW_symlink 'Y' +#define SHOW_filetype 'T' +#define SHOW_filename 'N' +#define SHOW_sizerdev 'Z' + +void usage(const char *); +void output(const struct stat *, const char *, + const char *, int, int); +int format1(const struct stat *, /* stat info */ + const char *, /* the file name */ + const char *, int, /* the format string itself */ + char *, size_t, /* a place to put the output */ + int, int, int, int, /* the parsed format */ + int, int); + +const char *timefmt; +int linkfail; + +#define addchar(s, c, nl) \ + do { \ + (void)fputc((c), (s)); \ + (*nl) = ((c) == '\n'); \ + } while (0/*CONSTCOND*/) + +int +main(int argc, char *argv[]) +{ + struct stat st; + int ch, rc, errs, am_readlink; + int lsF, fmtchar, usestat, fn, nonl, quiet; + const char *statfmt, *options, *synopsis; + + am_readlink = 0; + lsF = 0; + fmtchar = '\0'; + usestat = 0; + nonl = 0; + quiet = 0; + linkfail = 0; + statfmt = NULL; + timefmt = NULL; + + if (strcmp(getprogname(), "readlink") == 0) { + am_readlink = 1; + options = "fn"; + synopsis = "[-fn] [file ...]"; + statfmt = "%Y"; + fmtchar = 'f'; + quiet = 1; + } else { + options = "f:FlLnqrst:x"; + synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]"; + } + + while ((ch = getopt(argc, argv, options)) != -1) + switch (ch) { + case 'F': + lsF = 1; + break; + case 'L': + usestat = 1; + break; + case 'n': + nonl = 1; + break; + case 'q': + quiet = 1; + break; + case 'f': + if (am_readlink) { + statfmt = "%R"; + break; + } + statfmt = optarg; + /* FALLTHROUGH */ + case 'l': + case 'r': + case 's': + case 'x': + if (fmtchar != 0) + errx(1, "can't use format '%c' with '%c'", + fmtchar, ch); + fmtchar = ch; + break; + case 't': + timefmt = optarg; + break; + default: + usage(synopsis); + } + + argc -= optind; + argv += optind; + fn = 1; + + if (fmtchar == '\0') { + if (lsF) + fmtchar = 'l'; + else { + fmtchar = 'f'; + statfmt = DEF_FORMAT; + } + } + + if (lsF && fmtchar != 'l') + errx(1, "can't use format '%c' with -F", fmtchar); + + switch (fmtchar) { + case 'f': + /* statfmt already set */ + break; + case 'l': + statfmt = lsF ? LSF_FORMAT : LS_FORMAT; + break; + case 'r': + statfmt = RAW_FORMAT; + break; + case 's': + statfmt = SHELL_FORMAT; + break; + case 'x': + statfmt = LINUX_FORMAT; + if (timefmt == NULL) + timefmt = "%c"; + break; + default: + usage(synopsis); + /*NOTREACHED*/ + } + + if (timefmt == NULL) + timefmt = TIME_FORMAT; + + errs = 0; + do { + if (argc == 0) + rc = fstat(STDIN_FILENO, &st); + else if (usestat) { + /* + * Try stat() and if it fails, fall back to + * lstat() just in case we're examining a + * broken symlink. + */ + if ((rc = stat(argv[0], &st)) == -1 && + errno == ENOENT && + (rc = lstat(argv[0], &st)) == -1) + errno = ENOENT; + } + else + rc = lstat(argv[0], &st); + + if (rc == -1) { + errs = 1; + linkfail = 1; + if (!quiet) + warn("%s: %s", + argc == 0 ? "(stdin)" : argv[0], + usestat ? "stat" : "lstat"); + } + else + output(&st, argv[0], statfmt, fn, nonl); + + argv++; + argc--; + fn++; + } while (argc > 0); + + return (am_readlink ? linkfail : errs); +} + +void +usage(const char *synopsis) +{ + + (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis); + exit(1); +} + +/* + * Parses a format string. + */ +void +output(const struct stat *st, const char *file, + const char *statfmt, int fn, int nonl) +{ + int flags, size, prec, ofmt, hilo, what; + char buf[PATH_MAX + 4 + 1]; + const char *subfmt; + int nl, t, i; + + nl = 1; + while (*statfmt != '\0') { + + /* + * Non-format characters go straight out. + */ + if (*statfmt != FMT_MAGIC) { + addchar(stdout, *statfmt, &nl); + statfmt++; + continue; + } + + /* + * The current format "substring" starts here, + * and then we skip the magic. + */ + subfmt = statfmt; + statfmt++; + + /* + * Some simple one-character "formats". + */ + switch (*statfmt) { + case SIMPLE_NEWLINE: + addchar(stdout, '\n', &nl); + statfmt++; + continue; + case SIMPLE_TAB: + addchar(stdout, '\t', &nl); + statfmt++; + continue; + case SIMPLE_PERCENT: + addchar(stdout, '%', &nl); + statfmt++; + continue; + case SIMPLE_NUMBER: { + char num[12], *p; + + snprintf(num, sizeof(num), "%d", fn); + for (p = &num[0]; *p; p++) + addchar(stdout, *p, &nl); + statfmt++; + continue; + } + } + + /* + * This must be an actual format string. Format strings are + * similar to printf(3) formats up to a point, and are of + * the form: + * + * % required start of format + * [-# +0] opt. format characters + * size opt. field width + * . opt. decimal separator, followed by + * prec opt. precision + * fmt opt. output specifier (string, numeric, etc.) + * sub opt. sub field specifier (high, middle, low) + * datum required field specifier (size, mode, etc) + * + * Only the % and the datum selector are required. All data + * have reasonable default output forms. The "sub" specifier + * only applies to certain data (mode, dev, rdev, filetype). + * The symlink output defaults to STRING, yet will only emit + * the leading " -> " if STRING is explicitly specified. The + * sizerdev datum will generate rdev output for character or + * block devices, and size output for all others. + */ + flags = 0; + do { + if (*statfmt == FMT_POUND) + flags |= FLAG_POUND; + else if (*statfmt == FMT_SPACE) + flags |= FLAG_SPACE; + else if (*statfmt == FMT_PLUS) + flags |= FLAG_PLUS; + else if (*statfmt == FMT_ZERO) + flags |= FLAG_ZERO; + else if (*statfmt == FMT_MINUS) + flags |= FLAG_MINUS; + else + break; + statfmt++; + } while (1/*CONSTCOND*/); + + size = -1; + if (isdigit((unsigned)*statfmt)) { + size = 0; + while (isdigit((unsigned)*statfmt)) { + size = (size * 10) + (*statfmt - '0'); + statfmt++; + if (size < 0) + goto badfmt; + } + } + + prec = -1; + if (*statfmt == FMT_DOT) { + statfmt++; + + prec = 0; + while (isdigit((unsigned)*statfmt)) { + prec = (prec * 10) + (*statfmt - '0'); + statfmt++; + if (prec < 0) + goto badfmt; + } + } + +#define fmtcase(x, y) case (y): (x) = (y); statfmt++; break +#define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break + switch (*statfmt) { + fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL); + fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL); + fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED); + fmtcasef(ofmt, FMT_HEX, FMTF_HEX); + fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT); + fmtcasef(ofmt, FMT_STRING, FMTF_STRING); + default: + ofmt = 0; + break; + } + + switch (*statfmt) { + fmtcase(hilo, HIGH_PIECE); + fmtcase(hilo, MIDDLE_PIECE); + fmtcase(hilo, LOW_PIECE); + default: + hilo = 0; + break; + } + + switch (*statfmt) { + fmtcase(what, SHOW_realpath); + fmtcase(what, SHOW_st_dev); + fmtcase(what, SHOW_st_ino); + fmtcase(what, SHOW_st_mode); + fmtcase(what, SHOW_st_nlink); + fmtcase(what, SHOW_st_uid); + fmtcase(what, SHOW_st_gid); + fmtcase(what, SHOW_st_rdev); + fmtcase(what, SHOW_st_atime); + fmtcase(what, SHOW_st_mtime); + fmtcase(what, SHOW_st_ctime); + fmtcase(what, SHOW_st_btime); + fmtcase(what, SHOW_st_size); + fmtcase(what, SHOW_st_blocks); + fmtcase(what, SHOW_st_blksize); + fmtcase(what, SHOW_st_flags); + fmtcase(what, SHOW_st_gen); + fmtcase(what, SHOW_symlink); + fmtcase(what, SHOW_filetype); + fmtcase(what, SHOW_filename); + fmtcase(what, SHOW_sizerdev); + default: + goto badfmt; + } +#undef fmtcasef +#undef fmtcase + + t = format1(st, + file, + subfmt, statfmt - subfmt, + buf, sizeof(buf), + flags, size, prec, ofmt, hilo, what); + + for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++) + addchar(stdout, buf[i], &nl); + + continue; + + badfmt: + errx(1, "%.*s: bad format", + (int)(statfmt - subfmt + 1), subfmt); + } + + if (!nl && !nonl) + (void)fputc('\n', stdout); + (void)fflush(stdout); +} + +/* + * Arranges output according to a single parsed format substring. + */ +int +format1(const struct stat *st, + const char *file, + const char *fmt, int flen, + char *buf, size_t blen, + int flags, int size, int prec, int ofmt, + int hilo, int what) +{ + u_int64_t data; + char *stmp, lfmt[24], tmp[20]; + const char *sdata; + char smode[12], sid[12], path[PATH_MAX + 4]; + struct passwd *pw; + struct group *gr; + struct tm *tm; + time_t secs; + long nsecs; + int l, small, formats, gottime, shift; + + formats = 0; + small = 0; + gottime = 0; + secs = 0; + nsecs = 0; + shift = 0; + + /* + * First, pick out the data and tweak it based on hilo or + * specified output format (symlink output only). + */ + switch (what) { + case SHOW_st_dev: + case SHOW_st_rdev: + small = (sizeof(st->st_dev) == 4); + data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev; +#if HAVE_DEVNAME + sdata = (what == SHOW_st_dev) ? + devname(st->st_dev, S_IFBLK) : + devname(st->st_rdev, + S_ISCHR(st->st_mode) ? S_IFCHR : + S_ISBLK(st->st_mode) ? S_IFBLK : + 0U); + if (sdata == NULL) + sdata = "???"; +#endif /* HAVE_DEVNAME */ + if (hilo == HIGH_PIECE) { + data = major(data); + hilo = 0; + } + else if (hilo == LOW_PIECE) { + data = minor((unsigned)data); + hilo = 0; + } + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | +#if HAVE_DEVNAME + FMTF_STRING; +#else /* HAVE_DEVNAME */ + 0; +#endif /* HAVE_DEVNAME */ + if (ofmt == 0) + ofmt = FMTF_UNSIGNED; + break; + case SHOW_st_ino: + small = (sizeof(st->st_ino) == 4); + data = st->st_ino; + sdata = NULL; + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; + if (ofmt == 0) + ofmt = FMTF_UNSIGNED; + break; + case SHOW_st_mode: + small = (sizeof(st->st_mode) == 4); + data = st->st_mode; + strmode(st->st_mode, smode); + stmp = smode; + l = strlen(stmp); + if (stmp[l - 1] == ' ') + stmp[--l] = '\0'; + if (hilo == HIGH_PIECE) { + data >>= 12; + stmp += 1; + stmp[3] = '\0'; + hilo = 0; + } + else if (hilo == MIDDLE_PIECE) { + data = (data >> 9) & 07; + stmp += 4; + stmp[3] = '\0'; + hilo = 0; + } + else if (hilo == LOW_PIECE) { + data &= 0777; + stmp += 7; + stmp[3] = '\0'; + hilo = 0; + } + sdata = stmp; + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | + FMTF_STRING; + if (ofmt == 0) + ofmt = FMTF_OCTAL; + break; + case SHOW_st_nlink: + small = (sizeof(st->st_dev) == 4); + data = st->st_nlink; + sdata = NULL; + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; + if (ofmt == 0) + ofmt = FMTF_UNSIGNED; + break; + case SHOW_st_uid: + small = (sizeof(st->st_uid) == 4); + data = st->st_uid; + if ((pw = getpwuid(st->st_uid)) != NULL) + sdata = pw->pw_name; + else { + snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid); + sdata = sid; + } + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | + FMTF_STRING; + if (ofmt == 0) + ofmt = FMTF_UNSIGNED; + break; + case SHOW_st_gid: + small = (sizeof(st->st_gid) == 4); + data = st->st_gid; + if ((gr = getgrgid(st->st_gid)) != NULL) + sdata = gr->gr_name; + else { + snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid); + sdata = sid; + } + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | + FMTF_STRING; + if (ofmt == 0) + ofmt = FMTF_UNSIGNED; + break; + case SHOW_st_atime: + gottime = 1; + secs = st->st_atime; +#if HAVE_STRUCT_STAT_ST_MTIMENSEC + nsecs = st->st_atimensec; +#endif + /* FALLTHROUGH */ + case SHOW_st_mtime: + if (!gottime) { + gottime = 1; + secs = st->st_mtime; +#if HAVE_STRUCT_STAT_ST_MTIMENSEC + nsecs = st->st_mtimensec; +#endif + } + /* FALLTHROUGH */ + case SHOW_st_ctime: + if (!gottime) { + gottime = 1; + secs = st->st_ctime; +#if HAVE_STRUCT_STAT_ST_MTIMENSEC + nsecs = st->st_ctimensec; +#endif + } + /* FALLTHROUGH */ +#if HAVE_STRUCT_STAT_ST_BIRTHTIME + case SHOW_st_btime: + if (!gottime) { + gottime = 1; + secs = st->st_birthtime; +#if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC + nsecs = st->st_birthtimensec; +#endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */ + } +#endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ + small = (sizeof(secs) == 4); + data = secs; + tm = localtime(&secs); + if (tm == NULL) { + secs = 0; + tm = localtime(&secs); + } + (void)strftime(path, sizeof(path), timefmt, tm); + sdata = path; + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | + FMTF_FLOAT | FMTF_STRING; + if (ofmt == 0) + ofmt = FMTF_DECIMAL; + break; + case SHOW_st_size: + small = (sizeof(st->st_size) == 4); + data = st->st_size; + sdata = NULL; + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; + if (ofmt == 0) + ofmt = FMTF_UNSIGNED; + switch (hilo) { + case HIGH_PIECE: + shift = 30; /* gigabytes */ + hilo = 0; + break; + case MIDDLE_PIECE: + shift = 20; /* megabytes */ + hilo = 0; + break; + case LOW_PIECE: + shift = 10; /* kilobytes */ + hilo = 0; + break; + } + break; + case SHOW_st_blocks: + small = (sizeof(st->st_blocks) == 4); + data = st->st_blocks; + sdata = NULL; + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; + if (ofmt == 0) + ofmt = FMTF_UNSIGNED; + break; + case SHOW_st_blksize: + small = (sizeof(st->st_blksize) == 4); + data = st->st_blksize; + sdata = NULL; + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; + if (ofmt == 0) + ofmt = FMTF_UNSIGNED; + break; +#if HAVE_STRUCT_STAT_ST_FLAGS + case SHOW_st_flags: + small = (sizeof(st->st_flags) == 4); + data = st->st_flags; + sdata = NULL; + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; + if (ofmt == 0) + ofmt = FMTF_UNSIGNED; + break; +#endif /* HAVE_STRUCT_STAT_ST_FLAGS */ +#if HAVE_STRUCT_STAT_ST_GEN + case SHOW_st_gen: + small = (sizeof(st->st_gen) == 4); + data = st->st_gen; + sdata = NULL; + formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; + if (ofmt == 0) + ofmt = FMTF_UNSIGNED; + break; +#endif /* HAVE_STRUCT_STAT_ST_GEN */ + case SHOW_realpath: + small = 0; + data = 0; + if (file == NULL) { + (void)strlcpy(path, "(stdin)", sizeof(path)); + sdata = path; + } else { + snprintf(path, sizeof(path), " -> "); + if (realpath(file, path + 4) == NULL) { + linkfail = 1; + l = 0; + path[0] = '\0'; + } + sdata = path + (ofmt == FMTF_STRING ? 0 : 4); + } + + formats = FMTF_STRING; + if (ofmt == 0) + ofmt = FMTF_STRING; + break; + case SHOW_symlink: + small = 0; + data = 0; + if (S_ISLNK(st->st_mode)) { + snprintf(path, sizeof(path), " -> "); + l = readlink(file, path + 4, sizeof(path) - 4 - 1); + if (l == -1) { + linkfail = 1; + l = 0; + path[0] = '\0'; + } + path[l + 4] = '\0'; + sdata = path + (ofmt == FMTF_STRING ? 0 : 4); + } + else { + linkfail = 1; + sdata = ""; + } + formats = FMTF_STRING; + if (ofmt == 0) + ofmt = FMTF_STRING; + break; + case SHOW_filetype: + small = 0; + data = 0; + sdata = ""; + if (hilo == 0 || hilo == LOW_PIECE) { + switch (st->st_mode & S_IFMT) { + case S_IFIFO: sdata = "|"; break; + case S_IFDIR: sdata = "/"; break; + case S_IFREG: + if (st->st_mode & + (S_IXUSR | S_IXGRP | S_IXOTH)) + sdata = "*"; + break; + case S_IFLNK: sdata = "@"; break; +#ifdef S_IFSOCK + case S_IFSOCK: sdata = "="; break; +#endif +#ifdef S_IFWHT + case S_IFWHT: sdata = "%"; break; +#endif /* S_IFWHT */ +#ifdef S_IFDOOR + case S_IFDOOR: sdata = ">"; break; +#endif /* S_IFDOOR */ + } + hilo = 0; + } + else if (hilo == HIGH_PIECE) { + switch (st->st_mode & S_IFMT) { + case S_IFIFO: sdata = "Fifo File"; break; + case S_IFCHR: sdata = "Character Device"; break; + case S_IFDIR: sdata = "Directory"; break; + case S_IFBLK: sdata = "Block Device"; break; + case S_IFREG: sdata = "Regular File"; break; + case S_IFLNK: sdata = "Symbolic Link"; break; +#ifdef S_IFSOCK + case S_IFSOCK: sdata = "Socket"; break; +#endif +#ifdef S_IFWHT + case S_IFWHT: sdata = "Whiteout File"; break; +#endif /* S_IFWHT */ +#ifdef S_IFDOOR + case S_IFDOOR: sdata = "Door"; break; +#endif /* S_IFDOOR */ + default: sdata = "???"; break; + } + hilo = 0; + } + formats = FMTF_STRING; + if (ofmt == 0) + ofmt = FMTF_STRING; + break; + case SHOW_filename: + small = 0; + data = 0; + if (file == NULL) { + (void)strlcpy(path, "(stdin)", sizeof(path)); + if (hilo == HIGH_PIECE || hilo == LOW_PIECE) + hilo = 0; + } + else if (hilo == 0) + (void)strlcpy(path, file, sizeof(path)); + else { + char *s; + (void)strlcpy(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)strlcpy(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)strlcpy(path, s + 1, + sizeof(path)); + hilo = 0; + } + } + sdata = path; + formats = FMTF_STRING; + if (ofmt == 0) + ofmt = FMTF_STRING; + break; + case SHOW_sizerdev: + if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { + char majdev[20], mindev[20]; + int l1, l2; + + l1 = format1(st, + file, + fmt, flen, + majdev, sizeof(majdev), + flags, size, prec, + ofmt, HIGH_PIECE, SHOW_st_rdev); + l2 = format1(st, + file, + fmt, flen, + mindev, sizeof(mindev), + flags, size, prec, + ofmt, LOW_PIECE, SHOW_st_rdev); + return (snprintf(buf, blen, "%.*s,%.*s", + l1, majdev, l2, mindev)); + } + else { + return (format1(st, + file, + fmt, flen, + buf, blen, + flags, size, prec, + ofmt, 0, SHOW_st_size)); + } + /*NOTREACHED*/ + default: + errx(1, "%.*s: bad format", (int)flen, fmt); + } + + /* + * If a subdatum was specified but not supported, or an output + * format was selected that is not supported, that's an error. + */ + if (hilo != 0 || (ofmt & formats) == 0) + errx(1, "%.*s: bad format", (int)flen, fmt); + + /* + * Assemble the format string for passing to printf(3). + */ + lfmt[0] = '\0'; + (void)strcat(lfmt, "%"); + if (flags & FLAG_POUND) + (void)strcat(lfmt, "#"); + if (flags & FLAG_SPACE) + (void)strcat(lfmt, " "); + if (flags & FLAG_PLUS) + (void)strcat(lfmt, "+"); + if (flags & FLAG_MINUS) + (void)strcat(lfmt, "-"); + if (flags & FLAG_ZERO) + (void)strcat(lfmt, "0"); + + /* + * Only the timespecs support the FLOAT output format, and that + * requires work that differs from the other formats. + */ + if (ofmt == FMTF_FLOAT) { + /* + * Nothing after the decimal point, so just print seconds. + */ + if (prec == 0) { + if (size != -1) { + (void)snprintf(tmp, sizeof(tmp), "%d", size); + (void)strcat(lfmt, tmp); + } + (void)strcat(lfmt, "lld"); + return (snprintf(buf, blen, lfmt, + (long long)secs)); + } + + /* + * Unspecified precision gets all the precision we have: + * 9 digits. + */ + if (prec == -1) + prec = 9; + + /* + * Adjust the size for the decimal point and the digits + * that will follow. + */ + size -= prec + 1; + + /* + * Any leftover size that's legitimate will be used. + */ + if (size > 0) { + (void)snprintf(tmp, sizeof(tmp), "%d", size); + (void)strcat(lfmt, tmp); + } + /* Seconds: time_t cast to long long. */ + (void)strcat(lfmt, "lld"); + + /* + * The stuff after the decimal point always needs zero + * filling. + */ + (void)strcat(lfmt, ".%0"); + + /* + * We can "print" at most nine digits of precision. The + * rest we will pad on at the end. + * + * Nanoseconds: long. + */ + (void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec); + (void)strcat(lfmt, tmp); + + /* + * For precision of less that nine digits, trim off the + * less significant figures. + */ + for (; prec < 9; prec++) + nsecs /= 10; + + /* + * Use the format, and then tack on any zeroes that + * might be required to make up the requested precision. + */ + l = snprintf(buf, blen, lfmt, (long long)secs, nsecs); + for (; prec > 9 && l < (int)blen; prec--, l++) + (void)strcat(buf, "0"); + return (l); + } + + /* + * Add on size and precision, if specified, to the format. + */ + if (size != -1) { + (void)snprintf(tmp, sizeof(tmp), "%d", size); + (void)strcat(lfmt, tmp); + } + if (prec != -1) { + (void)snprintf(tmp, sizeof(tmp), ".%d", prec); + (void)strcat(lfmt, tmp); + } + + /* + * String output uses the temporary sdata. + */ + if (ofmt == FMTF_STRING) { + if (sdata == NULL) + errx(1, "%.*s: bad format", (int)flen, fmt); + (void)strcat(lfmt, "s"); + return (snprintf(buf, blen, lfmt, sdata)); + } + + /* + * Ensure that sign extension does not cause bad looking output + * for some forms. + */ + if (small && ofmt != FMTF_DECIMAL) + data = (u_int32_t)data; + + /* + * The four "numeric" output forms. + */ + (void)strcat(lfmt, "ll"); + switch (ofmt) { + case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break; + case FMTF_OCTAL: (void)strcat(lfmt, "o"); break; + case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break; + case FMTF_HEX: (void)strcat(lfmt, "x"); break; + } + + /* + * shift and round to nearest for kilobytes, megabytes, + * gigabytes. + */ + if (shift > 0) { + data >>= (shift - 1); + data++; + data >>= 1; + } + + return (snprintf(buf, blen, lfmt, data)); +}