1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003-2007 Tim Kientzle
5 * All rights reserved.
6 */
7
8 #include "bsdtar_platform.h"
9
10 #ifdef HAVE_SYS_STAT_H
11 #include <sys/stat.h>
12 #endif
13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h> /* Linux doesn't define mode_t, etc. in sys/stat.h. */
15 #endif
16 #include <ctype.h>
17 #ifdef HAVE_ERRNO_H
18 #include <errno.h>
19 #endif
20 #ifdef HAVE_IO_H
21 #include <io.h>
22 #endif
23 #ifdef HAVE_STDARG_H
24 #include <stdarg.h>
25 #endif
26 #ifdef HAVE_STDINT_H
27 #include <stdint.h>
28 #endif
29 #include <stdio.h>
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36 #ifdef HAVE_WCTYPE_H
37 #include <wctype.h>
38 #else
39 /* If we don't have wctype, we need to hack up some version of iswprint(). */
40 #define iswprint isprint
41 #endif
42
43 #include "bsdtar.h"
44 #include "err.h"
45 #include "passphrase.h"
46
47 static size_t bsdtar_expand_char(char *, size_t, size_t, char);
48 static const char *strip_components(const char *path, int elements);
49
50 #if defined(_WIN32) && !defined(__CYGWIN__)
51 #define read _read
52 #endif
53
54 /* TODO: Hack up a version of mbtowc for platforms with no wide
55 * character support at all. I think the following might suffice,
56 * but it needs careful testing.
57 * #if !HAVE_MBTOWC
58 * #define mbtowc(wcp, p, n) ((*wcp = *p), 1)
59 * #endif
60 */
61
62 /*
63 * Print a string, taking care with any non-printable characters.
64 *
65 * Note that we use a stack-allocated buffer to receive the formatted
66 * string if we can. This is partly performance (avoiding a call to
67 * malloc()), partly out of expedience (we have to call vsnprintf()
68 * before malloc() anyway to find out how big a buffer we need; we may
69 * as well point that first call at a small local buffer in case it
70 * works).
71 */
72
73 void
safe_fprintf(FILE * restrict f,const char * restrict fmt,...)74 safe_fprintf(FILE * restrict f, const char * restrict fmt, ...)
75 {
76 char fmtbuff_stack[256]; /* Place to format the printf() string. */
77 char outbuff[256]; /* Buffer for outgoing characters. */
78 char *fmtbuff_heap; /* If fmtbuff_stack is too small, we use malloc */
79 char *fmtbuff; /* Pointer to fmtbuff_stack or fmtbuff_heap. */
80 size_t fmtbuff_length;
81 int length, n;
82 va_list ap;
83 const char *p;
84 size_t i;
85 wchar_t wc;
86 char try_wc;
87
88 /* Use a stack-allocated buffer if we can, for speed and safety. */
89 memset(fmtbuff_stack, '\0', sizeof(fmtbuff_stack));
90 fmtbuff_heap = NULL;
91 fmtbuff_length = sizeof(fmtbuff_stack);
92 fmtbuff = fmtbuff_stack;
93
94 /* Try formatting into the stack buffer. */
95 va_start(ap, fmt);
96 length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap);
97 va_end(ap);
98
99 /* If vsnprintf will always fail, stop early. */
100 if (length < 0 && errno == EOVERFLOW)
101 return;
102
103 /* If the result was too large, allocate a buffer on the heap. */
104 while (length < 0 || (size_t)length >= fmtbuff_length) {
105 if (length >= 0 && (size_t)length >= fmtbuff_length)
106 fmtbuff_length = (size_t)length + 1;
107 else if (fmtbuff_length < 8192)
108 fmtbuff_length *= 2;
109 else if (fmtbuff_length < 1000000)
110 fmtbuff_length += fmtbuff_length / 4;
111 else {
112 fmtbuff[fmtbuff_length - 1] = '\0';
113 length = (int)strlen(fmtbuff);
114 break;
115 }
116 free(fmtbuff_heap);
117 fmtbuff_heap = malloc(fmtbuff_length);
118
119 /* Reformat the result into the heap buffer if we can. */
120 if (fmtbuff_heap != NULL) {
121 fmtbuff = fmtbuff_heap;
122 va_start(ap, fmt);
123 length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap);
124 va_end(ap);
125 } else {
126 /* Leave fmtbuff pointing to the truncated
127 * string in fmtbuff_stack. */
128 fmtbuff_stack[sizeof(fmtbuff_stack) - 1] = '\0';
129 fmtbuff = fmtbuff_stack;
130 length = (int)strlen(fmtbuff);
131 break;
132 }
133 }
134
135 /* Note: mbrtowc() has a cleaner API, but mbtowc() seems a bit
136 * more portable, so we use that here instead. */
137 if (mbtowc(NULL, NULL, 1) == -1) { /* Reset the shift state. */
138 /* mbtowc() should never fail in practice, but
139 * handle the theoretical error anyway. */
140 free(fmtbuff_heap);
141 return;
142 }
143
144 /* Write data, expanding unprintable characters. */
145 p = fmtbuff;
146 i = 0;
147 try_wc = 1;
148 while (*p != '\0') {
149
150 /* Convert to wide char, test if the wide
151 * char is printable in the current locale. */
152 if (try_wc && (n = mbtowc(&wc, p, length)) != -1) {
153 length -= n;
154 if (iswprint(wc) && wc != L'\\') {
155 /* Printable, copy the bytes through. */
156 while (n-- > 0)
157 outbuff[i++] = *p++;
158 } else {
159 /* Not printable, format the bytes. */
160 while (n-- > 0)
161 i += bsdtar_expand_char(
162 outbuff, sizeof(outbuff), i, *p++);
163 }
164 } else {
165 /* After any conversion failure, don't bother
166 * trying to convert the rest. */
167 i += bsdtar_expand_char(outbuff, sizeof(outbuff), i, *p++);
168 try_wc = 0;
169 }
170
171 /* If our output buffer is full, dump it and keep going. */
172 if (i > (sizeof(outbuff) - 128)) {
173 outbuff[i] = '\0';
174 fprintf(f, "%s", outbuff);
175 i = 0;
176 }
177 }
178 outbuff[i] = '\0';
179 fprintf(f, "%s", outbuff);
180
181 /* If we allocated a heap-based formatting buffer, free it now. */
182 free(fmtbuff_heap);
183 }
184
185 /*
186 * Render an arbitrary sequence of bytes into printable ASCII characters.
187 */
188 static size_t
bsdtar_expand_char(char * buff,size_t buffsize,size_t offset,char c)189 bsdtar_expand_char(char *buff, size_t buffsize, size_t offset, char c)
190 {
191 size_t i = offset;
192
193 if (isprint((unsigned char)c) && c != '\\')
194 buff[i++] = c;
195 else {
196 buff[i++] = '\\';
197 switch (c) {
198 case '\a': buff[i++] = 'a'; break;
199 case '\b': buff[i++] = 'b'; break;
200 case '\f': buff[i++] = 'f'; break;
201 case '\n': buff[i++] = 'n'; break;
202 #if '\r' != '\n'
203 /* On some platforms, \n and \r are the same. */
204 case '\r': buff[i++] = 'r'; break;
205 #endif
206 case '\t': buff[i++] = 't'; break;
207 case '\v': buff[i++] = 'v'; break;
208 case '\\': buff[i++] = '\\'; break;
209 default:
210 snprintf(buff + i, buffsize - i, "%03o",
211 0xFF & (unsigned int)c);
212 i += 3;
213 }
214 }
215
216 return (i - offset);
217 }
218
219 int
yes(const char * fmt,...)220 yes(const char *fmt, ...)
221 {
222 char buff[32];
223 char *p;
224 ssize_t l;
225 int read_fd = 2; /* stderr */
226
227 va_list ap;
228 va_start(ap, fmt);
229 vfprintf(stderr, fmt, ap);
230 va_end(ap);
231 fprintf(stderr, " (y/N)? ");
232 fflush(stderr);
233
234 #if defined(_WIN32) && !defined(__CYGWIN__)
235 /* To be resilient when stdin is a pipe, bsdtar prefers to read from
236 * stderr. On Windows, stderr cannot be read. The nearest "piping
237 * resilient" equivalent is reopening the console input handle.
238 */
239 read_fd = _open("CONIN$", O_RDONLY);
240 if (read_fd < 0) {
241 fprintf(stderr, "Keyboard read failed\n");
242 exit(1);
243 }
244 #endif
245
246 l = read(read_fd, buff, sizeof(buff) - 1);
247
248 #if defined(_WIN32) && !defined(__CYGWIN__)
249 _close(read_fd);
250 #endif
251
252 if (l < 0) {
253 fprintf(stderr, "Keyboard read failed\n");
254 exit(1);
255 }
256 if (l == 0)
257 return (0);
258 buff[l] = 0;
259
260 for (p = buff; *p != '\0'; p++) {
261 if (isspace((unsigned char)*p))
262 continue;
263 switch(*p) {
264 case 'y': case 'Y':
265 return (1);
266 case 'n': case 'N':
267 return (0);
268 default:
269 return (0);
270 }
271 }
272
273 return (0);
274 }
275
276 /*-
277 * The logic here for -C <dir> attempts to avoid
278 * chdir() as long as possible. For example:
279 * "-C /foo -C /bar file" needs chdir("/bar") but not chdir("/foo")
280 * "-C /foo -C bar file" needs chdir("/foo/bar")
281 * "-C /foo -C bar /file1" does not need chdir()
282 * "-C /foo -C bar /file1 file2" needs chdir("/foo/bar") before file2
283 *
284 * The only correct way to handle this is to record a "pending" chdir
285 * request and combine multiple requests intelligently until we
286 * need to process a non-absolute file. set_chdir() adds the new dir
287 * to the pending list; do_chdir() actually executes any pending chdir.
288 *
289 * This way, programs that build tar command lines don't have to worry
290 * about -C with non-existent directories; such requests will only
291 * fail if the directory must be accessed.
292 *
293 */
294 void
set_chdir(struct bsdtar * bsdtar,const char * newdir)295 set_chdir(struct bsdtar *bsdtar, const char *newdir)
296 {
297 #if defined(_WIN32) && !defined(__CYGWIN__)
298 if (newdir[0] == '/' || newdir[0] == '\\' ||
299 /* Detect this type, for example, "C:\" or "C:/" */
300 (((newdir[0] >= 'a' && newdir[0] <= 'z') ||
301 (newdir[0] >= 'A' && newdir[0] <= 'Z')) &&
302 newdir[1] == ':' && (newdir[2] == '/' || newdir[2] == '\\'))) {
303 #else
304 if (newdir[0] == '/') {
305 #endif
306 /* The -C /foo -C /bar case; dump first one. */
307 free(bsdtar->pending_chdir);
308 bsdtar->pending_chdir = NULL;
309 }
310 if (bsdtar->pending_chdir == NULL)
311 /* Easy case: no previously-saved dir. */
312 bsdtar->pending_chdir = strdup(newdir);
313 else {
314 /* The -C /foo -C bar case; concatenate */
315 char *old_pending = bsdtar->pending_chdir;
316 size_t old_len = strlen(old_pending);
317 size_t new_len = old_len + strlen(newdir) + 2;
318 bsdtar->pending_chdir = malloc(new_len);
319 if (old_pending[old_len - 1] == '/')
320 old_pending[old_len - 1] = '\0';
321 if (bsdtar->pending_chdir != NULL)
322 snprintf(bsdtar->pending_chdir, new_len, "%s/%s",
323 old_pending, newdir);
324 free(old_pending);
325 }
326 if (bsdtar->pending_chdir == NULL)
327 lafe_errc(1, errno, "No memory");
328 }
329
330 void
331 do_chdir(struct bsdtar *bsdtar)
332 {
333 if (bsdtar->pending_chdir == NULL)
334 return;
335
336 if (chdir(bsdtar->pending_chdir) != 0) {
337 lafe_errc(1, 0, "could not chdir to '%s'",
338 bsdtar->pending_chdir);
339 }
340 free(bsdtar->pending_chdir);
341 bsdtar->pending_chdir = NULL;
342 }
343
344 static const char *
345 strip_components(const char *p, int elements)
346 {
347 /* Skip as many elements as necessary. */
348 while (elements > 0) {
349 switch (*p++) {
350 case '/':
351 #if defined(_WIN32) && !defined(__CYGWIN__)
352 case '\\': /* Support \ path sep on Windows ONLY. */
353 #endif
354 elements--;
355 break;
356 case '\0':
357 /* Path is too short, skip it. */
358 return (NULL);
359 }
360 }
361
362 /* Skip any / characters. This handles short paths that have
363 * additional / termination. This also handles the case where
364 * the logic above stops in the middle of a duplicate //
365 * sequence (which would otherwise get converted to an
366 * absolute path). */
367 for (;;) {
368 switch (*p) {
369 case '/':
370 #if defined(_WIN32) && !defined(__CYGWIN__)
371 case '\\': /* Support \ path sep on Windows ONLY. */
372 #endif
373 ++p;
374 break;
375 case '\0':
376 return (NULL);
377 default:
378 return (p);
379 }
380 }
381 }
382
383 static void
384 warn_strip_leading_char(struct bsdtar *bsdtar, const char *c)
385 {
386 if (!bsdtar->warned_lead_slash) {
387 lafe_warnc(0,
388 "Removing leading '%c' from member names",
389 c[0]);
390 bsdtar->warned_lead_slash = 1;
391 }
392 }
393
394 static void
395 warn_strip_drive_letter(struct bsdtar *bsdtar)
396 {
397 if (!bsdtar->warned_lead_slash) {
398 lafe_warnc(0,
399 "Removing leading drive letter from "
400 "member names");
401 bsdtar->warned_lead_slash = 1;
402 }
403 }
404
405 /*
406 * Convert absolute path to non-absolute path by skipping leading
407 * absolute path prefixes.
408 */
409 static const char*
410 strip_absolute_path(struct bsdtar *bsdtar, const char *p)
411 {
412 const char *rp;
413
414 /* Remove leading "//./" or "//?/" or "//?/UNC/"
415 * (absolute path prefixes used by Windows API) */
416 if ((p[0] == '/' || p[0] == '\\') &&
417 (p[1] == '/' || p[1] == '\\') &&
418 (p[2] == '.' || p[2] == '?') &&
419 (p[3] == '/' || p[3] == '\\'))
420 {
421 if (p[2] == '?' &&
422 (p[4] == 'U' || p[4] == 'u') &&
423 (p[5] == 'N' || p[5] == 'n') &&
424 (p[6] == 'C' || p[6] == 'c') &&
425 (p[7] == '/' || p[7] == '\\'))
426 p += 8;
427 else
428 p += 4;
429 warn_strip_drive_letter(bsdtar);
430 }
431
432 /* Remove multiple leading slashes and Windows drive letters. */
433 do {
434 rp = p;
435 if (((p[0] >= 'a' && p[0] <= 'z') ||
436 (p[0] >= 'A' && p[0] <= 'Z')) &&
437 p[1] == ':') {
438 p += 2;
439 warn_strip_drive_letter(bsdtar);
440 }
441
442 /* Remove leading "/../", "/./", "//", etc. */
443 while (p[0] == '/' || p[0] == '\\') {
444 if (p[1] == '.' &&
445 p[2] == '.' &&
446 (p[3] == '/' || p[3] == '\\')) {
447 p += 3; /* Remove "/..", leave "/" for next pass. */
448 } else if (p[1] == '.' &&
449 (p[2] == '/' || p[2] == '\\')) {
450 p += 2; /* Remove "/.", leave "/" for next pass. */
451 } else
452 p += 1; /* Remove "/". */
453 warn_strip_leading_char(bsdtar, rp);
454 }
455 } while (rp != p);
456
457 return (p);
458 }
459
460 /*
461 * Handle --strip-components and any future path-rewriting options.
462 * Returns non-zero if the pathname should not be extracted.
463 *
464 * Note: The rewrites are applied uniformly to pathnames and hardlink
465 * names but not to symlink bodies. This is deliberate: Symlink
466 * bodies are not necessarily filenames. Even when they are, they
467 * need to be interpreted relative to the directory containing them,
468 * so simple rewrites like this are rarely appropriate.
469 *
470 * TODO: Support pax-style regex path rewrites.
471 */
472 int
473 edit_pathname(struct bsdtar *bsdtar, struct archive_entry *entry)
474 {
475 const char *name = archive_entry_pathname(entry);
476 const char *original_name = name;
477 const char *hardlinkname = archive_entry_hardlink(entry);
478 const char *original_hardlinkname = hardlinkname;
479 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H) || defined(HAVE_PCRE2POSIX_H)
480 char *subst_name;
481 int r;
482
483 /* Apply user-specified substitution to pathname. */
484 r = apply_substitution(bsdtar, name, &subst_name, 0, 0);
485 if (r == -1) {
486 lafe_warnc(0, "Invalid substitution, skipping entry");
487 return 1;
488 }
489 if (r == 1) {
490 archive_entry_copy_pathname(entry, subst_name);
491 if (*subst_name == '\0') {
492 free(subst_name);
493 return -1;
494 } else
495 free(subst_name);
496 name = archive_entry_pathname(entry);
497 original_name = name;
498 }
499
500 /* Apply user-specified substitution to hardlink target. */
501 if (hardlinkname != NULL) {
502 r = apply_substitution(bsdtar, hardlinkname, &subst_name, 0, 1);
503 if (r == -1) {
504 lafe_warnc(0, "Invalid substitution, skipping entry");
505 return 1;
506 }
507 if (r == 1) {
508 archive_entry_copy_hardlink(entry, subst_name);
509 free(subst_name);
510 }
511 hardlinkname = archive_entry_hardlink(entry);
512 original_hardlinkname = hardlinkname;
513 }
514
515 /* Apply user-specified substitution to symlink body. */
516 if (archive_entry_symlink(entry) != NULL) {
517 r = apply_substitution(bsdtar, archive_entry_symlink(entry), &subst_name, 1, 0);
518 if (r == -1) {
519 lafe_warnc(0, "Invalid substitution, skipping entry");
520 return 1;
521 }
522 if (r == 1) {
523 archive_entry_copy_symlink(entry, subst_name);
524 free(subst_name);
525 }
526 }
527 #endif
528
529 /* Strip leading dir names as per --strip-components option. */
530 if (bsdtar->strip_components > 0) {
531 name = strip_components(name, bsdtar->strip_components);
532 if (name == NULL)
533 return (1);
534
535 if (hardlinkname != NULL) {
536 hardlinkname = strip_components(hardlinkname,
537 bsdtar->strip_components);
538 if (hardlinkname == NULL)
539 return (1);
540 }
541 }
542
543 if ((bsdtar->flags & OPTFLAG_ABSOLUTE_PATHS) == 0) {
544 /* By default, don't write or restore absolute pathnames. */
545 name = strip_absolute_path(bsdtar, name);
546 if (*name == '\0')
547 name = ".";
548
549 if (hardlinkname != NULL) {
550 hardlinkname = strip_absolute_path(bsdtar, hardlinkname);
551 if (*hardlinkname == '\0')
552 return (1);
553 }
554 } else {
555 /* Strip redundant leading '/' characters. */
556 while (name[0] == '/' && name[1] == '/')
557 name++;
558 }
559
560 /* Replace name in archive_entry. */
561 if (name != original_name) {
562 archive_entry_copy_pathname(entry, name);
563 }
564 if (hardlinkname != original_hardlinkname) {
565 archive_entry_copy_hardlink(entry, hardlinkname);
566 }
567 return (0);
568 }
569
570 /*
571 * Apply --mtime and --clamp-mtime options.
572 */
573 void
574 edit_mtime(struct bsdtar *bsdtar, struct archive_entry *entry)
575 {
576 if (!bsdtar->has_mtime)
577 return;
578
579 __LA_TIME_T entry_mtime = archive_entry_mtime(entry);
580 if (!bsdtar->clamp_mtime || entry_mtime > bsdtar->mtime)
581 archive_entry_set_mtime(entry, bsdtar->mtime, 0);
582 }
583
584 /*
585 * It would be nice to just use printf() for formatting large numbers,
586 * but the compatibility problems are quite a headache. Hence the
587 * following simple utility function.
588 */
589 const char *
590 tar_i64toa(int64_t n0)
591 {
592 static char buff[24];
593 uint64_t n = n0 < 0 ? -n0 : n0;
594 char *p = buff + sizeof(buff);
595
596 *--p = '\0';
597 do {
598 *--p = '0' + (int)(n % 10);
599 } while (n /= 10);
600 if (n0 < 0)
601 *--p = '-';
602 return p;
603 }
604
605 /*
606 * Like strcmp(), but try to be a little more aware of the fact that
607 * we're comparing two paths. Right now, it just handles leading
608 * "./" and trailing '/' specially, so that "a/b/" == "./a/b"
609 *
610 * TODO: Make this better, so that "./a//b/./c/" == "a/b/c"
611 * TODO: After this works, push it down into libarchive.
612 * TODO: Publish the path normalization routines in libarchive so
613 * that bsdtar can normalize paths and use fast strcmp() instead
614 * of this.
615 *
616 * Note: This is currently only used within write.c, so should
617 * not handle \ path separators.
618 */
619
620 int
621 pathcmp(const char *a, const char *b)
622 {
623 /* Skip leading './' */
624 if (a[0] == '.' && a[1] == '/' && a[2] != '\0')
625 a += 2;
626 if (b[0] == '.' && b[1] == '/' && b[2] != '\0')
627 b += 2;
628 /* Find the first difference, or return (0) if none. */
629 while (*a == *b) {
630 if (*a == '\0')
631 return (0);
632 a++;
633 b++;
634 }
635 /*
636 * If one ends in '/' and the other one doesn't,
637 * they're the same.
638 */
639 if (a[0] == '/' && a[1] == '\0' && b[0] == '\0')
640 return (0);
641 if (a[0] == '\0' && b[0] == '/' && b[1] == '\0')
642 return (0);
643 /* They're really different, return the correct sign. */
644 return (*(const unsigned char *)a - *(const unsigned char *)b);
645 }
646
647 #define PPBUFF_SIZE 1024
648 const char *
649 passphrase_callback(struct archive *a, void *_client_data)
650 {
651 struct bsdtar *bsdtar = (struct bsdtar *)_client_data;
652 (void)a; /* UNUSED */
653
654 if (bsdtar->ppbuff == NULL) {
655 bsdtar->ppbuff = malloc(PPBUFF_SIZE);
656 if (bsdtar->ppbuff == NULL)
657 lafe_errc(1, errno, "Out of memory");
658 }
659 return lafe_readpassphrase("Enter passphrase:",
660 bsdtar->ppbuff, PPBUFF_SIZE);
661 }
662
663 void
664 passphrase_free(char *ppbuff)
665 {
666 if (ppbuff != NULL) {
667 memset(ppbuff, 0, PPBUFF_SIZE);
668 free(ppbuff);
669 }
670 }
671
672 /*
673 * Display information about the current file.
674 *
675 * The format here roughly duplicates the output of 'ls -l'.
676 * This is based on SUSv2, where 'tar tv' is documented as
677 * listing additional information in an "unspecified format,"
678 * and 'pax -l' is documented as using the same format as 'ls -l'.
679 */
680 void
681 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
682 {
683 char tmp[100];
684 size_t w;
685 const char *p;
686 const char *fmt;
687 time_t tim;
688 static time_t now;
689 struct tm *ltime;
690 #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
691 struct tm tmbuf;
692 #endif
693
694 /*
695 * We avoid collecting the entire list in memory at once by
696 * listing things as we see them. However, that also means we can't
697 * just pre-compute the field widths. Instead, we start with guesses
698 * and just widen them as necessary. These numbers are completely
699 * arbitrary.
700 */
701 if (!bsdtar->u_width) {
702 bsdtar->u_width = 6;
703 bsdtar->gs_width = 13;
704 }
705 if (!now)
706 time(&now);
707 fprintf(out, "%s %u ",
708 archive_entry_strmode(entry),
709 archive_entry_nlink(entry));
710
711 /* Use uname if it's present, else uid. */
712 p = archive_entry_uname(entry);
713 if ((p == NULL) || (*p == '\0')) {
714 snprintf(tmp, sizeof(tmp), "%lu ",
715 (unsigned long)archive_entry_uid(entry));
716 p = tmp;
717 }
718 w = strlen(p);
719 if (w > bsdtar->u_width)
720 bsdtar->u_width = w;
721 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
722
723 /* Use gname if it's present, else gid. */
724 p = archive_entry_gname(entry);
725 if (p != NULL && p[0] != '\0') {
726 fprintf(out, "%s", p);
727 w = strlen(p);
728 } else {
729 snprintf(tmp, sizeof(tmp), "%lu",
730 (unsigned long)archive_entry_gid(entry));
731 w = strlen(tmp);
732 fprintf(out, "%s", tmp);
733 }
734
735 /*
736 * Print device number or file size, right-aligned so as to make
737 * total width of group and devnum/filesize fields be gs_width.
738 * If gs_width is too small, grow it.
739 */
740 if (archive_entry_filetype(entry) == AE_IFCHR
741 || archive_entry_filetype(entry) == AE_IFBLK) {
742 snprintf(tmp, sizeof(tmp), "%lu,%lu",
743 (unsigned long)archive_entry_rdevmajor(entry),
744 (unsigned long)archive_entry_rdevminor(entry));
745 } else {
746 strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
747 }
748 if (w + strlen(tmp) >= bsdtar->gs_width)
749 bsdtar->gs_width = w+strlen(tmp)+1;
750 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
751
752 /* Format the time using 'ls -l' conventions. */
753 tim = archive_entry_mtime(entry);
754 #define HALF_YEAR (time_t)365 * 86400 / 2
755 #if defined(_WIN32) && !defined(__CYGWIN__)
756 #define DAY_FMT "%d" /* Windows' strftime function does not support %e format. */
757 #else
758 #define DAY_FMT "%e" /* Day number without leading zeros */
759 #endif
760 if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
761 fmt = bsdtar->day_first ? DAY_FMT " %b %Y" : "%b " DAY_FMT " %Y";
762 else
763 fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
764 #if defined(HAVE_LOCALTIME_S)
765 ltime = localtime_s(&tmbuf, &tim) ? NULL : &tmbuf;
766 #elif defined(HAVE_LOCALTIME_R)
767 ltime = localtime_r(&tim, &tmbuf);
768 #else
769 ltime = localtime(&tim);
770 #endif
771 if (ltime)
772 strftime(tmp, sizeof(tmp), fmt, ltime);
773 else
774 sprintf(tmp, "-- -- ----");
775 fprintf(out, " %s ", tmp);
776 safe_fprintf(out, "%s", archive_entry_pathname(entry));
777
778 /* Extra information for links. */
779 if (archive_entry_hardlink(entry)) /* Hard link */
780 safe_fprintf(out, " link to %s",
781 archive_entry_hardlink(entry));
782 else if (archive_entry_symlink(entry)) /* Symbolic link */
783 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
784 }
785