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