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 *

--- 21 unchanged lines hidden (view full) ---

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 *

--- 21 unchanged lines hidden (view full) ---

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 " \

--- 36 unchanged lines hidden (view full) ---

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 " \

--- 36 unchanged lines hidden (view full) ---

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'

--- 8 unchanged lines hidden (view full) ---

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'

--- 8 unchanged lines hidden (view full) ---

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)

--- 21 unchanged lines hidden (view full) ---

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)

--- 21 unchanged lines hidden (view full) ---

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

--- 52 unchanged lines hidden (view full) ---

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

--- 52 unchanged lines hidden (view full) ---

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);

--- 21 unchanged lines hidden (view full) ---

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);

--- 21 unchanged lines hidden (view full) ---

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;

--- 20 unchanged lines hidden (view full) ---

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;

--- 20 unchanged lines hidden (view full) ---

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] == ' ')

--- 11 unchanged lines hidden (view full) ---

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] == ' ')

--- 11 unchanged lines hidden (view full) ---

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) {

--- 19 unchanged lines hidden (view full) ---

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) {

--- 19 unchanged lines hidden (view full) ---

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,

--- 45 unchanged lines hidden (view full) ---

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,

--- 45 unchanged lines hidden (view full) ---

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 }

--- 63 unchanged lines hidden (view full) ---

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 }

--- 63 unchanged lines hidden (view full) ---

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}