2020-09-07 03:46:38 +03:00
|
|
|
/* $NetBSD: stat.c,v 1.46 2020/09/07 00:46:38 mrg Exp $ */
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
/*
|
2011-09-23 00:23:55 +04:00
|
|
|
* Copyright (c) 2002-2011 The NetBSD Foundation, Inc.
|
2002-04-27 20:37:19 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-10-27 03:12:41 +03:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
2013-01-03 17:28:41 +04:00
|
|
|
/* config checked libc, we need the prototype as well */
|
|
|
|
#undef HAVE_DEVNAME
|
2003-10-27 03:12:41 +03:00
|
|
|
#endif
|
|
|
|
|
2002-04-27 20:37:19 +04:00
|
|
|
#include <sys/cdefs.h>
|
2003-10-27 03:12:41 +03:00
|
|
|
#if !defined(lint)
|
2020-09-07 03:46:38 +03:00
|
|
|
__RCSID("$NetBSD: stat.c,v 1.46 2020/09/07 00:46:38 mrg Exp $");
|
2003-07-20 18:12:13 +04:00
|
|
|
#endif
|
|
|
|
|
2003-10-27 03:12:41 +03:00
|
|
|
#if ! HAVE_NBTOOL_CONFIG_H
|
2003-07-20 18:12:13 +04:00
|
|
|
#define HAVE_STRUCT_STAT_ST_FLAGS 1
|
2003-07-25 07:21:15 +04:00
|
|
|
#define HAVE_STRUCT_STAT_ST_GEN 1
|
|
|
|
#define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
|
2005-01-13 03:53:14 +03:00
|
|
|
#define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1
|
2003-07-25 07:21:15 +04:00
|
|
|
#define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
|
|
|
|
#define HAVE_DEVNAME 1
|
2003-10-27 03:12:41 +03:00
|
|
|
#endif /* HAVE_NBTOOL_CONFIG_H */
|
2002-04-27 20:37:19 +04:00
|
|
|
|
2003-05-08 17:05:38 +04:00
|
|
|
#include <sys/types.h>
|
2002-04-27 20:37:19 +04:00
|
|
|
#include <sys/stat.h>
|
2002-07-10 01:25:00 +04:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2002-08-13 18:05:41 +04:00
|
|
|
#include <err.h>
|
2004-05-28 08:48:31 +04:00
|
|
|
#include <errno.h>
|
2002-07-10 01:25:00 +04:00
|
|
|
#include <grp.h>
|
2002-08-14 00:15:06 +04:00
|
|
|
#include <limits.h>
|
2002-07-10 01:25:00 +04:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
2002-04-27 20:37:19 +04:00
|
|
|
#include <stdlib.h>
|
2002-08-13 18:05:41 +04:00
|
|
|
#include <string.h>
|
2002-07-10 01:25:00 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2011-09-23 00:23:55 +04:00
|
|
|
#include <vis.h>
|
2002-04-27 20:37:19 +04:00
|
|
|
|
2003-07-25 07:21:15 +04:00
|
|
|
#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 "
|
2012-12-03 01:13:07 +04:00
|
|
|
#define SHELL_B "st_birthtime=%SB "
|
2017-09-20 04:23:37 +03:00
|
|
|
#define LINUX_B "%n Birth: %SB"
|
2003-07-25 07:21:15 +04:00
|
|
|
#else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
|
|
|
|
#define DEF_B
|
|
|
|
#define RAW_B
|
|
|
|
#define SHELL_B
|
2017-09-20 04:23:37 +03:00
|
|
|
#define LINUX_B
|
2003-07-25 07:21:15 +04:00
|
|
|
#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 */
|
|
|
|
|
2002-04-27 20:37:19 +04:00
|
|
|
#define DEF_FORMAT \
|
2003-07-25 07:21:15 +04:00
|
|
|
"%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"
|
2002-04-27 20:37:19 +04:00
|
|
|
#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 " \
|
2012-12-03 01:13:07 +04:00
|
|
|
"st_atime=%Sa st_mtime=%Sm st_ctime=%Sc " SHELL_B \
|
2003-07-25 07:21:15 +04:00
|
|
|
"st_blksize=%k st_blocks=%b" SHELL_F
|
2002-04-27 20:37:19 +04:00
|
|
|
#define LINUX_FORMAT \
|
|
|
|
" File: \"%N\"%n" \
|
2017-09-20 00:45:28 +03:00
|
|
|
" Size: %-11z Blocks: %-11b IO Block: %-11k %HT%n" \
|
2002-04-27 20:37:19 +04:00
|
|
|
"Device: %Hd,%Ld Inode: %i Links: %l%n" \
|
2017-09-20 04:23:37 +03:00
|
|
|
" Mode: (%Mp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
|
2002-04-27 20:37:19 +04:00
|
|
|
"Access: %Sa%n" \
|
|
|
|
"Modify: %Sm%n" \
|
2017-09-20 04:23:37 +03:00
|
|
|
"Change: %Sc" \
|
|
|
|
LINUX_B
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
#define TIME_FORMAT "%b %e %T %Y"
|
|
|
|
|
2002-05-09 21:52:03 +04:00
|
|
|
#define FLAG_POUND 0x01
|
|
|
|
#define FLAG_SPACE 0x02
|
|
|
|
#define FLAG_PLUS 0x04
|
|
|
|
#define FLAG_ZERO 0x08
|
|
|
|
#define FLAG_MINUS 0x10
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
/*
|
2002-05-09 21:52:03 +04:00
|
|
|
* These format characters must all be unique, except the magic one.
|
2002-04-27 20:37:19 +04:00
|
|
|
*/
|
2002-05-09 21:52:03 +04:00
|
|
|
#define FMT_MAGIC '%'
|
|
|
|
#define FMT_DOT '.'
|
|
|
|
|
2002-04-27 20:37:19 +04:00
|
|
|
#define SIMPLE_NEWLINE 'n'
|
|
|
|
#define SIMPLE_TAB 't'
|
|
|
|
#define SIMPLE_PERCENT '%'
|
2002-05-09 21:52:03 +04:00
|
|
|
#define SIMPLE_NUMBER '@'
|
|
|
|
|
|
|
|
#define FMT_POUND '#'
|
|
|
|
#define FMT_SPACE ' '
|
|
|
|
#define FMT_PLUS '+'
|
|
|
|
#define FMT_ZERO '0'
|
|
|
|
#define FMT_MINUS '-'
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
#define FMT_DECIMAL 'D'
|
|
|
|
#define FMT_OCTAL 'O'
|
|
|
|
#define FMT_UNSIGNED 'U'
|
|
|
|
#define FMT_HEX 'X'
|
|
|
|
#define FMT_FLOAT 'F'
|
|
|
|
#define FMT_STRING 'S'
|
|
|
|
|
2002-07-09 21:22:26 +04:00
|
|
|
#define FMTF_DECIMAL 0x01
|
|
|
|
#define FMTF_OCTAL 0x02
|
|
|
|
#define FMTF_UNSIGNED 0x04
|
|
|
|
#define FMTF_HEX 0x08
|
|
|
|
#define FMTF_FLOAT 0x10
|
|
|
|
#define FMTF_STRING 0x20
|
|
|
|
|
2002-04-27 20:37:19 +04:00
|
|
|
#define HIGH_PIECE 'H'
|
|
|
|
#define MIDDLE_PIECE 'M'
|
|
|
|
#define LOW_PIECE 'L'
|
|
|
|
|
2006-10-07 14:41:50 +04:00
|
|
|
#define SHOW_realpath 'R'
|
2002-04-27 20:37:19 +04:00
|
|
|
#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'
|
2003-05-08 17:05:38 +04:00
|
|
|
#define SHOW_st_btime 'B'
|
2002-04-27 20:37:19 +04:00
|
|
|
#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'
|
|
|
|
|
2011-09-06 22:31:22 +04:00
|
|
|
static void usage(const char *) __dead;
|
|
|
|
static void output(const struct stat *, const char *,
|
2011-02-17 02:08:29 +03:00
|
|
|
const char *, int, int, int);
|
2011-09-06 22:31:22 +04:00
|
|
|
static int format1(const struct stat *, /* stat info */
|
2002-04-27 20:37:19 +04:00
|
|
|
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 */
|
2011-02-17 02:08:29 +03:00
|
|
|
int, int, int);
|
2002-04-27 20:37:19 +04:00
|
|
|
|
2011-09-06 22:31:22 +04:00
|
|
|
static const char *timefmt;
|
|
|
|
static int linkfail;
|
2002-04-27 20:37:19 +04:00
|
|
|
|
2002-07-08 22:48:42 +04:00
|
|
|
#define addchar(s, c, nl) \
|
2002-04-27 20:37:19 +04:00
|
|
|
do { \
|
2002-07-08 22:48:42 +04:00
|
|
|
(void)fputc((c), (s)); \
|
|
|
|
(*nl) = ((c) == '\n'); \
|
2002-04-27 20:37:19 +04:00
|
|
|
} while (0/*CONSTCOND*/)
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct stat st;
|
2002-07-08 22:48:42 +04:00
|
|
|
int ch, rc, errs, am_readlink;
|
|
|
|
int lsF, fmtchar, usestat, fn, nonl, quiet;
|
2009-04-14 03:02:36 +04:00
|
|
|
const char *statfmt, *options, *synopsis;
|
2002-04-27 20:37:19 +04:00
|
|
|
|
2002-07-08 22:48:42 +04:00
|
|
|
am_readlink = 0;
|
2002-04-27 20:37:19 +04:00
|
|
|
lsF = 0;
|
|
|
|
fmtchar = '\0';
|
|
|
|
usestat = 0;
|
|
|
|
nonl = 0;
|
2002-07-08 22:48:42 +04:00
|
|
|
quiet = 0;
|
|
|
|
linkfail = 0;
|
2002-04-27 20:37:19 +04:00
|
|
|
statfmt = NULL;
|
|
|
|
timefmt = NULL;
|
|
|
|
|
2011-02-17 02:08:29 +03:00
|
|
|
setprogname(argv[0]);
|
|
|
|
|
2002-07-08 22:48:42 +04:00
|
|
|
if (strcmp(getprogname(), "readlink") == 0) {
|
|
|
|
am_readlink = 1;
|
2011-02-17 02:08:29 +03:00
|
|
|
options = "fnqsv";
|
|
|
|
synopsis = "[-fnqsv] [file ...]";
|
2002-07-08 22:48:42 +04:00
|
|
|
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)
|
2002-04-27 20:37:19 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'F':
|
|
|
|
lsF = 1;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
usestat = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nonl = 1;
|
|
|
|
break;
|
2002-07-08 22:48:42 +04:00
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
2002-04-27 20:37:19 +04:00
|
|
|
case 'f':
|
2006-10-07 14:41:50 +04:00
|
|
|
if (am_readlink) {
|
|
|
|
statfmt = "%R";
|
|
|
|
break;
|
|
|
|
}
|
2002-04-27 20:37:19 +04:00
|
|
|
statfmt = optarg;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'l':
|
|
|
|
case 'r':
|
|
|
|
case 's':
|
2011-02-17 02:08:29 +03:00
|
|
|
if (am_readlink) {
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*FALLTHROUGH*/
|
2002-04-27 20:37:19 +04:00
|
|
|
case 'x':
|
|
|
|
if (fmtchar != 0)
|
|
|
|
errx(1, "can't use format '%c' with '%c'",
|
|
|
|
fmtchar, ch);
|
|
|
|
fmtchar = ch;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
timefmt = optarg;
|
|
|
|
break;
|
2011-02-17 02:08:29 +03:00
|
|
|
case 'v':
|
|
|
|
quiet = 0;
|
|
|
|
break;
|
2002-04-27 20:37:19 +04:00
|
|
|
default:
|
2002-07-08 22:48:42 +04:00
|
|
|
usage(synopsis);
|
2002-04-27 20:37:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2002-05-09 21:52:03 +04:00
|
|
|
fn = 1;
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
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;
|
2012-12-03 01:13:07 +04:00
|
|
|
if (timefmt == NULL)
|
|
|
|
timefmt = "%s";
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
statfmt = LINUX_FORMAT;
|
|
|
|
if (timefmt == NULL)
|
2017-09-20 20:45:25 +03:00
|
|
|
timefmt = "%Y-%m-%d %H:%M:%S.%f %z";
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
default:
|
2002-07-08 22:48:42 +04:00
|
|
|
usage(synopsis);
|
2002-04-27 20:37:19 +04:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timefmt == NULL)
|
|
|
|
timefmt = TIME_FORMAT;
|
|
|
|
|
|
|
|
errs = 0;
|
|
|
|
do {
|
|
|
|
if (argc == 0)
|
|
|
|
rc = fstat(STDIN_FILENO, &st);
|
2004-05-28 08:48:31 +04:00
|
|
|
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;
|
|
|
|
}
|
2002-04-27 20:37:19 +04:00
|
|
|
else
|
|
|
|
rc = lstat(argv[0], &st);
|
|
|
|
|
|
|
|
if (rc == -1) {
|
|
|
|
errs = 1;
|
2002-07-08 22:48:42 +04:00
|
|
|
linkfail = 1;
|
|
|
|
if (!quiet)
|
2004-12-31 06:24:31 +03:00
|
|
|
warn("%s: %s",
|
|
|
|
argc == 0 ? "(stdin)" : argv[0],
|
|
|
|
usestat ? "stat" : "lstat");
|
2002-04-27 20:37:19 +04:00
|
|
|
}
|
|
|
|
else
|
2011-02-17 02:08:29 +03:00
|
|
|
output(&st, argv[0], statfmt, fn, nonl, quiet);
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
argv++;
|
|
|
|
argc--;
|
2002-05-09 21:52:03 +04:00
|
|
|
fn++;
|
2002-04-27 20:37:19 +04:00
|
|
|
} while (argc > 0);
|
|
|
|
|
2002-07-08 22:48:42 +04:00
|
|
|
return (am_readlink ? linkfail : errs);
|
2002-04-27 20:37:19 +04:00
|
|
|
}
|
|
|
|
|
2011-09-06 22:31:22 +04:00
|
|
|
static void
|
2002-07-08 22:48:42 +04:00
|
|
|
usage(const char *synopsis)
|
2002-04-27 20:37:19 +04:00
|
|
|
{
|
|
|
|
|
2002-07-08 22:48:42 +04:00
|
|
|
(void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
|
2002-04-27 20:37:19 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parses a format string.
|
|
|
|
*/
|
2011-09-06 22:31:22 +04:00
|
|
|
static void
|
2002-04-27 20:37:19 +04:00
|
|
|
output(const struct stat *st, const char *file,
|
2011-02-17 02:08:29 +03:00
|
|
|
const char *statfmt, int fn, int nonl, int quiet)
|
2002-04-27 20:37:19 +04:00
|
|
|
{
|
|
|
|
int flags, size, prec, ofmt, hilo, what;
|
2011-09-23 00:23:55 +04:00
|
|
|
/*
|
|
|
|
* buf size is enough for an item of length PATH_MAX,
|
|
|
|
* multiplied by 4 for vis encoding, plus 4 for symlink
|
|
|
|
* " -> " prefix, plus 1 for \0 terminator.
|
|
|
|
*/
|
|
|
|
char buf[PATH_MAX * 4 + 4 + 1];
|
2002-04-27 20:37:19 +04:00
|
|
|
const char *subfmt;
|
|
|
|
int nl, t, i;
|
|
|
|
|
2002-07-08 22:48:42 +04:00
|
|
|
nl = 1;
|
2002-04-27 20:37:19 +04:00
|
|
|
while (*statfmt != '\0') {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Non-format characters go straight out.
|
|
|
|
*/
|
2002-05-09 21:52:03 +04:00
|
|
|
if (*statfmt != FMT_MAGIC) {
|
2002-07-08 22:48:42 +04:00
|
|
|
addchar(stdout, *statfmt, &nl);
|
2002-04-27 20:37:19 +04:00
|
|
|
statfmt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The current format "substring" starts here,
|
2002-05-09 21:52:03 +04:00
|
|
|
* and then we skip the magic.
|
2002-04-27 20:37:19 +04:00
|
|
|
*/
|
|
|
|
subfmt = statfmt;
|
|
|
|
statfmt++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some simple one-character "formats".
|
|
|
|
*/
|
|
|
|
switch (*statfmt) {
|
|
|
|
case SIMPLE_NEWLINE:
|
2002-07-08 22:48:42 +04:00
|
|
|
addchar(stdout, '\n', &nl);
|
2002-04-27 20:37:19 +04:00
|
|
|
statfmt++;
|
|
|
|
continue;
|
|
|
|
case SIMPLE_TAB:
|
2002-07-08 22:48:42 +04:00
|
|
|
addchar(stdout, '\t', &nl);
|
2002-04-27 20:37:19 +04:00
|
|
|
statfmt++;
|
|
|
|
continue;
|
|
|
|
case SIMPLE_PERCENT:
|
2002-07-08 22:48:42 +04:00
|
|
|
addchar(stdout, '%', &nl);
|
2002-04-27 20:37:19 +04:00
|
|
|
statfmt++;
|
|
|
|
continue;
|
2002-05-09 21:52:03 +04:00
|
|
|
case SIMPLE_NUMBER: {
|
|
|
|
char num[12], *p;
|
|
|
|
|
|
|
|
snprintf(num, sizeof(num), "%d", fn);
|
|
|
|
for (p = &num[0]; *p; p++)
|
2002-07-08 22:48:42 +04:00
|
|
|
addchar(stdout, *p, &nl);
|
2002-05-09 21:52:03 +04:00
|
|
|
statfmt++;
|
|
|
|
continue;
|
|
|
|
}
|
2002-04-27 20:37:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
2011-09-23 00:23:55 +04:00
|
|
|
* For STRING output, the # format requests vis encoding.
|
2002-04-27 20:37:19 +04:00
|
|
|
*/
|
|
|
|
flags = 0;
|
|
|
|
do {
|
2002-05-09 21:52:03 +04:00
|
|
|
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;
|
2002-04-27 20:37:19 +04:00
|
|
|
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;
|
2002-05-09 21:52:03 +04:00
|
|
|
if (*statfmt == FMT_DOT) {
|
2002-04-27 20:37:19 +04:00
|
|
|
statfmt++;
|
|
|
|
|
|
|
|
prec = 0;
|
|
|
|
while (isdigit((unsigned)*statfmt)) {
|
|
|
|
prec = (prec * 10) + (*statfmt - '0');
|
|
|
|
statfmt++;
|
|
|
|
if (prec < 0)
|
|
|
|
goto badfmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-09 21:22:26 +04:00
|
|
|
#define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
|
|
|
|
#define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
|
2002-04-27 20:37:19 +04:00
|
|
|
switch (*statfmt) {
|
2002-07-09 21:22:26 +04:00
|
|
|
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);
|
2002-04-27 20:37:19 +04:00
|
|
|
default:
|
|
|
|
ofmt = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*statfmt) {
|
|
|
|
fmtcase(hilo, HIGH_PIECE);
|
|
|
|
fmtcase(hilo, MIDDLE_PIECE);
|
|
|
|
fmtcase(hilo, LOW_PIECE);
|
|
|
|
default:
|
|
|
|
hilo = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*statfmt) {
|
2006-10-07 14:41:50 +04:00
|
|
|
fmtcase(what, SHOW_realpath);
|
2002-04-27 20:37:19 +04:00
|
|
|
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);
|
2003-05-08 17:05:38 +04:00
|
|
|
fmtcase(what, SHOW_st_btime);
|
2002-04-27 20:37:19 +04:00
|
|
|
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;
|
|
|
|
}
|
2002-07-09 21:22:26 +04:00
|
|
|
#undef fmtcasef
|
2002-04-27 20:37:19 +04:00
|
|
|
#undef fmtcase
|
|
|
|
|
|
|
|
t = format1(st,
|
|
|
|
file,
|
|
|
|
subfmt, statfmt - subfmt,
|
2002-07-08 22:48:42 +04:00
|
|
|
buf, sizeof(buf),
|
2011-02-17 02:08:29 +03:00
|
|
|
flags, size, prec, ofmt, hilo, what, quiet);
|
2002-04-27 20:37:19 +04:00
|
|
|
|
2009-04-14 03:02:36 +04:00
|
|
|
for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
|
2002-07-08 22:48:42 +04:00
|
|
|
addchar(stdout, buf[i], &nl);
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
badfmt:
|
|
|
|
errx(1, "%.*s: bad format",
|
|
|
|
(int)(statfmt - subfmt + 1), subfmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nl && !nonl)
|
2002-07-08 22:48:42 +04:00
|
|
|
(void)fputc('\n', stdout);
|
|
|
|
(void)fflush(stdout);
|
2002-04-27 20:37:19 +04:00
|
|
|
}
|
|
|
|
|
2017-09-20 00:45:28 +03:00
|
|
|
static const char *
|
|
|
|
fmttime(char *buf, size_t len, const char *fmt, time_t secs, long nsecs)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
2017-09-22 01:53:19 +03:00
|
|
|
const char *fpb, *fp1, *fp2; /* pointers into fmt */
|
|
|
|
char *fmt2 = NULL; /* replacement fmt (if not NULL) */
|
|
|
|
/* XXX init of next twp for stupid gcc only */
|
|
|
|
char *f2p = NULL; /* ptr into fmt2 - last added */
|
|
|
|
size_t flen = 0;
|
|
|
|
size_t o;
|
|
|
|
int sl;
|
2017-09-21 21:50:08 +03:00
|
|
|
|
2017-09-20 00:45:28 +03:00
|
|
|
if (localtime_r(&secs, &tm) == NULL) {
|
|
|
|
secs = 0;
|
|
|
|
(void)localtime_r(&secs, &tm);
|
|
|
|
}
|
2017-09-22 01:53:19 +03:00
|
|
|
for (fp1 = fpb = fmt; (fp2 = strchr(fp1, '%')) != NULL; ) {
|
|
|
|
if (fp2[1] != 'f') {
|
|
|
|
/* make sure we don't find the 2nd '%' in "%%" */
|
|
|
|
fp1 = fp2 + 1 + (fp2[1] != '\0');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (fmt2 == NULL) {
|
|
|
|
/* allow for ~100 %f's in the format ... */
|
|
|
|
flen = strlen(fmt) + 1024;
|
|
|
|
|
|
|
|
if ((fmt2 = calloc(flen, 1)) == NULL) {
|
|
|
|
fp1 = fp2 + 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
f2p = fmt2;
|
|
|
|
|
|
|
|
o = (size_t)(fp2 - fpb);
|
|
|
|
memcpy(f2p, fpb, o); /* must fit */
|
2017-09-20 00:45:28 +03:00
|
|
|
fmt = fmt2;
|
2017-09-22 01:53:19 +03:00
|
|
|
} else {
|
|
|
|
o = (size_t)(fp2 - fpb);
|
|
|
|
if (flen > o)
|
|
|
|
memcpy(f2p, fpb, o);
|
|
|
|
}
|
|
|
|
if (flen < o + 10) { /* 9 digits + \0 == 10 */
|
|
|
|
*f2p = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
f2p += o;
|
|
|
|
flen -= o;
|
|
|
|
sl = snprintf(f2p, flen, "%.9ld", nsecs);
|
|
|
|
if (sl == -1)
|
|
|
|
sl = 0;
|
|
|
|
f2p += sl;
|
|
|
|
*f2p = '\0';
|
|
|
|
flen -= sl;
|
|
|
|
fp1 = fp2 + 2;
|
|
|
|
fpb = fp1;
|
|
|
|
}
|
|
|
|
if (fmt2 != NULL) {
|
|
|
|
o = strlen(fpb);
|
|
|
|
if (flen > o) {
|
|
|
|
memcpy(f2p, fpb, o);
|
|
|
|
f2p[o] = '\0';
|
2017-09-20 00:45:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)strftime(buf, len, fmt, &tm);
|
|
|
|
free(fmt2);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2002-04-27 20:37:19 +04:00
|
|
|
/*
|
|
|
|
* Arranges output according to a single parsed format substring.
|
|
|
|
*/
|
2011-09-06 22:31:22 +04:00
|
|
|
static int
|
2002-04-27 20:37:19 +04:00
|
|
|
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,
|
2011-02-17 02:08:29 +03:00
|
|
|
int hilo, int what, int quiet)
|
2002-04-27 20:37:19 +04:00
|
|
|
{
|
2017-09-20 00:45:28 +03:00
|
|
|
uint64_t data;
|
2009-04-14 03:02:36 +04:00
|
|
|
char *stmp, lfmt[24], tmp[20];
|
|
|
|
const char *sdata;
|
2019-02-04 11:07:33 +03:00
|
|
|
char smode[12], sid[13], path[PATH_MAX + 4], visbuf[PATH_MAX * 4 + 4];
|
2002-04-27 20:37:19 +04:00
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
2003-10-26 23:43:13 +03:00
|
|
|
time_t secs;
|
|
|
|
long nsecs;
|
2011-09-23 00:23:55 +04:00
|
|
|
int l;
|
|
|
|
int formats; /* bitmap of allowed formats for this datum */
|
|
|
|
int small; /* true if datum is a small integer */
|
|
|
|
int gottime; /* true if secs and nsecs are valid */
|
|
|
|
int shift; /* powers of 2 to scale numbers before printing */
|
|
|
|
size_t prefixlen; /* length of constant prefix for string data */
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
formats = 0;
|
|
|
|
small = 0;
|
2003-10-26 23:43:13 +03:00
|
|
|
gottime = 0;
|
|
|
|
secs = 0;
|
|
|
|
nsecs = 0;
|
2005-06-23 07:13:24 +04:00
|
|
|
shift = 0;
|
2011-09-23 00:23:55 +04:00
|
|
|
prefixlen = 0;
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2003-07-25 07:21:15 +04:00
|
|
|
#if HAVE_DEVNAME
|
2002-04-27 20:37:19 +04:00
|
|
|
sdata = (what == SHOW_st_dev) ?
|
|
|
|
devname(st->st_dev, S_IFBLK) :
|
|
|
|
devname(st->st_rdev,
|
2013-01-03 17:28:41 +04:00
|
|
|
S_ISCHR(st->st_mode) ? S_IFCHR :
|
|
|
|
S_ISBLK(st->st_mode) ? S_IFBLK :
|
|
|
|
0U);
|
2002-05-31 20:45:16 +04:00
|
|
|
if (sdata == NULL)
|
|
|
|
sdata = "???";
|
2003-07-25 07:21:15 +04:00
|
|
|
#endif /* HAVE_DEVNAME */
|
2002-04-27 20:37:19 +04:00
|
|
|
if (hilo == HIGH_PIECE) {
|
|
|
|
data = major(data);
|
|
|
|
hilo = 0;
|
|
|
|
}
|
|
|
|
else if (hilo == LOW_PIECE) {
|
|
|
|
data = minor((unsigned)data);
|
|
|
|
hilo = 0;
|
|
|
|
}
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
|
2003-07-25 07:21:15 +04:00
|
|
|
#if HAVE_DEVNAME
|
2002-07-09 21:22:26 +04:00
|
|
|
FMTF_STRING;
|
2003-07-25 07:21:15 +04:00
|
|
|
#else /* HAVE_DEVNAME */
|
|
|
|
0;
|
|
|
|
#endif /* HAVE_DEVNAME */
|
2017-09-20 00:45:28 +03:00
|
|
|
if (ofmt == 0) {
|
|
|
|
if (data == (uint64_t)-1)
|
|
|
|
ofmt = FMTF_DECIMAL;
|
|
|
|
else
|
|
|
|
ofmt = FMTF_UNSIGNED;
|
|
|
|
}
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
case SHOW_st_ino:
|
|
|
|
small = (sizeof(st->st_ino) == 4);
|
|
|
|
data = st->st_ino;
|
|
|
|
sdata = NULL;
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_UNSIGNED;
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
case SHOW_st_mode:
|
|
|
|
small = (sizeof(st->st_mode) == 4);
|
|
|
|
data = st->st_mode;
|
|
|
|
strmode(st->st_mode, smode);
|
2009-04-14 03:02:36 +04:00
|
|
|
stmp = smode;
|
|
|
|
l = strlen(stmp);
|
|
|
|
if (stmp[l - 1] == ' ')
|
|
|
|
stmp[--l] = '\0';
|
2002-04-27 20:37:19 +04:00
|
|
|
if (hilo == HIGH_PIECE) {
|
|
|
|
data >>= 12;
|
2009-04-14 03:02:36 +04:00
|
|
|
stmp += 1;
|
|
|
|
stmp[3] = '\0';
|
2002-04-27 20:37:19 +04:00
|
|
|
hilo = 0;
|
|
|
|
}
|
|
|
|
else if (hilo == MIDDLE_PIECE) {
|
2002-05-09 21:52:03 +04:00
|
|
|
data = (data >> 9) & 07;
|
2009-04-14 03:02:36 +04:00
|
|
|
stmp += 4;
|
|
|
|
stmp[3] = '\0';
|
2002-04-27 20:37:19 +04:00
|
|
|
hilo = 0;
|
|
|
|
}
|
|
|
|
else if (hilo == LOW_PIECE) {
|
2002-05-09 21:52:03 +04:00
|
|
|
data &= 0777;
|
2009-04-14 03:02:36 +04:00
|
|
|
stmp += 7;
|
|
|
|
stmp[3] = '\0';
|
2002-04-27 20:37:19 +04:00
|
|
|
hilo = 0;
|
|
|
|
}
|
2009-04-14 03:02:36 +04:00
|
|
|
sdata = stmp;
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
|
|
|
|
FMTF_STRING;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_OCTAL;
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
case SHOW_st_nlink:
|
|
|
|
small = (sizeof(st->st_dev) == 4);
|
|
|
|
data = st->st_nlink;
|
|
|
|
sdata = NULL;
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_UNSIGNED;
|
2002-04-27 20:37:19 +04:00
|
|
|
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;
|
|
|
|
}
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
|
|
|
|
FMTF_STRING;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_UNSIGNED;
|
2002-04-27 20:37:19 +04:00
|
|
|
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;
|
|
|
|
}
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
|
|
|
|
FMTF_STRING;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_UNSIGNED;
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
case SHOW_st_atime:
|
2003-10-26 23:43:13 +03:00
|
|
|
gottime = 1;
|
|
|
|
secs = st->st_atime;
|
2003-10-27 03:16:21 +03:00
|
|
|
#if HAVE_STRUCT_STAT_ST_MTIMENSEC
|
2003-10-26 23:43:13 +03:00
|
|
|
nsecs = st->st_atimensec;
|
|
|
|
#endif
|
2002-04-27 20:37:19 +04:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case SHOW_st_mtime:
|
2003-10-26 23:43:13 +03:00
|
|
|
if (!gottime) {
|
2003-10-29 07:25:46 +03:00
|
|
|
gottime = 1;
|
2003-10-26 23:43:13 +03:00
|
|
|
secs = st->st_mtime;
|
2003-10-27 03:16:21 +03:00
|
|
|
#if HAVE_STRUCT_STAT_ST_MTIMENSEC
|
2003-10-26 23:43:13 +03:00
|
|
|
nsecs = st->st_mtimensec;
|
|
|
|
#endif
|
|
|
|
}
|
2002-04-27 20:37:19 +04:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case SHOW_st_ctime:
|
2003-10-26 23:43:13 +03:00
|
|
|
if (!gottime) {
|
2003-10-29 07:25:46 +03:00
|
|
|
gottime = 1;
|
2003-10-26 23:43:13 +03:00
|
|
|
secs = st->st_ctime;
|
2003-10-27 03:16:21 +03:00
|
|
|
#if HAVE_STRUCT_STAT_ST_MTIMENSEC
|
2003-10-26 23:43:13 +03:00
|
|
|
nsecs = st->st_ctimensec;
|
|
|
|
#endif
|
|
|
|
}
|
2003-07-25 07:21:15 +04:00
|
|
|
#if HAVE_STRUCT_STAT_ST_BIRTHTIME
|
2019-10-04 14:12:16 +03:00
|
|
|
/* FALLTHROUGH */
|
2003-05-08 17:05:38 +04:00
|
|
|
case SHOW_st_btime:
|
2003-10-26 23:43:13 +03:00
|
|
|
if (!gottime) {
|
2003-10-29 07:25:46 +03:00
|
|
|
gottime = 1;
|
2004-06-21 02:20:14 +04:00
|
|
|
secs = st->st_birthtime;
|
2005-01-13 03:53:14 +03:00
|
|
|
#if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
|
2004-06-21 02:20:14 +04:00
|
|
|
nsecs = st->st_birthtimensec;
|
2005-01-13 03:53:14 +03:00
|
|
|
#endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */
|
2003-10-26 23:43:13 +03:00
|
|
|
}
|
2003-07-25 07:21:15 +04:00
|
|
|
#endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
|
2003-10-26 23:43:13 +03:00
|
|
|
small = (sizeof(secs) == 4);
|
|
|
|
data = secs;
|
2017-09-20 00:45:28 +03:00
|
|
|
sdata = fmttime(path, sizeof(path), timefmt, secs, nsecs);
|
|
|
|
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
|
|
|
|
FMTF_FLOAT | FMTF_STRING;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_DECIMAL;
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
case SHOW_st_size:
|
|
|
|
small = (sizeof(st->st_size) == 4);
|
|
|
|
data = st->st_size;
|
|
|
|
sdata = NULL;
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_UNSIGNED;
|
2005-06-23 07:13:24 +04:00
|
|
|
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;
|
|
|
|
}
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
case SHOW_st_blocks:
|
|
|
|
small = (sizeof(st->st_blocks) == 4);
|
|
|
|
data = st->st_blocks;
|
|
|
|
sdata = NULL;
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_UNSIGNED;
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
case SHOW_st_blksize:
|
|
|
|
small = (sizeof(st->st_blksize) == 4);
|
|
|
|
data = st->st_blksize;
|
|
|
|
sdata = NULL;
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_UNSIGNED;
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
2003-07-20 18:12:13 +04:00
|
|
|
#if HAVE_STRUCT_STAT_ST_FLAGS
|
2002-04-27 20:37:19 +04:00
|
|
|
case SHOW_st_flags:
|
|
|
|
small = (sizeof(st->st_flags) == 4);
|
|
|
|
data = st->st_flags;
|
|
|
|
sdata = NULL;
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_UNSIGNED;
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
2003-07-25 07:21:15 +04:00
|
|
|
#endif /* HAVE_STRUCT_STAT_ST_FLAGS */
|
|
|
|
#if HAVE_STRUCT_STAT_ST_GEN
|
2002-04-27 20:37:19 +04:00
|
|
|
case SHOW_st_gen:
|
|
|
|
small = (sizeof(st->st_gen) == 4);
|
|
|
|
data = st->st_gen;
|
|
|
|
sdata = NULL;
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_UNSIGNED;
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
2003-07-25 07:21:15 +04:00
|
|
|
#endif /* HAVE_STRUCT_STAT_ST_GEN */
|
2006-10-07 14:41:50 +04:00
|
|
|
case SHOW_realpath:
|
|
|
|
small = 0;
|
|
|
|
data = 0;
|
2008-02-09 12:18:07 +03:00
|
|
|
if (file == NULL) {
|
2010-12-16 08:30:16 +03:00
|
|
|
(void)strlcpy(path, "(stdin)", sizeof(path));
|
2008-02-09 12:18:07 +03:00
|
|
|
sdata = path;
|
|
|
|
} else {
|
|
|
|
snprintf(path, sizeof(path), " -> ");
|
|
|
|
if (realpath(file, path + 4) == NULL) {
|
2011-02-17 02:08:29 +03:00
|
|
|
if (!quiet)
|
|
|
|
warn("realpath `%s'", file);
|
2008-02-09 12:18:07 +03:00
|
|
|
linkfail = 1;
|
|
|
|
l = 0;
|
|
|
|
path[0] = '\0';
|
|
|
|
}
|
2008-05-16 21:58:33 +04:00
|
|
|
sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
|
2011-09-23 00:23:55 +04:00
|
|
|
prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
|
2006-10-07 14:41:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
formats = FMTF_STRING;
|
|
|
|
if (ofmt == 0)
|
|
|
|
ofmt = FMTF_STRING;
|
|
|
|
break;
|
2002-04-27 20:37:19 +04:00
|
|
|
case SHOW_symlink:
|
|
|
|
small = 0;
|
|
|
|
data = 0;
|
|
|
|
if (S_ISLNK(st->st_mode)) {
|
|
|
|
snprintf(path, sizeof(path), " -> ");
|
2002-10-20 00:33:17 +04:00
|
|
|
l = readlink(file, path + 4, sizeof(path) - 4 - 1);
|
2002-04-27 20:37:19 +04:00
|
|
|
if (l == -1) {
|
2011-02-17 02:08:29 +03:00
|
|
|
if (!quiet)
|
|
|
|
warn("readlink `%s'", file);
|
2002-07-08 22:48:42 +04:00
|
|
|
linkfail = 1;
|
2002-04-27 20:37:19 +04:00
|
|
|
l = 0;
|
|
|
|
path[0] = '\0';
|
|
|
|
}
|
|
|
|
path[l + 4] = '\0';
|
2002-07-09 21:22:26 +04:00
|
|
|
sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
|
2011-09-23 00:23:55 +04:00
|
|
|
prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
|
2002-04-27 20:37:19 +04:00
|
|
|
}
|
2002-07-08 22:48:42 +04:00
|
|
|
else {
|
|
|
|
linkfail = 1;
|
2002-04-27 20:37:19 +04:00
|
|
|
sdata = "";
|
2002-07-08 22:48:42 +04:00
|
|
|
}
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_STRING;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_STRING;
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
case SHOW_filetype:
|
|
|
|
small = 0;
|
|
|
|
data = 0;
|
2009-04-14 03:02:36 +04:00
|
|
|
sdata = "";
|
2002-04-27 20:37:19 +04:00
|
|
|
if (hilo == 0 || hilo == LOW_PIECE) {
|
|
|
|
switch (st->st_mode & S_IFMT) {
|
2009-04-14 03:02:36 +04:00
|
|
|
case S_IFIFO: sdata = "|"; break;
|
|
|
|
case S_IFDIR: sdata = "/"; break;
|
2002-04-27 20:37:19 +04:00
|
|
|
case S_IFREG:
|
|
|
|
if (st->st_mode &
|
|
|
|
(S_IXUSR | S_IXGRP | S_IXOTH))
|
2009-04-14 03:02:36 +04:00
|
|
|
sdata = "*";
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
2009-04-14 03:02:36 +04:00
|
|
|
case S_IFLNK: sdata = "@"; break;
|
2004-06-21 02:20:14 +04:00
|
|
|
#ifdef S_IFSOCK
|
2009-04-14 03:02:36 +04:00
|
|
|
case S_IFSOCK: sdata = "="; break;
|
2004-06-21 02:20:14 +04:00
|
|
|
#endif
|
2003-07-25 07:21:15 +04:00
|
|
|
#ifdef S_IFWHT
|
2009-04-14 03:02:36 +04:00
|
|
|
case S_IFWHT: sdata = "%"; break;
|
2003-07-25 07:21:15 +04:00
|
|
|
#endif /* S_IFWHT */
|
|
|
|
#ifdef S_IFDOOR
|
2009-04-14 03:02:36 +04:00
|
|
|
case S_IFDOOR: sdata = ">"; break;
|
2003-07-25 07:21:15 +04:00
|
|
|
#endif /* S_IFDOOR */
|
2002-04-27 20:37:19 +04:00
|
|
|
}
|
|
|
|
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;
|
2004-06-21 02:20:14 +04:00
|
|
|
#ifdef S_IFSOCK
|
2002-04-27 20:37:19 +04:00
|
|
|
case S_IFSOCK: sdata = "Socket"; break;
|
2004-06-21 02:20:14 +04:00
|
|
|
#endif
|
2003-07-25 07:21:15 +04:00
|
|
|
#ifdef S_IFWHT
|
2002-04-27 20:37:19 +04:00
|
|
|
case S_IFWHT: sdata = "Whiteout File"; break;
|
2003-07-25 07:21:15 +04:00
|
|
|
#endif /* S_IFWHT */
|
|
|
|
#ifdef S_IFDOOR
|
|
|
|
case S_IFDOOR: sdata = "Door"; break;
|
|
|
|
#endif /* S_IFDOOR */
|
2002-04-27 20:37:19 +04:00
|
|
|
default: sdata = "???"; break;
|
|
|
|
}
|
|
|
|
hilo = 0;
|
|
|
|
}
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_STRING;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_STRING;
|
2002-04-27 20:37:19 +04:00
|
|
|
break;
|
|
|
|
case SHOW_filename:
|
|
|
|
small = 0;
|
|
|
|
data = 0;
|
2004-12-31 06:24:31 +03:00
|
|
|
if (file == NULL) {
|
2010-12-16 08:30:16 +03:00
|
|
|
(void)strlcpy(path, "(stdin)", sizeof(path));
|
2004-12-31 06:24:31 +03:00
|
|
|
if (hilo == HIGH_PIECE || hilo == LOW_PIECE)
|
|
|
|
hilo = 0;
|
|
|
|
}
|
|
|
|
else if (hilo == 0)
|
2010-12-16 08:30:16 +03:00
|
|
|
(void)strlcpy(path, file, sizeof(path));
|
2004-12-31 06:24:31 +03:00
|
|
|
else {
|
|
|
|
char *s;
|
2010-12-16 08:30:16 +03:00
|
|
|
(void)strlcpy(path, file, sizeof(path));
|
2004-12-31 06:24:31 +03:00
|
|
|
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)
|
2010-12-16 08:30:16 +03:00
|
|
|
(void)strlcpy(path, ".", sizeof(path));
|
2004-12-31 06:24:31 +03:00
|
|
|
else {
|
|
|
|
while (s != path && s[0] == '/')
|
|
|
|
*s-- = '\0';
|
|
|
|
}
|
|
|
|
hilo = 0;
|
|
|
|
}
|
|
|
|
else if (hilo == LOW_PIECE) {
|
|
|
|
if (s != NULL && s[1] != '\0')
|
2010-12-16 08:30:16 +03:00
|
|
|
(void)strlcpy(path, s + 1,
|
2004-12-31 06:24:31 +03:00
|
|
|
sizeof(path));
|
|
|
|
hilo = 0;
|
|
|
|
}
|
|
|
|
}
|
2002-04-27 20:37:19 +04:00
|
|
|
sdata = path;
|
2002-07-09 21:22:26 +04:00
|
|
|
formats = FMTF_STRING;
|
2002-04-27 20:37:19 +04:00
|
|
|
if (ofmt == 0)
|
2002-07-09 21:22:26 +04:00
|
|
|
ofmt = FMTF_STRING;
|
2002-04-27 20:37:19 +04:00
|
|
|
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,
|
2011-02-17 02:08:29 +03:00
|
|
|
ofmt, HIGH_PIECE, SHOW_st_rdev, quiet);
|
2002-04-27 20:37:19 +04:00
|
|
|
l2 = format1(st,
|
|
|
|
file,
|
|
|
|
fmt, flen,
|
|
|
|
mindev, sizeof(mindev),
|
|
|
|
flags, size, prec,
|
2011-02-17 02:08:29 +03:00
|
|
|
ofmt, LOW_PIECE, SHOW_st_rdev, quiet);
|
2002-04-27 20:37:19 +04:00
|
|
|
return (snprintf(buf, blen, "%.*s,%.*s",
|
|
|
|
l1, majdev, l2, mindev));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (format1(st,
|
|
|
|
file,
|
|
|
|
fmt, flen,
|
|
|
|
buf, blen,
|
|
|
|
flags, size, prec,
|
2011-02-17 02:08:29 +03:00
|
|
|
ofmt, 0, SHOW_st_size, quiet));
|
2002-04-27 20:37:19 +04:00
|
|
|
}
|
|
|
|
/*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);
|
|
|
|
|
2011-09-23 00:23:55 +04:00
|
|
|
/*
|
|
|
|
* FLAG_POUND with FMTF_STRING means use vis(3) encoding.
|
|
|
|
* First prefixlen chars are not encoded.
|
|
|
|
*/
|
|
|
|
if ((flags & FLAG_POUND) != 0 && ofmt == FMTF_STRING) {
|
|
|
|
flags &= !FLAG_POUND;
|
|
|
|
strncpy(visbuf, sdata, prefixlen);
|
2020-09-07 03:46:38 +03:00
|
|
|
/* Avoid GCC warnings. */
|
|
|
|
visbuf[prefixlen] = 0;
|
2011-09-23 00:23:55 +04:00
|
|
|
strnvis(visbuf + prefixlen, sizeof(visbuf) - prefixlen,
|
|
|
|
sdata + prefixlen, VIS_WHITE | VIS_OCTAL | VIS_CSTYLE);
|
|
|
|
sdata = visbuf;
|
|
|
|
}
|
|
|
|
|
2002-04-27 20:37:19 +04:00
|
|
|
/*
|
|
|
|
* Assemble the format string for passing to printf(3).
|
|
|
|
*/
|
|
|
|
lfmt[0] = '\0';
|
|
|
|
(void)strcat(lfmt, "%");
|
2002-05-09 21:52:03 +04:00
|
|
|
if (flags & FLAG_POUND)
|
2002-04-27 20:37:19 +04:00
|
|
|
(void)strcat(lfmt, "#");
|
2002-05-09 21:52:03 +04:00
|
|
|
if (flags & FLAG_SPACE)
|
2002-04-27 20:37:19 +04:00
|
|
|
(void)strcat(lfmt, " ");
|
2002-05-09 21:52:03 +04:00
|
|
|
if (flags & FLAG_PLUS)
|
2002-04-27 20:37:19 +04:00
|
|
|
(void)strcat(lfmt, "+");
|
2002-05-09 21:52:03 +04:00
|
|
|
if (flags & FLAG_MINUS)
|
2002-04-27 20:37:19 +04:00
|
|
|
(void)strcat(lfmt, "-");
|
2002-05-09 21:52:03 +04:00
|
|
|
if (flags & FLAG_ZERO)
|
2002-04-27 20:37:19 +04:00
|
|
|
(void)strcat(lfmt, "0");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only the timespecs support the FLOAT output format, and that
|
|
|
|
* requires work that differs from the other formats.
|
|
|
|
*/
|
2002-07-09 21:22:26 +04:00
|
|
|
if (ofmt == FMTF_FLOAT) {
|
2002-04-27 20:37:19 +04:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
2010-11-25 01:57:53 +03:00
|
|
|
(void)strcat(lfmt, "lld");
|
|
|
|
return (snprintf(buf, blen, lfmt,
|
|
|
|
(long long)secs));
|
2002-04-27 20:37:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
2010-11-25 07:33:30 +03:00
|
|
|
/* Seconds: time_t cast to long long. */
|
2010-11-25 01:57:53 +03:00
|
|
|
(void)strcat(lfmt, "lld");
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
2010-11-25 07:33:30 +03:00
|
|
|
*
|
|
|
|
* Nanoseconds: long.
|
2002-04-27 20:37:19 +04:00
|
|
|
*/
|
2010-11-25 01:57:53 +03:00
|
|
|
(void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec);
|
2002-04-27 20:37:19 +04:00
|
|
|
(void)strcat(lfmt, tmp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For precision of less that nine digits, trim off the
|
|
|
|
* less significant figures.
|
|
|
|
*/
|
|
|
|
for (; prec < 9; prec++)
|
2003-10-26 23:43:13 +03:00
|
|
|
nsecs /= 10;
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use the format, and then tack on any zeroes that
|
|
|
|
* might be required to make up the requested precision.
|
|
|
|
*/
|
2010-11-25 01:57:53 +03:00
|
|
|
l = snprintf(buf, blen, lfmt, (long long)secs, nsecs);
|
2009-04-14 03:02:36 +04:00
|
|
|
for (; prec > 9 && l < (int)blen; prec--, l++)
|
2002-04-27 20:37:19 +04:00
|
|
|
(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.
|
|
|
|
*/
|
2002-07-09 21:22:26 +04:00
|
|
|
if (ofmt == FMTF_STRING) {
|
2002-04-27 20:37:19 +04:00
|
|
|
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.
|
|
|
|
*/
|
2002-07-09 21:22:26 +04:00
|
|
|
if (small && ofmt != FMTF_DECIMAL)
|
2017-09-20 00:45:28 +03:00
|
|
|
data = (uint32_t)data;
|
2002-04-27 20:37:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The four "numeric" output forms.
|
|
|
|
*/
|
|
|
|
(void)strcat(lfmt, "ll");
|
|
|
|
switch (ofmt) {
|
2002-07-09 21:22:26 +04:00
|
|
|
case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
|
2005-06-23 07:13:24 +04:00
|
|
|
case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
|
2002-07-09 21:22:26 +04:00
|
|
|
case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
|
|
|
|
case FMTF_HEX: (void)strcat(lfmt, "x"); break;
|
2002-04-27 20:37:19 +04:00
|
|
|
}
|
|
|
|
|
2005-06-23 07:13:24 +04:00
|
|
|
/*
|
|
|
|
* shift and round to nearest for kilobytes, megabytes,
|
|
|
|
* gigabytes.
|
|
|
|
*/
|
|
|
|
if (shift > 0) {
|
|
|
|
data >>= (shift - 1);
|
|
|
|
data++;
|
|
|
|
data >>= 1;
|
|
|
|
}
|
|
|
|
|
2002-04-27 20:37:19 +04:00
|
|
|
return (snprintf(buf, blen, lfmt, data));
|
|
|
|
}
|