Virgin import of NetBSD's stat(1)
This commit is contained in:
parent
7a0a395027
commit
06b6d7d8a8
8
usr.bin/stat/Makefile
Normal file
8
usr.bin/stat/Makefile
Normal file
@ -0,0 +1,8 @@
|
||||
# $FreeBSD$
|
||||
|
||||
PROG= stat
|
||||
MAN= stat.1
|
||||
SRCS= stat.c
|
||||
WARNS?= 2
|
||||
|
||||
.include <bsd.prog.mk>
|
434
usr.bin/stat/stat.1
Normal file
434
usr.bin/stat/stat.1
Normal file
@ -0,0 +1,434 @@
|
||||
.\" $NetBSD: stat.1,v 1.4 2002/05/09 17:52:03 atatat 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 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.
|
||||
.\" 3. All advertising materials mentioning features or use of this software
|
||||
.\" must display the following acknowledgement:
|
||||
.\" This product includes software developed by the NetBSD
|
||||
.\" Foundation, Inc. and its contributors.
|
||||
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
|
||||
.\" contributors may be used to endorse or promote products derived
|
||||
.\" from this software without specific prior written permission.
|
||||
.\"
|
||||
.\" 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.
|
||||
.\"
|
||||
.\" $FreeBSD$
|
||||
.\"
|
||||
.Dd April 24, 2002
|
||||
.Dt STAT 1
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm stat
|
||||
.Nd display file status
|
||||
.Sh SYNOPSIS
|
||||
.Nm
|
||||
.Op Fl FLn
|
||||
.Oo
|
||||
.Fl f Ar format |
|
||||
.Fl l |
|
||||
.Fl r |
|
||||
.Fl s |
|
||||
.Fl x
|
||||
.Oc
|
||||
.Op Fl t Ar timefmt
|
||||
.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 path name leading to the file must be
|
||||
searchable. If no argument is given,
|
||||
.Nm
|
||||
displays information about the file descriptor for standard input.
|
||||
.Pp
|
||||
The information displayed is obtained by calling
|
||||
.Xr lstat 2
|
||||
with the given argument and evaluating the returned structure.
|
||||
.Pp
|
||||
The options are as follows:
|
||||
.Bl -tag -width Ds
|
||||
.It Fl F
|
||||
As in
|
||||
.Ic ls ,
|
||||
display a slash (/) immediately after each pathname that is a directory, an
|
||||
asterisk (*) after each that is executable, an at sign (@) after each symbolic
|
||||
link, a percent sign (%) after each whiteout, an equal sign (=) after each
|
||||
socket, and a vertical bar (|) after each that is a FIFO. The use of
|
||||
.Fl F
|
||||
implies
|
||||
.Fl l .
|
||||
.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 n
|
||||
Do not force a newline to appear at the end of each piece of output.
|
||||
.It Fl f Ar format
|
||||
Display information using the specified format. See the FORMATS section
|
||||
for a description of valid formats.
|
||||
.It Fl l
|
||||
Display output in
|
||||
.Ic ls Fl lT
|
||||
format.
|
||||
.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 ``shell output'', suitable for initializing variables.
|
||||
.It Fl x
|
||||
Display information in a more verbose way as known from some Linux
|
||||
distributions.
|
||||
.It Fl t Ar timefmt
|
||||
Display timestamps using the specified format. This format is
|
||||
passed directly to
|
||||
.Xr strftime 3 .
|
||||
.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 ``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, low). Only applies to
|
||||
the
|
||||
.Cm p ,
|
||||
.Cm d ,
|
||||
.Cm r ,
|
||||
and
|
||||
.Cm T
|
||||
output formats. It can be one of the following:
|
||||
.Bl -tag -width Ds
|
||||
.It Cm H
|
||||
``High'' -- specifies the major number for devices from
|
||||
.Cm r
|
||||
or
|
||||
.Cm d ,
|
||||
the ``user'' bits for permissions from the string form of
|
||||
.Cm p ,
|
||||
the file ``type'' bits from the numeric forms of
|
||||
.Cm p ,
|
||||
and the long output form of
|
||||
.Cm T .
|
||||
.It Cm L
|
||||
``Low'' -- specifies the minor number for devices from
|
||||
.Cm r
|
||||
or
|
||||
.Cm d ,
|
||||
the ``other'' bits for permissions from the string form of
|
||||
.Cm p ,
|
||||
the ``user'', ``group'', and ``other'' bits from the numeric forms of
|
||||
.Cm p ,
|
||||
and the
|
||||
.Ic ls -F
|
||||
style output character for file type when used with
|
||||
.Cm T
|
||||
(the use of
|
||||
.Cm L
|
||||
for this is optional).
|
||||
.It Cm M
|
||||
``Middle'' -- specifies the ``group'' bits for permissions from the
|
||||
string output form of
|
||||
.Cm p ,
|
||||
or the ``suid'', ``sgid'', and ``sticky'' bits for the numeric forms of
|
||||
.Cm p .
|
||||
.El
|
||||
.It Cm datum
|
||||
A required field specifier, being one of the following:
|
||||
.Bl -tag -width Ds
|
||||
.It Cm d
|
||||
Device upon which
|
||||
.Ar file
|
||||
resides.
|
||||
.It Cm i
|
||||
.Ar file Ap s
|
||||
inode number.
|
||||
.It Cm p
|
||||
File type and permissions.
|
||||
.It Cm l
|
||||
Number of hard links to
|
||||
.Ar file .
|
||||
.It Cm u , g
|
||||
User-id and group-id of
|
||||
.Ar file Ap s
|
||||
owner.
|
||||
.It Cm r
|
||||
Device number for character and block device special files.
|
||||
.It Cm a , m , c
|
||||
The time
|
||||
.Ar file
|
||||
was last accessed, modified, or of when the inode was last changed.
|
||||
.It Cm z
|
||||
The size of
|
||||
.Ar file
|
||||
in bytes.
|
||||
.It Cm b
|
||||
Number of blocks allocated for
|
||||
.Ar file .
|
||||
.It Cm k
|
||||
Optimal file system I/O operation block size.
|
||||
.It Cm f
|
||||
User defined flags for
|
||||
.Ar file .
|
||||
.It Cm v
|
||||
Inode generation number.
|
||||
.El
|
||||
.Pp
|
||||
The following four 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 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 ``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
|
||||
Given a symbolic link ``foo'' that points from /tmp/foo to /, 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
|
||||
lrwxrwxrwx 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_mtimespec
|
||||
1148 1015432481
|
||||
|
||||
\*[Gt] sh
|
||||
$ eval $(stat -s .profile)
|
||||
$ echo $st_size $st_mtimespec
|
||||
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 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
|
||||
The
|
||||
.Nm
|
||||
utility was written by Andrew Brown
|
||||
.Aq atatat@netbsd.org .
|
||||
This man page
|
||||
was written by Jan Schaumann
|
||||
.Aq jschauma@netbsd.org .
|
891
usr.bin/stat/stat.c
Normal file
891
usr.bin/stat/stat.c
Normal file
@ -0,0 +1,891 @@
|
||||
/* $NetBSD: stat.c,v 1.3 2002/05/31 16:45:16 atatat 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.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the NetBSD
|
||||
* Foundation, Inc. and its contributors.
|
||||
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#ifndef lint
|
||||
__RCSID("$NetBSD: stat.c,v 1.3 2002/05/31 16:45:16 atatat Exp $");
|
||||
__FBSDID("$FreeBSD$");
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <err.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
||||
#define DEF_FORMAT \
|
||||
"%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" %k %b %N"
|
||||
#define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c %k %b %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_atimespec=%a st_mtimespec=%m st_ctimespec=%c " \
|
||||
"st_blksize=%k st_blocks=%b"
|
||||
#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 HIGH_PIECE 'H'
|
||||
#define MIDDLE_PIECE 'M'
|
||||
#define LOW_PIECE 'L'
|
||||
|
||||
#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_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(void);
|
||||
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);
|
||||
|
||||
char *timefmt;
|
||||
|
||||
#define addchar(b, n, l, c, nl) \
|
||||
do { \
|
||||
if ((*(n)) < (l)) { \
|
||||
(b)[(*(n))++] = (c); \
|
||||
(*nl) = ((c) == '\n'); \
|
||||
} \
|
||||
} while (0/*CONSTCOND*/)
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct stat st;
|
||||
int ch, rc, errs;
|
||||
int lsF, fmtchar, usestat, fn, nonl;
|
||||
char *statfmt;
|
||||
|
||||
lsF = 0;
|
||||
fmtchar = '\0';
|
||||
usestat = 0;
|
||||
nonl = 0;
|
||||
statfmt = NULL;
|
||||
timefmt = NULL;
|
||||
|
||||
while ((ch = getopt(argc, argv, "f:FlLnrst:x")) != -1)
|
||||
switch (ch) {
|
||||
case 'F':
|
||||
lsF = 1;
|
||||
break;
|
||||
case 'L':
|
||||
usestat = 1;
|
||||
break;
|
||||
case 'n':
|
||||
nonl = 1;
|
||||
break;
|
||||
case 'f':
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
|
||||
if (timefmt == NULL)
|
||||
timefmt = TIME_FORMAT;
|
||||
|
||||
errs = 0;
|
||||
do {
|
||||
if (argc == 0)
|
||||
rc = fstat(STDIN_FILENO, &st);
|
||||
else if (usestat)
|
||||
rc = stat(argv[0], &st);
|
||||
else
|
||||
rc = lstat(argv[0], &st);
|
||||
|
||||
if (rc == -1) {
|
||||
errs = 1;
|
||||
warn("%s: stat", argc == 0 ? "(stdin)" : argv[0]);
|
||||
}
|
||||
else
|
||||
output(&st, argv[0], statfmt, fn, nonl);
|
||||
|
||||
argv++;
|
||||
argc--;
|
||||
fn++;
|
||||
} while (argc > 0);
|
||||
|
||||
return (errs);
|
||||
}
|
||||
|
||||
void
|
||||
usage(void)
|
||||
{
|
||||
|
||||
(void)fprintf(stderr,
|
||||
"usage: %s [-FlLnrsx] [-f format] [-t timefmt] [file ...]\n",
|
||||
getprogname());
|
||||
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[4096], subbuf[MAXPATHLEN];
|
||||
const char *subfmt;
|
||||
int nl, t, i;
|
||||
size_t len;
|
||||
|
||||
len = 0;
|
||||
while (*statfmt != '\0') {
|
||||
|
||||
/*
|
||||
* Non-format characters go straight out.
|
||||
*/
|
||||
if (*statfmt != FMT_MAGIC) {
|
||||
addchar(buf, &len, sizeof(buf), *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(buf, &len, sizeof(buf), '\n', &nl);
|
||||
statfmt++;
|
||||
continue;
|
||||
case SIMPLE_TAB:
|
||||
addchar(buf, &len, sizeof(buf), '\t', &nl);
|
||||
statfmt++;
|
||||
continue;
|
||||
case SIMPLE_PERCENT:
|
||||
addchar(buf, &len, sizeof(buf), '%', &nl);
|
||||
statfmt++;
|
||||
continue;
|
||||
case SIMPLE_NUMBER: {
|
||||
char num[12], *p;
|
||||
|
||||
snprintf(num, sizeof(num), "%d", fn);
|
||||
for (p = &num[0]; *p; p++)
|
||||
addchar(buf, &len, sizeof(buf), *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
|
||||
switch (*statfmt) {
|
||||
fmtcase(ofmt, FMT_DECIMAL);
|
||||
fmtcase(ofmt, FMT_OCTAL);
|
||||
fmtcase(ofmt, FMT_UNSIGNED);
|
||||
fmtcase(ofmt, FMT_HEX);
|
||||
fmtcase(ofmt, FMT_FLOAT);
|
||||
fmtcase(ofmt, FMT_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_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_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 fmtcase
|
||||
|
||||
t = format1(st,
|
||||
file,
|
||||
subfmt, statfmt - subfmt,
|
||||
subbuf, sizeof(subbuf),
|
||||
flags, size, prec, ofmt, hilo, what);
|
||||
|
||||
for (i = 0; i < t && i < sizeof(subbuf); i++)
|
||||
addchar(buf, &len, sizeof(buf), subbuf[i], &nl);
|
||||
|
||||
continue;
|
||||
|
||||
badfmt:
|
||||
errx(1, "%.*s: bad format",
|
||||
(int)(statfmt - subfmt + 1), subfmt);
|
||||
}
|
||||
|
||||
(void)write(STDOUT_FILENO, buf, len);
|
||||
if (!nl && !nonl)
|
||||
(void)write(STDOUT_FILENO, "\n", sizeof("\n") - 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 *sdata, lfmt[24], tmp[20];
|
||||
char smode[12], sid[12], path[MAXPATHLEN + 4];
|
||||
struct passwd *pw;
|
||||
struct group *gr;
|
||||
const struct timespec *tsp;
|
||||
struct timespec ts;
|
||||
struct tm *tm;
|
||||
int l, small, formats;
|
||||
|
||||
tsp = NULL;
|
||||
formats = 0;
|
||||
small = 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;
|
||||
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 = "???";
|
||||
if (hilo == HIGH_PIECE) {
|
||||
data = major(data);
|
||||
hilo = 0;
|
||||
}
|
||||
else if (hilo == LOW_PIECE) {
|
||||
data = minor((unsigned)data);
|
||||
hilo = 0;
|
||||
}
|
||||
formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
|
||||
FMT_STRING;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_UNSIGNED;
|
||||
break;
|
||||
case SHOW_st_ino:
|
||||
small = (sizeof(st->st_ino) == 4);
|
||||
data = st->st_ino;
|
||||
sdata = NULL;
|
||||
formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_UNSIGNED;
|
||||
break;
|
||||
case SHOW_st_mode:
|
||||
small = (sizeof(st->st_mode) == 4);
|
||||
data = st->st_mode;
|
||||
strmode(st->st_mode, smode);
|
||||
sdata = smode;
|
||||
l = strlen(sdata);
|
||||
if (sdata[l - 1] == ' ')
|
||||
sdata[--l] = '\0';
|
||||
if (hilo == HIGH_PIECE) {
|
||||
data >>= 12;
|
||||
sdata += 1;
|
||||
sdata[3] = '\0';
|
||||
hilo = 0;
|
||||
}
|
||||
else if (hilo == MIDDLE_PIECE) {
|
||||
data = (data >> 9) & 07;
|
||||
sdata += 4;
|
||||
sdata[3] = '\0';
|
||||
hilo = 0;
|
||||
}
|
||||
else if (hilo == LOW_PIECE) {
|
||||
data &= 0777;
|
||||
sdata += 7;
|
||||
sdata[3] = '\0';
|
||||
hilo = 0;
|
||||
}
|
||||
formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
|
||||
FMT_STRING;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_OCTAL;
|
||||
break;
|
||||
case SHOW_st_nlink:
|
||||
small = (sizeof(st->st_dev) == 4);
|
||||
data = st->st_nlink;
|
||||
sdata = NULL;
|
||||
formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_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 = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
|
||||
FMT_STRING;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_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 = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
|
||||
FMT_STRING;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_UNSIGNED;
|
||||
break;
|
||||
case SHOW_st_atime:
|
||||
tsp = &st->st_atimespec;
|
||||
/* FALLTHROUGH */
|
||||
case SHOW_st_mtime:
|
||||
if (tsp == NULL)
|
||||
tsp = &st->st_mtimespec;
|
||||
/* FALLTHROUGH */
|
||||
case SHOW_st_ctime:
|
||||
if (tsp == NULL)
|
||||
tsp = &st->st_ctimespec;
|
||||
ts = *tsp; /* copy so we can muck with it */
|
||||
small = (sizeof(ts.tv_sec) == 4);
|
||||
data = ts.tv_sec;
|
||||
small = 1;
|
||||
tm = localtime(&ts.tv_sec);
|
||||
(void)strftime(path, sizeof(path), timefmt, tm);
|
||||
sdata = path;
|
||||
formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
|
||||
FMT_FLOAT | FMT_STRING;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_DECIMAL;
|
||||
break;
|
||||
case SHOW_st_size:
|
||||
small = (sizeof(st->st_size) == 4);
|
||||
data = st->st_size;
|
||||
sdata = NULL;
|
||||
formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_UNSIGNED;
|
||||
break;
|
||||
case SHOW_st_blocks:
|
||||
small = (sizeof(st->st_blocks) == 4);
|
||||
data = st->st_blocks;
|
||||
sdata = NULL;
|
||||
formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_UNSIGNED;
|
||||
break;
|
||||
case SHOW_st_blksize:
|
||||
small = (sizeof(st->st_blksize) == 4);
|
||||
data = st->st_blksize;
|
||||
sdata = NULL;
|
||||
formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_UNSIGNED;
|
||||
break;
|
||||
case SHOW_st_flags:
|
||||
small = (sizeof(st->st_flags) == 4);
|
||||
data = st->st_flags;
|
||||
sdata = NULL;
|
||||
formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_UNSIGNED;
|
||||
break;
|
||||
case SHOW_st_gen:
|
||||
small = (sizeof(st->st_gen) == 4);
|
||||
data = st->st_gen;
|
||||
sdata = NULL;
|
||||
formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_UNSIGNED;
|
||||
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);
|
||||
if (l == -1) {
|
||||
l = 0;
|
||||
path[0] = '\0';
|
||||
}
|
||||
path[l + 4] = '\0';
|
||||
sdata = path + (ofmt == FMT_STRING ? 0 : 4);
|
||||
}
|
||||
else
|
||||
sdata = "";
|
||||
formats = FMT_STRING;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_STRING;
|
||||
break;
|
||||
case SHOW_filetype:
|
||||
small = 0;
|
||||
data = 0;
|
||||
sdata = smode;
|
||||
sdata[0] = '\0';
|
||||
if (hilo == 0 || hilo == LOW_PIECE) {
|
||||
switch (st->st_mode & S_IFMT) {
|
||||
case S_IFIFO: (void)strcat(sdata, "|"); break;
|
||||
case S_IFDIR: (void)strcat(sdata, "/"); break;
|
||||
case S_IFREG:
|
||||
if (st->st_mode &
|
||||
(S_IXUSR | S_IXGRP | S_IXOTH))
|
||||
(void)strcat(sdata, "*");
|
||||
break;
|
||||
case S_IFLNK: (void)strcat(sdata, "@"); break;
|
||||
case S_IFSOCK: (void)strcat(sdata, "="); break;
|
||||
case S_IFWHT: (void)strcat(sdata, "%"); break;
|
||||
}
|
||||
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;
|
||||
case S_IFSOCK: sdata = "Socket"; break;
|
||||
case S_IFWHT: sdata = "Whiteout File"; break;
|
||||
default: sdata = "???"; break;
|
||||
}
|
||||
hilo = 0;
|
||||
}
|
||||
formats = FMT_STRING;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_STRING;
|
||||
break;
|
||||
case SHOW_filename:
|
||||
small = 0;
|
||||
data = 0;
|
||||
if (file == NULL)
|
||||
(void)strncpy(path, "(stdin)", sizeof(path));
|
||||
else
|
||||
(void)strncpy(path, file, sizeof(path));
|
||||
sdata = path;
|
||||
formats = FMT_STRING;
|
||||
if (ofmt == 0)
|
||||
ofmt = FMT_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 == FMT_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, "d");
|
||||
return (snprintf(buf, blen, lfmt, ts.tv_sec));
|
||||
}
|
||||
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
(void)strcat(lfmt, "d");
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
(void)snprintf(tmp, sizeof(tmp), "%dd", 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++)
|
||||
ts.tv_nsec /= 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, ts.tv_sec, ts.tv_nsec);
|
||||
for (; prec > 9 && l < 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 == FMT_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 != FMT_DECIMAL)
|
||||
data = (u_int32_t)data;
|
||||
|
||||
/*
|
||||
* The four "numeric" output forms.
|
||||
*/
|
||||
(void)strcat(lfmt, "ll");
|
||||
switch (ofmt) {
|
||||
case FMT_DECIMAL: (void)strcat(lfmt, "d"); break;
|
||||
case FMT_OCTAL: (void)strcat(lfmt, "o"); break;
|
||||
case FMT_UNSIGNED: (void)strcat(lfmt, "u"); break;
|
||||
case FMT_HEX: (void)strcat(lfmt, "x"); break;
|
||||
}
|
||||
|
||||
return (snprintf(buf, blen, lfmt, data));
|
||||
}
|
Loading…
Reference in New Issue
Block a user