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