Subject: Re: stat(1)
To: Robert Elz <kre@munnari.OZ.AU>
From: Andrew Brown <atatat@atatdot.net>
List: current-users
Date: 12/29/2001 11:59:58
>  | 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).

well...it's arguable.  strftime() doesn't really operate directly on a
time_t, it uses a struct tm.  the formatting is also somewhat governed
by posix, so it's relatively uniform (except for %s is which probably
my favorite).  ps has no such requirements, so it does what it needs
to do with a much larger and more complex "structure".

>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.

a shell routine that could dump out the contents of a symbolic link is
something that people sometimes ask for...an ls which could take a
format string would make that trivial.  eg:

	% ln -s foo bar
	% stat -l bar
	lrwxr-xr-x 1 andrew staff 3 Dec 29 11:39:43 2001 bar -> foo
	% stat -f%L bar
	foo

sure, one can try to use awk or sed to strip stuff off, but it's
decidedly non-trivial and prone to weirdness in the face of long
strange filenames...

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

so's mine (of course), but the formatting is a little different.

>brandenburg$ stat -f '%t' /
>directory

	% stat -f%sM / ~/.cshrc
	directory
	file

>brandenburg$ stat -f '%t..1s' /
>d

	% stat -f"[%.1sM]" / ~/.cshrc
	[d]
	[f]
	% stat -f"[%cM]" / ~/.cshrc
	[/]
	[]

...and so on.  how it works is somewhat incidental.  the fact that
almost *everyone* finds the need for something like this is the
interesting bit.

-- 
|-----< "CODE WARRIOR" >-----|
codewarrior@daemon.org             * "ah!  i see you have the internet
twofsonet@graffiti.com (Andrew Brown)                that goes *ping*!"
andrew@crossbar.com       * "information is power -- share the wealth."