Subject: Re: stat(1)
To: Steven M. Bellovin <smb@research.att.com>
From: Robert Elz <kre@munnari.OZ.AU>
List: current-users
Date: 12/29/2001 21:50:24
    Date:        Sat, 29 Dec 2001 08:57:42 -0500
    From:        "Steven M. Bellovin" <smb@research.att.com>
    Message-ID:  <20011229135742.7B0077B56@berkshire.research.att.com>

  | Right now, we have (at least) date and ps that (in effect) apply 
  | user-defined formatting instructions to a structure dump.

Well, not really, date applies user defined formatting to a number (an
int of some size or other).  ps applies formatting to a whole bunch of
more or less unrelated data extracted from the kernel (some of it is from
struct proc, but not all - nor is all of struct proc available via ps).

  | You propose to add another.

Even if that were true, I'm not sure it is all that dangerous - though
it would certainly be nice to have a standard form to do these things.

  | Maybe what we really need is raw structure dumps, plus 
  | a generalized (controlled by /etc/struct?) translator into the 
  | individual fields, plus a prettyprinter.

For use in scripts, which is where I think this kind of thing is useful
(it is the only place I use ps's -o flag, and the vast majority of the
times I use date +) I suspect that might be a bit too much overhead.

Library routines that did much of that might be useful though, so if more
cases arise where shell level access to structs is meaningful (this generally
means where a sys call returns a struct - there aren't all that many of
those) the interface could at least be consistent.

FWIW - my "prettyprinter" is printf() (you can give my version of stat
a printf format, and that's what's used).   Eg:

brandenburg$ stat -f '%t' /
directory
brandenburg$ stat -f '%t..1s' /
d
brandenburg$ stat -f '%p' /
rwxr-xr-x
brandenburg$ stat -f '%t..1s%p' /
drwxr-xr-x

It is almost possible to emulate ls output - almost because I don't have
strftime in there yet (maybe never will), and there's no conditional
execution (no way to replace size by rdev for devices).  The current
format for times as strings is ..

brandenburg$ stat -f '%m.s' /
2001-10-14 20:11:53

Of course, it is also possible to get them as numbers (and because printf
is used, in decimal, octal or hex) ...

brandenburg$ stat -f '%a.#o %m.d %c.#x' /
07412325434 1003065113 0x3bc98f19

Or if you want, in nanosecs (the user doesn't need to know the
size of the field) ...

brandenburg$ stat -f '%an.#o %mn.d %cn.#x' /
070017657456755636600 1003065113286494000 0xdeb9a67f4074730

kre

ps: I'd never even considered making my tool generate csh format variable
assignments, the -g flag just alters the default format, which is

%t..1s%p.s %l.d %u.s %g.s %s.d %f

and which produces:

brandenburg$ stat /
drwxr-xr-x 19 root wheel 512 /

(currently anyway - ls without the times - in practice this is never
used, anything using this supplies a format) to produce ...

brandenburg$ stat -g /
INUM=2; SIZE=512; BLOCKS=2; USER='root'; GROUP='wheel'; PERMS=755;
TYPE='directory'; FLAGS='-------'; GEN=0; DEV='0,0'; RDEV='0,248'; LINKS=19;
BLOCKSIZE=8192; MTIME=1003065113; ATIME=1009363740; CTIME=1003065113;

The output is actually one long line - the semi-colons would be unnecessary for
parsing by sh, but make this work as input to awk as well.  If someone wanted
csh ugliness, then adding a format to make it would be easy (if misguided).