Home | History | Annotate | Download | only in stat
History log of /src/usr.bin/stat/stat.c
RevisionDateAuthorComments
 1.55  15-May-2025  nia stat(1): Eliminate a compiler warning

This code at the moment is a bit magical, as it uses pointer arithemtic
on a string and boolean type to return either "lstat" or "stat".

Use the ternary operator instead to make the code easier to read and make
Clang happier.

No functional change intended(tm).
 1.54  03-May-2025  kre Make readlink(1) compliant with the POSIX-2004 (Issue 8).

POSIX 2004 requires that if a given file name to readlink(1)
doesn't refer to a symlink, an error be printed.

That conflicts with historic usage on NetBSD where readlink is
silent in that case (except for the exit status) -- rather than
altering our default behaviour, look for POSIXLY_CORRECT in the
environment, and issue an error only in that case (just means
switching between -q and -v as the default noise option).

While here, generate usage if no file args are given - POSIX
requires at least 1 (it actually specifies exactly 1) and while
we can extend that without issues to allow more than one, allowing
0 args (just because we do that for stat(1) and readlink(1) is
the same code) makes no sense at all for readlink - stdin cannot
possibly be a symbolic link.

Also, in the case of stat(1) if we do have no args, and the
fstat(STDIN) fails for some reason, don't claim that it is
lstat (or stat, depending upon -L) which failed:
stat: (stdin): lstat: Bad file descriptor
makes no sense at all. This can occur with something like
stat <&-
which is kind of obscure, but it is easy to DTRT.

No pullups planned.
 1.53  14-Mar-2024  rillig branches: 1.53.2;
stat: don't allow numeric formatting flags on strings

Calling snprintf with "%+s" invokes undefined behavior.
 1.52  14-Mar-2024  kre While the change in 1.51 certainly retained binary compat with
what was in 1.50 (while silencing LINT) - it was clearly not the
correct change to make. The code used !FLAG_POUND where it
clearly meant ~FLAG_POUND ... the former is 0, so &= 0 could
be replaced by =0 changing nothing. But that's not what it
should have been doing, other flags should not have been
removed here, just FLAG_POUND.

