stat.c revision 216196
197952Sdougb/* 297952Sdougb * Copyright (c) 2002 The NetBSD Foundation, Inc. 397952Sdougb * All rights reserved. 497952Sdougb * 597952Sdougb * This code is derived from software contributed to The NetBSD Foundation 697952Sdougb * by Andrew Brown. 797952Sdougb * 897952Sdougb * Redistribution and use in source and binary forms, with or without 997952Sdougb * modification, are permitted provided that the following conditions 1097952Sdougb * are met: 1197952Sdougb * 1. Redistributions of source code must retain the above copyright 1297952Sdougb * notice, this list of conditions and the following disclaimer. 1397952Sdougb * 2. Redistributions in binary form must reproduce the above copyright 1497952Sdougb * notice, this list of conditions and the following disclaimer in the 1597952Sdougb * documentation and/or other materials provided with the distribution. 1697952Sdougb * 1797952Sdougb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1897952Sdougb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1997952Sdougb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2097952Sdougb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2197952Sdougb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2297952Sdougb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2397952Sdougb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2497952Sdougb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2597952Sdougb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2697952Sdougb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2797952Sdougb * POSSIBILITY OF SUCH DAMAGE. 2897952Sdougb */ 2997952Sdougb 30114924Sdougb#include <sys/cdefs.h> 31101773Sdougb#if 0 3297952Sdougb#ifndef lint 33216196Sdougb__RCSID("$NetBSD: stat.c,v 1.18 2004/05/28 04:48:31 atatat Exp $"); 3497952Sdougb#endif 35101773Sdougb#endif 3697952Sdougb 37101773Sdougb__FBSDID("$FreeBSD: head/usr.bin/stat/stat.c 216196 2010-12-05 09:33:04Z dougb $"); 38101773Sdougb 39120836Sdougb#if HAVE_CONFIG_H 40120836Sdougb#include "config.h" 41120836Sdougb#else /* HAVE_CONFIG_H */ 42120836Sdougb#define HAVE_STRUCT_STAT_ST_FLAGS 1 43120836Sdougb#define HAVE_STRUCT_STAT_ST_GEN 1 44120836Sdougb#define HAVE_STRUCT_STAT_ST_BIRTHTIME 1 45120836Sdougb#define HAVE_STRUCT_STAT_ST_MTIMENSEC 1 46120836Sdougb#define HAVE_DEVNAME 1 47120836Sdougb#endif /* HAVE_CONFIG_H */ 48120836Sdougb 49188498Sed#include <sys/param.h> 50101897Sdougb#include <sys/types.h> 5197952Sdougb#include <sys/stat.h> 52101773Sdougb 53101773Sdougb#include <ctype.h> 5497952Sdougb#include <err.h> 55216196Sdougb#include <errno.h> 56101773Sdougb#include <grp.h> 57101897Sdougb#include <limits.h> 58188498Sed#include <paths.h> 59101773Sdougb#include <pwd.h> 6097952Sdougb#include <stdio.h> 6197952Sdougb#include <stdlib.h> 62101773Sdougb#include <string.h> 63101773Sdougb#include <time.h> 64101773Sdougb#include <unistd.h> 6597952Sdougb 66120836Sdougb#if HAVE_STRUCT_STAT_ST_FLAGS 67120836Sdougb#define DEF_F "%#Xf " 68120836Sdougb#define RAW_F "%f " 69120836Sdougb#define SHELL_F " st_flags=%f" 70120836Sdougb#else /* HAVE_STRUCT_STAT_ST_FLAGS */ 71120836Sdougb#define DEF_F 72120836Sdougb#define RAW_F 73120836Sdougb#define SHELL_F 74120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 75120836Sdougb 76120836Sdougb#if HAVE_STRUCT_STAT_ST_BIRTHTIME 77120836Sdougb#define DEF_B "\"%SB\" " 78120836Sdougb#define RAW_B "%B " 79120836Sdougb#define SHELL_B "st_birthtime=%B " 80120836Sdougb#else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 81120836Sdougb#define DEF_B 82120836Sdougb#define RAW_B 83120836Sdougb#define SHELL_B 84120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 85120836Sdougb 86120836Sdougb#if HAVE_STRUCT_STAT_ST_ATIM 87120836Sdougb#define st_atimespec st_atim 88120836Sdougb#define st_ctimespec st_ctim 89120836Sdougb#define st_mtimespec st_mtim 90120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_ATIM */ 91120836Sdougb 9297952Sdougb#define DEF_FORMAT \ 93120836Sdougb "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \ 94120836Sdougb "%k %b " DEF_F "%N" 95120836Sdougb#define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \ 96120836Sdougb "%k %b " RAW_F "%N" 9797952Sdougb#define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY" 9897952Sdougb#define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY" 9997952Sdougb#define SHELL_FORMAT \ 10097952Sdougb "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \ 10197952Sdougb "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \ 102120836Sdougb "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \ 103120836Sdougb "st_blksize=%k st_blocks=%b" SHELL_F 10497952Sdougb#define LINUX_FORMAT \ 10597952Sdougb " File: \"%N\"%n" \ 10697952Sdougb " Size: %-11z FileType: %HT%n" \ 107188481Smaxim " Mode: (%OMp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \ 10897952Sdougb "Device: %Hd,%Ld Inode: %i Links: %l%n" \ 10997952Sdougb "Access: %Sa%n" \ 11097952Sdougb "Modify: %Sm%n" \ 11197952Sdougb "Change: %Sc" 11297952Sdougb 11397952Sdougb#define TIME_FORMAT "%b %e %T %Y" 11497952Sdougb 11597952Sdougb#define FLAG_POUND 0x01 11697952Sdougb#define FLAG_SPACE 0x02 11797952Sdougb#define FLAG_PLUS 0x04 11897952Sdougb#define FLAG_ZERO 0x08 11997952Sdougb#define FLAG_MINUS 0x10 12097952Sdougb 12197952Sdougb/* 12297952Sdougb * These format characters must all be unique, except the magic one. 12397952Sdougb */ 12497952Sdougb#define FMT_MAGIC '%' 12597952Sdougb#define FMT_DOT '.' 12697952Sdougb 12797952Sdougb#define SIMPLE_NEWLINE 'n' 12897952Sdougb#define SIMPLE_TAB 't' 12997952Sdougb#define SIMPLE_PERCENT '%' 13097952Sdougb#define SIMPLE_NUMBER '@' 13197952Sdougb 13297952Sdougb#define FMT_POUND '#' 13397952Sdougb#define FMT_SPACE ' ' 13497952Sdougb#define FMT_PLUS '+' 13597952Sdougb#define FMT_ZERO '0' 13697952Sdougb#define FMT_MINUS '-' 13797952Sdougb 13897952Sdougb#define FMT_DECIMAL 'D' 13997952Sdougb#define FMT_OCTAL 'O' 14097952Sdougb#define FMT_UNSIGNED 'U' 14197952Sdougb#define FMT_HEX 'X' 14297952Sdougb#define FMT_FLOAT 'F' 14397952Sdougb#define FMT_STRING 'S' 14497952Sdougb 145101773Sdougb#define FMTF_DECIMAL 0x01 146101773Sdougb#define FMTF_OCTAL 0x02 147101773Sdougb#define FMTF_UNSIGNED 0x04 148101773Sdougb#define FMTF_HEX 0x08 149101773Sdougb#define FMTF_FLOAT 0x10 150101773Sdougb#define FMTF_STRING 0x20 151101773Sdougb 15297952Sdougb#define HIGH_PIECE 'H' 15397952Sdougb#define MIDDLE_PIECE 'M' 15497952Sdougb#define LOW_PIECE 'L' 15597952Sdougb 15697952Sdougb#define SHOW_st_dev 'd' 15797952Sdougb#define SHOW_st_ino 'i' 15897952Sdougb#define SHOW_st_mode 'p' 15997952Sdougb#define SHOW_st_nlink 'l' 16097952Sdougb#define SHOW_st_uid 'u' 16197952Sdougb#define SHOW_st_gid 'g' 16297952Sdougb#define SHOW_st_rdev 'r' 16397952Sdougb#define SHOW_st_atime 'a' 16497952Sdougb#define SHOW_st_mtime 'm' 16597952Sdougb#define SHOW_st_ctime 'c' 166114924Sdougb#define SHOW_st_btime 'B' 16797952Sdougb#define SHOW_st_size 'z' 16897952Sdougb#define SHOW_st_blocks 'b' 16997952Sdougb#define SHOW_st_blksize 'k' 17097952Sdougb#define SHOW_st_flags 'f' 17197952Sdougb#define SHOW_st_gen 'v' 17297952Sdougb#define SHOW_symlink 'Y' 17397952Sdougb#define SHOW_filetype 'T' 17497952Sdougb#define SHOW_filename 'N' 17597952Sdougb#define SHOW_sizerdev 'Z' 17697952Sdougb 177101773Sdougbvoid usage(const char *); 17897952Sdougbvoid output(const struct stat *, const char *, 179101773Sdougb const char *, int, int, int); 18097952Sdougbint format1(const struct stat *, /* stat info */ 18197952Sdougb const char *, /* the file name */ 18297952Sdougb const char *, int, /* the format string itself */ 18397952Sdougb char *, size_t, /* a place to put the output */ 18497952Sdougb int, int, int, int, /* the parsed format */ 18597952Sdougb int, int); 186207153Sjilles#if HAVE_STRUCT_STAT_ST_FLAGS 187207153Sjilleschar *xfflagstostr(unsigned long); 188207153Sjilles#endif 18997952Sdougb 19097952Sdougbchar *timefmt; 191101773Sdougbint linkfail; 19297952Sdougb 193101773Sdougb#define addchar(s, c, nl) \ 19497952Sdougb do { \ 195101773Sdougb (void)fputc((c), (s)); \ 196101773Sdougb (*nl) = ((c) == '\n'); \ 19797952Sdougb } while (0/*CONSTCOND*/) 19897952Sdougb 19997952Sdougbint 20097952Sdougbmain(int argc, char *argv[]) 20197952Sdougb{ 20297952Sdougb struct stat st; 203101773Sdougb int ch, rc, errs, am_readlink; 204101773Sdougb int lsF, fmtchar, usestat, fn, nonl, quiet; 205101773Sdougb char *statfmt, *options, *synopsis; 206188498Sed char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV; 207188498Sed const char *file; 20897952Sdougb 209101773Sdougb am_readlink = 0; 21097952Sdougb lsF = 0; 21197952Sdougb fmtchar = '\0'; 21297952Sdougb usestat = 0; 21397952Sdougb nonl = 0; 214101773Sdougb quiet = 0; 215101773Sdougb linkfail = 0; 21697952Sdougb statfmt = NULL; 21797952Sdougb timefmt = NULL; 21897952Sdougb 219101773Sdougb if (strcmp(getprogname(), "readlink") == 0) { 220101773Sdougb am_readlink = 1; 221101773Sdougb options = "n"; 222101773Sdougb synopsis = "[-n] [file ...]"; 223101773Sdougb statfmt = "%Y"; 224101773Sdougb fmtchar = 'f'; 225101773Sdougb quiet = 1; 226101773Sdougb } else { 227101773Sdougb options = "f:FlLnqrst:x"; 228101773Sdougb synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]"; 229101773Sdougb } 230101773Sdougb 231101773Sdougb while ((ch = getopt(argc, argv, options)) != -1) 23297952Sdougb switch (ch) { 23397952Sdougb case 'F': 23497952Sdougb lsF = 1; 23597952Sdougb break; 23697952Sdougb case 'L': 23797952Sdougb usestat = 1; 23897952Sdougb break; 23997952Sdougb case 'n': 24097952Sdougb nonl = 1; 24197952Sdougb break; 242101773Sdougb case 'q': 243101773Sdougb quiet = 1; 244101773Sdougb break; 24597952Sdougb case 'f': 24697952Sdougb statfmt = optarg; 24797952Sdougb /* FALLTHROUGH */ 24897952Sdougb case 'l': 24997952Sdougb case 'r': 25097952Sdougb case 's': 25197952Sdougb case 'x': 25297952Sdougb if (fmtchar != 0) 25397952Sdougb errx(1, "can't use format '%c' with '%c'", 25497952Sdougb fmtchar, ch); 25597952Sdougb fmtchar = ch; 25697952Sdougb break; 25797952Sdougb case 't': 25897952Sdougb timefmt = optarg; 25997952Sdougb break; 26097952Sdougb default: 261101773Sdougb usage(synopsis); 26297952Sdougb } 26397952Sdougb 26497952Sdougb argc -= optind; 26597952Sdougb argv += optind; 26697952Sdougb fn = 1; 26797952Sdougb 26897952Sdougb if (fmtchar == '\0') { 26997952Sdougb if (lsF) 27097952Sdougb fmtchar = 'l'; 27197952Sdougb else { 27297952Sdougb fmtchar = 'f'; 27397952Sdougb statfmt = DEF_FORMAT; 27497952Sdougb } 27597952Sdougb } 27697952Sdougb 27797952Sdougb if (lsF && fmtchar != 'l') 27897952Sdougb errx(1, "can't use format '%c' with -F", fmtchar); 27997952Sdougb 28097952Sdougb switch (fmtchar) { 28197952Sdougb case 'f': 28297952Sdougb /* statfmt already set */ 28397952Sdougb break; 28497952Sdougb case 'l': 28597952Sdougb statfmt = lsF ? LSF_FORMAT : LS_FORMAT; 28697952Sdougb break; 28797952Sdougb case 'r': 28897952Sdougb statfmt = RAW_FORMAT; 28997952Sdougb break; 29097952Sdougb case 's': 29197952Sdougb statfmt = SHELL_FORMAT; 29297952Sdougb break; 29397952Sdougb case 'x': 29497952Sdougb statfmt = LINUX_FORMAT; 29597952Sdougb if (timefmt == NULL) 29697952Sdougb timefmt = "%c"; 29797952Sdougb break; 29897952Sdougb default: 299101773Sdougb usage(synopsis); 30097952Sdougb /*NOTREACHED*/ 30197952Sdougb } 30297952Sdougb 30397952Sdougb if (timefmt == NULL) 30497952Sdougb timefmt = TIME_FORMAT; 30597952Sdougb 30697952Sdougb errs = 0; 30797952Sdougb do { 308188498Sed if (argc == 0) { 309188498Sed if (fdevname_r(STDIN_FILENO, dname + 310188498Sed sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL) 311188498Sed file = dname; 312188498Sed else 313188498Sed file = "(stdin)"; 31497952Sdougb rc = fstat(STDIN_FILENO, &st); 315188498Sed } else { 316188498Sed file = argv[0]; 317216196Sdougb if (usestat) { 318216196Sdougb /* 319216196Sdougb * Try stat() and if it fails, fall back to 320216196Sdougb * lstat() just in case we're examining a 321216196Sdougb * broken symlink. 322216196Sdougb */ 323216196Sdougb if ((rc = stat(file, &st)) == -1 && 324216196Sdougb errno == ENOENT && 325216196Sdougb (rc = lstat(file, &st)) == -1) 326216196Sdougb errno = ENOENT; 327216196Sdougb } 328188498Sed else 329188498Sed rc = lstat(file, &st); 330188498Sed } 33197952Sdougb 33297952Sdougb if (rc == -1) { 33397952Sdougb errs = 1; 334101773Sdougb linkfail = 1; 335101773Sdougb if (!quiet) 336188498Sed warn("%s: stat", file); 33797952Sdougb } 33897952Sdougb else 339188498Sed output(&st, file, statfmt, fn, nonl, quiet); 34097952Sdougb 34197952Sdougb argv++; 34297952Sdougb argc--; 34397952Sdougb fn++; 34497952Sdougb } while (argc > 0); 34597952Sdougb 346101773Sdougb return (am_readlink ? linkfail : errs); 34797952Sdougb} 34897952Sdougb 349207153Sjilles#if HAVE_STRUCT_STAT_ST_FLAGS 350207153Sjilles/* 351207153Sjilles * fflagstostr() wrapper that leaks only once 352207153Sjilles */ 353207153Sjilleschar * 354207153Sjillesxfflagstostr(unsigned long fflags) 355207153Sjilles{ 356207153Sjilles static char *str = NULL; 357207153Sjilles 358207153Sjilles if (str != NULL) 359207153Sjilles free(str); 360207153Sjilles 361207153Sjilles str = fflagstostr(fflags); 362207153Sjilles if (str == NULL) 363207153Sjilles err(1, "fflagstostr"); 364207153Sjilles return (str); 365207153Sjilles} 366207153Sjilles#endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 367207153Sjilles 36897952Sdougbvoid 369101773Sdougbusage(const char *synopsis) 37097952Sdougb{ 37197952Sdougb 372101773Sdougb (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis); 37397952Sdougb exit(1); 37497952Sdougb} 37597952Sdougb 37697952Sdougb/* 37797952Sdougb * Parses a format string. 37897952Sdougb */ 37997952Sdougbvoid 38097952Sdougboutput(const struct stat *st, const char *file, 381101773Sdougb const char *statfmt, int fn, int nonl, int quiet) 38297952Sdougb{ 38397952Sdougb int flags, size, prec, ofmt, hilo, what; 384101773Sdougb char buf[PATH_MAX]; 38597952Sdougb const char *subfmt; 38697952Sdougb int nl, t, i; 38797952Sdougb 388101773Sdougb nl = 1; 38997952Sdougb while (*statfmt != '\0') { 39097952Sdougb 39197952Sdougb /* 39297952Sdougb * Non-format characters go straight out. 39397952Sdougb */ 39497952Sdougb if (*statfmt != FMT_MAGIC) { 395101773Sdougb addchar(stdout, *statfmt, &nl); 39697952Sdougb statfmt++; 39797952Sdougb continue; 39897952Sdougb } 39997952Sdougb 40097952Sdougb /* 40197952Sdougb * The current format "substring" starts here, 40297952Sdougb * and then we skip the magic. 40397952Sdougb */ 40497952Sdougb subfmt = statfmt; 40597952Sdougb statfmt++; 40697952Sdougb 40797952Sdougb /* 40897952Sdougb * Some simple one-character "formats". 40997952Sdougb */ 41097952Sdougb switch (*statfmt) { 41197952Sdougb case SIMPLE_NEWLINE: 412101773Sdougb addchar(stdout, '\n', &nl); 41397952Sdougb statfmt++; 41497952Sdougb continue; 41597952Sdougb case SIMPLE_TAB: 416101773Sdougb addchar(stdout, '\t', &nl); 41797952Sdougb statfmt++; 41897952Sdougb continue; 41997952Sdougb case SIMPLE_PERCENT: 420101773Sdougb addchar(stdout, '%', &nl); 42197952Sdougb statfmt++; 42297952Sdougb continue; 42397952Sdougb case SIMPLE_NUMBER: { 42497952Sdougb char num[12], *p; 42597952Sdougb 42697952Sdougb snprintf(num, sizeof(num), "%d", fn); 42797952Sdougb for (p = &num[0]; *p; p++) 428101773Sdougb addchar(stdout, *p, &nl); 42997952Sdougb statfmt++; 43097952Sdougb continue; 43197952Sdougb } 43297952Sdougb } 43397952Sdougb 43497952Sdougb /* 43597952Sdougb * This must be an actual format string. Format strings are 43697952Sdougb * similar to printf(3) formats up to a point, and are of 43797952Sdougb * the form: 43897952Sdougb * 43997952Sdougb * % required start of format 44097952Sdougb * [-# +0] opt. format characters 44197952Sdougb * size opt. field width 44297952Sdougb * . opt. decimal separator, followed by 44397952Sdougb * prec opt. precision 44497952Sdougb * fmt opt. output specifier (string, numeric, etc.) 44597952Sdougb * sub opt. sub field specifier (high, middle, low) 44697952Sdougb * datum required field specifier (size, mode, etc) 44797952Sdougb * 44897952Sdougb * Only the % and the datum selector are required. All data 44997952Sdougb * have reasonable default output forms. The "sub" specifier 45097952Sdougb * only applies to certain data (mode, dev, rdev, filetype). 45197952Sdougb * The symlink output defaults to STRING, yet will only emit 45297952Sdougb * the leading " -> " if STRING is explicitly specified. The 45397952Sdougb * sizerdev datum will generate rdev output for character or 45497952Sdougb * block devices, and size output for all others. 45597952Sdougb */ 45697952Sdougb flags = 0; 45797952Sdougb do { 45897952Sdougb if (*statfmt == FMT_POUND) 45997952Sdougb flags |= FLAG_POUND; 46097952Sdougb else if (*statfmt == FMT_SPACE) 46197952Sdougb flags |= FLAG_SPACE; 46297952Sdougb else if (*statfmt == FMT_PLUS) 46397952Sdougb flags |= FLAG_PLUS; 46497952Sdougb else if (*statfmt == FMT_ZERO) 46597952Sdougb flags |= FLAG_ZERO; 46697952Sdougb else if (*statfmt == FMT_MINUS) 46797952Sdougb flags |= FLAG_MINUS; 46897952Sdougb else 46997952Sdougb break; 47097952Sdougb statfmt++; 47197952Sdougb } while (1/*CONSTCOND*/); 47297952Sdougb 47397952Sdougb size = -1; 47497952Sdougb if (isdigit((unsigned)*statfmt)) { 47597952Sdougb size = 0; 47697952Sdougb while (isdigit((unsigned)*statfmt)) { 47797952Sdougb size = (size * 10) + (*statfmt - '0'); 47897952Sdougb statfmt++; 47997952Sdougb if (size < 0) 48097952Sdougb goto badfmt; 48197952Sdougb } 48297952Sdougb } 48397952Sdougb 48497952Sdougb prec = -1; 48597952Sdougb if (*statfmt == FMT_DOT) { 48697952Sdougb statfmt++; 48797952Sdougb 48897952Sdougb prec = 0; 48997952Sdougb while (isdigit((unsigned)*statfmt)) { 49097952Sdougb prec = (prec * 10) + (*statfmt - '0'); 49197952Sdougb statfmt++; 49297952Sdougb if (prec < 0) 49397952Sdougb goto badfmt; 49497952Sdougb } 49597952Sdougb } 49697952Sdougb 497101773Sdougb#define fmtcase(x, y) case (y): (x) = (y); statfmt++; break 498101773Sdougb#define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break 49997952Sdougb switch (*statfmt) { 500101773Sdougb fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL); 501101773Sdougb fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL); 502101773Sdougb fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED); 503101773Sdougb fmtcasef(ofmt, FMT_HEX, FMTF_HEX); 504101773Sdougb fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT); 505101773Sdougb fmtcasef(ofmt, FMT_STRING, FMTF_STRING); 50697952Sdougb default: 50797952Sdougb ofmt = 0; 50897952Sdougb break; 50997952Sdougb } 51097952Sdougb 51197952Sdougb switch (*statfmt) { 51297952Sdougb fmtcase(hilo, HIGH_PIECE); 51397952Sdougb fmtcase(hilo, MIDDLE_PIECE); 51497952Sdougb fmtcase(hilo, LOW_PIECE); 51597952Sdougb default: 51697952Sdougb hilo = 0; 51797952Sdougb break; 51897952Sdougb } 51997952Sdougb 52097952Sdougb switch (*statfmt) { 52197952Sdougb fmtcase(what, SHOW_st_dev); 52297952Sdougb fmtcase(what, SHOW_st_ino); 52397952Sdougb fmtcase(what, SHOW_st_mode); 52497952Sdougb fmtcase(what, SHOW_st_nlink); 52597952Sdougb fmtcase(what, SHOW_st_uid); 52697952Sdougb fmtcase(what, SHOW_st_gid); 52797952Sdougb fmtcase(what, SHOW_st_rdev); 52897952Sdougb fmtcase(what, SHOW_st_atime); 52997952Sdougb fmtcase(what, SHOW_st_mtime); 53097952Sdougb fmtcase(what, SHOW_st_ctime); 531114924Sdougb fmtcase(what, SHOW_st_btime); 53297952Sdougb fmtcase(what, SHOW_st_size); 53397952Sdougb fmtcase(what, SHOW_st_blocks); 53497952Sdougb fmtcase(what, SHOW_st_blksize); 53597952Sdougb fmtcase(what, SHOW_st_flags); 53697952Sdougb fmtcase(what, SHOW_st_gen); 53797952Sdougb fmtcase(what, SHOW_symlink); 53897952Sdougb fmtcase(what, SHOW_filetype); 53997952Sdougb fmtcase(what, SHOW_filename); 54097952Sdougb fmtcase(what, SHOW_sizerdev); 54197952Sdougb default: 54297952Sdougb goto badfmt; 54397952Sdougb } 544101773Sdougb#undef fmtcasef 54597952Sdougb#undef fmtcase 54697952Sdougb 54797952Sdougb t = format1(st, 54897952Sdougb file, 54997952Sdougb subfmt, statfmt - subfmt, 550101773Sdougb buf, sizeof(buf), 55197952Sdougb flags, size, prec, ofmt, hilo, what); 55297952Sdougb 553101773Sdougb for (i = 0; i < t && i < sizeof(buf); i++) 554101773Sdougb addchar(stdout, buf[i], &nl); 55597952Sdougb 55697952Sdougb continue; 55797952Sdougb 55897952Sdougb badfmt: 55997952Sdougb errx(1, "%.*s: bad format", 56097952Sdougb (int)(statfmt - subfmt + 1), subfmt); 56197952Sdougb } 56297952Sdougb 56397952Sdougb if (!nl && !nonl) 564101773Sdougb (void)fputc('\n', stdout); 565101773Sdougb (void)fflush(stdout); 56697952Sdougb} 56797952Sdougb 56897952Sdougb/* 56997952Sdougb * Arranges output according to a single parsed format substring. 57097952Sdougb */ 57197952Sdougbint 57297952Sdougbformat1(const struct stat *st, 57397952Sdougb const char *file, 57497952Sdougb const char *fmt, int flen, 57597952Sdougb char *buf, size_t blen, 57697952Sdougb int flags, int size, int prec, int ofmt, 57797952Sdougb int hilo, int what) 57897952Sdougb{ 57997952Sdougb u_int64_t data; 58097952Sdougb char *sdata, lfmt[24], tmp[20]; 581101773Sdougb char smode[12], sid[12], path[PATH_MAX + 4]; 58297952Sdougb struct passwd *pw; 58397952Sdougb struct group *gr; 58497952Sdougb const struct timespec *tsp; 58597952Sdougb struct timespec ts; 58697952Sdougb struct tm *tm; 58797952Sdougb int l, small, formats; 58897952Sdougb 58997952Sdougb tsp = NULL; 59097952Sdougb formats = 0; 59197952Sdougb small = 0; 59297952Sdougb 59397952Sdougb /* 59497952Sdougb * First, pick out the data and tweak it based on hilo or 59597952Sdougb * specified output format (symlink output only). 59697952Sdougb */ 59797952Sdougb switch (what) { 59897952Sdougb case SHOW_st_dev: 59997952Sdougb case SHOW_st_rdev: 60097952Sdougb small = (sizeof(st->st_dev) == 4); 60197952Sdougb data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev; 602120836Sdougb#if HAVE_DEVNAME 60397952Sdougb sdata = (what == SHOW_st_dev) ? 60497952Sdougb devname(st->st_dev, S_IFBLK) : 60597952Sdougb devname(st->st_rdev, 60697952Sdougb S_ISCHR(st->st_mode) ? S_IFCHR : 60797952Sdougb S_ISBLK(st->st_mode) ? S_IFBLK : 60897952Sdougb 0U); 60997952Sdougb if (sdata == NULL) 61097952Sdougb sdata = "???"; 611120836Sdougb#endif /* HAVE_DEVNAME */ 61297952Sdougb if (hilo == HIGH_PIECE) { 61397952Sdougb data = major(data); 61497952Sdougb hilo = 0; 61597952Sdougb } 61697952Sdougb else if (hilo == LOW_PIECE) { 61797952Sdougb data = minor((unsigned)data); 61897952Sdougb hilo = 0; 61997952Sdougb } 620101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 621120836Sdougb#if HAVE_DEVNAME 622101773Sdougb FMTF_STRING; 623120836Sdougb#else /* HAVE_DEVNAME */ 624120836Sdougb 0; 625120836Sdougb#endif /* HAVE_DEVNAME */ 62697952Sdougb if (ofmt == 0) 627101773Sdougb ofmt = FMTF_UNSIGNED; 62897952Sdougb break; 62997952Sdougb case SHOW_st_ino: 63097952Sdougb small = (sizeof(st->st_ino) == 4); 63197952Sdougb data = st->st_ino; 63297952Sdougb sdata = NULL; 633101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 63497952Sdougb if (ofmt == 0) 635101773Sdougb ofmt = FMTF_UNSIGNED; 63697952Sdougb break; 63797952Sdougb case SHOW_st_mode: 63897952Sdougb small = (sizeof(st->st_mode) == 4); 63997952Sdougb data = st->st_mode; 64097952Sdougb strmode(st->st_mode, smode); 64197952Sdougb sdata = smode; 64297952Sdougb l = strlen(sdata); 64397952Sdougb if (sdata[l - 1] == ' ') 64497952Sdougb sdata[--l] = '\0'; 64597952Sdougb if (hilo == HIGH_PIECE) { 64697952Sdougb data >>= 12; 64797952Sdougb sdata += 1; 64897952Sdougb sdata[3] = '\0'; 64997952Sdougb hilo = 0; 65097952Sdougb } 65197952Sdougb else if (hilo == MIDDLE_PIECE) { 65297952Sdougb data = (data >> 9) & 07; 65397952Sdougb sdata += 4; 65497952Sdougb sdata[3] = '\0'; 65597952Sdougb hilo = 0; 65697952Sdougb } 65797952Sdougb else if (hilo == LOW_PIECE) { 65897952Sdougb data &= 0777; 65997952Sdougb sdata += 7; 66097952Sdougb sdata[3] = '\0'; 66197952Sdougb hilo = 0; 66297952Sdougb } 663101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 664101773Sdougb FMTF_STRING; 66597952Sdougb if (ofmt == 0) 666101773Sdougb ofmt = FMTF_OCTAL; 66797952Sdougb break; 66897952Sdougb case SHOW_st_nlink: 66997952Sdougb small = (sizeof(st->st_dev) == 4); 67097952Sdougb data = st->st_nlink; 67197952Sdougb sdata = NULL; 672101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 67397952Sdougb if (ofmt == 0) 674101773Sdougb ofmt = FMTF_UNSIGNED; 67597952Sdougb break; 67697952Sdougb case SHOW_st_uid: 67797952Sdougb small = (sizeof(st->st_uid) == 4); 67897952Sdougb data = st->st_uid; 67997952Sdougb if ((pw = getpwuid(st->st_uid)) != NULL) 68097952Sdougb sdata = pw->pw_name; 68197952Sdougb else { 68297952Sdougb snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid); 68397952Sdougb sdata = sid; 68497952Sdougb } 685101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 686101773Sdougb FMTF_STRING; 68797952Sdougb if (ofmt == 0) 688101773Sdougb ofmt = FMTF_UNSIGNED; 68997952Sdougb break; 69097952Sdougb case SHOW_st_gid: 69197952Sdougb small = (sizeof(st->st_gid) == 4); 69297952Sdougb data = st->st_gid; 69397952Sdougb if ((gr = getgrgid(st->st_gid)) != NULL) 69497952Sdougb sdata = gr->gr_name; 69597952Sdougb else { 69697952Sdougb snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid); 69797952Sdougb sdata = sid; 69897952Sdougb } 699101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 700101773Sdougb FMTF_STRING; 70197952Sdougb if (ofmt == 0) 702101773Sdougb ofmt = FMTF_UNSIGNED; 70397952Sdougb break; 70497952Sdougb case SHOW_st_atime: 70597952Sdougb tsp = &st->st_atimespec; 70697952Sdougb /* FALLTHROUGH */ 70797952Sdougb case SHOW_st_mtime: 70897952Sdougb if (tsp == NULL) 70997952Sdougb tsp = &st->st_mtimespec; 71097952Sdougb /* FALLTHROUGH */ 71197952Sdougb case SHOW_st_ctime: 71297952Sdougb if (tsp == NULL) 71397952Sdougb tsp = &st->st_ctimespec; 714114924Sdougb /* FALLTHROUGH */ 715120836Sdougb#if HAVE_STRUCT_STAT_ST_BIRTHTIME 716114924Sdougb case SHOW_st_btime: 717114924Sdougb if (tsp == NULL) 718114924Sdougb tsp = &st->st_birthtimespec; 719120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 72097952Sdougb ts = *tsp; /* copy so we can muck with it */ 72197952Sdougb small = (sizeof(ts.tv_sec) == 4); 72297952Sdougb data = ts.tv_sec; 72397952Sdougb small = 1; 72497952Sdougb tm = localtime(&ts.tv_sec); 72597952Sdougb (void)strftime(path, sizeof(path), timefmt, tm); 72697952Sdougb sdata = path; 727101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 728101773Sdougb FMTF_FLOAT | FMTF_STRING; 72997952Sdougb if (ofmt == 0) 730101773Sdougb ofmt = FMTF_DECIMAL; 73197952Sdougb break; 73297952Sdougb case SHOW_st_size: 73397952Sdougb small = (sizeof(st->st_size) == 4); 73497952Sdougb data = st->st_size; 73597952Sdougb sdata = NULL; 736101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 73797952Sdougb if (ofmt == 0) 738101773Sdougb ofmt = FMTF_UNSIGNED; 73997952Sdougb break; 74097952Sdougb case SHOW_st_blocks: 74197952Sdougb small = (sizeof(st->st_blocks) == 4); 74297952Sdougb data = st->st_blocks; 74397952Sdougb sdata = NULL; 744101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 74597952Sdougb if (ofmt == 0) 746101773Sdougb ofmt = FMTF_UNSIGNED; 74797952Sdougb break; 74897952Sdougb case SHOW_st_blksize: 74997952Sdougb small = (sizeof(st->st_blksize) == 4); 75097952Sdougb data = st->st_blksize; 75197952Sdougb sdata = NULL; 752101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 75397952Sdougb if (ofmt == 0) 754101773Sdougb ofmt = FMTF_UNSIGNED; 75597952Sdougb break; 756120836Sdougb#if HAVE_STRUCT_STAT_ST_FLAGS 75797952Sdougb case SHOW_st_flags: 75897952Sdougb small = (sizeof(st->st_flags) == 4); 75997952Sdougb data = st->st_flags; 760207153Sjilles sdata = xfflagstostr(st->st_flags); 761207153Sjilles if (*sdata == '\0') 762207153Sjilles sdata = "-"; 763207153Sjilles formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 764207153Sjilles FMTF_STRING; 76597952Sdougb if (ofmt == 0) 766101773Sdougb ofmt = FMTF_UNSIGNED; 76797952Sdougb break; 768120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 769120836Sdougb#if HAVE_STRUCT_STAT_ST_GEN 77097952Sdougb case SHOW_st_gen: 77197952Sdougb small = (sizeof(st->st_gen) == 4); 77297952Sdougb data = st->st_gen; 77397952Sdougb sdata = NULL; 774101773Sdougb formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 77597952Sdougb if (ofmt == 0) 776101773Sdougb ofmt = FMTF_UNSIGNED; 77797952Sdougb break; 778120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_GEN */ 77997952Sdougb case SHOW_symlink: 78097952Sdougb small = 0; 78197952Sdougb data = 0; 78297952Sdougb if (S_ISLNK(st->st_mode)) { 78397952Sdougb snprintf(path, sizeof(path), " -> "); 784105913Sdougb l = readlink(file, path + 4, sizeof(path) - 4 - 1); 78597952Sdougb if (l == -1) { 786101773Sdougb linkfail = 1; 78797952Sdougb l = 0; 78897952Sdougb path[0] = '\0'; 78997952Sdougb } 79097952Sdougb path[l + 4] = '\0'; 791101773Sdougb sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 79297952Sdougb } 793101773Sdougb else { 794101773Sdougb linkfail = 1; 79597952Sdougb sdata = ""; 796101773Sdougb } 797101773Sdougb formats = FMTF_STRING; 79897952Sdougb if (ofmt == 0) 799101773Sdougb ofmt = FMTF_STRING; 80097952Sdougb break; 80197952Sdougb case SHOW_filetype: 80297952Sdougb small = 0; 80397952Sdougb data = 0; 80497952Sdougb sdata = smode; 80597952Sdougb sdata[0] = '\0'; 80697952Sdougb if (hilo == 0 || hilo == LOW_PIECE) { 80797952Sdougb switch (st->st_mode & S_IFMT) { 80897952Sdougb case S_IFIFO: (void)strcat(sdata, "|"); break; 80997952Sdougb case S_IFDIR: (void)strcat(sdata, "/"); break; 81097952Sdougb case S_IFREG: 81197952Sdougb if (st->st_mode & 81297952Sdougb (S_IXUSR | S_IXGRP | S_IXOTH)) 81397952Sdougb (void)strcat(sdata, "*"); 81497952Sdougb break; 81597952Sdougb case S_IFLNK: (void)strcat(sdata, "@"); break; 81697952Sdougb case S_IFSOCK: (void)strcat(sdata, "="); break; 817120836Sdougb#ifdef S_IFWHT 81897952Sdougb case S_IFWHT: (void)strcat(sdata, "%"); break; 819120836Sdougb#endif /* S_IFWHT */ 820120836Sdougb#ifdef S_IFDOOR 821120836Sdougb case S_IFDOOR: (void)strcat(sdata, ">"); break; 822120836Sdougb#endif /* S_IFDOOR */ 82397952Sdougb } 82497952Sdougb hilo = 0; 82597952Sdougb } 82697952Sdougb else if (hilo == HIGH_PIECE) { 82797952Sdougb switch (st->st_mode & S_IFMT) { 82897952Sdougb case S_IFIFO: sdata = "Fifo File"; break; 82997952Sdougb case S_IFCHR: sdata = "Character Device"; break; 83097952Sdougb case S_IFDIR: sdata = "Directory"; break; 83197952Sdougb case S_IFBLK: sdata = "Block Device"; break; 83297952Sdougb case S_IFREG: sdata = "Regular File"; break; 83397952Sdougb case S_IFLNK: sdata = "Symbolic Link"; break; 83497952Sdougb case S_IFSOCK: sdata = "Socket"; break; 835120836Sdougb#ifdef S_IFWHT 83697952Sdougb case S_IFWHT: sdata = "Whiteout File"; break; 837120836Sdougb#endif /* S_IFWHT */ 838120836Sdougb#ifdef S_IFDOOR 839120836Sdougb case S_IFDOOR: sdata = "Door"; break; 840120836Sdougb#endif /* S_IFDOOR */ 84197952Sdougb default: sdata = "???"; break; 84297952Sdougb } 84397952Sdougb hilo = 0; 84497952Sdougb } 845101773Sdougb formats = FMTF_STRING; 84697952Sdougb if (ofmt == 0) 847101773Sdougb ofmt = FMTF_STRING; 84897952Sdougb break; 84997952Sdougb case SHOW_filename: 85097952Sdougb small = 0; 85197952Sdougb data = 0; 852188498Sed (void)strncpy(path, file, sizeof(path)); 85397952Sdougb sdata = path; 854101773Sdougb formats = FMTF_STRING; 85597952Sdougb if (ofmt == 0) 856101773Sdougb ofmt = FMTF_STRING; 85797952Sdougb break; 85897952Sdougb case SHOW_sizerdev: 85997952Sdougb if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { 86097952Sdougb char majdev[20], mindev[20]; 86197952Sdougb int l1, l2; 86297952Sdougb 86397952Sdougb l1 = format1(st, 86497952Sdougb file, 86597952Sdougb fmt, flen, 86697952Sdougb majdev, sizeof(majdev), 86797952Sdougb flags, size, prec, 86897952Sdougb ofmt, HIGH_PIECE, SHOW_st_rdev); 86997952Sdougb l2 = format1(st, 87097952Sdougb file, 87197952Sdougb fmt, flen, 87297952Sdougb mindev, sizeof(mindev), 87397952Sdougb flags, size, prec, 87497952Sdougb ofmt, LOW_PIECE, SHOW_st_rdev); 87597952Sdougb return (snprintf(buf, blen, "%.*s,%.*s", 87697952Sdougb l1, majdev, l2, mindev)); 87797952Sdougb } 87897952Sdougb else { 87997952Sdougb return (format1(st, 88097952Sdougb file, 88197952Sdougb fmt, flen, 88297952Sdougb buf, blen, 88397952Sdougb flags, size, prec, 88497952Sdougb ofmt, 0, SHOW_st_size)); 88597952Sdougb } 88697952Sdougb /*NOTREACHED*/ 88797952Sdougb default: 88897952Sdougb errx(1, "%.*s: bad format", (int)flen, fmt); 88997952Sdougb } 89097952Sdougb 89197952Sdougb /* 89297952Sdougb * If a subdatum was specified but not supported, or an output 89397952Sdougb * format was selected that is not supported, that's an error. 89497952Sdougb */ 89597952Sdougb if (hilo != 0 || (ofmt & formats) == 0) 89697952Sdougb errx(1, "%.*s: bad format", (int)flen, fmt); 89797952Sdougb 89897952Sdougb /* 89997952Sdougb * Assemble the format string for passing to printf(3). 90097952Sdougb */ 90197952Sdougb lfmt[0] = '\0'; 90297952Sdougb (void)strcat(lfmt, "%"); 90397952Sdougb if (flags & FLAG_POUND) 90497952Sdougb (void)strcat(lfmt, "#"); 90597952Sdougb if (flags & FLAG_SPACE) 90697952Sdougb (void)strcat(lfmt, " "); 90797952Sdougb if (flags & FLAG_PLUS) 90897952Sdougb (void)strcat(lfmt, "+"); 90997952Sdougb if (flags & FLAG_MINUS) 91097952Sdougb (void)strcat(lfmt, "-"); 91197952Sdougb if (flags & FLAG_ZERO) 91297952Sdougb (void)strcat(lfmt, "0"); 91397952Sdougb 91497952Sdougb /* 91597952Sdougb * Only the timespecs support the FLOAT output format, and that 91697952Sdougb * requires work that differs from the other formats. 91797952Sdougb */ 918101773Sdougb if (ofmt == FMTF_FLOAT) { 91997952Sdougb /* 92097952Sdougb * Nothing after the decimal point, so just print seconds. 92197952Sdougb */ 92297952Sdougb if (prec == 0) { 92397952Sdougb if (size != -1) { 92497952Sdougb (void)snprintf(tmp, sizeof(tmp), "%d", size); 92597952Sdougb (void)strcat(lfmt, tmp); 92697952Sdougb } 92797952Sdougb (void)strcat(lfmt, "d"); 92897952Sdougb return (snprintf(buf, blen, lfmt, ts.tv_sec)); 92997952Sdougb } 93097952Sdougb 93197952Sdougb /* 93297952Sdougb * Unspecified precision gets all the precision we have: 93397952Sdougb * 9 digits. 93497952Sdougb */ 93597952Sdougb if (prec == -1) 93697952Sdougb prec = 9; 93797952Sdougb 93897952Sdougb /* 93997952Sdougb * Adjust the size for the decimal point and the digits 94097952Sdougb * that will follow. 94197952Sdougb */ 94297952Sdougb size -= prec + 1; 94397952Sdougb 94497952Sdougb /* 94597952Sdougb * Any leftover size that's legitimate will be used. 94697952Sdougb */ 94797952Sdougb if (size > 0) { 94897952Sdougb (void)snprintf(tmp, sizeof(tmp), "%d", size); 94997952Sdougb (void)strcat(lfmt, tmp); 95097952Sdougb } 95197952Sdougb (void)strcat(lfmt, "d"); 95297952Sdougb 95397952Sdougb /* 95497952Sdougb * The stuff after the decimal point always needs zero 95597952Sdougb * filling. 95697952Sdougb */ 95797952Sdougb (void)strcat(lfmt, ".%0"); 95897952Sdougb 95997952Sdougb /* 96097952Sdougb * We can "print" at most nine digits of precision. The 96197952Sdougb * rest we will pad on at the end. 96297952Sdougb */ 96397952Sdougb (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec); 96497952Sdougb (void)strcat(lfmt, tmp); 96597952Sdougb 96697952Sdougb /* 96797952Sdougb * For precision of less that nine digits, trim off the 96897952Sdougb * less significant figures. 96997952Sdougb */ 97097952Sdougb for (; prec < 9; prec++) 97197952Sdougb ts.tv_nsec /= 10; 97297952Sdougb 97397952Sdougb /* 97497952Sdougb * Use the format, and then tack on any zeroes that 97597952Sdougb * might be required to make up the requested precision. 97697952Sdougb */ 97797952Sdougb l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec); 97897952Sdougb for (; prec > 9 && l < blen; prec--, l++) 97997952Sdougb (void)strcat(buf, "0"); 98097952Sdougb return (l); 98197952Sdougb } 98297952Sdougb 98397952Sdougb /* 98497952Sdougb * Add on size and precision, if specified, to the format. 98597952Sdougb */ 98697952Sdougb if (size != -1) { 98797952Sdougb (void)snprintf(tmp, sizeof(tmp), "%d", size); 98897952Sdougb (void)strcat(lfmt, tmp); 98997952Sdougb } 99097952Sdougb if (prec != -1) { 99197952Sdougb (void)snprintf(tmp, sizeof(tmp), ".%d", prec); 99297952Sdougb (void)strcat(lfmt, tmp); 99397952Sdougb } 99497952Sdougb 99597952Sdougb /* 99697952Sdougb * String output uses the temporary sdata. 99797952Sdougb */ 998101773Sdougb if (ofmt == FMTF_STRING) { 99997952Sdougb if (sdata == NULL) 100097952Sdougb errx(1, "%.*s: bad format", (int)flen, fmt); 100197952Sdougb (void)strcat(lfmt, "s"); 100297952Sdougb return (snprintf(buf, blen, lfmt, sdata)); 100397952Sdougb } 100497952Sdougb 100597952Sdougb /* 100697952Sdougb * Ensure that sign extension does not cause bad looking output 100797952Sdougb * for some forms. 100897952Sdougb */ 1009101773Sdougb if (small && ofmt != FMTF_DECIMAL) 101097952Sdougb data = (u_int32_t)data; 101197952Sdougb 101297952Sdougb /* 101397952Sdougb * The four "numeric" output forms. 101497952Sdougb */ 101597952Sdougb (void)strcat(lfmt, "ll"); 101697952Sdougb switch (ofmt) { 1017101773Sdougb case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break; 1018101773Sdougb case FMTF_OCTAL: (void)strcat(lfmt, "o"); break; 1019101773Sdougb case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break; 1020101773Sdougb case FMTF_HEX: (void)strcat(lfmt, "x"); break; 102197952Sdougb } 102297952Sdougb 102397952Sdougb return (snprintf(buf, blen, lfmt, data)); 102497952Sdougb} 1025