1*fa7c2eeeSguenther /* $OpenBSD: stat.c,v 1.25 2023/08/06 19:36:13 guenther Exp $ */
2599b0986Sotto /* $NetBSD: stat.c,v 1.19 2004/06/20 22:20:16 jmc Exp $ */
3599b0986Sotto
4599b0986Sotto /*
5599b0986Sotto * Copyright (c) 2002 The NetBSD Foundation, Inc.
6599b0986Sotto * All rights reserved.
7599b0986Sotto *
8599b0986Sotto * This code is derived from software contributed to The NetBSD Foundation
9599b0986Sotto * by Andrew Brown.
10599b0986Sotto *
11599b0986Sotto * Redistribution and use in source and binary forms, with or without
12599b0986Sotto * modification, are permitted provided that the following conditions
13599b0986Sotto * are met:
14599b0986Sotto * 1. Redistributions of source code must retain the above copyright
15599b0986Sotto * notice, this list of conditions and the following disclaimer.
16599b0986Sotto * 2. Redistributions in binary form must reproduce the above copyright
17599b0986Sotto * notice, this list of conditions and the following disclaimer in the
18599b0986Sotto * documentation and/or other materials provided with the distribution.
19599b0986Sotto *
20599b0986Sotto * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21599b0986Sotto * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22599b0986Sotto * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23599b0986Sotto * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24599b0986Sotto * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25599b0986Sotto * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26599b0986Sotto * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27599b0986Sotto * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28599b0986Sotto * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29599b0986Sotto * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30599b0986Sotto * POSSIBILITY OF SUCH DAMAGE.
31599b0986Sotto */
32599b0986Sotto
33599b0986Sotto #include <sys/types.h>
34599b0986Sotto #include <sys/stat.h>
35599b0986Sotto
36599b0986Sotto #include <ctype.h>
37599b0986Sotto #include <err.h>
38599b0986Sotto #include <errno.h>
39599b0986Sotto #include <grp.h>
40599b0986Sotto #include <limits.h>
41599b0986Sotto #include <pwd.h>
42599b0986Sotto #include <stdio.h>
43599b0986Sotto #include <stdlib.h>
44599b0986Sotto #include <string.h>
45599b0986Sotto #include <time.h>
46599b0986Sotto #include <unistd.h>
47599b0986Sotto
48599b0986Sotto #define DEF_FORMAT \
490cdfeff1Sotto "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " \
50794cb5bbSotto "%k %b %#Xf %N"
510cdfeff1Sotto #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " \
52794cb5bbSotto "%k %b %f %N"
53599b0986Sotto #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
54599b0986Sotto #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
55599b0986Sotto #define SHELL_FORMAT \
56599b0986Sotto "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
57599b0986Sotto "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
580cdfeff1Sotto "st_atime=%a st_mtime=%m st_ctime=%c " \
59794cb5bbSotto "st_blksize=%k st_blocks=%b st_flags=%f"
60599b0986Sotto #define LINUX_FORMAT \
61599b0986Sotto " File: \"%N\"%n" \
62599b0986Sotto " Size: %-11z FileType: %HT%n" \
632d13ed19Sguenther " Mode: (%01Mp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
64599b0986Sotto "Device: %Hd,%Ld Inode: %i Links: %l%n" \
65599b0986Sotto "Access: %Sa%n" \
66599b0986Sotto "Modify: %Sm%n" \
67599b0986Sotto "Change: %Sc"
68599b0986Sotto
69599b0986Sotto #define TIME_FORMAT "%b %e %T %Y"
70599b0986Sotto
71599b0986Sotto #define FLAG_POUND 0x01
72599b0986Sotto #define FLAG_SPACE 0x02
73599b0986Sotto #define FLAG_PLUS 0x04
74599b0986Sotto #define FLAG_ZERO 0x08
75599b0986Sotto #define FLAG_MINUS 0x10
76599b0986Sotto
77599b0986Sotto /*
78599b0986Sotto * These format characters must all be unique, except the magic one.
79599b0986Sotto */
80599b0986Sotto #define FMT_MAGIC '%'
81599b0986Sotto #define FMT_DOT '.'
82599b0986Sotto
83599b0986Sotto #define SIMPLE_NEWLINE 'n'
84599b0986Sotto #define SIMPLE_TAB 't'
85599b0986Sotto #define SIMPLE_PERCENT '%'
86599b0986Sotto #define SIMPLE_NUMBER '@'
87599b0986Sotto
88599b0986Sotto #define FMT_POUND '#'
89599b0986Sotto #define FMT_SPACE ' '
90599b0986Sotto #define FMT_PLUS '+'
91599b0986Sotto #define FMT_ZERO '0'
92599b0986Sotto #define FMT_MINUS '-'
93599b0986Sotto
94599b0986Sotto #define FMT_DECIMAL 'D'
95599b0986Sotto #define FMT_OCTAL 'O'
96599b0986Sotto #define FMT_UNSIGNED 'U'
97599b0986Sotto #define FMT_HEX 'X'
98599b0986Sotto #define FMT_FLOAT 'F'
99599b0986Sotto #define FMT_STRING 'S'
100599b0986Sotto
101599b0986Sotto #define FMTF_DECIMAL 0x01
102599b0986Sotto #define FMTF_OCTAL 0x02
103599b0986Sotto #define FMTF_UNSIGNED 0x04
104599b0986Sotto #define FMTF_HEX 0x08
105599b0986Sotto #define FMTF_FLOAT 0x10
106599b0986Sotto #define FMTF_STRING 0x20
107599b0986Sotto
108599b0986Sotto #define HIGH_PIECE 'H'
109599b0986Sotto #define MIDDLE_PIECE 'M'
110599b0986Sotto #define LOW_PIECE 'L'
111599b0986Sotto
112599b0986Sotto #define SHOW_st_dev 'd'
113599b0986Sotto #define SHOW_st_ino 'i'
114599b0986Sotto #define SHOW_st_mode 'p'
115599b0986Sotto #define SHOW_st_nlink 'l'
116599b0986Sotto #define SHOW_st_uid 'u'
117599b0986Sotto #define SHOW_st_gid 'g'
118599b0986Sotto #define SHOW_st_rdev 'r'
119599b0986Sotto #define SHOW_st_atime 'a'
120599b0986Sotto #define SHOW_st_mtime 'm'
121599b0986Sotto #define SHOW_st_ctime 'c'
122599b0986Sotto #define SHOW_st_btime 'B'
123599b0986Sotto #define SHOW_st_size 'z'
124599b0986Sotto #define SHOW_st_blocks 'b'
125599b0986Sotto #define SHOW_st_blksize 'k'
126599b0986Sotto #define SHOW_st_flags 'f'
127599b0986Sotto #define SHOW_st_gen 'v'
128599b0986Sotto #define SHOW_symlink 'Y'
129599b0986Sotto #define SHOW_filetype 'T'
130599b0986Sotto #define SHOW_filename 'N'
131599b0986Sotto #define SHOW_sizerdev 'Z'
132599b0986Sotto
133599b0986Sotto void usage(const char *);
134599b0986Sotto void output(const struct stat *, const char *,
1350a405068Sderaadt const char *, int, int);
136599b0986Sotto int format1(const struct stat *, /* stat info */
137599b0986Sotto const char *, /* the file name */
138599b0986Sotto const char *, int, /* the format string itself */
139599b0986Sotto char *, size_t, /* a place to put the output */
140599b0986Sotto int, int, int, int, /* the parsed format */
141599b0986Sotto int, int);
142599b0986Sotto
143599b0986Sotto char *timefmt;
144599b0986Sotto
145599b0986Sotto #define addchar(s, c, nl) \
146599b0986Sotto do { \
147599b0986Sotto (void)fputc((c), (s)); \
148599b0986Sotto (*nl) = ((c) == '\n'); \
149599b0986Sotto } while (0/*CONSTCOND*/)
150599b0986Sotto
151599b0986Sotto extern char *__progname;
152599b0986Sotto
153599b0986Sotto int
main(int argc,char * argv[])154599b0986Sotto main(int argc, char *argv[])
155599b0986Sotto {
156599b0986Sotto struct stat st;
1570a405068Sderaadt int ch, rc, errs;
158599b0986Sotto int lsF, fmtchar, usestat, fn, nonl, quiet;
159599b0986Sotto char *statfmt, *options, *synopsis;
160599b0986Sotto
161ecfe3494Sderaadt if (pledge("stdio rpath getpw", NULL) == -1)
1620bd1216cSderaadt err(1, "pledge");
163cc7a658aSderaadt
164599b0986Sotto lsF = 0;
165599b0986Sotto fmtchar = '\0';
166599b0986Sotto usestat = 0;
167599b0986Sotto nonl = 0;
168599b0986Sotto quiet = 0;
169599b0986Sotto statfmt = NULL;
170599b0986Sotto timefmt = NULL;
171599b0986Sotto
172599b0986Sotto options = "f:FlLnqrst:x";
17376f9bc4aSsobrado synopsis = "[-FLnq] [-f format | -l | -r | -s | -x] "
17476f9bc4aSsobrado "[-t timefmt] [file ...]";
175599b0986Sotto
176599b0986Sotto while ((ch = getopt(argc, argv, options)) != -1)
177599b0986Sotto switch (ch) {
178599b0986Sotto case 'F':
179599b0986Sotto lsF = 1;
180599b0986Sotto break;
181599b0986Sotto case 'L':
182599b0986Sotto usestat = 1;
183599b0986Sotto break;
184599b0986Sotto case 'n':
185599b0986Sotto nonl = 1;
186599b0986Sotto break;
187599b0986Sotto case 'q':
188599b0986Sotto quiet = 1;
189599b0986Sotto break;
190599b0986Sotto case 'f':
191599b0986Sotto statfmt = optarg;
192599b0986Sotto /* FALLTHROUGH */
193599b0986Sotto case 'l':
194599b0986Sotto case 'r':
195599b0986Sotto case 's':
196599b0986Sotto case 'x':
197599b0986Sotto if (fmtchar != 0)
198599b0986Sotto errx(1, "can't use format '%c' with '%c'",
199599b0986Sotto fmtchar, ch);
200599b0986Sotto fmtchar = ch;
201599b0986Sotto break;
202599b0986Sotto case 't':
203599b0986Sotto timefmt = optarg;
204599b0986Sotto break;
205599b0986Sotto default:
206599b0986Sotto usage(synopsis);
207599b0986Sotto }
208599b0986Sotto
209599b0986Sotto argc -= optind;
210599b0986Sotto argv += optind;
211599b0986Sotto fn = 1;
212599b0986Sotto
213599b0986Sotto if (fmtchar == '\0') {
214599b0986Sotto if (lsF)
215599b0986Sotto fmtchar = 'l';
216599b0986Sotto else {
217599b0986Sotto fmtchar = 'f';
218599b0986Sotto statfmt = DEF_FORMAT;
219599b0986Sotto }
220599b0986Sotto }
221599b0986Sotto
222599b0986Sotto if (lsF && fmtchar != 'l')
223599b0986Sotto errx(1, "can't use format '%c' with -F", fmtchar);
224599b0986Sotto
225599b0986Sotto switch (fmtchar) {
226599b0986Sotto case 'f':
227599b0986Sotto /* statfmt already set */
228599b0986Sotto break;
229599b0986Sotto case 'l':
230599b0986Sotto statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
231599b0986Sotto break;
232599b0986Sotto case 'r':
233599b0986Sotto statfmt = RAW_FORMAT;
234599b0986Sotto break;
235599b0986Sotto case 's':
236599b0986Sotto statfmt = SHELL_FORMAT;
237599b0986Sotto break;
238599b0986Sotto case 'x':
239599b0986Sotto statfmt = LINUX_FORMAT;
240599b0986Sotto if (timefmt == NULL)
241599b0986Sotto timefmt = "%c";
242599b0986Sotto break;
243599b0986Sotto default:
244599b0986Sotto usage(synopsis);
245599b0986Sotto /*NOTREACHED*/
246599b0986Sotto }
247599b0986Sotto
248599b0986Sotto if (timefmt == NULL)
249599b0986Sotto timefmt = TIME_FORMAT;
250599b0986Sotto
251599b0986Sotto errs = 0;
252599b0986Sotto do {
253599b0986Sotto if (argc == 0)
254599b0986Sotto rc = fstat(STDIN_FILENO, &st);
255599b0986Sotto else if (usestat) {
256599b0986Sotto /*
257599b0986Sotto * Try stat() and if it fails, fall back to
258599b0986Sotto * lstat() just in case we're examining a
259599b0986Sotto * broken symlink.
260599b0986Sotto */
261599b0986Sotto if ((rc = stat(argv[0], &st)) == -1 &&
262599b0986Sotto errno == ENOENT &&
263599b0986Sotto (rc = lstat(argv[0], &st)) == -1)
264599b0986Sotto errno = ENOENT;
26527f2b7fcSderaadt } else
266599b0986Sotto rc = lstat(argv[0], &st);
267599b0986Sotto
268599b0986Sotto if (rc == -1) {
269599b0986Sotto errs = 1;
270599b0986Sotto if (!quiet)
271f8227edbSpedro warn("%s",
272599b0986Sotto argc == 0 ? "(stdin)" : argv[0]);
27327f2b7fcSderaadt } else
2740a405068Sderaadt output(&st, argv[0], statfmt, fn, nonl);
275599b0986Sotto
276599b0986Sotto argv++;
277599b0986Sotto argc--;
278599b0986Sotto fn++;
279599b0986Sotto } while (argc > 0);
280599b0986Sotto
2810a405068Sderaadt return (errs);
282599b0986Sotto }
283599b0986Sotto
284599b0986Sotto void
usage(const char * synopsis)285599b0986Sotto usage(const char *synopsis)
286599b0986Sotto {
287599b0986Sotto
288599b0986Sotto (void)fprintf(stderr, "usage: %s %s\n", __progname, synopsis);
289599b0986Sotto exit(1);
290599b0986Sotto }
291599b0986Sotto
292599b0986Sotto /*
293599b0986Sotto * Parses a format string.
294599b0986Sotto */
295599b0986Sotto void
output(const struct stat * st,const char * file,const char * statfmt,int fn,int nonl)296599b0986Sotto output(const struct stat *st, const char *file,
2970a405068Sderaadt const char *statfmt, int fn, int nonl)
298599b0986Sotto {
299599b0986Sotto int flags, size, prec, ofmt, hilo, what;
300e3eef151Sotto char buf[PATH_MAX + 4 + 1];
301599b0986Sotto const char *subfmt;
302599b0986Sotto int nl, t, i;
303599b0986Sotto
304599b0986Sotto nl = 1;
305599b0986Sotto while (*statfmt != '\0') {
306599b0986Sotto
307599b0986Sotto /*
308599b0986Sotto * Non-format characters go straight out.
309599b0986Sotto */
310599b0986Sotto if (*statfmt != FMT_MAGIC) {
311599b0986Sotto addchar(stdout, *statfmt, &nl);
312599b0986Sotto statfmt++;
313599b0986Sotto continue;
314599b0986Sotto }
315599b0986Sotto
316599b0986Sotto /*
317599b0986Sotto * The current format "substring" starts here,
318599b0986Sotto * and then we skip the magic.
319599b0986Sotto */
320599b0986Sotto subfmt = statfmt;
321599b0986Sotto statfmt++;
322599b0986Sotto
323599b0986Sotto /*
324599b0986Sotto * Some simple one-character "formats".
325599b0986Sotto */
326599b0986Sotto switch (*statfmt) {
327599b0986Sotto case SIMPLE_NEWLINE:
328599b0986Sotto addchar(stdout, '\n', &nl);
329599b0986Sotto statfmt++;
330599b0986Sotto continue;
331599b0986Sotto case SIMPLE_TAB:
332599b0986Sotto addchar(stdout, '\t', &nl);
333599b0986Sotto statfmt++;
334599b0986Sotto continue;
335599b0986Sotto case SIMPLE_PERCENT:
336599b0986Sotto addchar(stdout, '%', &nl);
337599b0986Sotto statfmt++;
338599b0986Sotto continue;
339599b0986Sotto case SIMPLE_NUMBER: {
340599b0986Sotto char num[12], *p;
341599b0986Sotto
342599b0986Sotto snprintf(num, sizeof(num), "%d", fn);
343599b0986Sotto for (p = &num[0]; *p; p++)
344599b0986Sotto addchar(stdout, *p, &nl);
345599b0986Sotto statfmt++;
346599b0986Sotto continue;
347599b0986Sotto }
348599b0986Sotto }
349599b0986Sotto
350599b0986Sotto /*
351599b0986Sotto * This must be an actual format string. Format strings are
352599b0986Sotto * similar to printf(3) formats up to a point, and are of
353599b0986Sotto * the form:
354599b0986Sotto *
355599b0986Sotto * % required start of format
356599b0986Sotto * [-# +0] opt. format characters
357599b0986Sotto * size opt. field width
358599b0986Sotto * . opt. decimal separator, followed by
359599b0986Sotto * prec opt. precision
360599b0986Sotto * fmt opt. output specifier (string, numeric, etc.)
361599b0986Sotto * sub opt. sub field specifier (high, middle, low)
362599b0986Sotto * datum required field specifier (size, mode, etc)
363599b0986Sotto *
364599b0986Sotto * Only the % and the datum selector are required. All data
365599b0986Sotto * have reasonable default output forms. The "sub" specifier
366599b0986Sotto * only applies to certain data (mode, dev, rdev, filetype).
367599b0986Sotto * The symlink output defaults to STRING, yet will only emit
368599b0986Sotto * the leading " -> " if STRING is explicitly specified. The
369599b0986Sotto * sizerdev datum will generate rdev output for character or
370599b0986Sotto * block devices, and size output for all others.
371599b0986Sotto */
372599b0986Sotto flags = 0;
373599b0986Sotto do {
374599b0986Sotto if (*statfmt == FMT_POUND)
375599b0986Sotto flags |= FLAG_POUND;
376599b0986Sotto else if (*statfmt == FMT_SPACE)
377599b0986Sotto flags |= FLAG_SPACE;
378599b0986Sotto else if (*statfmt == FMT_PLUS)
379599b0986Sotto flags |= FLAG_PLUS;
380599b0986Sotto else if (*statfmt == FMT_ZERO)
381599b0986Sotto flags |= FLAG_ZERO;
382599b0986Sotto else if (*statfmt == FMT_MINUS)
383599b0986Sotto flags |= FLAG_MINUS;
384599b0986Sotto else
385599b0986Sotto break;
386599b0986Sotto statfmt++;
387599b0986Sotto } while (1/*CONSTCOND*/);
388599b0986Sotto
389599b0986Sotto size = -1;
39028ce9ad7Sderaadt if (isdigit((unsigned char)*statfmt)) {
391599b0986Sotto size = 0;
39228ce9ad7Sderaadt while (isdigit((unsigned char)*statfmt)) {
393599b0986Sotto size = (size * 10) + (*statfmt - '0');
394599b0986Sotto statfmt++;
395599b0986Sotto if (size < 0)
396599b0986Sotto goto badfmt;
397599b0986Sotto }
398599b0986Sotto }
399599b0986Sotto
400599b0986Sotto prec = -1;
401599b0986Sotto if (*statfmt == FMT_DOT) {
402599b0986Sotto statfmt++;
403599b0986Sotto
404599b0986Sotto prec = 0;
40528ce9ad7Sderaadt while (isdigit((unsigned char)*statfmt)) {
406599b0986Sotto prec = (prec * 10) + (*statfmt - '0');
407599b0986Sotto statfmt++;
408599b0986Sotto if (prec < 0)
409599b0986Sotto goto badfmt;
410599b0986Sotto }
411599b0986Sotto }
412599b0986Sotto
413599b0986Sotto #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
414599b0986Sotto #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
415599b0986Sotto switch (*statfmt) {
416599b0986Sotto fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
417599b0986Sotto fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
418599b0986Sotto fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
419599b0986Sotto fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
420599b0986Sotto fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
421599b0986Sotto fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
422599b0986Sotto default:
423599b0986Sotto ofmt = 0;
424599b0986Sotto break;
425599b0986Sotto }
426599b0986Sotto
427599b0986Sotto switch (*statfmt) {
428599b0986Sotto fmtcase(hilo, HIGH_PIECE);
429599b0986Sotto fmtcase(hilo, MIDDLE_PIECE);
430599b0986Sotto fmtcase(hilo, LOW_PIECE);
431599b0986Sotto default:
432599b0986Sotto hilo = 0;
433599b0986Sotto break;
434599b0986Sotto }
435599b0986Sotto
436599b0986Sotto switch (*statfmt) {
437599b0986Sotto fmtcase(what, SHOW_st_dev);
438599b0986Sotto fmtcase(what, SHOW_st_ino);
439599b0986Sotto fmtcase(what, SHOW_st_mode);
440599b0986Sotto fmtcase(what, SHOW_st_nlink);
441599b0986Sotto fmtcase(what, SHOW_st_uid);
442599b0986Sotto fmtcase(what, SHOW_st_gid);
443599b0986Sotto fmtcase(what, SHOW_st_rdev);
444599b0986Sotto fmtcase(what, SHOW_st_atime);
445599b0986Sotto fmtcase(what, SHOW_st_mtime);
446599b0986Sotto fmtcase(what, SHOW_st_ctime);
447599b0986Sotto fmtcase(what, SHOW_st_btime);
448599b0986Sotto fmtcase(what, SHOW_st_size);
449599b0986Sotto fmtcase(what, SHOW_st_blocks);
450599b0986Sotto fmtcase(what, SHOW_st_blksize);
451599b0986Sotto fmtcase(what, SHOW_st_flags);
452599b0986Sotto fmtcase(what, SHOW_st_gen);
453599b0986Sotto fmtcase(what, SHOW_symlink);
454599b0986Sotto fmtcase(what, SHOW_filetype);
455599b0986Sotto fmtcase(what, SHOW_filename);
456599b0986Sotto fmtcase(what, SHOW_sizerdev);
457599b0986Sotto default:
458599b0986Sotto goto badfmt;
459599b0986Sotto }
460599b0986Sotto #undef fmtcasef
461599b0986Sotto #undef fmtcase
462599b0986Sotto
46327f2b7fcSderaadt t = format1(st, file, subfmt, statfmt - subfmt, buf,
46427f2b7fcSderaadt sizeof(buf), flags, size, prec, ofmt, hilo, what);
465599b0986Sotto
466e3eef151Sotto for (i = 0; i < t && i < sizeof(buf) - 1; i++)
467599b0986Sotto addchar(stdout, buf[i], &nl);
468599b0986Sotto
469599b0986Sotto continue;
470599b0986Sotto
471599b0986Sotto badfmt:
472599b0986Sotto errx(1, "%.*s: bad format",
473599b0986Sotto (int)(statfmt - subfmt + 1), subfmt);
474599b0986Sotto }
475599b0986Sotto
476599b0986Sotto if (!nl && !nonl)
477599b0986Sotto (void)fputc('\n', stdout);
478599b0986Sotto (void)fflush(stdout);
479599b0986Sotto }
480599b0986Sotto
481599b0986Sotto /*
482599b0986Sotto * Arranges output according to a single parsed format substring.
483599b0986Sotto */
484599b0986Sotto int
format1(const struct stat * st,const char * file,const char * fmt,int flen,char * buf,size_t blen,int flags,int size,int prec,int ofmt,int hilo,int what)485599b0986Sotto format1(const struct stat *st,
486599b0986Sotto const char *file,
487599b0986Sotto const char *fmt, int flen,
488599b0986Sotto char *buf, size_t blen,
489599b0986Sotto int flags, int size, int prec, int ofmt,
490599b0986Sotto int hilo, int what)
491599b0986Sotto {
492599b0986Sotto u_int64_t data;
49302f16a9dSmillert char lfmt[24], tmp[20];
494599b0986Sotto char smode[12], sid[12], path[PATH_MAX + 4];
49502f16a9dSmillert const char *sdata;
496599b0986Sotto struct tm *tm;
497599b0986Sotto time_t secs;
498599b0986Sotto long nsecs;
499f35fea8bSderaadt int l, small, formats, gottime, n;
500599b0986Sotto
501599b0986Sotto formats = 0;
502599b0986Sotto small = 0;
503599b0986Sotto gottime = 0;
504599b0986Sotto secs = 0;
505599b0986Sotto nsecs = 0;
506599b0986Sotto
507599b0986Sotto /*
508599b0986Sotto * First, pick out the data and tweak it based on hilo or
509599b0986Sotto * specified output format (symlink output only).
510599b0986Sotto */
511599b0986Sotto switch (what) {
512599b0986Sotto case SHOW_st_dev:
513599b0986Sotto case SHOW_st_rdev:
514599b0986Sotto small = (sizeof(st->st_dev) == 4);
515599b0986Sotto data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
516599b0986Sotto sdata = (what == SHOW_st_dev) ?
517599b0986Sotto devname(st->st_dev, S_IFBLK) :
518599b0986Sotto devname(st->st_rdev,
519599b0986Sotto S_ISCHR(st->st_mode) ? S_IFCHR :
520599b0986Sotto S_ISBLK(st->st_mode) ? S_IFBLK :
521599b0986Sotto 0U);
522599b0986Sotto if (sdata == NULL)
523599b0986Sotto sdata = "???";
524599b0986Sotto if (hilo == HIGH_PIECE) {
525599b0986Sotto data = major(data);
526599b0986Sotto hilo = 0;
52727f2b7fcSderaadt } else if (hilo == LOW_PIECE) {
528599b0986Sotto data = minor((unsigned)data);
529599b0986Sotto hilo = 0;
530599b0986Sotto }
531599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
532599b0986Sotto FMTF_STRING;
533599b0986Sotto if (ofmt == 0)
534599b0986Sotto ofmt = FMTF_UNSIGNED;
535599b0986Sotto break;
536599b0986Sotto case SHOW_st_ino:
537599b0986Sotto small = (sizeof(st->st_ino) == 4);
538599b0986Sotto data = st->st_ino;
539599b0986Sotto sdata = NULL;
540599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
541599b0986Sotto if (ofmt == 0)
542599b0986Sotto ofmt = FMTF_UNSIGNED;
543599b0986Sotto break;
544599b0986Sotto case SHOW_st_mode:
545599b0986Sotto small = (sizeof(st->st_mode) == 4);
546599b0986Sotto data = st->st_mode;
547599b0986Sotto strmode(st->st_mode, smode);
54802f16a9dSmillert l = strlen(smode);
54902f16a9dSmillert if (smode[l - 1] == ' ')
55002f16a9dSmillert smode[--l] = '\0';
55102f16a9dSmillert switch (hilo) {
55202f16a9dSmillert case HIGH_PIECE:
553599b0986Sotto data >>= 12;
55402f16a9dSmillert smode[4] = '\0';
55502f16a9dSmillert sdata = smode + 1;
55602f16a9dSmillert break;
55702f16a9dSmillert case MIDDLE_PIECE:
558599b0986Sotto data = (data >> 9) & 07;
55902f16a9dSmillert smode[7] = '\0';
56002f16a9dSmillert sdata = smode + 4;
56102f16a9dSmillert break;
56202f16a9dSmillert case LOW_PIECE:
563599b0986Sotto data &= 0777;
56402f16a9dSmillert smode[10] = '\0';
56502f16a9dSmillert sdata = smode + 7;
56602f16a9dSmillert break;
56702f16a9dSmillert default:
56802f16a9dSmillert sdata = smode;
56902f16a9dSmillert break;
570599b0986Sotto }
57102f16a9dSmillert hilo = 0;
572599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
573599b0986Sotto FMTF_STRING;
574599b0986Sotto if (ofmt == 0)
575599b0986Sotto ofmt = FMTF_OCTAL;
576599b0986Sotto break;
577599b0986Sotto case SHOW_st_nlink:
578599b0986Sotto small = (sizeof(st->st_dev) == 4);
579599b0986Sotto data = st->st_nlink;
580599b0986Sotto sdata = NULL;
581599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
582599b0986Sotto if (ofmt == 0)
583599b0986Sotto ofmt = FMTF_UNSIGNED;
584599b0986Sotto break;
585599b0986Sotto case SHOW_st_uid:
586599b0986Sotto small = (sizeof(st->st_uid) == 4);
587599b0986Sotto data = st->st_uid;
58802f16a9dSmillert sdata = user_from_uid(st->st_uid, 1);
58902f16a9dSmillert if (sdata == NULL) {
590599b0986Sotto snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
591599b0986Sotto sdata = sid;
592599b0986Sotto }
593599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
594599b0986Sotto FMTF_STRING;
595599b0986Sotto if (ofmt == 0)
596599b0986Sotto ofmt = FMTF_UNSIGNED;
597599b0986Sotto break;
598599b0986Sotto case SHOW_st_gid:
599599b0986Sotto small = (sizeof(st->st_gid) == 4);
600599b0986Sotto data = st->st_gid;
601f102d685Stb sdata = group_from_gid(st->st_gid, 1);
60202f16a9dSmillert if (sdata == NULL) {
603599b0986Sotto snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
604599b0986Sotto sdata = sid;
605599b0986Sotto }
606599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
607599b0986Sotto FMTF_STRING;
608599b0986Sotto if (ofmt == 0)
609599b0986Sotto ofmt = FMTF_UNSIGNED;
610599b0986Sotto break;
611599b0986Sotto case SHOW_st_atime:
612599b0986Sotto gottime = 1;
613599b0986Sotto secs = st->st_atime;
614*fa7c2eeeSguenther nsecs = st->st_atim.tv_nsec;
615599b0986Sotto /* FALLTHROUGH */
616599b0986Sotto case SHOW_st_mtime:
617599b0986Sotto if (!gottime) {
618599b0986Sotto gottime = 1;
619599b0986Sotto secs = st->st_mtime;
620*fa7c2eeeSguenther nsecs = st->st_mtim.tv_nsec;
621599b0986Sotto }
622599b0986Sotto /* FALLTHROUGH */
623599b0986Sotto case SHOW_st_ctime:
624599b0986Sotto if (!gottime) {
625599b0986Sotto gottime = 1;
626599b0986Sotto secs = st->st_ctime;
627*fa7c2eeeSguenther nsecs = st->st_ctim.tv_nsec;
628599b0986Sotto }
629599b0986Sotto /* FALLTHROUGH */
630599b0986Sotto case SHOW_st_btime:
631599b0986Sotto if (!gottime) {
632599b0986Sotto gottime = 1;
633*fa7c2eeeSguenther secs = st->__st_birthtime;
634*fa7c2eeeSguenther nsecs = st->__st_birthtim.tv_nsec;
635599b0986Sotto }
636599b0986Sotto small = (sizeof(secs) == 4);
637599b0986Sotto data = secs;
638599b0986Sotto tm = localtime(&secs);
639599b0986Sotto (void)strftime(path, sizeof(path), timefmt, tm);
640599b0986Sotto sdata = path;
641599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
642599b0986Sotto FMTF_FLOAT | FMTF_STRING;
643599b0986Sotto if (ofmt == 0)
644599b0986Sotto ofmt = FMTF_DECIMAL;
645599b0986Sotto break;
646599b0986Sotto case SHOW_st_size:
647599b0986Sotto small = (sizeof(st->st_size) == 4);
648599b0986Sotto data = st->st_size;
649599b0986Sotto sdata = NULL;
650599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
651599b0986Sotto if (ofmt == 0)
652599b0986Sotto ofmt = FMTF_UNSIGNED;
653599b0986Sotto break;
654599b0986Sotto case SHOW_st_blocks:
655599b0986Sotto small = (sizeof(st->st_blocks) == 4);
656599b0986Sotto data = st->st_blocks;
657599b0986Sotto sdata = NULL;
658599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
659599b0986Sotto if (ofmt == 0)
660599b0986Sotto ofmt = FMTF_UNSIGNED;
661599b0986Sotto break;
662599b0986Sotto case SHOW_st_blksize:
663599b0986Sotto small = (sizeof(st->st_blksize) == 4);
664599b0986Sotto data = st->st_blksize;
665599b0986Sotto sdata = NULL;
666599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
667599b0986Sotto if (ofmt == 0)
668599b0986Sotto ofmt = FMTF_UNSIGNED;
669599b0986Sotto break;
670599b0986Sotto case SHOW_st_flags:
671599b0986Sotto small = (sizeof(st->st_flags) == 4);
672599b0986Sotto data = st->st_flags;
673599b0986Sotto sdata = NULL;
674599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
675599b0986Sotto if (ofmt == 0)
676599b0986Sotto ofmt = FMTF_UNSIGNED;
677599b0986Sotto break;
678599b0986Sotto case SHOW_st_gen:
679599b0986Sotto small = (sizeof(st->st_gen) == 4);
680599b0986Sotto data = st->st_gen;
681599b0986Sotto sdata = NULL;
682599b0986Sotto formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
683599b0986Sotto if (ofmt == 0)
684599b0986Sotto ofmt = FMTF_UNSIGNED;
685599b0986Sotto break;
686599b0986Sotto case SHOW_symlink:
687599b0986Sotto small = 0;
688599b0986Sotto data = 0;
689599b0986Sotto if (S_ISLNK(st->st_mode)) {
690599b0986Sotto snprintf(path, sizeof(path), " -> ");
691599b0986Sotto l = readlink(file, path + 4, sizeof(path) - 4 - 1);
692599b0986Sotto if (l == -1) {
693599b0986Sotto l = 0;
694599b0986Sotto path[0] = '\0';
695599b0986Sotto }
696599b0986Sotto path[l + 4] = '\0';
697599b0986Sotto sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
698d1ca0b67Slum } else
699599b0986Sotto sdata = "";
700d1ca0b67Slum
701599b0986Sotto formats = FMTF_STRING;
702599b0986Sotto if (ofmt == 0)
703599b0986Sotto ofmt = FMTF_STRING;
704599b0986Sotto break;
705599b0986Sotto case SHOW_filetype:
706599b0986Sotto small = 0;
707599b0986Sotto data = 0;
708599b0986Sotto sdata = smode;
70902f16a9dSmillert smode[0] = '\0';
710599b0986Sotto if (hilo == 0 || hilo == LOW_PIECE) {
711599b0986Sotto switch (st->st_mode & S_IFMT) {
712599b0986Sotto case S_IFIFO:
71302f16a9dSmillert (void)strlcat(smode, "|", sizeof(smode));
714599b0986Sotto break;
715599b0986Sotto case S_IFDIR:
71602f16a9dSmillert (void)strlcat(smode, "/", sizeof(smode));
717599b0986Sotto break;
718599b0986Sotto case S_IFREG:
719599b0986Sotto if (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
72002f16a9dSmillert (void)strlcat(smode, "*",
721599b0986Sotto sizeof(smode));
722599b0986Sotto break;
723599b0986Sotto case S_IFLNK:
72402f16a9dSmillert (void)strlcat(smode, "@", sizeof(smode));
725599b0986Sotto break;
726599b0986Sotto case S_IFSOCK:
72702f16a9dSmillert (void)strlcat(smode, "=", sizeof(smode));
728599b0986Sotto break;
729599b0986Sotto }
730599b0986Sotto hilo = 0;
73127f2b7fcSderaadt } else if (hilo == HIGH_PIECE) {
732599b0986Sotto switch (st->st_mode & S_IFMT) {
733599b0986Sotto case S_IFIFO: sdata = "Fifo File"; break;
734599b0986Sotto case S_IFCHR: sdata = "Character Device"; break;
735599b0986Sotto case S_IFDIR: sdata = "Directory"; break;
736599b0986Sotto case S_IFBLK: sdata = "Block Device"; break;
737599b0986Sotto case S_IFREG: sdata = "Regular File"; break;
738599b0986Sotto case S_IFLNK: sdata = "Symbolic Link"; break;
739599b0986Sotto case S_IFSOCK: sdata = "Socket"; break;
740599b0986Sotto default: sdata = "???"; break;
741599b0986Sotto }
742599b0986Sotto hilo = 0;
743599b0986Sotto }
744599b0986Sotto formats = FMTF_STRING;
745599b0986Sotto if (ofmt == 0)
746599b0986Sotto ofmt = FMTF_STRING;
747599b0986Sotto break;
748599b0986Sotto case SHOW_filename:
749599b0986Sotto small = 0;
750599b0986Sotto data = 0;
751599b0986Sotto if (file == NULL)
752599b0986Sotto (void)strlcpy(path, "(stdin)", sizeof(path));
753599b0986Sotto else
754599b0986Sotto (void)strlcpy(path, file, sizeof(path));
755599b0986Sotto sdata = path;
756599b0986Sotto formats = FMTF_STRING;
757599b0986Sotto if (ofmt == 0)
758599b0986Sotto ofmt = FMTF_STRING;
759599b0986Sotto break;
760599b0986Sotto case SHOW_sizerdev:
761599b0986Sotto if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
762599b0986Sotto char majdev[20], mindev[20];
763599b0986Sotto int l1, l2;
764599b0986Sotto
76527f2b7fcSderaadt l1 = format1(st, file, fmt, flen,
76627f2b7fcSderaadt majdev, sizeof(majdev), flags, size, prec,
767599b0986Sotto ofmt, HIGH_PIECE, SHOW_st_rdev);
76827f2b7fcSderaadt l2 = format1(st, file, fmt, flen,
76927f2b7fcSderaadt mindev, sizeof(mindev), flags, size, prec,
770599b0986Sotto ofmt, LOW_PIECE, SHOW_st_rdev);
771f35fea8bSderaadt n = snprintf(buf, blen, "%.*s,%.*s",
772f35fea8bSderaadt l1, majdev, l2, mindev);
773f35fea8bSderaadt return (n >= blen ? blen : n);
77427f2b7fcSderaadt } else {
77527f2b7fcSderaadt return (format1(st, file, fmt, flen, buf, blen,
77627f2b7fcSderaadt flags, size, prec, ofmt, 0, SHOW_st_size));
777599b0986Sotto }
778599b0986Sotto /*NOTREACHED*/
779599b0986Sotto default:
780599b0986Sotto errx(1, "%.*s: bad format", (int)flen, fmt);
781599b0986Sotto }
782599b0986Sotto
783599b0986Sotto /*
784599b0986Sotto * If a subdatum was specified but not supported, or an output
785599b0986Sotto * format was selected that is not supported, that's an error.
786599b0986Sotto */
787599b0986Sotto if (hilo != 0 || (ofmt & formats) == 0)
788599b0986Sotto errx(1, "%.*s: bad format", (int)flen, fmt);
789599b0986Sotto
790599b0986Sotto /*
791599b0986Sotto * Assemble the format string for passing to printf(3).
792599b0986Sotto */
793599b0986Sotto lfmt[0] = '\0';
794599b0986Sotto (void)strlcat(lfmt, "%", sizeof(lfmt));
795599b0986Sotto if (flags & FLAG_POUND)
796599b0986Sotto (void)strlcat(lfmt, "#", sizeof(lfmt));
797599b0986Sotto if (flags & FLAG_SPACE)
798599b0986Sotto (void)strlcat(lfmt, " ", sizeof(lfmt));
799599b0986Sotto if (flags & FLAG_PLUS)
800599b0986Sotto (void)strlcat(lfmt, "+", sizeof(lfmt));
801599b0986Sotto if (flags & FLAG_MINUS)
802599b0986Sotto (void)strlcat(lfmt, "-", sizeof(lfmt));
803599b0986Sotto if (flags & FLAG_ZERO)
804599b0986Sotto (void)strlcat(lfmt, "0", sizeof(lfmt));
805599b0986Sotto
806599b0986Sotto /*
807599b0986Sotto * Only the timespecs support the FLOAT output format, and that
808599b0986Sotto * requires work that differs from the other formats.
809599b0986Sotto */
810599b0986Sotto if (ofmt == FMTF_FLOAT) {
811599b0986Sotto /*
812599b0986Sotto * Nothing after the decimal point, so just print seconds.
813599b0986Sotto */
814599b0986Sotto if (prec == 0) {
815599b0986Sotto if (size != -1) {
816599b0986Sotto (void)snprintf(tmp, sizeof(tmp), "%d", size);
817599b0986Sotto (void)strlcat(lfmt, tmp, sizeof(lfmt));
818599b0986Sotto }
819a4b17896Sguenther (void)strlcat(lfmt, "lld", sizeof(lfmt));
820a4b17896Sguenther n = snprintf(buf, blen, lfmt, (long long)secs);
821f35fea8bSderaadt return (n >= blen ? blen : n);
822599b0986Sotto }
823599b0986Sotto
824599b0986Sotto /*
825599b0986Sotto * Unspecified precision gets all the precision we have:
826599b0986Sotto * 9 digits.
827599b0986Sotto */
828599b0986Sotto if (prec == -1)
829599b0986Sotto prec = 9;
830599b0986Sotto
831599b0986Sotto /*
832599b0986Sotto * Adjust the size for the decimal point and the digits
833599b0986Sotto * that will follow.
834599b0986Sotto */
835599b0986Sotto size -= prec + 1;
836599b0986Sotto
837599b0986Sotto /*
838599b0986Sotto * Any leftover size that's legitimate will be used.
839599b0986Sotto */
840599b0986Sotto if (size > 0) {
841599b0986Sotto (void)snprintf(tmp, sizeof(tmp), "%d", size);
842599b0986Sotto (void)strlcat(lfmt, tmp, sizeof(lfmt));
843599b0986Sotto }
844a4b17896Sguenther (void)strlcat(lfmt, "lld", sizeof(lfmt));
845599b0986Sotto
846599b0986Sotto /*
847599b0986Sotto * The stuff after the decimal point always needs zero
848599b0986Sotto * filling.
849599b0986Sotto */
850599b0986Sotto (void)strlcat(lfmt, ".%0", sizeof(lfmt));
851599b0986Sotto
852599b0986Sotto /*
853599b0986Sotto * We can "print" at most nine digits of precision. The
854599b0986Sotto * rest we will pad on at the end.
855599b0986Sotto */
856a4b17896Sguenther (void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec);
857599b0986Sotto (void)strlcat(lfmt, tmp, sizeof(lfmt));
858599b0986Sotto
859599b0986Sotto /*
860599b0986Sotto * For precision of less that nine digits, trim off the
861599b0986Sotto * less significant figures.
862599b0986Sotto */
863599b0986Sotto for (; prec < 9; prec++)
864599b0986Sotto nsecs /= 10;
865599b0986Sotto
866599b0986Sotto /*
867599b0986Sotto * Use the format, and then tack on any zeroes that
868599b0986Sotto * might be required to make up the requested precision.
869599b0986Sotto */
870a4b17896Sguenther l = snprintf(buf, blen, lfmt, (long long)secs, nsecs);
871f35fea8bSderaadt if (l >= blen)
872f35fea8bSderaadt return (l);
873599b0986Sotto for (; prec > 9 && l < blen; prec--, l++)
87423f91ea0Sotto (void)strlcat(buf, "0", blen);
875599b0986Sotto return (l);
876599b0986Sotto }
877599b0986Sotto
878599b0986Sotto /*
879599b0986Sotto * Add on size and precision, if specified, to the format.
880599b0986Sotto */
881599b0986Sotto if (size != -1) {
882599b0986Sotto (void)snprintf(tmp, sizeof(tmp), "%d", size);
883599b0986Sotto (void)strlcat(lfmt, tmp, sizeof(lfmt));
884599b0986Sotto }
885599b0986Sotto if (prec != -1) {
886599b0986Sotto (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
887599b0986Sotto (void)strlcat(lfmt, tmp, sizeof(lfmt));
888599b0986Sotto }
889599b0986Sotto
890599b0986Sotto /*
891599b0986Sotto * String output uses the temporary sdata.
892599b0986Sotto */
893599b0986Sotto if (ofmt == FMTF_STRING) {
894599b0986Sotto if (sdata == NULL)
895599b0986Sotto errx(1, "%.*s: bad format", (int)flen, fmt);
896599b0986Sotto (void)strlcat(lfmt, "s", sizeof(lfmt));
897f35fea8bSderaadt n = snprintf(buf, blen, lfmt, sdata);
898f35fea8bSderaadt return (n >= blen ? blen : n);
899599b0986Sotto }
900599b0986Sotto
901599b0986Sotto /*
902599b0986Sotto * Ensure that sign extension does not cause bad looking output
903599b0986Sotto * for some forms.
904599b0986Sotto */
905599b0986Sotto if (small && ofmt != FMTF_DECIMAL)
906599b0986Sotto data = (u_int32_t)data;
907599b0986Sotto
908599b0986Sotto /*
909599b0986Sotto * The four "numeric" output forms.
910599b0986Sotto */
911599b0986Sotto (void)strlcat(lfmt, "ll", sizeof(lfmt));
912599b0986Sotto switch (ofmt) {
913599b0986Sotto case FMTF_DECIMAL: (void)strlcat(lfmt, "d", sizeof(lfmt)); break;
914599b0986Sotto case FMTF_OCTAL: (void)strlcat(lfmt, "o", sizeof(lfmt)); break;
915599b0986Sotto case FMTF_UNSIGNED: (void)strlcat(lfmt, "u", sizeof(lfmt)); break;
916599b0986Sotto case FMTF_HEX: (void)strlcat(lfmt, "x", sizeof(lfmt)); break;
917599b0986Sotto }
918599b0986Sotto
919f35fea8bSderaadt n = snprintf(buf, blen, lfmt, data);
920f35fea8bSderaadt return (n >= blen ? blen : n);
921599b0986Sotto }
922