This problem seems to have existed since support for %#s
was first added in 2011, which kind of suggests how rarely
that format, particularly with other flags (like %#-s)
has ever been used (with no other flags, the bug would not
be noticed).
 1.51  14-Mar-2024  rillig stat: fix lint warning about constant argument to '!'

No binary change.
 1.50  29-Jan-2024  christos fix tools build
 1.49  29-Jan-2024  christos PR/57891: Ricardo Branco: add symbolic flags printing (from FreeBSD)
 1.48  22-Jun-2022  kre Some corrections to stat to make it more reasonable, and comply with
the man page, in particular, in one of the EXAMPLES it is claimed:

This example produces output very similar to that from find ... -ls
(except that find(1) displays the time in a different format, and find(1)
sometimes adds one or more spaces after the comma in “major,minor” for
device nodes):

and then uses %9Z in the format, but conveniently does not use a
device file to demonstrate.

If it had, the result would have been:

8033 0 brw-r----- 1 root operator 4, 51 Jun 8 02:44:45 2022 /dev/sd3

Note the spaces which were claimed to exist (sometimes) in find(1) output
but not with stat. Oops.

Omitting those spaces seems like a good idea, so does using the same
field width for the result in both cases, rather than producing 9 chars
in the ordinary file case, and 19 (2 * 9 + 1) in the device case (for %9Z).

Now we will get:

8033 0 brw-r----- 1 root operator 4,51 Jun 8 02:44:45 2022 /dev/sd3

While here, also make the file number (%@) when reading stdin be 0, it is
not the 1st file in the arg list.

Some (very minor) KNF applied at the same time.

In stat.1 make these changes explicit.
 1.47  27-Aug-2021  rillig stat: fix undefined behavior when calling ctype functions

lint says: warning: argument to 'function from <ctype.h>' must be cast
to 'unsigned char', not to 'unsigned int' [342]
 1.46  07-Sep-2020  mrg avoid a GCC stringop truncate warning.
 1.45  04-Oct-2019  mrg adjust fallthru comment.
 1.44  04-Feb-2019  mrg avoid a potential buffer truncation.
 1.43  21-Sep-2017  kre branches: 1.43.4;
Handle the new %f (as an addition to the strftime format - -t arg)
correctly, in particular %%f does not contain the %f format, and like
all strftime conversions, %f can occur more than once (not that it is
likely to happen, but just in case...)
 1.42  21-Sep-2017  kre KNF, and since this gets compiled as a tool, avoid C99 declarations.
 1.41  20-Sep-2017  kre Set the default time format for linux mode (-x) in the correct place,
so it only applies to -x (in particular, not to -s) and only if another
time format has not been explicitly selected (earlier in the arg list).

With luck, this will make lots of tests, which depended upon "stat -s"
actually generating correct sh assignment statements, go back to working again.
 1.40  20-Sep-2017  christos - add the sticky bits to mode
- fix linux Birth for the havenots
code review via email from @@@
 1.39  19-Sep-2017  christos - if st_dev/st_rdev == NODEV print -1 instead of unsigned
- update to the latest linux stat(1) format for -x (kind of)
- add the ability to display nanoseconds, like linux does
- change u_intX_t -> uintX_t
 1.38  03-Jan-2013  dsl Unset HAVE_DEVNAME for tools build.
config only checks for the symbol in libc, we need the correct prototype.
Which is excluded even on NetBSD (_NETBSD_SOURCE isn't defined in stdlib.h).
 1.37  02-Dec-2012  erh Honor the -t option to set the time format when running stat -s.
Document the format used by -s on the man page.
 1.36  22-Sep-2011  apb branches: 1.36.2; 1.36.8;
Use vis(3) encoding when a string format is modified by the '#' flag.
Also bump copyright years.
 1.35  06-Sep-2011  joerg static + __dead
 1.34  16-Feb-2011  christos Add verbose mode for readlink.
 1.33  15-Jan-2011  njoly branches: 1.33.2;
Do not crash if a date cannot be represented (localtime returning
NULL), use the Epoch value instead.
 1.32  16-Dec-2010  dholland Don't pass unused argument. From Doug Barton.
 1.31  16-Dec-2010  dholland Use strlcpy, not strncpy, when the desired semantics are strlcpy's
rather than strncpy's.
 1.30  25-Nov-2010  dholland Improve previous with comments.
 1.29  24-Nov-2010  dholland Don't printf time_t with %d; fixes PR 44128 from yamt. With this change it
successfully prints mtimes after 2038.
 1.28  13-Apr-2009  lukem Fix WARNS=4 issues (-Wcast-qual -Wsign-compare)
 1.27  16-May-2008  atatat branches: 1.27.6;
The ofmt variable is actually a bit mask (not the character that was
in the format string) so that we can "or" it with the bits in the
formats variable. This fixes the missing " -> " in front of the real
path (when you use %SR).

Also, the ?: needs another space.
 1.26  28-Apr-2008  martin branches: 1.26.2;
Remove clause 3 and 4 from TNF licenses
 1.25  09-Feb-2008  mlelstv branches: 1.25.4;
Fix a segfault when doing 'stat -f %R' on the stdin file handle, instead
fake the filename '(stdin)' like the %N format.
 1.24  07-Oct-2006  elad branches: 1.24.2; 1.24.4; 1.24.8;
PR/34662: martijnb at atlas dot ipv6 dot stack dot nl: readlink doesn't
grok -f, and there's no alternative (+fix)

Patch applied with minor tweak (%y -> %R, as it was already taken) plus
some nits from myself. Thanks!
 1.23  23-Jun-2005  atatat Bend the 'L', 'M', and 'H' modifiers to work on the size, whereby it
is rounded to the nearest kilobyte, megabyte, or gigabyte.
Implemented at lukem's request since some things can't deal with
overly large numbers when files are really large.

Have to do something like humanize_number(3), but that interface isn't
really what I'm looking for. I think. More examination required.
 1.22  22-Apr-2005  atatat Fix a trivial truncation case, and eliminate a corner case that might
print a nul character.
 1.21  13-Jan-2005  jmc Now that tools/compat provides a check, make sure st_birthtimensec exists
before using it
 1.20  31-Dec-2004  atatat Add "dirname"- and "basename"-like functionality to stat(1) by way of
the "high" and "low" selectors on the filename.
 1.19  20-Jun-2004  jmc Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')

Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).

Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.

Fixes PR's: PR#17762 PR#25944
 1.18  28-May-2004  atatat If using stat (the -L flag) and it fails, fall back to lstat(). It
may be the case that we're examining a broken symlink, and anything is
better than nothing.
 1.17  29-Oct-2003  atatat branches: 1.17.2;
Take the time to set gottime so that you don't have to waste time
trying to figure out why the time isn't the time you expected all the
time.
 1.16  27-Oct-2003  lukem be consistent using #if HAVE_STRUCT_STAT_ST_xxx (versus #ifdef)
 1.15  27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.14  26-Oct-2003  chs fix for systems which don't have st_*timespec. PR 23214.
 1.13  25-Jul-2003  atatat More properly host-tool-ify stat
 1.12  23-Jul-2003  lukem * don't compile in support for st_birth #ifdef HOSTPROG
* remove %B (birth) from default format strings, since %B isn't available
#ifdef HOSTPROG, and it's not standard enough to be fussed about anyway
 1.11  20-Jul-2003  lukem first attempt at making this a HOSTPROG
 1.10  08-May-2003  atatat Add %B to print the st_birthtime of an inode (adjusting the "raw"
format and "shell" format appropriately), and rename the st_*timespec
fields in the shell-style output to be st_*time, since that's what
they really are.
 1.9  19-Oct-2002  provos use readlink with bufsize - 1; approved thorpej.
 1.8  13-Aug-2002  atatat Use limits.h instead of sys/syslimits.h since it's more potrable.
 1.7  13-Aug-2002  atatat Style sync with FreeBSD. Changes via Doug Barton <DougB@FreeBSD.org>.
 1.6  09-Jul-2002  atatat Header reorganization makes a shorter list. From Doug Barton <DougB@FreeBSD.org>
 1.5  09-Jul-2002  atatat The bitwise or-ing together of letters is almost *guaranteed* to do
the wrong thing, as in this case. I should read the preprocessor
output more often.
 1.4  08-Jul-2002  atatat A new command line option, -q, suppresses *stat(2) failure messages.
Output is now done via stdio, instead of with my stupid homegrown
buffering (I don't even know why I did it that way in the first
place). Also, from Johan Karlsson <johan@freebsd.org>, eliminate a
spurious newline if no output was generated, and link stat(1) to
readlink(1) for similarity to OpenBSD.
 1.3  31-May-2002  atatat When asked for the string representation of a device that isn't
found by devname(3), don't report a "bad format"...just use "???"
instead.
 1.2  09-May-2002  atatat branches: 1.2.2;
Add a simple format that translates to the current file number, fix an
off by one error, and #define a bunch more things to make it clearer.
 1.1  27-Apr-2002  atatat Add a stat(1) program to /usr/bin. It provides a simple formatting
interface, with reasonable defaults, to the information provided by
stat(2) so that scripts can use the data therein much more easily.
 1.2.2.1  02-Jun-2002  tv Pull up revision 1.3 (requested by atatat in ticket #112):
When asked for the string representation of a device that isn't
found by devname(3), don't report a "bad format"...just use "???"
instead.
 1.17.2.2  22-Jun-2004  tron Pull up revision 1.19 (requested by jmc in ticket #527):
Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')
Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).
Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.
Fixes PR's: PR#17762 PR#25944
 1.17.2.1  30-May-2004  tron Pull up revision 1.18 (requested by atatat in ticket #412):
If using stat (the -L flag) and it fails, fall back to lstat(). It
may be the case that we're examining a broken symlink, and anything is
better than nothing.
 1.24.8.1  23-Mar-2008  matt sync with HEAD
 1.24.4.1  03-Jun-2008  skrll Sync with netbsd-4.
 1.24.2.1  22-Feb-2008  bouyer Pull up following revision(s) (requested by mlelstv in ticket #1072):
usr.bin/stat/stat.c: revision 1.25
Fix a segfault when doing 'stat -f %R' on the stdin file handle, instead
fake the filename '(stdin)' like the %N format.
 1.25.4.1  18-May-2008  yamt sync with head.
 1.26.2.1  23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.27.6.1  13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.33.2.1  17-Feb-2011  bouyer Sync with HEAD
 1.36.8.1  25-Feb-2013  tls resync with head
 1.36.2.2  23-Jan-2013  yamt sync with head
 1.36.2.1  16-Jan-2013  yamt sync with (a bit old) head
 1.43.4.2  13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.43.4.1  10-Jun-2019  christos Sync with HEAD
 1.53.2.1  02-Aug-2025  perseant Sync with HEAD

RSS XML Feed