stat.c revision 1.36 1 1.36 apb /* $NetBSD: stat.c,v 1.36 2011/09/22 20:23:56 apb Exp $ */
2 1.1 atatat
3 1.1 atatat /*
4 1.36 apb * Copyright (c) 2002-2011 The NetBSD Foundation, Inc.
5 1.1 atatat * All rights reserved.
6 1.1 atatat *
7 1.1 atatat * This code is derived from software contributed to The NetBSD Foundation
8 1.1 atatat * by Andrew Brown.
9 1.1 atatat *
10 1.1 atatat * Redistribution and use in source and binary forms, with or without
11 1.1 atatat * modification, are permitted provided that the following conditions
12 1.1 atatat * are met:
13 1.1 atatat * 1. Redistributions of source code must retain the above copyright
14 1.1 atatat * notice, this list of conditions and the following disclaimer.
15 1.1 atatat * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 atatat * notice, this list of conditions and the following disclaimer in the
17 1.1 atatat * documentation and/or other materials provided with the distribution.
18 1.1 atatat *
19 1.1 atatat * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 atatat * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 atatat * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 atatat * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 atatat * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 atatat * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 atatat * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 atatat * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 atatat * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 atatat * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 atatat * POSSIBILITY OF SUCH DAMAGE.
30 1.1 atatat */
31 1.1 atatat
32 1.15 lukem #if HAVE_NBTOOL_CONFIG_H
33 1.15 lukem #include "nbtool_config.h"
34 1.15 lukem #endif
35 1.15 lukem
36 1.1 atatat #include <sys/cdefs.h>
37 1.15 lukem #if !defined(lint)
38 1.36 apb __RCSID("$NetBSD: stat.c,v 1.36 2011/09/22 20:23:56 apb Exp $");
39 1.11 lukem #endif
40 1.11 lukem
41 1.15 lukem #if ! HAVE_NBTOOL_CONFIG_H
42 1.11 lukem #define HAVE_STRUCT_STAT_ST_FLAGS 1
43 1.13 atatat #define HAVE_STRUCT_STAT_ST_GEN 1
44 1.13 atatat #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
45 1.21 jmc #define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1
46 1.13 atatat #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
47 1.13 atatat #define HAVE_DEVNAME 1
48 1.15 lukem #endif /* HAVE_NBTOOL_CONFIG_H */
49 1.1 atatat
50 1.10 atatat #include <sys/types.h>
51 1.1 atatat #include <sys/stat.h>
52 1.6 atatat
53 1.6 atatat #include <ctype.h>
54 1.7 atatat #include <err.h>
55 1.18 atatat #include <errno.h>
56 1.6 atatat #include <grp.h>
57 1.8 atatat #include <limits.h>
58 1.6 atatat #include <pwd.h>
59 1.6 atatat #include <stdio.h>
60 1.7 atatat #include <stdlib.h>
61 1.1 atatat #include <string.h>
62 1.6 atatat #include <time.h>
63 1.6 atatat #include <unistd.h>
64 1.36 apb #include <vis.h>
65 1.1 atatat
66 1.13 atatat #if HAVE_STRUCT_STAT_ST_FLAGS
67 1.13 atatat #define DEF_F "%#Xf "
68 1.13 atatat #define RAW_F "%f "
69 1.13 atatat #define SHELL_F " st_flags=%f"
70 1.13 atatat #else /* HAVE_STRUCT_STAT_ST_FLAGS */
71 1.13 atatat #define DEF_F
72 1.13 atatat #define RAW_F
73 1.13 atatat #define SHELL_F
74 1.13 atatat #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
75 1.13 atatat
76 1.13 atatat #if HAVE_STRUCT_STAT_ST_BIRTHTIME
77 1.13 atatat #define DEF_B "\"%SB\" "
78 1.13 atatat #define RAW_B "%B "
79 1.13 atatat #define SHELL_B "st_birthtime=%B "
80 1.13 atatat #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
81 1.13 atatat #define DEF_B
82 1.13 atatat #define RAW_B
83 1.13 atatat #define SHELL_B
84 1.13 atatat #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
85 1.13 atatat
86 1.13 atatat #if HAVE_STRUCT_STAT_ST_ATIM
87 1.13 atatat #define st_atimespec st_atim
88 1.13 atatat #define st_ctimespec st_ctim
89 1.13 atatat #define st_mtimespec st_mtim
90 1.13 atatat #endif /* HAVE_STRUCT_STAT_ST_ATIM */
91 1.13 atatat
92 1.1 atatat #define DEF_FORMAT \
93 1.13 atatat "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
94 1.13 atatat "%k %b " DEF_F "%N"
95 1.13 atatat #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
96 1.13 atatat "%k %b " RAW_F "%N"
97 1.1 atatat #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
98 1.1 atatat #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
99 1.1 atatat #define SHELL_FORMAT \
100 1.1 atatat "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
101 1.1 atatat "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
102 1.13 atatat "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
103 1.13 atatat "st_blksize=%k st_blocks=%b" SHELL_F
104 1.1 atatat #define LINUX_FORMAT \
105 1.1 atatat " File: \"%N\"%n" \
106 1.1 atatat " Size: %-11z FileType: %HT%n" \
107 1.1 atatat " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
108 1.1 atatat "Device: %Hd,%Ld Inode: %i Links: %l%n" \
109 1.1 atatat "Access: %Sa%n" \
110 1.1 atatat "Modify: %Sm%n" \
111 1.1 atatat "Change: %Sc"
112 1.1 atatat
113 1.1 atatat #define TIME_FORMAT "%b %e %T %Y"
114 1.1 atatat
115 1.2 atatat #define FLAG_POUND 0x01
116 1.2 atatat #define FLAG_SPACE 0x02
117 1.2 atatat #define FLAG_PLUS 0x04
118 1.2 atatat #define FLAG_ZERO 0x08
119 1.2 atatat #define FLAG_MINUS 0x10
120 1.1 atatat
121 1.1 atatat /*
122 1.2 atatat * These format characters must all be unique, except the magic one.
123 1.1 atatat */
124 1.2 atatat #define FMT_MAGIC '%'
125 1.2 atatat #define FMT_DOT '.'
126 1.2 atatat
127 1.1 atatat #define SIMPLE_NEWLINE 'n'
128 1.1 atatat #define SIMPLE_TAB 't'
129 1.1 atatat #define SIMPLE_PERCENT '%'
130 1.2 atatat #define SIMPLE_NUMBER '@'
131 1.2 atatat
132 1.2 atatat #define FMT_POUND '#'
133 1.2 atatat #define FMT_SPACE ' '
134 1.2 atatat #define FMT_PLUS '+'
135 1.2 atatat #define FMT_ZERO '0'
136 1.2 atatat #define FMT_MINUS '-'
137 1.1 atatat
138 1.1 atatat #define FMT_DECIMAL 'D'
139 1.1 atatat #define FMT_OCTAL 'O'
140 1.1 atatat #define FMT_UNSIGNED 'U'
141 1.1 atatat #define FMT_HEX 'X'
142 1.1 atatat #define FMT_FLOAT 'F'
143 1.1 atatat #define FMT_STRING 'S'
144 1.1 atatat
145 1.5 atatat #define FMTF_DECIMAL 0x01
146 1.5 atatat #define FMTF_OCTAL 0x02
147 1.5 atatat #define FMTF_UNSIGNED 0x04
148 1.5 atatat #define FMTF_HEX 0x08
149 1.5 atatat #define FMTF_FLOAT 0x10
150 1.5 atatat #define FMTF_STRING 0x20
151 1.5 atatat
152 1.1 atatat #define HIGH_PIECE 'H'
153 1.1 atatat #define MIDDLE_PIECE 'M'
154 1.1 atatat #define LOW_PIECE 'L'
155 1.1 atatat
156 1.24 elad #define SHOW_realpath 'R'
157 1.1 atatat #define SHOW_st_dev 'd'
158 1.1 atatat #define SHOW_st_ino 'i'
159 1.1 atatat #define SHOW_st_mode 'p'
160 1.1 atatat #define SHOW_st_nlink 'l'
161 1.1 atatat #define SHOW_st_uid 'u'
162 1.1 atatat #define SHOW_st_gid 'g'
163 1.1 atatat #define SHOW_st_rdev 'r'
164 1.1 atatat #define SHOW_st_atime 'a'
165 1.1 atatat #define SHOW_st_mtime 'm'
166 1.1 atatat #define SHOW_st_ctime 'c'
167 1.10 atatat #define SHOW_st_btime 'B'
168 1.1 atatat #define SHOW_st_size 'z'
169 1.1 atatat #define SHOW_st_blocks 'b'
170 1.1 atatat #define SHOW_st_blksize 'k'
171 1.1 atatat #define SHOW_st_flags 'f'
172 1.1 atatat #define SHOW_st_gen 'v'
173 1.1 atatat #define SHOW_symlink 'Y'
174 1.1 atatat #define SHOW_filetype 'T'
175 1.1 atatat #define SHOW_filename 'N'
176 1.1 atatat #define SHOW_sizerdev 'Z'
177 1.1 atatat
178 1.35 joerg static void usage(const char *) __dead;
179 1.35 joerg static void output(const struct stat *, const char *,
180 1.34 christos const char *, int, int, int);
181 1.35 joerg static int format1(const struct stat *, /* stat info */
182 1.1 atatat const char *, /* the file name */
183 1.1 atatat const char *, int, /* the format string itself */
184 1.1 atatat char *, size_t, /* a place to put the output */
185 1.1 atatat int, int, int, int, /* the parsed format */
186 1.34 christos int, int, int);
187 1.1 atatat
188 1.35 joerg static const char *timefmt;
189 1.35 joerg static int linkfail;
190 1.1 atatat
191 1.4 atatat #define addchar(s, c, nl) \
192 1.1 atatat do { \
193 1.4 atatat (void)fputc((c), (s)); \
194 1.4 atatat (*nl) = ((c) == '\n'); \
195 1.1 atatat } while (0/*CONSTCOND*/)
196 1.1 atatat
197 1.1 atatat int
198 1.1 atatat main(int argc, char *argv[])
199 1.1 atatat {
200 1.1 atatat struct stat st;
201 1.4 atatat int ch, rc, errs, am_readlink;
202 1.4 atatat int lsF, fmtchar, usestat, fn, nonl, quiet;
203 1.28 lukem const char *statfmt, *options, *synopsis;
204 1.1 atatat
205 1.4 atatat am_readlink = 0;
206 1.1 atatat lsF = 0;
207 1.1 atatat fmtchar = '\0';
208 1.1 atatat usestat = 0;
209 1.1 atatat nonl = 0;
210 1.4 atatat quiet = 0;
211 1.4 atatat linkfail = 0;
212 1.1 atatat statfmt = NULL;
213 1.1 atatat timefmt = NULL;
214 1.1 atatat
215 1.34 christos setprogname(argv[0]);
216 1.34 christos
217 1.4 atatat if (strcmp(getprogname(), "readlink") == 0) {
218 1.4 atatat am_readlink = 1;
219 1.34 christos options = "fnqsv";
220 1.34 christos synopsis = "[-fnqsv] [file ...]";
221 1.4 atatat statfmt = "%Y";
222 1.4 atatat fmtchar = 'f';
223 1.4 atatat quiet = 1;
224 1.4 atatat } else {
225 1.4 atatat options = "f:FlLnqrst:x";
226 1.4 atatat synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
227 1.4 atatat }
228 1.4 atatat
229 1.4 atatat while ((ch = getopt(argc, argv, options)) != -1)
230 1.1 atatat switch (ch) {
231 1.1 atatat case 'F':
232 1.1 atatat lsF = 1;
233 1.1 atatat break;
234 1.1 atatat case 'L':
235 1.1 atatat usestat = 1;
236 1.1 atatat break;
237 1.1 atatat case 'n':
238 1.1 atatat nonl = 1;
239 1.1 atatat break;
240 1.4 atatat case 'q':
241 1.4 atatat quiet = 1;
242 1.4 atatat break;
243 1.1 atatat case 'f':
244 1.24 elad if (am_readlink) {
245 1.24 elad statfmt = "%R";
246 1.24 elad break;
247 1.24 elad }
248 1.1 atatat statfmt = optarg;
249 1.1 atatat /* FALLTHROUGH */
250 1.1 atatat case 'l':
251 1.1 atatat case 'r':
252 1.1 atatat case 's':
253 1.34 christos if (am_readlink) {
254 1.34 christos quiet = 1;
255 1.34 christos break;
256 1.34 christos }
257 1.34 christos /*FALLTHROUGH*/
258 1.1 atatat case 'x':
259 1.1 atatat if (fmtchar != 0)
260 1.1 atatat errx(1, "can't use format '%c' with '%c'",
261 1.1 atatat fmtchar, ch);
262 1.1 atatat fmtchar = ch;
263 1.1 atatat break;
264 1.1 atatat case 't':
265 1.1 atatat timefmt = optarg;
266 1.1 atatat break;
267 1.34 christos case 'v':
268 1.34 christos quiet = 0;
269 1.34 christos break;
270 1.1 atatat default:
271 1.4 atatat usage(synopsis);
272 1.1 atatat }
273 1.1 atatat
274 1.1 atatat argc -= optind;
275 1.1 atatat argv += optind;
276 1.2 atatat fn = 1;
277 1.1 atatat
278 1.1 atatat if (fmtchar == '\0') {
279 1.1 atatat if (lsF)
280 1.1 atatat fmtchar = 'l';
281 1.1 atatat else {
282 1.1 atatat fmtchar = 'f';
283 1.1 atatat statfmt = DEF_FORMAT;
284 1.1 atatat }
285 1.1 atatat }
286 1.1 atatat
287 1.1 atatat if (lsF && fmtchar != 'l')
288 1.1 atatat errx(1, "can't use format '%c' with -F", fmtchar);
289 1.1 atatat
290 1.1 atatat switch (fmtchar) {
291 1.1 atatat case 'f':
292 1.1 atatat /* statfmt already set */
293 1.1 atatat break;
294 1.1 atatat case 'l':
295 1.1 atatat statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
296 1.1 atatat break;
297 1.1 atatat case 'r':
298 1.1 atatat statfmt = RAW_FORMAT;
299 1.1 atatat break;
300 1.1 atatat case 's':
301 1.1 atatat statfmt = SHELL_FORMAT;
302 1.1 atatat break;
303 1.1 atatat case 'x':
304 1.1 atatat statfmt = LINUX_FORMAT;
305 1.1 atatat if (timefmt == NULL)
306 1.1 atatat timefmt = "%c";
307 1.1 atatat break;
308 1.1 atatat default:
309 1.4 atatat usage(synopsis);
310 1.1 atatat /*NOTREACHED*/
311 1.1 atatat }
312 1.1 atatat
313 1.1 atatat if (timefmt == NULL)
314 1.1 atatat timefmt = TIME_FORMAT;
315 1.1 atatat
316 1.1 atatat errs = 0;
317 1.1 atatat do {
318 1.1 atatat if (argc == 0)
319 1.1 atatat rc = fstat(STDIN_FILENO, &st);
320 1.18 atatat else if (usestat) {
321 1.18 atatat /*
322 1.18 atatat * Try stat() and if it fails, fall back to
323 1.18 atatat * lstat() just in case we're examining a
324 1.18 atatat * broken symlink.
325 1.18 atatat */
326 1.18 atatat if ((rc = stat(argv[0], &st)) == -1 &&
327 1.18 atatat errno == ENOENT &&
328 1.18 atatat (rc = lstat(argv[0], &st)) == -1)
329 1.18 atatat errno = ENOENT;
330 1.18 atatat }
331 1.1 atatat else
332 1.1 atatat rc = lstat(argv[0], &st);
333 1.1 atatat
334 1.1 atatat if (rc == -1) {
335 1.1 atatat errs = 1;
336 1.4 atatat linkfail = 1;
337 1.4 atatat if (!quiet)
338 1.20 atatat warn("%s: %s",
339 1.20 atatat argc == 0 ? "(stdin)" : argv[0],
340 1.20 atatat usestat ? "stat" : "lstat");
341 1.1 atatat }
342 1.1 atatat else
343 1.34 christos output(&st, argv[0], statfmt, fn, nonl, quiet);
344 1.1 atatat
345 1.1 atatat argv++;
346 1.1 atatat argc--;
347 1.2 atatat fn++;
348 1.1 atatat } while (argc > 0);
349 1.1 atatat
350 1.4 atatat return (am_readlink ? linkfail : errs);
351 1.1 atatat }
352 1.1 atatat
353 1.35 joerg static void
354 1.4 atatat usage(const char *synopsis)
355 1.1 atatat {
356 1.1 atatat
357 1.4 atatat (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
358 1.1 atatat exit(1);
359 1.1 atatat }
360 1.1 atatat
361 1.1 atatat /*
362 1.1 atatat * Parses a format string.
363 1.1 atatat */
364 1.35 joerg static void
365 1.1 atatat output(const struct stat *st, const char *file,
366 1.34 christos const char *statfmt, int fn, int nonl, int quiet)
367 1.1 atatat {
368 1.1 atatat int flags, size, prec, ofmt, hilo, what;
369 1.36 apb /*
370 1.36 apb * buf size is enough for an item of length PATH_MAX,
371 1.36 apb * multiplied by 4 for vis encoding, plus 4 for symlink
372 1.36 apb * " -> " prefix, plus 1 for \0 terminator.
373 1.36 apb */
374 1.36 apb char buf[PATH_MAX * 4 + 4 + 1];
375 1.1 atatat const char *subfmt;
376 1.1 atatat int nl, t, i;
377 1.1 atatat
378 1.4 atatat nl = 1;
379 1.1 atatat while (*statfmt != '\0') {
380 1.1 atatat
381 1.1 atatat /*
382 1.1 atatat * Non-format characters go straight out.
383 1.1 atatat */
384 1.2 atatat if (*statfmt != FMT_MAGIC) {
385 1.4 atatat addchar(stdout, *statfmt, &nl);
386 1.1 atatat statfmt++;
387 1.1 atatat continue;
388 1.1 atatat }
389 1.1 atatat
390 1.1 atatat /*
391 1.1 atatat * The current format "substring" starts here,
392 1.2 atatat * and then we skip the magic.
393 1.1 atatat */
394 1.1 atatat subfmt = statfmt;
395 1.1 atatat statfmt++;
396 1.1 atatat
397 1.1 atatat /*
398 1.1 atatat * Some simple one-character "formats".
399 1.1 atatat */
400 1.1 atatat switch (*statfmt) {
401 1.1 atatat case SIMPLE_NEWLINE:
402 1.4 atatat addchar(stdout, '\n', &nl);
403 1.1 atatat statfmt++;
404 1.1 atatat continue;
405 1.1 atatat case SIMPLE_TAB:
406 1.4 atatat addchar(stdout, '\t', &nl);
407 1.1 atatat statfmt++;
408 1.1 atatat continue;
409 1.1 atatat case SIMPLE_PERCENT:
410 1.4 atatat addchar(stdout, '%', &nl);
411 1.1 atatat statfmt++;
412 1.1 atatat continue;
413 1.2 atatat case SIMPLE_NUMBER: {
414 1.2 atatat char num[12], *p;
415 1.2 atatat
416 1.2 atatat snprintf(num, sizeof(num), "%d", fn);
417 1.2 atatat for (p = &num[0]; *p; p++)
418 1.4 atatat addchar(stdout, *p, &nl);
419 1.2 atatat statfmt++;
420 1.2 atatat continue;
421 1.2 atatat }
422 1.1 atatat }
423 1.1 atatat
424 1.1 atatat /*
425 1.1 atatat * This must be an actual format string. Format strings are
426 1.1 atatat * similar to printf(3) formats up to a point, and are of
427 1.1 atatat * the form:
428 1.1 atatat *
429 1.1 atatat * % required start of format
430 1.1 atatat * [-# +0] opt. format characters
431 1.1 atatat * size opt. field width
432 1.1 atatat * . opt. decimal separator, followed by
433 1.1 atatat * prec opt. precision
434 1.1 atatat * fmt opt. output specifier (string, numeric, etc.)
435 1.1 atatat * sub opt. sub field specifier (high, middle, low)
436 1.1 atatat * datum required field specifier (size, mode, etc)
437 1.1 atatat *
438 1.1 atatat * Only the % and the datum selector are required. All data
439 1.1 atatat * have reasonable default output forms. The "sub" specifier
440 1.1 atatat * only applies to certain data (mode, dev, rdev, filetype).
441 1.1 atatat * The symlink output defaults to STRING, yet will only emit
442 1.1 atatat * the leading " -> " if STRING is explicitly specified. The
443 1.1 atatat * sizerdev datum will generate rdev output for character or
444 1.1 atatat * block devices, and size output for all others.
445 1.36 apb * For STRING output, the # format requests vis encoding.
446 1.1 atatat */
447 1.1 atatat flags = 0;
448 1.1 atatat do {
449 1.2 atatat if (*statfmt == FMT_POUND)
450 1.2 atatat flags |= FLAG_POUND;
451 1.2 atatat else if (*statfmt == FMT_SPACE)
452 1.2 atatat flags |= FLAG_SPACE;
453 1.2 atatat else if (*statfmt == FMT_PLUS)
454 1.2 atatat flags |= FLAG_PLUS;
455 1.2 atatat else if (*statfmt == FMT_ZERO)
456 1.2 atatat flags |= FLAG_ZERO;
457 1.2 atatat else if (*statfmt == FMT_MINUS)
458 1.2 atatat flags |= FLAG_MINUS;
459 1.1 atatat else
460 1.1 atatat break;
461 1.1 atatat statfmt++;
462 1.1 atatat } while (1/*CONSTCOND*/);
463 1.1 atatat
464 1.1 atatat size = -1;
465 1.1 atatat if (isdigit((unsigned)*statfmt)) {
466 1.1 atatat size = 0;
467 1.1 atatat while (isdigit((unsigned)*statfmt)) {
468 1.1 atatat size = (size * 10) + (*statfmt - '0');
469 1.1 atatat statfmt++;
470 1.1 atatat if (size < 0)
471 1.1 atatat goto badfmt;
472 1.1 atatat }
473 1.1 atatat }
474 1.1 atatat
475 1.1 atatat prec = -1;
476 1.2 atatat if (*statfmt == FMT_DOT) {
477 1.1 atatat statfmt++;
478 1.1 atatat
479 1.1 atatat prec = 0;
480 1.1 atatat while (isdigit((unsigned)*statfmt)) {
481 1.1 atatat prec = (prec * 10) + (*statfmt - '0');
482 1.1 atatat statfmt++;
483 1.1 atatat if (prec < 0)
484 1.1 atatat goto badfmt;
485 1.1 atatat }
486 1.1 atatat }
487 1.1 atatat
488 1.5 atatat #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
489 1.5 atatat #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
490 1.1 atatat switch (*statfmt) {
491 1.5 atatat fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
492 1.5 atatat fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
493 1.5 atatat fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
494 1.5 atatat fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
495 1.5 atatat fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
496 1.5 atatat fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
497 1.1 atatat default:
498 1.1 atatat ofmt = 0;
499 1.1 atatat break;
500 1.1 atatat }
501 1.1 atatat
502 1.1 atatat switch (*statfmt) {
503 1.1 atatat fmtcase(hilo, HIGH_PIECE);
504 1.1 atatat fmtcase(hilo, MIDDLE_PIECE);
505 1.1 atatat fmtcase(hilo, LOW_PIECE);
506 1.1 atatat default:
507 1.1 atatat hilo = 0;
508 1.1 atatat break;
509 1.1 atatat }
510 1.1 atatat
511 1.1 atatat switch (*statfmt) {
512 1.24 elad fmtcase(what, SHOW_realpath);
513 1.1 atatat fmtcase(what, SHOW_st_dev);
514 1.1 atatat fmtcase(what, SHOW_st_ino);
515 1.1 atatat fmtcase(what, SHOW_st_mode);
516 1.1 atatat fmtcase(what, SHOW_st_nlink);
517 1.1 atatat fmtcase(what, SHOW_st_uid);
518 1.1 atatat fmtcase(what, SHOW_st_gid);
519 1.1 atatat fmtcase(what, SHOW_st_rdev);
520 1.1 atatat fmtcase(what, SHOW_st_atime);
521 1.1 atatat fmtcase(what, SHOW_st_mtime);
522 1.1 atatat fmtcase(what, SHOW_st_ctime);
523 1.10 atatat fmtcase(what, SHOW_st_btime);
524 1.1 atatat fmtcase(what, SHOW_st_size);
525 1.1 atatat fmtcase(what, SHOW_st_blocks);
526 1.1 atatat fmtcase(what, SHOW_st_blksize);
527 1.1 atatat fmtcase(what, SHOW_st_flags);
528 1.1 atatat fmtcase(what, SHOW_st_gen);
529 1.1 atatat fmtcase(what, SHOW_symlink);
530 1.1 atatat fmtcase(what, SHOW_filetype);
531 1.1 atatat fmtcase(what, SHOW_filename);
532 1.1 atatat fmtcase(what, SHOW_sizerdev);
533 1.1 atatat default:
534 1.1 atatat goto badfmt;
535 1.1 atatat }
536 1.5 atatat #undef fmtcasef
537 1.1 atatat #undef fmtcase
538 1.1 atatat
539 1.1 atatat t = format1(st,
540 1.1 atatat file,
541 1.1 atatat subfmt, statfmt - subfmt,
542 1.4 atatat buf, sizeof(buf),
543 1.34 christos flags, size, prec, ofmt, hilo, what, quiet);
544 1.1 atatat
545 1.28 lukem for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
546 1.4 atatat addchar(stdout, buf[i], &nl);
547 1.1 atatat
548 1.1 atatat continue;
549 1.1 atatat
550 1.1 atatat badfmt:
551 1.1 atatat errx(1, "%.*s: bad format",
552 1.1 atatat (int)(statfmt - subfmt + 1), subfmt);
553 1.1 atatat }
554 1.1 atatat
555 1.1 atatat if (!nl && !nonl)
556 1.4 atatat (void)fputc('\n', stdout);
557 1.4 atatat (void)fflush(stdout);
558 1.1 atatat }
559 1.1 atatat
560 1.1 atatat /*
561 1.1 atatat * Arranges output according to a single parsed format substring.
562 1.1 atatat */
563 1.35 joerg static int
564 1.1 atatat format1(const struct stat *st,
565 1.1 atatat const char *file,
566 1.1 atatat const char *fmt, int flen,
567 1.1 atatat char *buf, size_t blen,
568 1.1 atatat int flags, int size, int prec, int ofmt,
569 1.34 christos int hilo, int what, int quiet)
570 1.1 atatat {
571 1.1 atatat u_int64_t data;
572 1.28 lukem char *stmp, lfmt[24], tmp[20];
573 1.28 lukem const char *sdata;
574 1.36 apb char smode[12], sid[12], path[PATH_MAX + 4], visbuf[PATH_MAX * 4 + 4];
575 1.1 atatat struct passwd *pw;
576 1.1 atatat struct group *gr;
577 1.1 atatat struct tm *tm;
578 1.14 chs time_t secs;
579 1.14 chs long nsecs;
580 1.36 apb int l;
581 1.36 apb int formats; /* bitmap of allowed formats for this datum */
582 1.36 apb int small; /* true if datum is a small integer */
583 1.36 apb int gottime; /* true if secs and nsecs are valid */
584 1.36 apb int shift; /* powers of 2 to scale numbers before printing */
585 1.36 apb size_t prefixlen; /* length of constant prefix for string data */
586 1.1 atatat
587 1.1 atatat formats = 0;
588 1.1 atatat small = 0;
589 1.14 chs gottime = 0;
590 1.14 chs secs = 0;
591 1.14 chs nsecs = 0;
592 1.23 atatat shift = 0;
593 1.36 apb prefixlen = 0;
594 1.1 atatat
595 1.1 atatat /*
596 1.1 atatat * First, pick out the data and tweak it based on hilo or
597 1.1 atatat * specified output format (symlink output only).
598 1.1 atatat */
599 1.1 atatat switch (what) {
600 1.1 atatat case SHOW_st_dev:
601 1.1 atatat case SHOW_st_rdev:
602 1.1 atatat small = (sizeof(st->st_dev) == 4);
603 1.1 atatat data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
604 1.13 atatat #if HAVE_DEVNAME
605 1.1 atatat sdata = (what == SHOW_st_dev) ?
606 1.1 atatat devname(st->st_dev, S_IFBLK) :
607 1.1 atatat devname(st->st_rdev,
608 1.1 atatat S_ISCHR(st->st_mode) ? S_IFCHR :
609 1.1 atatat S_ISBLK(st->st_mode) ? S_IFBLK :
610 1.1 atatat 0U);
611 1.3 atatat if (sdata == NULL)
612 1.3 atatat sdata = "???";
613 1.13 atatat #endif /* HAVE_DEVNAME */
614 1.1 atatat if (hilo == HIGH_PIECE) {
615 1.1 atatat data = major(data);
616 1.1 atatat hilo = 0;
617 1.1 atatat }
618 1.1 atatat else if (hilo == LOW_PIECE) {
619 1.1 atatat data = minor((unsigned)data);
620 1.1 atatat hilo = 0;
621 1.1 atatat }
622 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
623 1.13 atatat #if HAVE_DEVNAME
624 1.5 atatat FMTF_STRING;
625 1.13 atatat #else /* HAVE_DEVNAME */
626 1.13 atatat 0;
627 1.13 atatat #endif /* HAVE_DEVNAME */
628 1.1 atatat if (ofmt == 0)
629 1.5 atatat ofmt = FMTF_UNSIGNED;
630 1.1 atatat break;
631 1.1 atatat case SHOW_st_ino:
632 1.1 atatat small = (sizeof(st->st_ino) == 4);
633 1.1 atatat data = st->st_ino;
634 1.1 atatat sdata = NULL;
635 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
636 1.1 atatat if (ofmt == 0)
637 1.5 atatat ofmt = FMTF_UNSIGNED;
638 1.1 atatat break;
639 1.1 atatat case SHOW_st_mode:
640 1.1 atatat small = (sizeof(st->st_mode) == 4);
641 1.1 atatat data = st->st_mode;
642 1.1 atatat strmode(st->st_mode, smode);
643 1.28 lukem stmp = smode;
644 1.28 lukem l = strlen(stmp);
645 1.28 lukem if (stmp[l - 1] == ' ')
646 1.28 lukem stmp[--l] = '\0';
647 1.1 atatat if (hilo == HIGH_PIECE) {
648 1.1 atatat data >>= 12;
649 1.28 lukem stmp += 1;
650 1.28 lukem stmp[3] = '\0';
651 1.1 atatat hilo = 0;
652 1.1 atatat }
653 1.1 atatat else if (hilo == MIDDLE_PIECE) {
654 1.2 atatat data = (data >> 9) & 07;
655 1.28 lukem stmp += 4;
656 1.28 lukem stmp[3] = '\0';
657 1.1 atatat hilo = 0;
658 1.1 atatat }
659 1.1 atatat else if (hilo == LOW_PIECE) {
660 1.2 atatat data &= 0777;
661 1.28 lukem stmp += 7;
662 1.28 lukem stmp[3] = '\0';
663 1.1 atatat hilo = 0;
664 1.1 atatat }
665 1.28 lukem sdata = stmp;
666 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
667 1.5 atatat FMTF_STRING;
668 1.1 atatat if (ofmt == 0)
669 1.5 atatat ofmt = FMTF_OCTAL;
670 1.1 atatat break;
671 1.1 atatat case SHOW_st_nlink:
672 1.1 atatat small = (sizeof(st->st_dev) == 4);
673 1.1 atatat data = st->st_nlink;
674 1.1 atatat sdata = NULL;
675 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
676 1.1 atatat if (ofmt == 0)
677 1.5 atatat ofmt = FMTF_UNSIGNED;
678 1.1 atatat break;
679 1.1 atatat case SHOW_st_uid:
680 1.1 atatat small = (sizeof(st->st_uid) == 4);
681 1.1 atatat data = st->st_uid;
682 1.1 atatat if ((pw = getpwuid(st->st_uid)) != NULL)
683 1.1 atatat sdata = pw->pw_name;
684 1.1 atatat else {
685 1.1 atatat snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
686 1.1 atatat sdata = sid;
687 1.1 atatat }
688 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
689 1.5 atatat FMTF_STRING;
690 1.1 atatat if (ofmt == 0)
691 1.5 atatat ofmt = FMTF_UNSIGNED;
692 1.1 atatat break;
693 1.1 atatat case SHOW_st_gid:
694 1.1 atatat small = (sizeof(st->st_gid) == 4);
695 1.1 atatat data = st->st_gid;
696 1.1 atatat if ((gr = getgrgid(st->st_gid)) != NULL)
697 1.1 atatat sdata = gr->gr_name;
698 1.1 atatat else {
699 1.1 atatat snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
700 1.1 atatat sdata = sid;
701 1.1 atatat }
702 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
703 1.5 atatat FMTF_STRING;
704 1.1 atatat if (ofmt == 0)
705 1.5 atatat ofmt = FMTF_UNSIGNED;
706 1.1 atatat break;
707 1.1 atatat case SHOW_st_atime:
708 1.14 chs gottime = 1;
709 1.14 chs secs = st->st_atime;
710 1.16 lukem #if HAVE_STRUCT_STAT_ST_MTIMENSEC
711 1.14 chs nsecs = st->st_atimensec;
712 1.14 chs #endif
713 1.1 atatat /* FALLTHROUGH */
714 1.1 atatat case SHOW_st_mtime:
715 1.14 chs if (!gottime) {
716 1.17 atatat gottime = 1;
717 1.14 chs secs = st->st_mtime;
718 1.16 lukem #if HAVE_STRUCT_STAT_ST_MTIMENSEC
719 1.14 chs nsecs = st->st_mtimensec;
720 1.14 chs #endif
721 1.14 chs }
722 1.1 atatat /* FALLTHROUGH */
723 1.1 atatat case SHOW_st_ctime:
724 1.14 chs if (!gottime) {
725 1.17 atatat gottime = 1;
726 1.14 chs secs = st->st_ctime;
727 1.16 lukem #if HAVE_STRUCT_STAT_ST_MTIMENSEC
728 1.14 chs nsecs = st->st_ctimensec;
729 1.14 chs #endif
730 1.14 chs }
731 1.10 atatat /* FALLTHROUGH */
732 1.13 atatat #if HAVE_STRUCT_STAT_ST_BIRTHTIME
733 1.10 atatat case SHOW_st_btime:
734 1.14 chs if (!gottime) {
735 1.17 atatat gottime = 1;
736 1.19 jmc secs = st->st_birthtime;
737 1.21 jmc #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
738 1.19 jmc nsecs = st->st_birthtimensec;
739 1.21 jmc #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */
740 1.14 chs }
741 1.13 atatat #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
742 1.14 chs small = (sizeof(secs) == 4);
743 1.14 chs data = secs;
744 1.14 chs tm = localtime(&secs);
745 1.33 njoly if (tm == NULL) {
746 1.33 njoly secs = 0;
747 1.33 njoly tm = localtime(&secs);
748 1.33 njoly }
749 1.1 atatat (void)strftime(path, sizeof(path), timefmt, tm);
750 1.1 atatat sdata = path;
751 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
752 1.5 atatat FMTF_FLOAT | FMTF_STRING;
753 1.1 atatat if (ofmt == 0)
754 1.5 atatat ofmt = FMTF_DECIMAL;
755 1.1 atatat break;
756 1.1 atatat case SHOW_st_size:
757 1.1 atatat small = (sizeof(st->st_size) == 4);
758 1.1 atatat data = st->st_size;
759 1.1 atatat sdata = NULL;
760 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
761 1.1 atatat if (ofmt == 0)
762 1.5 atatat ofmt = FMTF_UNSIGNED;
763 1.23 atatat switch (hilo) {
764 1.23 atatat case HIGH_PIECE:
765 1.23 atatat shift = 30; /* gigabytes */
766 1.23 atatat hilo = 0;
767 1.23 atatat break;
768 1.23 atatat case MIDDLE_PIECE:
769 1.23 atatat shift = 20; /* megabytes */
770 1.23 atatat hilo = 0;
771 1.23 atatat break;
772 1.23 atatat case LOW_PIECE:
773 1.23 atatat shift = 10; /* kilobytes */
774 1.23 atatat hilo = 0;
775 1.23 atatat break;
776 1.23 atatat }
777 1.1 atatat break;
778 1.1 atatat case SHOW_st_blocks:
779 1.1 atatat small = (sizeof(st->st_blocks) == 4);
780 1.1 atatat data = st->st_blocks;
781 1.1 atatat sdata = NULL;
782 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
783 1.1 atatat if (ofmt == 0)
784 1.5 atatat ofmt = FMTF_UNSIGNED;
785 1.1 atatat break;
786 1.1 atatat case SHOW_st_blksize:
787 1.1 atatat small = (sizeof(st->st_blksize) == 4);
788 1.1 atatat data = st->st_blksize;
789 1.1 atatat sdata = NULL;
790 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
791 1.1 atatat if (ofmt == 0)
792 1.5 atatat ofmt = FMTF_UNSIGNED;
793 1.1 atatat break;
794 1.11 lukem #if HAVE_STRUCT_STAT_ST_FLAGS
795 1.1 atatat case SHOW_st_flags:
796 1.1 atatat small = (sizeof(st->st_flags) == 4);
797 1.1 atatat data = st->st_flags;
798 1.1 atatat sdata = NULL;
799 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
800 1.1 atatat if (ofmt == 0)
801 1.5 atatat ofmt = FMTF_UNSIGNED;
802 1.1 atatat break;
803 1.13 atatat #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
804 1.13 atatat #if HAVE_STRUCT_STAT_ST_GEN
805 1.1 atatat case SHOW_st_gen:
806 1.1 atatat small = (sizeof(st->st_gen) == 4);
807 1.1 atatat data = st->st_gen;
808 1.1 atatat sdata = NULL;
809 1.5 atatat formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
810 1.1 atatat if (ofmt == 0)
811 1.5 atatat ofmt = FMTF_UNSIGNED;
812 1.1 atatat break;
813 1.13 atatat #endif /* HAVE_STRUCT_STAT_ST_GEN */
814 1.24 elad case SHOW_realpath:
815 1.24 elad small = 0;
816 1.24 elad data = 0;
817 1.25 mlelstv if (file == NULL) {
818 1.31 dholland (void)strlcpy(path, "(stdin)", sizeof(path));
819 1.25 mlelstv sdata = path;
820 1.25 mlelstv } else {
821 1.25 mlelstv snprintf(path, sizeof(path), " -> ");
822 1.25 mlelstv if (realpath(file, path + 4) == NULL) {
823 1.34 christos if (!quiet)
824 1.34 christos warn("realpath `%s'", file);
825 1.25 mlelstv linkfail = 1;
826 1.25 mlelstv l = 0;
827 1.25 mlelstv path[0] = '\0';
828 1.25 mlelstv }
829 1.27 atatat sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
830 1.36 apb prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
831 1.24 elad }
832 1.24 elad
833 1.24 elad formats = FMTF_STRING;
834 1.24 elad if (ofmt == 0)
835 1.24 elad ofmt = FMTF_STRING;
836 1.24 elad break;
837 1.1 atatat case SHOW_symlink:
838 1.1 atatat small = 0;
839 1.1 atatat data = 0;
840 1.1 atatat if (S_ISLNK(st->st_mode)) {
841 1.1 atatat snprintf(path, sizeof(path), " -> ");
842 1.9 provos l = readlink(file, path + 4, sizeof(path) - 4 - 1);
843 1.1 atatat if (l == -1) {
844 1.34 christos if (!quiet)
845 1.34 christos warn("readlink `%s'", file);
846 1.4 atatat linkfail = 1;
847 1.1 atatat l = 0;
848 1.1 atatat path[0] = '\0';
849 1.1 atatat }
850 1.1 atatat path[l + 4] = '\0';
851 1.5 atatat sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
852 1.36 apb prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
853 1.1 atatat }
854 1.4 atatat else {
855 1.4 atatat linkfail = 1;
856 1.1 atatat sdata = "";
857 1.4 atatat }
858 1.5 atatat formats = FMTF_STRING;
859 1.1 atatat if (ofmt == 0)
860 1.5 atatat ofmt = FMTF_STRING;
861 1.1 atatat break;
862 1.1 atatat case SHOW_filetype:
863 1.1 atatat small = 0;
864 1.1 atatat data = 0;
865 1.28 lukem sdata = "";
866 1.1 atatat if (hilo == 0 || hilo == LOW_PIECE) {
867 1.1 atatat switch (st->st_mode & S_IFMT) {
868 1.28 lukem case S_IFIFO: sdata = "|"; break;
869 1.28 lukem case S_IFDIR: sdata = "/"; break;
870 1.1 atatat case S_IFREG:
871 1.1 atatat if (st->st_mode &
872 1.1 atatat (S_IXUSR | S_IXGRP | S_IXOTH))
873 1.28 lukem sdata = "*";
874 1.1 atatat break;
875 1.28 lukem case S_IFLNK: sdata = "@"; break;
876 1.19 jmc #ifdef S_IFSOCK
877 1.28 lukem case S_IFSOCK: sdata = "="; break;
878 1.19 jmc #endif
879 1.13 atatat #ifdef S_IFWHT
880 1.28 lukem case S_IFWHT: sdata = "%"; break;
881 1.13 atatat #endif /* S_IFWHT */
882 1.13 atatat #ifdef S_IFDOOR
883 1.28 lukem case S_IFDOOR: sdata = ">"; break;
884 1.13 atatat #endif /* S_IFDOOR */
885 1.1 atatat }
886 1.1 atatat hilo = 0;
887 1.1 atatat }
888 1.1 atatat else if (hilo == HIGH_PIECE) {
889 1.1 atatat switch (st->st_mode & S_IFMT) {
890 1.1 atatat case S_IFIFO: sdata = "Fifo File"; break;
891 1.1 atatat case S_IFCHR: sdata = "Character Device"; break;
892 1.1 atatat case S_IFDIR: sdata = "Directory"; break;
893 1.1 atatat case S_IFBLK: sdata = "Block Device"; break;
894 1.1 atatat case S_IFREG: sdata = "Regular File"; break;
895 1.1 atatat case S_IFLNK: sdata = "Symbolic Link"; break;
896 1.19 jmc #ifdef S_IFSOCK
897 1.1 atatat case S_IFSOCK: sdata = "Socket"; break;
898 1.19 jmc #endif
899 1.13 atatat #ifdef S_IFWHT
900 1.1 atatat case S_IFWHT: sdata = "Whiteout File"; break;
901 1.13 atatat #endif /* S_IFWHT */
902 1.13 atatat #ifdef S_IFDOOR
903 1.13 atatat case S_IFDOOR: sdata = "Door"; break;
904 1.13 atatat #endif /* S_IFDOOR */
905 1.1 atatat default: sdata = "???"; break;
906 1.1 atatat }
907 1.1 atatat hilo = 0;
908 1.1 atatat }
909 1.5 atatat formats = FMTF_STRING;
910 1.1 atatat if (ofmt == 0)
911 1.5 atatat ofmt = FMTF_STRING;
912 1.1 atatat break;
913 1.1 atatat case SHOW_filename:
914 1.1 atatat small = 0;
915 1.1 atatat data = 0;
916 1.20 atatat if (file == NULL) {
917 1.31 dholland (void)strlcpy(path, "(stdin)", sizeof(path));
918 1.20 atatat if (hilo == HIGH_PIECE || hilo == LOW_PIECE)
919 1.20 atatat hilo = 0;
920 1.20 atatat }
921 1.20 atatat else if (hilo == 0)
922 1.31 dholland (void)strlcpy(path, file, sizeof(path));
923 1.20 atatat else {
924 1.20 atatat char *s;
925 1.31 dholland (void)strlcpy(path, file, sizeof(path));
926 1.20 atatat s = strrchr(path, '/');
927 1.20 atatat if (s != NULL) {
928 1.20 atatat /* trim off trailing /'s */
929 1.20 atatat while (s != path &&
930 1.20 atatat s[0] == '/' && s[1] == '\0')
931 1.20 atatat *s-- = '\0';
932 1.20 atatat s = strrchr(path, '/');
933 1.20 atatat }
934 1.20 atatat if (hilo == HIGH_PIECE) {
935 1.20 atatat if (s == NULL)
936 1.31 dholland (void)strlcpy(path, ".", sizeof(path));
937 1.20 atatat else {
938 1.20 atatat while (s != path && s[0] == '/')
939 1.20 atatat *s-- = '\0';
940 1.20 atatat }
941 1.20 atatat hilo = 0;
942 1.20 atatat }
943 1.20 atatat else if (hilo == LOW_PIECE) {
944 1.20 atatat if (s != NULL && s[1] != '\0')
945 1.31 dholland (void)strlcpy(path, s + 1,
946 1.20 atatat sizeof(path));
947 1.20 atatat hilo = 0;
948 1.20 atatat }
949 1.20 atatat }
950 1.1 atatat sdata = path;
951 1.5 atatat formats = FMTF_STRING;
952 1.1 atatat if (ofmt == 0)
953 1.5 atatat ofmt = FMTF_STRING;
954 1.1 atatat break;
955 1.1 atatat case SHOW_sizerdev:
956 1.1 atatat if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
957 1.1 atatat char majdev[20], mindev[20];
958 1.1 atatat int l1, l2;
959 1.1 atatat
960 1.1 atatat l1 = format1(st,
961 1.1 atatat file,
962 1.1 atatat fmt, flen,
963 1.1 atatat majdev, sizeof(majdev),
964 1.1 atatat flags, size, prec,
965 1.34 christos ofmt, HIGH_PIECE, SHOW_st_rdev, quiet);
966 1.1 atatat l2 = format1(st,
967 1.1 atatat file,
968 1.1 atatat fmt, flen,
969 1.1 atatat mindev, sizeof(mindev),
970 1.1 atatat flags, size, prec,
971 1.34 christos ofmt, LOW_PIECE, SHOW_st_rdev, quiet);
972 1.1 atatat return (snprintf(buf, blen, "%.*s,%.*s",
973 1.1 atatat l1, majdev, l2, mindev));
974 1.1 atatat }
975 1.1 atatat else {
976 1.1 atatat return (format1(st,
977 1.1 atatat file,
978 1.1 atatat fmt, flen,
979 1.1 atatat buf, blen,
980 1.1 atatat flags, size, prec,
981 1.34 christos ofmt, 0, SHOW_st_size, quiet));
982 1.1 atatat }
983 1.1 atatat /*NOTREACHED*/
984 1.1 atatat default:
985 1.1 atatat errx(1, "%.*s: bad format", (int)flen, fmt);
986 1.1 atatat }
987 1.1 atatat
988 1.1 atatat /*
989 1.1 atatat * If a subdatum was specified but not supported, or an output
990 1.1 atatat * format was selected that is not supported, that's an error.
991 1.1 atatat */
992 1.1 atatat if (hilo != 0 || (ofmt & formats) == 0)
993 1.1 atatat errx(1, "%.*s: bad format", (int)flen, fmt);
994 1.1 atatat
995 1.1 atatat /*
996 1.36 apb * FLAG_POUND with FMTF_STRING means use vis(3) encoding.
997 1.36 apb * First prefixlen chars are not encoded.
998 1.36 apb */
999 1.36 apb if ((flags & FLAG_POUND) != 0 && ofmt == FMTF_STRING) {
1000 1.36 apb flags &= !FLAG_POUND;
1001 1.36 apb strncpy(visbuf, sdata, prefixlen);
1002 1.36 apb strnvis(visbuf + prefixlen, sizeof(visbuf) - prefixlen,
1003 1.36 apb sdata + prefixlen, VIS_WHITE | VIS_OCTAL | VIS_CSTYLE);
1004 1.36 apb sdata = visbuf;
1005 1.36 apb }
1006 1.36 apb
1007 1.36 apb /*
1008 1.1 atatat * Assemble the format string for passing to printf(3).
1009 1.1 atatat */
1010 1.1 atatat lfmt[0] = '\0';
1011 1.1 atatat (void)strcat(lfmt, "%");
1012 1.2 atatat if (flags & FLAG_POUND)
1013 1.1 atatat (void)strcat(lfmt, "#");
1014 1.2 atatat if (flags & FLAG_SPACE)
1015 1.1 atatat (void)strcat(lfmt, " ");
1016 1.2 atatat if (flags & FLAG_PLUS)
1017 1.1 atatat (void)strcat(lfmt, "+");
1018 1.2 atatat if (flags & FLAG_MINUS)
1019 1.1 atatat (void)strcat(lfmt, "-");
1020 1.2 atatat if (flags & FLAG_ZERO)
1021 1.1 atatat (void)strcat(lfmt, "0");
1022 1.1 atatat
1023 1.1 atatat /*
1024 1.1 atatat * Only the timespecs support the FLOAT output format, and that
1025 1.1 atatat * requires work that differs from the other formats.
1026 1.1 atatat */
1027 1.5 atatat if (ofmt == FMTF_FLOAT) {
1028 1.1 atatat /*
1029 1.1 atatat * Nothing after the decimal point, so just print seconds.
1030 1.1 atatat */
1031 1.1 atatat if (prec == 0) {
1032 1.1 atatat if (size != -1) {
1033 1.1 atatat (void)snprintf(tmp, sizeof(tmp), "%d", size);
1034 1.1 atatat (void)strcat(lfmt, tmp);
1035 1.1 atatat }
1036 1.29 dholland (void)strcat(lfmt, "lld");
1037 1.29 dholland return (snprintf(buf, blen, lfmt,
1038 1.29 dholland (long long)secs));
1039 1.1 atatat }
1040 1.1 atatat
1041 1.1 atatat /*
1042 1.1 atatat * Unspecified precision gets all the precision we have:
1043 1.1 atatat * 9 digits.
1044 1.1 atatat */
1045 1.1 atatat if (prec == -1)
1046 1.1 atatat prec = 9;
1047 1.1 atatat
1048 1.1 atatat /*
1049 1.1 atatat * Adjust the size for the decimal point and the digits
1050 1.1 atatat * that will follow.
1051 1.1 atatat */
1052 1.1 atatat size -= prec + 1;
1053 1.1 atatat
1054 1.1 atatat /*
1055 1.1 atatat * Any leftover size that's legitimate will be used.
1056 1.1 atatat */
1057 1.1 atatat if (size > 0) {
1058 1.1 atatat (void)snprintf(tmp, sizeof(tmp), "%d", size);
1059 1.1 atatat (void)strcat(lfmt, tmp);
1060 1.1 atatat }
1061 1.30 dholland /* Seconds: time_t cast to long long. */
1062 1.29 dholland (void)strcat(lfmt, "lld");
1063 1.1 atatat
1064 1.1 atatat /*
1065 1.1 atatat * The stuff after the decimal point always needs zero
1066 1.1 atatat * filling.
1067 1.1 atatat */
1068 1.1 atatat (void)strcat(lfmt, ".%0");
1069 1.1 atatat
1070 1.1 atatat /*
1071 1.1 atatat * We can "print" at most nine digits of precision. The
1072 1.1 atatat * rest we will pad on at the end.
1073 1.30 dholland *
1074 1.30 dholland * Nanoseconds: long.
1075 1.1 atatat */
1076 1.29 dholland (void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec);
1077 1.1 atatat (void)strcat(lfmt, tmp);
1078 1.1 atatat
1079 1.1 atatat /*
1080 1.1 atatat * For precision of less that nine digits, trim off the
1081 1.1 atatat * less significant figures.
1082 1.1 atatat */
1083 1.1 atatat for (; prec < 9; prec++)
1084 1.14 chs nsecs /= 10;
1085 1.1 atatat
1086 1.1 atatat /*
1087 1.1 atatat * Use the format, and then tack on any zeroes that
1088 1.1 atatat * might be required to make up the requested precision.
1089 1.1 atatat */
1090 1.29 dholland l = snprintf(buf, blen, lfmt, (long long)secs, nsecs);
1091 1.28 lukem for (; prec > 9 && l < (int)blen; prec--, l++)
1092 1.1 atatat (void)strcat(buf, "0");
1093 1.1 atatat return (l);
1094 1.1 atatat }
1095 1.1 atatat
1096 1.1 atatat /*
1097 1.1 atatat * Add on size and precision, if specified, to the format.
1098 1.1 atatat */
1099 1.1 atatat if (size != -1) {
1100 1.1 atatat (void)snprintf(tmp, sizeof(tmp), "%d", size);
1101 1.1 atatat (void)strcat(lfmt, tmp);
1102 1.1 atatat }
1103 1.1 atatat if (prec != -1) {
1104 1.1 atatat (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1105 1.1 atatat (void)strcat(lfmt, tmp);
1106 1.1 atatat }
1107 1.1 atatat
1108 1.1 atatat /*
1109 1.1 atatat * String output uses the temporary sdata.
1110 1.1 atatat */
1111 1.5 atatat if (ofmt == FMTF_STRING) {
1112 1.1 atatat if (sdata == NULL)
1113 1.1 atatat errx(1, "%.*s: bad format", (int)flen, fmt);
1114 1.1 atatat (void)strcat(lfmt, "s");
1115 1.1 atatat return (snprintf(buf, blen, lfmt, sdata));
1116 1.1 atatat }
1117 1.1 atatat
1118 1.1 atatat /*
1119 1.1 atatat * Ensure that sign extension does not cause bad looking output
1120 1.1 atatat * for some forms.
1121 1.1 atatat */
1122 1.5 atatat if (small && ofmt != FMTF_DECIMAL)
1123 1.1 atatat data = (u_int32_t)data;
1124 1.1 atatat
1125 1.1 atatat /*
1126 1.1 atatat * The four "numeric" output forms.
1127 1.1 atatat */
1128 1.1 atatat (void)strcat(lfmt, "ll");
1129 1.1 atatat switch (ofmt) {
1130 1.5 atatat case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1131 1.23 atatat case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1132 1.5 atatat case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1133 1.5 atatat case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1134 1.1 atatat }
1135 1.1 atatat
1136 1.23 atatat /*
1137 1.23 atatat * shift and round to nearest for kilobytes, megabytes,
1138 1.23 atatat * gigabytes.
1139 1.23 atatat */
1140 1.23 atatat if (shift > 0) {
1141 1.23 atatat data >>= (shift - 1);
1142 1.23 atatat data++;
1143 1.23 atatat data >>= 1;
1144 1.23 atatat }
1145 1.23 atatat
1146 1.1 atatat return (snprintf(buf, blen, lfmt, data));
1147 1.1 atatat }
1148