1*474b24f9SLionel Sambuc /* $NetBSD: stat.c,v 1.36 2011/09/22 20:23:56 apb Exp $ */ 249cab1c7SVivek Prakash 349cab1c7SVivek Prakash /* 4*474b24f9SLionel Sambuc * Copyright (c) 2002-2011 The NetBSD Foundation, Inc. 549cab1c7SVivek Prakash * All rights reserved. 649cab1c7SVivek Prakash * 749cab1c7SVivek Prakash * This code is derived from software contributed to The NetBSD Foundation 849cab1c7SVivek Prakash * by Andrew Brown. 949cab1c7SVivek Prakash * 1049cab1c7SVivek Prakash * Redistribution and use in source and binary forms, with or without 1149cab1c7SVivek Prakash * modification, are permitted provided that the following conditions 1249cab1c7SVivek Prakash * are met: 1349cab1c7SVivek Prakash * 1. Redistributions of source code must retain the above copyright 1449cab1c7SVivek Prakash * notice, this list of conditions and the following disclaimer. 1549cab1c7SVivek Prakash * 2. Redistributions in binary form must reproduce the above copyright 1649cab1c7SVivek Prakash * notice, this list of conditions and the following disclaimer in the 1749cab1c7SVivek Prakash * documentation and/or other materials provided with the distribution. 1849cab1c7SVivek Prakash * 1949cab1c7SVivek Prakash * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2049cab1c7SVivek Prakash * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2149cab1c7SVivek Prakash * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2249cab1c7SVivek Prakash * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2349cab1c7SVivek Prakash * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2449cab1c7SVivek Prakash * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2549cab1c7SVivek Prakash * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2649cab1c7SVivek Prakash * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2749cab1c7SVivek Prakash * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2849cab1c7SVivek Prakash * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2949cab1c7SVivek Prakash * POSSIBILITY OF SUCH DAMAGE. 3049cab1c7SVivek Prakash */ 3149cab1c7SVivek Prakash 3249cab1c7SVivek Prakash #if HAVE_NBTOOL_CONFIG_H 3349cab1c7SVivek Prakash #include "nbtool_config.h" 3449cab1c7SVivek Prakash #endif 3549cab1c7SVivek Prakash 3649cab1c7SVivek Prakash #include <sys/cdefs.h> 3749cab1c7SVivek Prakash #if !defined(lint) 38*474b24f9SLionel Sambuc __RCSID("$NetBSD: stat.c,v 1.36 2011/09/22 20:23:56 apb Exp $"); 3949cab1c7SVivek Prakash #endif 4049cab1c7SVivek Prakash 4149cab1c7SVivek Prakash #if ! HAVE_NBTOOL_CONFIG_H 4249cab1c7SVivek Prakash #define HAVE_STRUCT_STAT_ST_FLAGS 1 4349cab1c7SVivek Prakash #define HAVE_STRUCT_STAT_ST_GEN 1 4449cab1c7SVivek Prakash #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1 4549cab1c7SVivek Prakash #define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1 4649cab1c7SVivek Prakash #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1 4749cab1c7SVivek Prakash #ifdef __minix 4849cab1c7SVivek Prakash /* Not supported in Minix. */ 4949cab1c7SVivek Prakash #define HAVE_DEVNAME 0 5049cab1c7SVivek Prakash #else /* __minix */ 5149cab1c7SVivek Prakash #define HAVE_DEVNAME 1 5249cab1c7SVivek Prakash #endif /* __minx */ 5349cab1c7SVivek Prakash #endif /* HAVE_NBTOOL_CONFIG_H */ 5449cab1c7SVivek Prakash 5549cab1c7SVivek Prakash #include <sys/types.h> 5649cab1c7SVivek Prakash #include <sys/stat.h> 5749cab1c7SVivek Prakash 5849cab1c7SVivek Prakash #include <ctype.h> 5949cab1c7SVivek Prakash #include <err.h> 6049cab1c7SVivek Prakash #include <errno.h> 6149cab1c7SVivek Prakash #include <grp.h> 6249cab1c7SVivek Prakash #include <limits.h> 6349cab1c7SVivek Prakash #include <pwd.h> 6449cab1c7SVivek Prakash #include <stdio.h> 6549cab1c7SVivek Prakash #include <stdlib.h> 6649cab1c7SVivek Prakash #include <string.h> 6749cab1c7SVivek Prakash #include <time.h> 6849cab1c7SVivek Prakash #include <unistd.h> 69*474b24f9SLionel Sambuc #include <vis.h> 7049cab1c7SVivek Prakash 7149cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_FLAGS 7249cab1c7SVivek Prakash #define DEF_F "%#Xf " 7349cab1c7SVivek Prakash #define RAW_F "%f " 7449cab1c7SVivek Prakash #define SHELL_F " st_flags=%f" 7549cab1c7SVivek Prakash #else /* HAVE_STRUCT_STAT_ST_FLAGS */ 7649cab1c7SVivek Prakash #define DEF_F 7749cab1c7SVivek Prakash #define RAW_F 7849cab1c7SVivek Prakash #define SHELL_F 7949cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 8049cab1c7SVivek Prakash 8149cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_BIRTHTIME 8249cab1c7SVivek Prakash #define DEF_B "\"%SB\" " 8349cab1c7SVivek Prakash #define RAW_B "%B " 8449cab1c7SVivek Prakash #define SHELL_B "st_birthtime=%B " 8549cab1c7SVivek Prakash #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 8649cab1c7SVivek Prakash #define DEF_B 8749cab1c7SVivek Prakash #define RAW_B 8849cab1c7SVivek Prakash #define SHELL_B 8949cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 9049cab1c7SVivek Prakash 9149cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_ATIM 9249cab1c7SVivek Prakash #define st_atimespec st_atim 9349cab1c7SVivek Prakash #define st_ctimespec st_ctim 9449cab1c7SVivek Prakash #define st_mtimespec st_mtim 9549cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_ATIM */ 9649cab1c7SVivek Prakash 9749cab1c7SVivek Prakash #define DEF_FORMAT \ 9849cab1c7SVivek Prakash "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \ 9949cab1c7SVivek Prakash "%k %b " DEF_F "%N" 10049cab1c7SVivek Prakash #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \ 10149cab1c7SVivek Prakash "%k %b " RAW_F "%N" 10249cab1c7SVivek Prakash #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY" 10349cab1c7SVivek Prakash #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY" 10449cab1c7SVivek Prakash #define SHELL_FORMAT \ 10549cab1c7SVivek Prakash "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \ 10649cab1c7SVivek Prakash "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \ 10749cab1c7SVivek Prakash "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \ 10849cab1c7SVivek Prakash "st_blksize=%k st_blocks=%b" SHELL_F 10949cab1c7SVivek Prakash #define LINUX_FORMAT \ 11049cab1c7SVivek Prakash " File: \"%N\"%n" \ 11149cab1c7SVivek Prakash " Size: %-11z FileType: %HT%n" \ 11249cab1c7SVivek Prakash " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \ 11349cab1c7SVivek Prakash "Device: %Hd,%Ld Inode: %i Links: %l%n" \ 11449cab1c7SVivek Prakash "Access: %Sa%n" \ 11549cab1c7SVivek Prakash "Modify: %Sm%n" \ 11649cab1c7SVivek Prakash "Change: %Sc" 11749cab1c7SVivek Prakash 11849cab1c7SVivek Prakash #define TIME_FORMAT "%b %e %T %Y" 11949cab1c7SVivek Prakash 12049cab1c7SVivek Prakash #define FLAG_POUND 0x01 12149cab1c7SVivek Prakash #define FLAG_SPACE 0x02 12249cab1c7SVivek Prakash #define FLAG_PLUS 0x04 12349cab1c7SVivek Prakash #define FLAG_ZERO 0x08 12449cab1c7SVivek Prakash #define FLAG_MINUS 0x10 12549cab1c7SVivek Prakash 12649cab1c7SVivek Prakash /* 12749cab1c7SVivek Prakash * These format characters must all be unique, except the magic one. 12849cab1c7SVivek Prakash */ 12949cab1c7SVivek Prakash #define FMT_MAGIC '%' 13049cab1c7SVivek Prakash #define FMT_DOT '.' 13149cab1c7SVivek Prakash 13249cab1c7SVivek Prakash #define SIMPLE_NEWLINE 'n' 13349cab1c7SVivek Prakash #define SIMPLE_TAB 't' 13449cab1c7SVivek Prakash #define SIMPLE_PERCENT '%' 13549cab1c7SVivek Prakash #define SIMPLE_NUMBER '@' 13649cab1c7SVivek Prakash 13749cab1c7SVivek Prakash #define FMT_POUND '#' 13849cab1c7SVivek Prakash #define FMT_SPACE ' ' 13949cab1c7SVivek Prakash #define FMT_PLUS '+' 14049cab1c7SVivek Prakash #define FMT_ZERO '0' 14149cab1c7SVivek Prakash #define FMT_MINUS '-' 14249cab1c7SVivek Prakash 14349cab1c7SVivek Prakash #define FMT_DECIMAL 'D' 14449cab1c7SVivek Prakash #define FMT_OCTAL 'O' 14549cab1c7SVivek Prakash #define FMT_UNSIGNED 'U' 14649cab1c7SVivek Prakash #define FMT_HEX 'X' 14749cab1c7SVivek Prakash #define FMT_FLOAT 'F' 14849cab1c7SVivek Prakash #define FMT_STRING 'S' 14949cab1c7SVivek Prakash 15049cab1c7SVivek Prakash #define FMTF_DECIMAL 0x01 15149cab1c7SVivek Prakash #define FMTF_OCTAL 0x02 15249cab1c7SVivek Prakash #define FMTF_UNSIGNED 0x04 15349cab1c7SVivek Prakash #define FMTF_HEX 0x08 15449cab1c7SVivek Prakash #define FMTF_FLOAT 0x10 15549cab1c7SVivek Prakash #define FMTF_STRING 0x20 15649cab1c7SVivek Prakash 15749cab1c7SVivek Prakash #define HIGH_PIECE 'H' 15849cab1c7SVivek Prakash #define MIDDLE_PIECE 'M' 15949cab1c7SVivek Prakash #define LOW_PIECE 'L' 16049cab1c7SVivek Prakash 16149cab1c7SVivek Prakash #define SHOW_realpath 'R' 16249cab1c7SVivek Prakash #define SHOW_st_dev 'd' 16349cab1c7SVivek Prakash #define SHOW_st_ino 'i' 16449cab1c7SVivek Prakash #define SHOW_st_mode 'p' 16549cab1c7SVivek Prakash #define SHOW_st_nlink 'l' 16649cab1c7SVivek Prakash #define SHOW_st_uid 'u' 16749cab1c7SVivek Prakash #define SHOW_st_gid 'g' 16849cab1c7SVivek Prakash #define SHOW_st_rdev 'r' 16949cab1c7SVivek Prakash #define SHOW_st_atime 'a' 17049cab1c7SVivek Prakash #define SHOW_st_mtime 'm' 17149cab1c7SVivek Prakash #define SHOW_st_ctime 'c' 17249cab1c7SVivek Prakash #define SHOW_st_btime 'B' 17349cab1c7SVivek Prakash #define SHOW_st_size 'z' 17449cab1c7SVivek Prakash #define SHOW_st_blocks 'b' 17549cab1c7SVivek Prakash #define SHOW_st_blksize 'k' 17649cab1c7SVivek Prakash #define SHOW_st_flags 'f' 17749cab1c7SVivek Prakash #define SHOW_st_gen 'v' 17849cab1c7SVivek Prakash #define SHOW_symlink 'Y' 17949cab1c7SVivek Prakash #define SHOW_filetype 'T' 18049cab1c7SVivek Prakash #define SHOW_filename 'N' 18149cab1c7SVivek Prakash #define SHOW_sizerdev 'Z' 18249cab1c7SVivek Prakash 183*474b24f9SLionel Sambuc static void usage(const char *) __dead; 184*474b24f9SLionel Sambuc static void output(const struct stat *, const char *, 185*474b24f9SLionel Sambuc const char *, int, int, int); 186*474b24f9SLionel Sambuc static int format1(const struct stat *, /* stat info */ 18749cab1c7SVivek Prakash const char *, /* the file name */ 18849cab1c7SVivek Prakash const char *, int, /* the format string itself */ 18949cab1c7SVivek Prakash char *, size_t, /* a place to put the output */ 19049cab1c7SVivek Prakash int, int, int, int, /* the parsed format */ 191*474b24f9SLionel Sambuc int, int, int); 19249cab1c7SVivek Prakash 193*474b24f9SLionel Sambuc static const char *timefmt; 194*474b24f9SLionel Sambuc static int linkfail; 19549cab1c7SVivek Prakash 19649cab1c7SVivek Prakash #define addchar(s, c, nl) \ 19749cab1c7SVivek Prakash do { \ 19849cab1c7SVivek Prakash (void)fputc((c), (s)); \ 19949cab1c7SVivek Prakash (*nl) = ((c) == '\n'); \ 20049cab1c7SVivek Prakash } while (0/*CONSTCOND*/) 20149cab1c7SVivek Prakash 20249cab1c7SVivek Prakash int 20349cab1c7SVivek Prakash main(int argc, char *argv[]) 20449cab1c7SVivek Prakash { 20549cab1c7SVivek Prakash struct stat st; 20649cab1c7SVivek Prakash int ch, rc, errs, am_readlink; 20749cab1c7SVivek Prakash int lsF, fmtchar, usestat, fn, nonl, quiet; 20849cab1c7SVivek Prakash const char *statfmt, *options, *synopsis; 20949cab1c7SVivek Prakash 21049cab1c7SVivek Prakash am_readlink = 0; 21149cab1c7SVivek Prakash lsF = 0; 21249cab1c7SVivek Prakash fmtchar = '\0'; 21349cab1c7SVivek Prakash usestat = 0; 21449cab1c7SVivek Prakash nonl = 0; 21549cab1c7SVivek Prakash quiet = 0; 21649cab1c7SVivek Prakash linkfail = 0; 21749cab1c7SVivek Prakash statfmt = NULL; 21849cab1c7SVivek Prakash timefmt = NULL; 21949cab1c7SVivek Prakash 220*474b24f9SLionel Sambuc setprogname(argv[0]); 221*474b24f9SLionel Sambuc 22249cab1c7SVivek Prakash if (strcmp(getprogname(), "readlink") == 0) { 22349cab1c7SVivek Prakash am_readlink = 1; 224*474b24f9SLionel Sambuc options = "fnqsv"; 225*474b24f9SLionel Sambuc synopsis = "[-fnqsv] [file ...]"; 22649cab1c7SVivek Prakash statfmt = "%Y"; 22749cab1c7SVivek Prakash fmtchar = 'f'; 22849cab1c7SVivek Prakash quiet = 1; 22949cab1c7SVivek Prakash } else { 23049cab1c7SVivek Prakash options = "f:FlLnqrst:x"; 23149cab1c7SVivek Prakash synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]"; 23249cab1c7SVivek Prakash } 23349cab1c7SVivek Prakash 23449cab1c7SVivek Prakash while ((ch = getopt(argc, argv, options)) != -1) 23549cab1c7SVivek Prakash switch (ch) { 23649cab1c7SVivek Prakash case 'F': 23749cab1c7SVivek Prakash lsF = 1; 23849cab1c7SVivek Prakash break; 23949cab1c7SVivek Prakash case 'L': 24049cab1c7SVivek Prakash usestat = 1; 24149cab1c7SVivek Prakash break; 24249cab1c7SVivek Prakash case 'n': 24349cab1c7SVivek Prakash nonl = 1; 24449cab1c7SVivek Prakash break; 24549cab1c7SVivek Prakash case 'q': 24649cab1c7SVivek Prakash quiet = 1; 24749cab1c7SVivek Prakash break; 24849cab1c7SVivek Prakash case 'f': 24949cab1c7SVivek Prakash if (am_readlink) { 25049cab1c7SVivek Prakash statfmt = "%R"; 25149cab1c7SVivek Prakash break; 25249cab1c7SVivek Prakash } 25349cab1c7SVivek Prakash statfmt = optarg; 25449cab1c7SVivek Prakash /* FALLTHROUGH */ 25549cab1c7SVivek Prakash case 'l': 25649cab1c7SVivek Prakash case 'r': 25749cab1c7SVivek Prakash case 's': 258*474b24f9SLionel Sambuc if (am_readlink) { 259*474b24f9SLionel Sambuc quiet = 1; 260*474b24f9SLionel Sambuc break; 261*474b24f9SLionel Sambuc } 262*474b24f9SLionel Sambuc /*FALLTHROUGH*/ 26349cab1c7SVivek Prakash case 'x': 26449cab1c7SVivek Prakash if (fmtchar != 0) 26549cab1c7SVivek Prakash errx(1, "can't use format '%c' with '%c'", 26649cab1c7SVivek Prakash fmtchar, ch); 26749cab1c7SVivek Prakash fmtchar = ch; 26849cab1c7SVivek Prakash break; 26949cab1c7SVivek Prakash case 't': 27049cab1c7SVivek Prakash timefmt = optarg; 27149cab1c7SVivek Prakash break; 272*474b24f9SLionel Sambuc case 'v': 273*474b24f9SLionel Sambuc quiet = 0; 274*474b24f9SLionel Sambuc break; 27549cab1c7SVivek Prakash default: 27649cab1c7SVivek Prakash usage(synopsis); 27749cab1c7SVivek Prakash } 27849cab1c7SVivek Prakash 27949cab1c7SVivek Prakash argc -= optind; 28049cab1c7SVivek Prakash argv += optind; 28149cab1c7SVivek Prakash fn = 1; 28249cab1c7SVivek Prakash 28349cab1c7SVivek Prakash if (fmtchar == '\0') { 28449cab1c7SVivek Prakash if (lsF) 28549cab1c7SVivek Prakash fmtchar = 'l'; 28649cab1c7SVivek Prakash else { 28749cab1c7SVivek Prakash fmtchar = 'f'; 28849cab1c7SVivek Prakash statfmt = DEF_FORMAT; 28949cab1c7SVivek Prakash } 29049cab1c7SVivek Prakash } 29149cab1c7SVivek Prakash 29249cab1c7SVivek Prakash if (lsF && fmtchar != 'l') 29349cab1c7SVivek Prakash errx(1, "can't use format '%c' with -F", fmtchar); 29449cab1c7SVivek Prakash 29549cab1c7SVivek Prakash switch (fmtchar) { 29649cab1c7SVivek Prakash case 'f': 29749cab1c7SVivek Prakash /* statfmt already set */ 29849cab1c7SVivek Prakash break; 29949cab1c7SVivek Prakash case 'l': 30049cab1c7SVivek Prakash statfmt = lsF ? LSF_FORMAT : LS_FORMAT; 30149cab1c7SVivek Prakash break; 30249cab1c7SVivek Prakash case 'r': 30349cab1c7SVivek Prakash statfmt = RAW_FORMAT; 30449cab1c7SVivek Prakash break; 30549cab1c7SVivek Prakash case 's': 30649cab1c7SVivek Prakash statfmt = SHELL_FORMAT; 30749cab1c7SVivek Prakash break; 30849cab1c7SVivek Prakash case 'x': 30949cab1c7SVivek Prakash statfmt = LINUX_FORMAT; 31049cab1c7SVivek Prakash if (timefmt == NULL) 31149cab1c7SVivek Prakash timefmt = "%c"; 31249cab1c7SVivek Prakash break; 31349cab1c7SVivek Prakash default: 31449cab1c7SVivek Prakash usage(synopsis); 31549cab1c7SVivek Prakash /*NOTREACHED*/ 31649cab1c7SVivek Prakash } 31749cab1c7SVivek Prakash 31849cab1c7SVivek Prakash if (timefmt == NULL) 31949cab1c7SVivek Prakash timefmt = TIME_FORMAT; 32049cab1c7SVivek Prakash 32149cab1c7SVivek Prakash errs = 0; 32249cab1c7SVivek Prakash do { 32349cab1c7SVivek Prakash if (argc == 0) 32449cab1c7SVivek Prakash rc = fstat(STDIN_FILENO, &st); 32549cab1c7SVivek Prakash else if (usestat) { 32649cab1c7SVivek Prakash /* 32749cab1c7SVivek Prakash * Try stat() and if it fails, fall back to 32849cab1c7SVivek Prakash * lstat() just in case we're examining a 32949cab1c7SVivek Prakash * broken symlink. 33049cab1c7SVivek Prakash */ 33149cab1c7SVivek Prakash if ((rc = stat(argv[0], &st)) == -1 && 33249cab1c7SVivek Prakash errno == ENOENT && 33349cab1c7SVivek Prakash (rc = lstat(argv[0], &st)) == -1) 33449cab1c7SVivek Prakash errno = ENOENT; 33549cab1c7SVivek Prakash } 33649cab1c7SVivek Prakash else 33749cab1c7SVivek Prakash rc = lstat(argv[0], &st); 33849cab1c7SVivek Prakash 33949cab1c7SVivek Prakash if (rc == -1) { 34049cab1c7SVivek Prakash errs = 1; 34149cab1c7SVivek Prakash linkfail = 1; 34249cab1c7SVivek Prakash if (!quiet) 34349cab1c7SVivek Prakash warn("%s: %s", 34449cab1c7SVivek Prakash argc == 0 ? "(stdin)" : argv[0], 34549cab1c7SVivek Prakash usestat ? "stat" : "lstat"); 34649cab1c7SVivek Prakash } 34749cab1c7SVivek Prakash else 348*474b24f9SLionel Sambuc output(&st, argv[0], statfmt, fn, nonl, quiet); 34949cab1c7SVivek Prakash 35049cab1c7SVivek Prakash argv++; 35149cab1c7SVivek Prakash argc--; 35249cab1c7SVivek Prakash fn++; 35349cab1c7SVivek Prakash } while (argc > 0); 35449cab1c7SVivek Prakash 35549cab1c7SVivek Prakash return (am_readlink ? linkfail : errs); 35649cab1c7SVivek Prakash } 35749cab1c7SVivek Prakash 358*474b24f9SLionel Sambuc static void 35949cab1c7SVivek Prakash usage(const char *synopsis) 36049cab1c7SVivek Prakash { 36149cab1c7SVivek Prakash 36249cab1c7SVivek Prakash (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis); 36349cab1c7SVivek Prakash exit(1); 36449cab1c7SVivek Prakash } 36549cab1c7SVivek Prakash 36649cab1c7SVivek Prakash /* 36749cab1c7SVivek Prakash * Parses a format string. 36849cab1c7SVivek Prakash */ 369*474b24f9SLionel Sambuc static void 37049cab1c7SVivek Prakash output(const struct stat *st, const char *file, 371*474b24f9SLionel Sambuc const char *statfmt, int fn, int nonl, int quiet) 37249cab1c7SVivek Prakash { 37349cab1c7SVivek Prakash int flags, size, prec, ofmt, hilo, what; 374*474b24f9SLionel Sambuc /* 375*474b24f9SLionel Sambuc * buf size is enough for an item of length PATH_MAX, 376*474b24f9SLionel Sambuc * multiplied by 4 for vis encoding, plus 4 for symlink 377*474b24f9SLionel Sambuc * " -> " prefix, plus 1 for \0 terminator. 378*474b24f9SLionel Sambuc */ 379*474b24f9SLionel Sambuc char buf[PATH_MAX * 4 + 4 + 1]; 38049cab1c7SVivek Prakash const char *subfmt; 38149cab1c7SVivek Prakash int nl, t, i; 38249cab1c7SVivek Prakash 38349cab1c7SVivek Prakash nl = 1; 38449cab1c7SVivek Prakash while (*statfmt != '\0') { 38549cab1c7SVivek Prakash 38649cab1c7SVivek Prakash /* 38749cab1c7SVivek Prakash * Non-format characters go straight out. 38849cab1c7SVivek Prakash */ 38949cab1c7SVivek Prakash if (*statfmt != FMT_MAGIC) { 39049cab1c7SVivek Prakash addchar(stdout, *statfmt, &nl); 39149cab1c7SVivek Prakash statfmt++; 39249cab1c7SVivek Prakash continue; 39349cab1c7SVivek Prakash } 39449cab1c7SVivek Prakash 39549cab1c7SVivek Prakash /* 39649cab1c7SVivek Prakash * The current format "substring" starts here, 39749cab1c7SVivek Prakash * and then we skip the magic. 39849cab1c7SVivek Prakash */ 39949cab1c7SVivek Prakash subfmt = statfmt; 40049cab1c7SVivek Prakash statfmt++; 40149cab1c7SVivek Prakash 40249cab1c7SVivek Prakash /* 40349cab1c7SVivek Prakash * Some simple one-character "formats". 40449cab1c7SVivek Prakash */ 40549cab1c7SVivek Prakash switch (*statfmt) { 40649cab1c7SVivek Prakash case SIMPLE_NEWLINE: 40749cab1c7SVivek Prakash addchar(stdout, '\n', &nl); 40849cab1c7SVivek Prakash statfmt++; 40949cab1c7SVivek Prakash continue; 41049cab1c7SVivek Prakash case SIMPLE_TAB: 41149cab1c7SVivek Prakash addchar(stdout, '\t', &nl); 41249cab1c7SVivek Prakash statfmt++; 41349cab1c7SVivek Prakash continue; 41449cab1c7SVivek Prakash case SIMPLE_PERCENT: 41549cab1c7SVivek Prakash addchar(stdout, '%', &nl); 41649cab1c7SVivek Prakash statfmt++; 41749cab1c7SVivek Prakash continue; 41849cab1c7SVivek Prakash case SIMPLE_NUMBER: { 41949cab1c7SVivek Prakash char num[12], *p; 42049cab1c7SVivek Prakash 42149cab1c7SVivek Prakash snprintf(num, sizeof(num), "%d", fn); 42249cab1c7SVivek Prakash for (p = &num[0]; *p; p++) 42349cab1c7SVivek Prakash addchar(stdout, *p, &nl); 42449cab1c7SVivek Prakash statfmt++; 42549cab1c7SVivek Prakash continue; 42649cab1c7SVivek Prakash } 42749cab1c7SVivek Prakash } 42849cab1c7SVivek Prakash 42949cab1c7SVivek Prakash /* 43049cab1c7SVivek Prakash * This must be an actual format string. Format strings are 43149cab1c7SVivek Prakash * similar to printf(3) formats up to a point, and are of 43249cab1c7SVivek Prakash * the form: 43349cab1c7SVivek Prakash * 43449cab1c7SVivek Prakash * % required start of format 43549cab1c7SVivek Prakash * [-# +0] opt. format characters 43649cab1c7SVivek Prakash * size opt. field width 43749cab1c7SVivek Prakash * . opt. decimal separator, followed by 43849cab1c7SVivek Prakash * prec opt. precision 43949cab1c7SVivek Prakash * fmt opt. output specifier (string, numeric, etc.) 44049cab1c7SVivek Prakash * sub opt. sub field specifier (high, middle, low) 44149cab1c7SVivek Prakash * datum required field specifier (size, mode, etc) 44249cab1c7SVivek Prakash * 44349cab1c7SVivek Prakash * Only the % and the datum selector are required. All data 44449cab1c7SVivek Prakash * have reasonable default output forms. The "sub" specifier 44549cab1c7SVivek Prakash * only applies to certain data (mode, dev, rdev, filetype). 44649cab1c7SVivek Prakash * The symlink output defaults to STRING, yet will only emit 44749cab1c7SVivek Prakash * the leading " -> " if STRING is explicitly specified. The 44849cab1c7SVivek Prakash * sizerdev datum will generate rdev output for character or 44949cab1c7SVivek Prakash * block devices, and size output for all others. 450*474b24f9SLionel Sambuc * For STRING output, the # format requests vis encoding. 45149cab1c7SVivek Prakash */ 45249cab1c7SVivek Prakash flags = 0; 45349cab1c7SVivek Prakash do { 45449cab1c7SVivek Prakash if (*statfmt == FMT_POUND) 45549cab1c7SVivek Prakash flags |= FLAG_POUND; 45649cab1c7SVivek Prakash else if (*statfmt == FMT_SPACE) 45749cab1c7SVivek Prakash flags |= FLAG_SPACE; 45849cab1c7SVivek Prakash else if (*statfmt == FMT_PLUS) 45949cab1c7SVivek Prakash flags |= FLAG_PLUS; 46049cab1c7SVivek Prakash else if (*statfmt == FMT_ZERO) 46149cab1c7SVivek Prakash flags |= FLAG_ZERO; 46249cab1c7SVivek Prakash else if (*statfmt == FMT_MINUS) 46349cab1c7SVivek Prakash flags |= FLAG_MINUS; 46449cab1c7SVivek Prakash else 46549cab1c7SVivek Prakash break; 46649cab1c7SVivek Prakash statfmt++; 46749cab1c7SVivek Prakash } while (1/*CONSTCOND*/); 46849cab1c7SVivek Prakash 46949cab1c7SVivek Prakash size = -1; 47049cab1c7SVivek Prakash if (isdigit((unsigned)*statfmt)) { 47149cab1c7SVivek Prakash size = 0; 47249cab1c7SVivek Prakash while (isdigit((unsigned)*statfmt)) { 47349cab1c7SVivek Prakash size = (size * 10) + (*statfmt - '0'); 47449cab1c7SVivek Prakash statfmt++; 47549cab1c7SVivek Prakash if (size < 0) 47649cab1c7SVivek Prakash goto badfmt; 47749cab1c7SVivek Prakash } 47849cab1c7SVivek Prakash } 47949cab1c7SVivek Prakash 48049cab1c7SVivek Prakash prec = -1; 48149cab1c7SVivek Prakash if (*statfmt == FMT_DOT) { 48249cab1c7SVivek Prakash statfmt++; 48349cab1c7SVivek Prakash 48449cab1c7SVivek Prakash prec = 0; 48549cab1c7SVivek Prakash while (isdigit((unsigned)*statfmt)) { 48649cab1c7SVivek Prakash prec = (prec * 10) + (*statfmt - '0'); 48749cab1c7SVivek Prakash statfmt++; 48849cab1c7SVivek Prakash if (prec < 0) 48949cab1c7SVivek Prakash goto badfmt; 49049cab1c7SVivek Prakash } 49149cab1c7SVivek Prakash } 49249cab1c7SVivek Prakash 49349cab1c7SVivek Prakash #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break 49449cab1c7SVivek Prakash #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break 49549cab1c7SVivek Prakash switch (*statfmt) { 49649cab1c7SVivek Prakash fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL); 49749cab1c7SVivek Prakash fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL); 49849cab1c7SVivek Prakash fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED); 49949cab1c7SVivek Prakash fmtcasef(ofmt, FMT_HEX, FMTF_HEX); 50049cab1c7SVivek Prakash fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT); 50149cab1c7SVivek Prakash fmtcasef(ofmt, FMT_STRING, FMTF_STRING); 50249cab1c7SVivek Prakash default: 50349cab1c7SVivek Prakash ofmt = 0; 50449cab1c7SVivek Prakash break; 50549cab1c7SVivek Prakash } 50649cab1c7SVivek Prakash 50749cab1c7SVivek Prakash switch (*statfmt) { 50849cab1c7SVivek Prakash fmtcase(hilo, HIGH_PIECE); 50949cab1c7SVivek Prakash fmtcase(hilo, MIDDLE_PIECE); 51049cab1c7SVivek Prakash fmtcase(hilo, LOW_PIECE); 51149cab1c7SVivek Prakash default: 51249cab1c7SVivek Prakash hilo = 0; 51349cab1c7SVivek Prakash break; 51449cab1c7SVivek Prakash } 51549cab1c7SVivek Prakash 51649cab1c7SVivek Prakash switch (*statfmt) { 51749cab1c7SVivek Prakash fmtcase(what, SHOW_realpath); 51849cab1c7SVivek Prakash fmtcase(what, SHOW_st_dev); 51949cab1c7SVivek Prakash fmtcase(what, SHOW_st_ino); 52049cab1c7SVivek Prakash fmtcase(what, SHOW_st_mode); 52149cab1c7SVivek Prakash fmtcase(what, SHOW_st_nlink); 52249cab1c7SVivek Prakash fmtcase(what, SHOW_st_uid); 52349cab1c7SVivek Prakash fmtcase(what, SHOW_st_gid); 52449cab1c7SVivek Prakash fmtcase(what, SHOW_st_rdev); 52549cab1c7SVivek Prakash fmtcase(what, SHOW_st_atime); 52649cab1c7SVivek Prakash fmtcase(what, SHOW_st_mtime); 52749cab1c7SVivek Prakash fmtcase(what, SHOW_st_ctime); 52849cab1c7SVivek Prakash fmtcase(what, SHOW_st_btime); 52949cab1c7SVivek Prakash fmtcase(what, SHOW_st_size); 53049cab1c7SVivek Prakash fmtcase(what, SHOW_st_blocks); 53149cab1c7SVivek Prakash fmtcase(what, SHOW_st_blksize); 53249cab1c7SVivek Prakash fmtcase(what, SHOW_st_flags); 53349cab1c7SVivek Prakash fmtcase(what, SHOW_st_gen); 53449cab1c7SVivek Prakash fmtcase(what, SHOW_symlink); 53549cab1c7SVivek Prakash fmtcase(what, SHOW_filetype); 53649cab1c7SVivek Prakash fmtcase(what, SHOW_filename); 53749cab1c7SVivek Prakash fmtcase(what, SHOW_sizerdev); 53849cab1c7SVivek Prakash default: 53949cab1c7SVivek Prakash goto badfmt; 54049cab1c7SVivek Prakash } 54149cab1c7SVivek Prakash #undef fmtcasef 54249cab1c7SVivek Prakash #undef fmtcase 54349cab1c7SVivek Prakash 54449cab1c7SVivek Prakash t = format1(st, 54549cab1c7SVivek Prakash file, 54649cab1c7SVivek Prakash subfmt, statfmt - subfmt, 54749cab1c7SVivek Prakash buf, sizeof(buf), 548*474b24f9SLionel Sambuc flags, size, prec, ofmt, hilo, what, quiet); 54949cab1c7SVivek Prakash 55049cab1c7SVivek Prakash for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++) 55149cab1c7SVivek Prakash addchar(stdout, buf[i], &nl); 55249cab1c7SVivek Prakash 55349cab1c7SVivek Prakash continue; 55449cab1c7SVivek Prakash 55549cab1c7SVivek Prakash badfmt: 55649cab1c7SVivek Prakash errx(1, "%.*s: bad format", 55749cab1c7SVivek Prakash (int)(statfmt - subfmt + 1), subfmt); 55849cab1c7SVivek Prakash } 55949cab1c7SVivek Prakash 56049cab1c7SVivek Prakash if (!nl && !nonl) 56149cab1c7SVivek Prakash (void)fputc('\n', stdout); 56249cab1c7SVivek Prakash (void)fflush(stdout); 56349cab1c7SVivek Prakash } 56449cab1c7SVivek Prakash 56549cab1c7SVivek Prakash /* 56649cab1c7SVivek Prakash * Arranges output according to a single parsed format substring. 56749cab1c7SVivek Prakash */ 568*474b24f9SLionel Sambuc static int 56949cab1c7SVivek Prakash format1(const struct stat *st, 57049cab1c7SVivek Prakash const char *file, 57149cab1c7SVivek Prakash const char *fmt, int flen, 57249cab1c7SVivek Prakash char *buf, size_t blen, 57349cab1c7SVivek Prakash int flags, int size, int prec, int ofmt, 574*474b24f9SLionel Sambuc int hilo, int what, int quiet) 57549cab1c7SVivek Prakash { 57649cab1c7SVivek Prakash u_int64_t data; 57749cab1c7SVivek Prakash char *stmp, lfmt[24], tmp[20]; 57849cab1c7SVivek Prakash const char *sdata; 579*474b24f9SLionel Sambuc char smode[12], sid[12], path[PATH_MAX + 4], visbuf[PATH_MAX * 4 + 4]; 58049cab1c7SVivek Prakash struct passwd *pw; 58149cab1c7SVivek Prakash struct group *gr; 58249cab1c7SVivek Prakash struct tm *tm; 58349cab1c7SVivek Prakash time_t secs; 58449cab1c7SVivek Prakash long nsecs; 585*474b24f9SLionel Sambuc int l; 586*474b24f9SLionel Sambuc int formats; /* bitmap of allowed formats for this datum */ 587*474b24f9SLionel Sambuc int small; /* true if datum is a small integer */ 588*474b24f9SLionel Sambuc int gottime; /* true if secs and nsecs are valid */ 589*474b24f9SLionel Sambuc int shift; /* powers of 2 to scale numbers before printing */ 590*474b24f9SLionel Sambuc size_t prefixlen; /* length of constant prefix for string data */ 59149cab1c7SVivek Prakash 59249cab1c7SVivek Prakash formats = 0; 59349cab1c7SVivek Prakash small = 0; 59449cab1c7SVivek Prakash gottime = 0; 59549cab1c7SVivek Prakash secs = 0; 59649cab1c7SVivek Prakash nsecs = 0; 59749cab1c7SVivek Prakash shift = 0; 598*474b24f9SLionel Sambuc prefixlen = 0; 59949cab1c7SVivek Prakash 60049cab1c7SVivek Prakash /* 60149cab1c7SVivek Prakash * First, pick out the data and tweak it based on hilo or 60249cab1c7SVivek Prakash * specified output format (symlink output only). 60349cab1c7SVivek Prakash */ 60449cab1c7SVivek Prakash switch (what) { 60549cab1c7SVivek Prakash case SHOW_st_dev: 60649cab1c7SVivek Prakash case SHOW_st_rdev: 60749cab1c7SVivek Prakash small = (sizeof(st->st_dev) == 4); 60849cab1c7SVivek Prakash data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev; 60949cab1c7SVivek Prakash #if HAVE_DEVNAME 61049cab1c7SVivek Prakash sdata = (what == SHOW_st_dev) ? 61149cab1c7SVivek Prakash devname(st->st_dev, S_IFBLK) : 61249cab1c7SVivek Prakash devname(st->st_rdev, 61349cab1c7SVivek Prakash S_ISCHR(st->st_mode) ? S_IFCHR : 61449cab1c7SVivek Prakash S_ISBLK(st->st_mode) ? S_IFBLK : 61549cab1c7SVivek Prakash 0U); 61649cab1c7SVivek Prakash if (sdata == NULL) 61749cab1c7SVivek Prakash sdata = "???"; 61849cab1c7SVivek Prakash #endif /* HAVE_DEVNAME */ 61949cab1c7SVivek Prakash if (hilo == HIGH_PIECE) { 62049cab1c7SVivek Prakash data = major(data); 62149cab1c7SVivek Prakash hilo = 0; 62249cab1c7SVivek Prakash } 62349cab1c7SVivek Prakash else if (hilo == LOW_PIECE) { 62449cab1c7SVivek Prakash data = minor((unsigned)data); 62549cab1c7SVivek Prakash hilo = 0; 62649cab1c7SVivek Prakash } 62749cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 62849cab1c7SVivek Prakash #if HAVE_DEVNAME 62949cab1c7SVivek Prakash FMTF_STRING; 63049cab1c7SVivek Prakash #else /* HAVE_DEVNAME */ 63149cab1c7SVivek Prakash 0; 63249cab1c7SVivek Prakash #endif /* HAVE_DEVNAME */ 63349cab1c7SVivek Prakash if (ofmt == 0) 63449cab1c7SVivek Prakash ofmt = FMTF_UNSIGNED; 63549cab1c7SVivek Prakash break; 63649cab1c7SVivek Prakash case SHOW_st_ino: 63749cab1c7SVivek Prakash small = (sizeof(st->st_ino) == 4); 63849cab1c7SVivek Prakash data = st->st_ino; 63949cab1c7SVivek Prakash sdata = NULL; 64049cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 64149cab1c7SVivek Prakash if (ofmt == 0) 64249cab1c7SVivek Prakash ofmt = FMTF_UNSIGNED; 64349cab1c7SVivek Prakash break; 64449cab1c7SVivek Prakash case SHOW_st_mode: 64549cab1c7SVivek Prakash small = (sizeof(st->st_mode) == 4); 64649cab1c7SVivek Prakash data = st->st_mode; 64749cab1c7SVivek Prakash strmode(st->st_mode, smode); 64849cab1c7SVivek Prakash stmp = smode; 64949cab1c7SVivek Prakash l = strlen(stmp); 65049cab1c7SVivek Prakash if (stmp[l - 1] == ' ') 65149cab1c7SVivek Prakash stmp[--l] = '\0'; 65249cab1c7SVivek Prakash if (hilo == HIGH_PIECE) { 65349cab1c7SVivek Prakash data >>= 12; 65449cab1c7SVivek Prakash stmp += 1; 65549cab1c7SVivek Prakash stmp[3] = '\0'; 65649cab1c7SVivek Prakash hilo = 0; 65749cab1c7SVivek Prakash } 65849cab1c7SVivek Prakash else if (hilo == MIDDLE_PIECE) { 65949cab1c7SVivek Prakash data = (data >> 9) & 07; 66049cab1c7SVivek Prakash stmp += 4; 66149cab1c7SVivek Prakash stmp[3] = '\0'; 66249cab1c7SVivek Prakash hilo = 0; 66349cab1c7SVivek Prakash } 66449cab1c7SVivek Prakash else if (hilo == LOW_PIECE) { 66549cab1c7SVivek Prakash data &= 0777; 66649cab1c7SVivek Prakash stmp += 7; 66749cab1c7SVivek Prakash stmp[3] = '\0'; 66849cab1c7SVivek Prakash hilo = 0; 66949cab1c7SVivek Prakash } 67049cab1c7SVivek Prakash sdata = stmp; 67149cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 67249cab1c7SVivek Prakash FMTF_STRING; 67349cab1c7SVivek Prakash if (ofmt == 0) 67449cab1c7SVivek Prakash ofmt = FMTF_OCTAL; 67549cab1c7SVivek Prakash break; 67649cab1c7SVivek Prakash case SHOW_st_nlink: 67749cab1c7SVivek Prakash small = (sizeof(st->st_dev) == 4); 67849cab1c7SVivek Prakash data = st->st_nlink; 67949cab1c7SVivek Prakash sdata = NULL; 68049cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 68149cab1c7SVivek Prakash if (ofmt == 0) 68249cab1c7SVivek Prakash ofmt = FMTF_UNSIGNED; 68349cab1c7SVivek Prakash break; 68449cab1c7SVivek Prakash case SHOW_st_uid: 68549cab1c7SVivek Prakash small = (sizeof(st->st_uid) == 4); 68649cab1c7SVivek Prakash data = st->st_uid; 68749cab1c7SVivek Prakash if ((pw = getpwuid(st->st_uid)) != NULL) 68849cab1c7SVivek Prakash sdata = pw->pw_name; 68949cab1c7SVivek Prakash else { 69049cab1c7SVivek Prakash snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid); 69149cab1c7SVivek Prakash sdata = sid; 69249cab1c7SVivek Prakash } 69349cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 69449cab1c7SVivek Prakash FMTF_STRING; 69549cab1c7SVivek Prakash if (ofmt == 0) 69649cab1c7SVivek Prakash ofmt = FMTF_UNSIGNED; 69749cab1c7SVivek Prakash break; 69849cab1c7SVivek Prakash case SHOW_st_gid: 69949cab1c7SVivek Prakash small = (sizeof(st->st_gid) == 4); 70049cab1c7SVivek Prakash data = st->st_gid; 70149cab1c7SVivek Prakash if ((gr = getgrgid(st->st_gid)) != NULL) 70249cab1c7SVivek Prakash sdata = gr->gr_name; 70349cab1c7SVivek Prakash else { 70449cab1c7SVivek Prakash snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid); 70549cab1c7SVivek Prakash sdata = sid; 70649cab1c7SVivek Prakash } 70749cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 70849cab1c7SVivek Prakash FMTF_STRING; 70949cab1c7SVivek Prakash if (ofmt == 0) 71049cab1c7SVivek Prakash ofmt = FMTF_UNSIGNED; 71149cab1c7SVivek Prakash break; 71249cab1c7SVivek Prakash case SHOW_st_atime: 71349cab1c7SVivek Prakash gottime = 1; 71449cab1c7SVivek Prakash secs = st->st_atime; 71549cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_MTIMENSEC 71649cab1c7SVivek Prakash nsecs = st->st_atimensec; 71749cab1c7SVivek Prakash #endif 71849cab1c7SVivek Prakash /* FALLTHROUGH */ 71949cab1c7SVivek Prakash case SHOW_st_mtime: 72049cab1c7SVivek Prakash if (!gottime) { 72149cab1c7SVivek Prakash gottime = 1; 72249cab1c7SVivek Prakash secs = st->st_mtime; 72349cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_MTIMENSEC 72449cab1c7SVivek Prakash nsecs = st->st_mtimensec; 72549cab1c7SVivek Prakash #endif 72649cab1c7SVivek Prakash } 72749cab1c7SVivek Prakash /* FALLTHROUGH */ 72849cab1c7SVivek Prakash case SHOW_st_ctime: 72949cab1c7SVivek Prakash if (!gottime) { 73049cab1c7SVivek Prakash gottime = 1; 73149cab1c7SVivek Prakash secs = st->st_ctime; 73249cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_MTIMENSEC 73349cab1c7SVivek Prakash nsecs = st->st_ctimensec; 73449cab1c7SVivek Prakash #endif 73549cab1c7SVivek Prakash } 73649cab1c7SVivek Prakash /* FALLTHROUGH */ 73749cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_BIRTHTIME 73849cab1c7SVivek Prakash case SHOW_st_btime: 73949cab1c7SVivek Prakash if (!gottime) { 74049cab1c7SVivek Prakash gottime = 1; 74149cab1c7SVivek Prakash secs = st->st_birthtime; 74249cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 74349cab1c7SVivek Prakash nsecs = st->st_birthtimensec; 74449cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */ 74549cab1c7SVivek Prakash } 74649cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 74749cab1c7SVivek Prakash small = (sizeof(secs) == 4); 74849cab1c7SVivek Prakash data = secs; 74949cab1c7SVivek Prakash tm = localtime(&secs); 75049cab1c7SVivek Prakash if (tm == NULL) { 75149cab1c7SVivek Prakash secs = 0; 75249cab1c7SVivek Prakash tm = localtime(&secs); 75349cab1c7SVivek Prakash } 75449cab1c7SVivek Prakash (void)strftime(path, sizeof(path), timefmt, tm); 75549cab1c7SVivek Prakash sdata = path; 75649cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 75749cab1c7SVivek Prakash FMTF_FLOAT | FMTF_STRING; 75849cab1c7SVivek Prakash if (ofmt == 0) 75949cab1c7SVivek Prakash ofmt = FMTF_DECIMAL; 76049cab1c7SVivek Prakash break; 76149cab1c7SVivek Prakash case SHOW_st_size: 76249cab1c7SVivek Prakash small = (sizeof(st->st_size) == 4); 76349cab1c7SVivek Prakash data = st->st_size; 76449cab1c7SVivek Prakash sdata = NULL; 76549cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 76649cab1c7SVivek Prakash if (ofmt == 0) 76749cab1c7SVivek Prakash ofmt = FMTF_UNSIGNED; 76849cab1c7SVivek Prakash switch (hilo) { 76949cab1c7SVivek Prakash case HIGH_PIECE: 77049cab1c7SVivek Prakash shift = 30; /* gigabytes */ 77149cab1c7SVivek Prakash hilo = 0; 77249cab1c7SVivek Prakash break; 77349cab1c7SVivek Prakash case MIDDLE_PIECE: 77449cab1c7SVivek Prakash shift = 20; /* megabytes */ 77549cab1c7SVivek Prakash hilo = 0; 77649cab1c7SVivek Prakash break; 77749cab1c7SVivek Prakash case LOW_PIECE: 77849cab1c7SVivek Prakash shift = 10; /* kilobytes */ 77949cab1c7SVivek Prakash hilo = 0; 78049cab1c7SVivek Prakash break; 78149cab1c7SVivek Prakash } 78249cab1c7SVivek Prakash break; 78349cab1c7SVivek Prakash case SHOW_st_blocks: 78449cab1c7SVivek Prakash small = (sizeof(st->st_blocks) == 4); 78549cab1c7SVivek Prakash data = st->st_blocks; 78649cab1c7SVivek Prakash sdata = NULL; 78749cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 78849cab1c7SVivek Prakash if (ofmt == 0) 78949cab1c7SVivek Prakash ofmt = FMTF_UNSIGNED; 79049cab1c7SVivek Prakash break; 79149cab1c7SVivek Prakash case SHOW_st_blksize: 79249cab1c7SVivek Prakash small = (sizeof(st->st_blksize) == 4); 79349cab1c7SVivek Prakash data = st->st_blksize; 79449cab1c7SVivek Prakash sdata = NULL; 79549cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 79649cab1c7SVivek Prakash if (ofmt == 0) 79749cab1c7SVivek Prakash ofmt = FMTF_UNSIGNED; 79849cab1c7SVivek Prakash break; 79949cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_FLAGS 80049cab1c7SVivek Prakash case SHOW_st_flags: 80149cab1c7SVivek Prakash small = (sizeof(st->st_flags) == 4); 80249cab1c7SVivek Prakash data = st->st_flags; 80349cab1c7SVivek Prakash sdata = NULL; 80449cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 80549cab1c7SVivek Prakash if (ofmt == 0) 80649cab1c7SVivek Prakash ofmt = FMTF_UNSIGNED; 80749cab1c7SVivek Prakash break; 80849cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 80949cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_GEN 81049cab1c7SVivek Prakash case SHOW_st_gen: 81149cab1c7SVivek Prakash small = (sizeof(st->st_gen) == 4); 81249cab1c7SVivek Prakash data = st->st_gen; 81349cab1c7SVivek Prakash sdata = NULL; 81449cab1c7SVivek Prakash formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 81549cab1c7SVivek Prakash if (ofmt == 0) 81649cab1c7SVivek Prakash ofmt = FMTF_UNSIGNED; 81749cab1c7SVivek Prakash break; 81849cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_GEN */ 81949cab1c7SVivek Prakash case SHOW_realpath: 82049cab1c7SVivek Prakash small = 0; 82149cab1c7SVivek Prakash data = 0; 82249cab1c7SVivek Prakash if (file == NULL) { 82349cab1c7SVivek Prakash (void)strlcpy(path, "(stdin)", sizeof(path)); 82449cab1c7SVivek Prakash sdata = path; 82549cab1c7SVivek Prakash } else { 82649cab1c7SVivek Prakash snprintf(path, sizeof(path), " -> "); 82749cab1c7SVivek Prakash if (realpath(file, path + 4) == NULL) { 828*474b24f9SLionel Sambuc if (!quiet) 829*474b24f9SLionel Sambuc warn("realpath `%s'", file); 83049cab1c7SVivek Prakash linkfail = 1; 83149cab1c7SVivek Prakash l = 0; 83249cab1c7SVivek Prakash path[0] = '\0'; 83349cab1c7SVivek Prakash } 83449cab1c7SVivek Prakash sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 835*474b24f9SLionel Sambuc prefixlen = (ofmt == FMTF_STRING ? 4 : 0); 83649cab1c7SVivek Prakash } 83749cab1c7SVivek Prakash 83849cab1c7SVivek Prakash formats = FMTF_STRING; 83949cab1c7SVivek Prakash if (ofmt == 0) 84049cab1c7SVivek Prakash ofmt = FMTF_STRING; 84149cab1c7SVivek Prakash break; 84249cab1c7SVivek Prakash case SHOW_symlink: 84349cab1c7SVivek Prakash small = 0; 84449cab1c7SVivek Prakash data = 0; 84549cab1c7SVivek Prakash if (S_ISLNK(st->st_mode)) { 84649cab1c7SVivek Prakash snprintf(path, sizeof(path), " -> "); 84749cab1c7SVivek Prakash l = readlink(file, path + 4, sizeof(path) - 4 - 1); 84849cab1c7SVivek Prakash if (l == -1) { 849*474b24f9SLionel Sambuc if (!quiet) 850*474b24f9SLionel Sambuc warn("readlink `%s'", file); 85149cab1c7SVivek Prakash linkfail = 1; 85249cab1c7SVivek Prakash l = 0; 85349cab1c7SVivek Prakash path[0] = '\0'; 85449cab1c7SVivek Prakash } 85549cab1c7SVivek Prakash path[l + 4] = '\0'; 85649cab1c7SVivek Prakash sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 857*474b24f9SLionel Sambuc prefixlen = (ofmt == FMTF_STRING ? 4 : 0); 85849cab1c7SVivek Prakash } 85949cab1c7SVivek Prakash else { 86049cab1c7SVivek Prakash linkfail = 1; 86149cab1c7SVivek Prakash sdata = ""; 86249cab1c7SVivek Prakash } 86349cab1c7SVivek Prakash formats = FMTF_STRING; 86449cab1c7SVivek Prakash if (ofmt == 0) 86549cab1c7SVivek Prakash ofmt = FMTF_STRING; 86649cab1c7SVivek Prakash break; 86749cab1c7SVivek Prakash case SHOW_filetype: 86849cab1c7SVivek Prakash small = 0; 86949cab1c7SVivek Prakash data = 0; 87049cab1c7SVivek Prakash sdata = ""; 87149cab1c7SVivek Prakash if (hilo == 0 || hilo == LOW_PIECE) { 87249cab1c7SVivek Prakash switch (st->st_mode & S_IFMT) { 87349cab1c7SVivek Prakash case S_IFIFO: sdata = "|"; break; 87449cab1c7SVivek Prakash case S_IFDIR: sdata = "/"; break; 87549cab1c7SVivek Prakash case S_IFREG: 87649cab1c7SVivek Prakash if (st->st_mode & 87749cab1c7SVivek Prakash (S_IXUSR | S_IXGRP | S_IXOTH)) 87849cab1c7SVivek Prakash sdata = "*"; 87949cab1c7SVivek Prakash break; 88049cab1c7SVivek Prakash case S_IFLNK: sdata = "@"; break; 88149cab1c7SVivek Prakash #ifdef S_IFSOCK 88249cab1c7SVivek Prakash case S_IFSOCK: sdata = "="; break; 88349cab1c7SVivek Prakash #endif 88449cab1c7SVivek Prakash #ifdef S_IFWHT 88549cab1c7SVivek Prakash case S_IFWHT: sdata = "%"; break; 88649cab1c7SVivek Prakash #endif /* S_IFWHT */ 88749cab1c7SVivek Prakash #ifdef S_IFDOOR 88849cab1c7SVivek Prakash case S_IFDOOR: sdata = ">"; break; 88949cab1c7SVivek Prakash #endif /* S_IFDOOR */ 89049cab1c7SVivek Prakash } 89149cab1c7SVivek Prakash hilo = 0; 89249cab1c7SVivek Prakash } 89349cab1c7SVivek Prakash else if (hilo == HIGH_PIECE) { 89449cab1c7SVivek Prakash switch (st->st_mode & S_IFMT) { 89549cab1c7SVivek Prakash case S_IFIFO: sdata = "Fifo File"; break; 89649cab1c7SVivek Prakash case S_IFCHR: sdata = "Character Device"; break; 89749cab1c7SVivek Prakash case S_IFDIR: sdata = "Directory"; break; 89849cab1c7SVivek Prakash case S_IFBLK: sdata = "Block Device"; break; 89949cab1c7SVivek Prakash case S_IFREG: sdata = "Regular File"; break; 90049cab1c7SVivek Prakash case S_IFLNK: sdata = "Symbolic Link"; break; 90149cab1c7SVivek Prakash #ifdef S_IFSOCK 90249cab1c7SVivek Prakash case S_IFSOCK: sdata = "Socket"; break; 90349cab1c7SVivek Prakash #endif 90449cab1c7SVivek Prakash #ifdef S_IFWHT 90549cab1c7SVivek Prakash case S_IFWHT: sdata = "Whiteout File"; break; 90649cab1c7SVivek Prakash #endif /* S_IFWHT */ 90749cab1c7SVivek Prakash #ifdef S_IFDOOR 90849cab1c7SVivek Prakash case S_IFDOOR: sdata = "Door"; break; 90949cab1c7SVivek Prakash #endif /* S_IFDOOR */ 91049cab1c7SVivek Prakash default: sdata = "???"; break; 91149cab1c7SVivek Prakash } 91249cab1c7SVivek Prakash hilo = 0; 91349cab1c7SVivek Prakash } 91449cab1c7SVivek Prakash formats = FMTF_STRING; 91549cab1c7SVivek Prakash if (ofmt == 0) 91649cab1c7SVivek Prakash ofmt = FMTF_STRING; 91749cab1c7SVivek Prakash break; 91849cab1c7SVivek Prakash case SHOW_filename: 91949cab1c7SVivek Prakash small = 0; 92049cab1c7SVivek Prakash data = 0; 92149cab1c7SVivek Prakash if (file == NULL) { 92249cab1c7SVivek Prakash (void)strlcpy(path, "(stdin)", sizeof(path)); 92349cab1c7SVivek Prakash if (hilo == HIGH_PIECE || hilo == LOW_PIECE) 92449cab1c7SVivek Prakash hilo = 0; 92549cab1c7SVivek Prakash } 92649cab1c7SVivek Prakash else if (hilo == 0) 92749cab1c7SVivek Prakash (void)strlcpy(path, file, sizeof(path)); 92849cab1c7SVivek Prakash else { 92949cab1c7SVivek Prakash char *s; 93049cab1c7SVivek Prakash (void)strlcpy(path, file, sizeof(path)); 93149cab1c7SVivek Prakash s = strrchr(path, '/'); 93249cab1c7SVivek Prakash if (s != NULL) { 93349cab1c7SVivek Prakash /* trim off trailing /'s */ 93449cab1c7SVivek Prakash while (s != path && 93549cab1c7SVivek Prakash s[0] == '/' && s[1] == '\0') 93649cab1c7SVivek Prakash *s-- = '\0'; 93749cab1c7SVivek Prakash s = strrchr(path, '/'); 93849cab1c7SVivek Prakash } 93949cab1c7SVivek Prakash if (hilo == HIGH_PIECE) { 94049cab1c7SVivek Prakash if (s == NULL) 94149cab1c7SVivek Prakash (void)strlcpy(path, ".", sizeof(path)); 94249cab1c7SVivek Prakash else { 94349cab1c7SVivek Prakash while (s != path && s[0] == '/') 94449cab1c7SVivek Prakash *s-- = '\0'; 94549cab1c7SVivek Prakash } 94649cab1c7SVivek Prakash hilo = 0; 94749cab1c7SVivek Prakash } 94849cab1c7SVivek Prakash else if (hilo == LOW_PIECE) { 94949cab1c7SVivek Prakash if (s != NULL && s[1] != '\0') 95049cab1c7SVivek Prakash (void)strlcpy(path, s + 1, 95149cab1c7SVivek Prakash sizeof(path)); 95249cab1c7SVivek Prakash hilo = 0; 95349cab1c7SVivek Prakash } 95449cab1c7SVivek Prakash } 95549cab1c7SVivek Prakash sdata = path; 95649cab1c7SVivek Prakash formats = FMTF_STRING; 95749cab1c7SVivek Prakash if (ofmt == 0) 95849cab1c7SVivek Prakash ofmt = FMTF_STRING; 95949cab1c7SVivek Prakash break; 96049cab1c7SVivek Prakash case SHOW_sizerdev: 96149cab1c7SVivek Prakash if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { 96249cab1c7SVivek Prakash char majdev[20], mindev[20]; 96349cab1c7SVivek Prakash int l1, l2; 96449cab1c7SVivek Prakash 96549cab1c7SVivek Prakash l1 = format1(st, 96649cab1c7SVivek Prakash file, 96749cab1c7SVivek Prakash fmt, flen, 96849cab1c7SVivek Prakash majdev, sizeof(majdev), 96949cab1c7SVivek Prakash flags, size, prec, 970*474b24f9SLionel Sambuc ofmt, HIGH_PIECE, SHOW_st_rdev, quiet); 97149cab1c7SVivek Prakash l2 = format1(st, 97249cab1c7SVivek Prakash file, 97349cab1c7SVivek Prakash fmt, flen, 97449cab1c7SVivek Prakash mindev, sizeof(mindev), 97549cab1c7SVivek Prakash flags, size, prec, 976*474b24f9SLionel Sambuc ofmt, LOW_PIECE, SHOW_st_rdev, quiet); 97749cab1c7SVivek Prakash return (snprintf(buf, blen, "%.*s,%.*s", 97849cab1c7SVivek Prakash l1, majdev, l2, mindev)); 97949cab1c7SVivek Prakash } 98049cab1c7SVivek Prakash else { 98149cab1c7SVivek Prakash return (format1(st, 98249cab1c7SVivek Prakash file, 98349cab1c7SVivek Prakash fmt, flen, 98449cab1c7SVivek Prakash buf, blen, 98549cab1c7SVivek Prakash flags, size, prec, 986*474b24f9SLionel Sambuc ofmt, 0, SHOW_st_size, quiet)); 98749cab1c7SVivek Prakash } 98849cab1c7SVivek Prakash /*NOTREACHED*/ 98949cab1c7SVivek Prakash default: 99049cab1c7SVivek Prakash errx(1, "%.*s: bad format", (int)flen, fmt); 99149cab1c7SVivek Prakash } 99249cab1c7SVivek Prakash 99349cab1c7SVivek Prakash /* 99449cab1c7SVivek Prakash * If a subdatum was specified but not supported, or an output 99549cab1c7SVivek Prakash * format was selected that is not supported, that's an error. 99649cab1c7SVivek Prakash */ 99749cab1c7SVivek Prakash if (hilo != 0 || (ofmt & formats) == 0) 99849cab1c7SVivek Prakash errx(1, "%.*s: bad format", (int)flen, fmt); 99949cab1c7SVivek Prakash 100049cab1c7SVivek Prakash /* 1001*474b24f9SLionel Sambuc * FLAG_POUND with FMTF_STRING means use vis(3) encoding. 1002*474b24f9SLionel Sambuc * First prefixlen chars are not encoded. 1003*474b24f9SLionel Sambuc */ 1004*474b24f9SLionel Sambuc if ((flags & FLAG_POUND) != 0 && ofmt == FMTF_STRING) { 1005*474b24f9SLionel Sambuc flags &= !FLAG_POUND; 1006*474b24f9SLionel Sambuc strncpy(visbuf, sdata, prefixlen); 1007*474b24f9SLionel Sambuc strnvis(visbuf + prefixlen, sizeof(visbuf) - prefixlen, 1008*474b24f9SLionel Sambuc sdata + prefixlen, VIS_WHITE | VIS_OCTAL | VIS_CSTYLE); 1009*474b24f9SLionel Sambuc sdata = visbuf; 1010*474b24f9SLionel Sambuc } 1011*474b24f9SLionel Sambuc 1012*474b24f9SLionel Sambuc /* 101349cab1c7SVivek Prakash * Assemble the format string for passing to printf(3). 101449cab1c7SVivek Prakash */ 101549cab1c7SVivek Prakash lfmt[0] = '\0'; 101649cab1c7SVivek Prakash (void)strcat(lfmt, "%"); 101749cab1c7SVivek Prakash if (flags & FLAG_POUND) 101849cab1c7SVivek Prakash (void)strcat(lfmt, "#"); 101949cab1c7SVivek Prakash if (flags & FLAG_SPACE) 102049cab1c7SVivek Prakash (void)strcat(lfmt, " "); 102149cab1c7SVivek Prakash if (flags & FLAG_PLUS) 102249cab1c7SVivek Prakash (void)strcat(lfmt, "+"); 102349cab1c7SVivek Prakash if (flags & FLAG_MINUS) 102449cab1c7SVivek Prakash (void)strcat(lfmt, "-"); 102549cab1c7SVivek Prakash if (flags & FLAG_ZERO) 102649cab1c7SVivek Prakash (void)strcat(lfmt, "0"); 102749cab1c7SVivek Prakash 102849cab1c7SVivek Prakash /* 102949cab1c7SVivek Prakash * Only the timespecs support the FLOAT output format, and that 103049cab1c7SVivek Prakash * requires work that differs from the other formats. 103149cab1c7SVivek Prakash */ 103249cab1c7SVivek Prakash if (ofmt == FMTF_FLOAT) { 103349cab1c7SVivek Prakash /* 103449cab1c7SVivek Prakash * Nothing after the decimal point, so just print seconds. 103549cab1c7SVivek Prakash */ 103649cab1c7SVivek Prakash if (prec == 0) { 103749cab1c7SVivek Prakash if (size != -1) { 103849cab1c7SVivek Prakash (void)snprintf(tmp, sizeof(tmp), "%d", size); 103949cab1c7SVivek Prakash (void)strcat(lfmt, tmp); 104049cab1c7SVivek Prakash } 104149cab1c7SVivek Prakash (void)strcat(lfmt, "lld"); 104249cab1c7SVivek Prakash return (snprintf(buf, blen, lfmt, 104349cab1c7SVivek Prakash (long long)secs)); 104449cab1c7SVivek Prakash } 104549cab1c7SVivek Prakash 104649cab1c7SVivek Prakash /* 104749cab1c7SVivek Prakash * Unspecified precision gets all the precision we have: 104849cab1c7SVivek Prakash * 9 digits. 104949cab1c7SVivek Prakash */ 105049cab1c7SVivek Prakash if (prec == -1) 105149cab1c7SVivek Prakash prec = 9; 105249cab1c7SVivek Prakash 105349cab1c7SVivek Prakash /* 105449cab1c7SVivek Prakash * Adjust the size for the decimal point and the digits 105549cab1c7SVivek Prakash * that will follow. 105649cab1c7SVivek Prakash */ 105749cab1c7SVivek Prakash size -= prec + 1; 105849cab1c7SVivek Prakash 105949cab1c7SVivek Prakash /* 106049cab1c7SVivek Prakash * Any leftover size that's legitimate will be used. 106149cab1c7SVivek Prakash */ 106249cab1c7SVivek Prakash if (size > 0) { 106349cab1c7SVivek Prakash (void)snprintf(tmp, sizeof(tmp), "%d", size); 106449cab1c7SVivek Prakash (void)strcat(lfmt, tmp); 106549cab1c7SVivek Prakash } 106649cab1c7SVivek Prakash /* Seconds: time_t cast to long long. */ 106749cab1c7SVivek Prakash (void)strcat(lfmt, "lld"); 106849cab1c7SVivek Prakash 106949cab1c7SVivek Prakash /* 107049cab1c7SVivek Prakash * The stuff after the decimal point always needs zero 107149cab1c7SVivek Prakash * filling. 107249cab1c7SVivek Prakash */ 107349cab1c7SVivek Prakash (void)strcat(lfmt, ".%0"); 107449cab1c7SVivek Prakash 107549cab1c7SVivek Prakash /* 107649cab1c7SVivek Prakash * We can "print" at most nine digits of precision. The 107749cab1c7SVivek Prakash * rest we will pad on at the end. 107849cab1c7SVivek Prakash * 107949cab1c7SVivek Prakash * Nanoseconds: long. 108049cab1c7SVivek Prakash */ 108149cab1c7SVivek Prakash (void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec); 108249cab1c7SVivek Prakash (void)strcat(lfmt, tmp); 108349cab1c7SVivek Prakash 108449cab1c7SVivek Prakash /* 108549cab1c7SVivek Prakash * For precision of less that nine digits, trim off the 108649cab1c7SVivek Prakash * less significant figures. 108749cab1c7SVivek Prakash */ 108849cab1c7SVivek Prakash for (; prec < 9; prec++) 108949cab1c7SVivek Prakash nsecs /= 10; 109049cab1c7SVivek Prakash 109149cab1c7SVivek Prakash /* 109249cab1c7SVivek Prakash * Use the format, and then tack on any zeroes that 109349cab1c7SVivek Prakash * might be required to make up the requested precision. 109449cab1c7SVivek Prakash */ 109549cab1c7SVivek Prakash l = snprintf(buf, blen, lfmt, (long long)secs, nsecs); 109649cab1c7SVivek Prakash for (; prec > 9 && l < (int)blen; prec--, l++) 109749cab1c7SVivek Prakash (void)strcat(buf, "0"); 109849cab1c7SVivek Prakash return (l); 109949cab1c7SVivek Prakash } 110049cab1c7SVivek Prakash 110149cab1c7SVivek Prakash /* 110249cab1c7SVivek Prakash * Add on size and precision, if specified, to the format. 110349cab1c7SVivek Prakash */ 110449cab1c7SVivek Prakash if (size != -1) { 110549cab1c7SVivek Prakash (void)snprintf(tmp, sizeof(tmp), "%d", size); 110649cab1c7SVivek Prakash (void)strcat(lfmt, tmp); 110749cab1c7SVivek Prakash } 110849cab1c7SVivek Prakash if (prec != -1) { 110949cab1c7SVivek Prakash (void)snprintf(tmp, sizeof(tmp), ".%d", prec); 111049cab1c7SVivek Prakash (void)strcat(lfmt, tmp); 111149cab1c7SVivek Prakash } 111249cab1c7SVivek Prakash 111349cab1c7SVivek Prakash /* 111449cab1c7SVivek Prakash * String output uses the temporary sdata. 111549cab1c7SVivek Prakash */ 111649cab1c7SVivek Prakash if (ofmt == FMTF_STRING) { 111749cab1c7SVivek Prakash if (sdata == NULL) 111849cab1c7SVivek Prakash errx(1, "%.*s: bad format", (int)flen, fmt); 111949cab1c7SVivek Prakash (void)strcat(lfmt, "s"); 112049cab1c7SVivek Prakash return (snprintf(buf, blen, lfmt, sdata)); 112149cab1c7SVivek Prakash } 112249cab1c7SVivek Prakash 112349cab1c7SVivek Prakash /* 112449cab1c7SVivek Prakash * Ensure that sign extension does not cause bad looking output 112549cab1c7SVivek Prakash * for some forms. 112649cab1c7SVivek Prakash */ 112749cab1c7SVivek Prakash if (small && ofmt != FMTF_DECIMAL) 112849cab1c7SVivek Prakash data = (u_int32_t)data; 112949cab1c7SVivek Prakash 113049cab1c7SVivek Prakash /* 113149cab1c7SVivek Prakash * The four "numeric" output forms. 113249cab1c7SVivek Prakash */ 113349cab1c7SVivek Prakash (void)strcat(lfmt, "ll"); 113449cab1c7SVivek Prakash switch (ofmt) { 113549cab1c7SVivek Prakash case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break; 113649cab1c7SVivek Prakash case FMTF_OCTAL: (void)strcat(lfmt, "o"); break; 113749cab1c7SVivek Prakash case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break; 113849cab1c7SVivek Prakash case FMTF_HEX: (void)strcat(lfmt, "x"); break; 113949cab1c7SVivek Prakash } 114049cab1c7SVivek Prakash 114149cab1c7SVivek Prakash /* 114249cab1c7SVivek Prakash * shift and round to nearest for kilobytes, megabytes, 114349cab1c7SVivek Prakash * gigabytes. 114449cab1c7SVivek Prakash */ 114549cab1c7SVivek Prakash if (shift > 0) { 114649cab1c7SVivek Prakash data >>= (shift - 1); 114749cab1c7SVivek Prakash data++; 114849cab1c7SVivek Prakash data >>= 1; 114949cab1c7SVivek Prakash } 115049cab1c7SVivek Prakash 115149cab1c7SVivek Prakash return (snprintf(buf, blen, lfmt, data)); 115249cab1c7SVivek Prakash } 1153