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