160b4ad09SPeter Avalos /*-
260b4ad09SPeter Avalos * Copyright (c) 2003-2007 Tim Kientzle
360b4ad09SPeter Avalos * All rights reserved.
460b4ad09SPeter Avalos *
560b4ad09SPeter Avalos * Redistribution and use in source and binary forms, with or without
660b4ad09SPeter Avalos * modification, are permitted provided that the following conditions
760b4ad09SPeter Avalos * are met:
860b4ad09SPeter Avalos * 1. Redistributions of source code must retain the above copyright
960b4ad09SPeter Avalos * notice, this list of conditions and the following disclaimer.
1060b4ad09SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
1160b4ad09SPeter Avalos * notice, this list of conditions and the following disclaimer in the
1260b4ad09SPeter Avalos * documentation and/or other materials provided with the distribution.
1360b4ad09SPeter Avalos *
1460b4ad09SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1560b4ad09SPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1660b4ad09SPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1760b4ad09SPeter Avalos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1860b4ad09SPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1960b4ad09SPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2060b4ad09SPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2160b4ad09SPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2260b4ad09SPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2360b4ad09SPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2460b4ad09SPeter Avalos */
2560b4ad09SPeter Avalos
2660b4ad09SPeter Avalos #include "bsdtar_platform.h"
278029ab02SPeter Avalos __FBSDID("$FreeBSD: src/usr.bin/tar/util.c,v 1.23 2008/12/15 06:00:25 kientzle Exp $");
2860b4ad09SPeter Avalos
2960b4ad09SPeter Avalos #ifdef HAVE_SYS_STAT_H
3060b4ad09SPeter Avalos #include <sys/stat.h>
3160b4ad09SPeter Avalos #endif
3260b4ad09SPeter Avalos #ifdef HAVE_SYS_TYPES_H
3360b4ad09SPeter Avalos #include <sys/types.h> /* Linux doesn't define mode_t, etc. in sys/stat.h. */
3460b4ad09SPeter Avalos #endif
3560b4ad09SPeter Avalos #include <ctype.h>
3660b4ad09SPeter Avalos #ifdef HAVE_ERRNO_H
3760b4ad09SPeter Avalos #include <errno.h>
3860b4ad09SPeter Avalos #endif
399c82a63eSPeter Avalos #ifdef HAVE_IO_H
409c82a63eSPeter Avalos #include <io.h>
419c82a63eSPeter Avalos #endif
4260b4ad09SPeter Avalos #ifdef HAVE_STDARG_H
4360b4ad09SPeter Avalos #include <stdarg.h>
4460b4ad09SPeter Avalos #endif
459c82a63eSPeter Avalos #ifdef HAVE_STDINT_H
469c82a63eSPeter Avalos #include <stdint.h>
479c82a63eSPeter Avalos #endif
4860b4ad09SPeter Avalos #include <stdio.h>
4960b4ad09SPeter Avalos #ifdef HAVE_STDLIB_H
5060b4ad09SPeter Avalos #include <stdlib.h>
5160b4ad09SPeter Avalos #endif
5260b4ad09SPeter Avalos #ifdef HAVE_STRING_H
5360b4ad09SPeter Avalos #include <string.h>
5460b4ad09SPeter Avalos #endif
558029ab02SPeter Avalos #ifdef HAVE_WCTYPE_H
568029ab02SPeter Avalos #include <wctype.h>
578029ab02SPeter Avalos #else
588029ab02SPeter Avalos /* If we don't have wctype, we need to hack up some version of iswprint(). */
598029ab02SPeter Avalos #define iswprint isprint
608029ab02SPeter Avalos #endif
6160b4ad09SPeter Avalos
6260b4ad09SPeter Avalos #include "bsdtar.h"
639c82a63eSPeter Avalos #include "err.h"
646b384f39SPeter Avalos #include "passphrase.h"
6560b4ad09SPeter Avalos
668029ab02SPeter Avalos static size_t bsdtar_expand_char(char *, size_t, char);
678029ab02SPeter Avalos static const char *strip_components(const char *path, int elements);
688029ab02SPeter Avalos
699c82a63eSPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
709c82a63eSPeter Avalos #define read _read
719c82a63eSPeter Avalos #endif
729c82a63eSPeter Avalos
738029ab02SPeter Avalos /* TODO: Hack up a version of mbtowc for platforms with no wide
748029ab02SPeter Avalos * character support at all. I think the following might suffice,
758029ab02SPeter Avalos * but it needs careful testing.
768029ab02SPeter Avalos * #if !HAVE_MBTOWC
778029ab02SPeter Avalos * #define mbtowc(wcp, p, n) ((*wcp = *p), 1)
788029ab02SPeter Avalos * #endif
798029ab02SPeter Avalos */
8060b4ad09SPeter Avalos
8160b4ad09SPeter Avalos /*
8260b4ad09SPeter Avalos * Print a string, taking care with any non-printable characters.
838029ab02SPeter Avalos *
848029ab02SPeter Avalos * Note that we use a stack-allocated buffer to receive the formatted
858029ab02SPeter Avalos * string if we can. This is partly performance (avoiding a call to
868029ab02SPeter Avalos * malloc()), partly out of expedience (we have to call vsnprintf()
878029ab02SPeter Avalos * before malloc() anyway to find out how big a buffer we need; we may
888029ab02SPeter Avalos * as well point that first call at a small local buffer in case it
898029ab02SPeter Avalos * works), but mostly for safety (so we can use this to print messages
908029ab02SPeter Avalos * about out-of-memory conditions).
9160b4ad09SPeter Avalos */
9260b4ad09SPeter Avalos
9360b4ad09SPeter Avalos void
safe_fprintf(FILE * f,const char * fmt,...)9460b4ad09SPeter Avalos safe_fprintf(FILE *f, const char *fmt, ...)
9560b4ad09SPeter Avalos {
968029ab02SPeter Avalos char fmtbuff_stack[256]; /* Place to format the printf() string. */
978029ab02SPeter Avalos char outbuff[256]; /* Buffer for outgoing characters. */
988029ab02SPeter Avalos char *fmtbuff_heap; /* If fmtbuff_stack is too small, we use malloc */
998029ab02SPeter Avalos char *fmtbuff; /* Pointer to fmtbuff_stack or fmtbuff_heap. */
1008029ab02SPeter Avalos int fmtbuff_length;
1019c82a63eSPeter Avalos int length, n;
10260b4ad09SPeter Avalos va_list ap;
1038029ab02SPeter Avalos const char *p;
10460b4ad09SPeter Avalos unsigned i;
1058029ab02SPeter Avalos wchar_t wc;
1068029ab02SPeter Avalos char try_wc;
10760b4ad09SPeter Avalos
10860b4ad09SPeter Avalos /* Use a stack-allocated buffer if we can, for speed and safety. */
1098029ab02SPeter Avalos fmtbuff_heap = NULL;
1108029ab02SPeter Avalos fmtbuff_length = sizeof(fmtbuff_stack);
1118029ab02SPeter Avalos fmtbuff = fmtbuff_stack;
11260b4ad09SPeter Avalos
1138029ab02SPeter Avalos /* Try formatting into the stack buffer. */
11460b4ad09SPeter Avalos va_start(ap, fmt);
1158029ab02SPeter Avalos length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap);
11660b4ad09SPeter Avalos va_end(ap);
1178029ab02SPeter Avalos
1188029ab02SPeter Avalos /* If the result was too large, allocate a buffer on the heap. */
11955c601bbSPeter Avalos while (length < 0 || length >= fmtbuff_length) {
12055c601bbSPeter Avalos if (length >= fmtbuff_length)
1218029ab02SPeter Avalos fmtbuff_length = length+1;
12255c601bbSPeter Avalos else if (fmtbuff_length < 8192)
12355c601bbSPeter Avalos fmtbuff_length *= 2;
124d4d8193eSPeter Avalos else if (fmtbuff_length < 1000000)
12555c601bbSPeter Avalos fmtbuff_length += fmtbuff_length / 4;
126d4d8193eSPeter Avalos else {
127d4d8193eSPeter Avalos length = fmtbuff_length;
12855c601bbSPeter Avalos fmtbuff_heap[length-1] = '\0';
12955c601bbSPeter Avalos break;
13055c601bbSPeter Avalos }
13155c601bbSPeter Avalos free(fmtbuff_heap);
1328029ab02SPeter Avalos fmtbuff_heap = malloc(fmtbuff_length);
1338029ab02SPeter Avalos
1348029ab02SPeter Avalos /* Reformat the result into the heap buffer if we can. */
1358029ab02SPeter Avalos if (fmtbuff_heap != NULL) {
1368029ab02SPeter Avalos fmtbuff = fmtbuff_heap;
13760b4ad09SPeter Avalos va_start(ap, fmt);
1388029ab02SPeter Avalos length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap);
13960b4ad09SPeter Avalos va_end(ap);
1408029ab02SPeter Avalos } else {
1418029ab02SPeter Avalos /* Leave fmtbuff pointing to the truncated
1428029ab02SPeter Avalos * string in fmtbuff_stack. */
143e95abc47Szrj fmtbuff = fmtbuff_stack;
1448029ab02SPeter Avalos length = sizeof(fmtbuff_stack) - 1;
14555c601bbSPeter Avalos break;
14660b4ad09SPeter Avalos }
14760b4ad09SPeter Avalos }
14860b4ad09SPeter Avalos
1498029ab02SPeter Avalos /* Note: mbrtowc() has a cleaner API, but mbtowc() seems a bit
1508029ab02SPeter Avalos * more portable, so we use that here instead. */
151d4d8193eSPeter Avalos if (mbtowc(NULL, NULL, 1) == -1) { /* Reset the shift state. */
152d4d8193eSPeter Avalos /* mbtowc() should never fail in practice, but
153d4d8193eSPeter Avalos * handle the theoretical error anyway. */
154d4d8193eSPeter Avalos free(fmtbuff_heap);
155d4d8193eSPeter Avalos return;
156d4d8193eSPeter Avalos }
1578029ab02SPeter Avalos
15860b4ad09SPeter Avalos /* Write data, expanding unprintable characters. */
1598029ab02SPeter Avalos p = fmtbuff;
16060b4ad09SPeter Avalos i = 0;
1618029ab02SPeter Avalos try_wc = 1;
16260b4ad09SPeter Avalos while (*p != '\0') {
16360b4ad09SPeter Avalos
1648029ab02SPeter Avalos /* Convert to wide char, test if the wide
1658029ab02SPeter Avalos * char is printable in the current locale. */
1668029ab02SPeter Avalos if (try_wc && (n = mbtowc(&wc, p, length)) != -1) {
1678029ab02SPeter Avalos length -= n;
1688029ab02SPeter Avalos if (iswprint(wc) && wc != L'\\') {
1698029ab02SPeter Avalos /* Printable, copy the bytes through. */
1708029ab02SPeter Avalos while (n-- > 0)
1718029ab02SPeter Avalos outbuff[i++] = *p++;
1728029ab02SPeter Avalos } else {
1738029ab02SPeter Avalos /* Not printable, format the bytes. */
1748029ab02SPeter Avalos while (n-- > 0)
1759c82a63eSPeter Avalos i += (unsigned)bsdtar_expand_char(
1768029ab02SPeter Avalos outbuff, i, *p++);
1778029ab02SPeter Avalos }
1788029ab02SPeter Avalos } else {
1798029ab02SPeter Avalos /* After any conversion failure, don't bother
1808029ab02SPeter Avalos * trying to convert the rest. */
1819c82a63eSPeter Avalos i += (unsigned)bsdtar_expand_char(outbuff, i, *p++);
1828029ab02SPeter Avalos try_wc = 0;
1838029ab02SPeter Avalos }
1848029ab02SPeter Avalos
1858029ab02SPeter Avalos /* If our output buffer is full, dump it and keep going. */
186e95abc47Szrj if (i > (sizeof(outbuff) - 128)) {
1879c82a63eSPeter Avalos outbuff[i] = '\0';
1888029ab02SPeter Avalos fprintf(f, "%s", outbuff);
1898029ab02SPeter Avalos i = 0;
1908029ab02SPeter Avalos }
1918029ab02SPeter Avalos }
1929c82a63eSPeter Avalos outbuff[i] = '\0';
1938029ab02SPeter Avalos fprintf(f, "%s", outbuff);
1948029ab02SPeter Avalos
1958029ab02SPeter Avalos /* If we allocated a heap-based formatting buffer, free it now. */
1968029ab02SPeter Avalos free(fmtbuff_heap);
1978029ab02SPeter Avalos }
1988029ab02SPeter Avalos
1998029ab02SPeter Avalos /*
2008029ab02SPeter Avalos * Render an arbitrary sequence of bytes into printable ASCII characters.
2018029ab02SPeter Avalos */
2028029ab02SPeter Avalos static size_t
bsdtar_expand_char(char * buff,size_t offset,char c)2038029ab02SPeter Avalos bsdtar_expand_char(char *buff, size_t offset, char c)
2048029ab02SPeter Avalos {
2058029ab02SPeter Avalos size_t i = offset;
2068029ab02SPeter Avalos
2078029ab02SPeter Avalos if (isprint((unsigned char)c) && c != '\\')
2088029ab02SPeter Avalos buff[i++] = c;
20960b4ad09SPeter Avalos else {
2108029ab02SPeter Avalos buff[i++] = '\\';
21160b4ad09SPeter Avalos switch (c) {
2128029ab02SPeter Avalos case '\a': buff[i++] = 'a'; break;
2138029ab02SPeter Avalos case '\b': buff[i++] = 'b'; break;
2148029ab02SPeter Avalos case '\f': buff[i++] = 'f'; break;
2158029ab02SPeter Avalos case '\n': buff[i++] = 'n'; break;
21660b4ad09SPeter Avalos #if '\r' != '\n'
21760b4ad09SPeter Avalos /* On some platforms, \n and \r are the same. */
2188029ab02SPeter Avalos case '\r': buff[i++] = 'r'; break;
21960b4ad09SPeter Avalos #endif
2208029ab02SPeter Avalos case '\t': buff[i++] = 't'; break;
2218029ab02SPeter Avalos case '\v': buff[i++] = 'v'; break;
2228029ab02SPeter Avalos case '\\': buff[i++] = '\\'; break;
22360b4ad09SPeter Avalos default:
2248029ab02SPeter Avalos sprintf(buff + i, "%03o", 0xFF & (int)c);
22560b4ad09SPeter Avalos i += 3;
22660b4ad09SPeter Avalos }
22760b4ad09SPeter Avalos }
22860b4ad09SPeter Avalos
2298029ab02SPeter Avalos return (i - offset);
23060b4ad09SPeter Avalos }
23160b4ad09SPeter Avalos
23260b4ad09SPeter Avalos int
yes(const char * fmt,...)23360b4ad09SPeter Avalos yes(const char *fmt, ...)
23460b4ad09SPeter Avalos {
23560b4ad09SPeter Avalos char buff[32];
23660b4ad09SPeter Avalos char *p;
23760b4ad09SPeter Avalos ssize_t l;
23860b4ad09SPeter Avalos
23960b4ad09SPeter Avalos va_list ap;
24060b4ad09SPeter Avalos va_start(ap, fmt);
24160b4ad09SPeter Avalos vfprintf(stderr, fmt, ap);
24260b4ad09SPeter Avalos va_end(ap);
24360b4ad09SPeter Avalos fprintf(stderr, " (y/N)? ");
24460b4ad09SPeter Avalos fflush(stderr);
24560b4ad09SPeter Avalos
24660b4ad09SPeter Avalos l = read(2, buff, sizeof(buff) - 1);
247c09f92d2SPeter Avalos if (l < 0) {
248c09f92d2SPeter Avalos fprintf(stderr, "Keyboard read failed\n");
249c09f92d2SPeter Avalos exit(1);
250c09f92d2SPeter Avalos }
251c09f92d2SPeter Avalos if (l == 0)
25260b4ad09SPeter Avalos return (0);
25360b4ad09SPeter Avalos buff[l] = 0;
25460b4ad09SPeter Avalos
25560b4ad09SPeter Avalos for (p = buff; *p != '\0'; p++) {
2568029ab02SPeter Avalos if (isspace((unsigned char)*p))
25760b4ad09SPeter Avalos continue;
25860b4ad09SPeter Avalos switch(*p) {
25960b4ad09SPeter Avalos case 'y': case 'Y':
26060b4ad09SPeter Avalos return (1);
26160b4ad09SPeter Avalos case 'n': case 'N':
26260b4ad09SPeter Avalos return (0);
26360b4ad09SPeter Avalos default:
26460b4ad09SPeter Avalos return (0);
26560b4ad09SPeter Avalos }
26660b4ad09SPeter Avalos }
26760b4ad09SPeter Avalos
26860b4ad09SPeter Avalos return (0);
26960b4ad09SPeter Avalos }
27060b4ad09SPeter Avalos
27160b4ad09SPeter Avalos /*-
27260b4ad09SPeter Avalos * The logic here for -C <dir> attempts to avoid
27360b4ad09SPeter Avalos * chdir() as long as possible. For example:
27460b4ad09SPeter Avalos * "-C /foo -C /bar file" needs chdir("/bar") but not chdir("/foo")
27560b4ad09SPeter Avalos * "-C /foo -C bar file" needs chdir("/foo/bar")
27660b4ad09SPeter Avalos * "-C /foo -C bar /file1" does not need chdir()
27760b4ad09SPeter Avalos * "-C /foo -C bar /file1 file2" needs chdir("/foo/bar") before file2
27860b4ad09SPeter Avalos *
27960b4ad09SPeter Avalos * The only correct way to handle this is to record a "pending" chdir
28060b4ad09SPeter Avalos * request and combine multiple requests intelligently until we
28160b4ad09SPeter Avalos * need to process a non-absolute file. set_chdir() adds the new dir
28260b4ad09SPeter Avalos * to the pending list; do_chdir() actually executes any pending chdir.
28360b4ad09SPeter Avalos *
28460b4ad09SPeter Avalos * This way, programs that build tar command lines don't have to worry
28560b4ad09SPeter Avalos * about -C with non-existent directories; such requests will only
28660b4ad09SPeter Avalos * fail if the directory must be accessed.
2879c82a63eSPeter Avalos *
28860b4ad09SPeter Avalos */
28960b4ad09SPeter Avalos void
set_chdir(struct bsdtar * bsdtar,const char * newdir)29060b4ad09SPeter Avalos set_chdir(struct bsdtar *bsdtar, const char *newdir)
29160b4ad09SPeter Avalos {
292c09f92d2SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
293c09f92d2SPeter Avalos if (newdir[0] == '/' || newdir[0] == '\\' ||
294c09f92d2SPeter Avalos /* Detect this type, for example, "C:\" or "C:/" */
295c09f92d2SPeter Avalos (((newdir[0] >= 'a' && newdir[0] <= 'z') ||
296c09f92d2SPeter Avalos (newdir[0] >= 'A' && newdir[0] <= 'Z')) &&
297c09f92d2SPeter Avalos newdir[1] == ':' && (newdir[2] == '/' || newdir[2] == '\\'))) {
298c09f92d2SPeter Avalos #else
29960b4ad09SPeter Avalos if (newdir[0] == '/') {
300c09f92d2SPeter Avalos #endif
30160b4ad09SPeter Avalos /* The -C /foo -C /bar case; dump first one. */
30260b4ad09SPeter Avalos free(bsdtar->pending_chdir);
30360b4ad09SPeter Avalos bsdtar->pending_chdir = NULL;
30460b4ad09SPeter Avalos }
30560b4ad09SPeter Avalos if (bsdtar->pending_chdir == NULL)
30660b4ad09SPeter Avalos /* Easy case: no previously-saved dir. */
30760b4ad09SPeter Avalos bsdtar->pending_chdir = strdup(newdir);
30860b4ad09SPeter Avalos else {
30960b4ad09SPeter Avalos /* The -C /foo -C bar case; concatenate */
31060b4ad09SPeter Avalos char *old_pending = bsdtar->pending_chdir;
31160b4ad09SPeter Avalos size_t old_len = strlen(old_pending);
31260b4ad09SPeter Avalos bsdtar->pending_chdir = malloc(old_len + strlen(newdir) + 2);
31360b4ad09SPeter Avalos if (old_pending[old_len - 1] == '/')
31460b4ad09SPeter Avalos old_pending[old_len - 1] = '\0';
31560b4ad09SPeter Avalos if (bsdtar->pending_chdir != NULL)
31660b4ad09SPeter Avalos sprintf(bsdtar->pending_chdir, "%s/%s",
31760b4ad09SPeter Avalos old_pending, newdir);
31860b4ad09SPeter Avalos free(old_pending);
31960b4ad09SPeter Avalos }
32060b4ad09SPeter Avalos if (bsdtar->pending_chdir == NULL)
3219c82a63eSPeter Avalos lafe_errc(1, errno, "No memory");
32260b4ad09SPeter Avalos }
32360b4ad09SPeter Avalos
32460b4ad09SPeter Avalos void
32560b4ad09SPeter Avalos do_chdir(struct bsdtar *bsdtar)
32660b4ad09SPeter Avalos {
32760b4ad09SPeter Avalos if (bsdtar->pending_chdir == NULL)
32860b4ad09SPeter Avalos return;
32960b4ad09SPeter Avalos
33060b4ad09SPeter Avalos if (chdir(bsdtar->pending_chdir) != 0) {
3319c82a63eSPeter Avalos lafe_errc(1, 0, "could not chdir to '%s'\n",
33260b4ad09SPeter Avalos bsdtar->pending_chdir);
33360b4ad09SPeter Avalos }
33460b4ad09SPeter Avalos free(bsdtar->pending_chdir);
33560b4ad09SPeter Avalos bsdtar->pending_chdir = NULL;
33660b4ad09SPeter Avalos }
33760b4ad09SPeter Avalos
3389c82a63eSPeter Avalos static const char *
3399c82a63eSPeter Avalos strip_components(const char *p, int elements)
3408029ab02SPeter Avalos {
3419c82a63eSPeter Avalos /* Skip as many elements as necessary. */
3428029ab02SPeter Avalos while (elements > 0) {
3438029ab02SPeter Avalos switch (*p++) {
3448029ab02SPeter Avalos case '/':
3459c82a63eSPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
3469c82a63eSPeter Avalos case '\\': /* Support \ path sep on Windows ONLY. */
3479c82a63eSPeter Avalos #endif
3488029ab02SPeter Avalos elements--;
3498029ab02SPeter Avalos break;
3508029ab02SPeter Avalos case '\0':
3518029ab02SPeter Avalos /* Path is too short, skip it. */
3528029ab02SPeter Avalos return (NULL);
3538029ab02SPeter Avalos }
3548029ab02SPeter Avalos }
3558029ab02SPeter Avalos
3569c82a63eSPeter Avalos /* Skip any / characters. This handles short paths that have
3579c82a63eSPeter Avalos * additional / termination. This also handles the case where
3589c82a63eSPeter Avalos * the logic above stops in the middle of a duplicate //
3599c82a63eSPeter Avalos * sequence (which would otherwise get converted to an
3609c82a63eSPeter Avalos * absolute path). */
3619c82a63eSPeter Avalos for (;;) {
3629c82a63eSPeter Avalos switch (*p) {
3639c82a63eSPeter Avalos case '/':
3649c82a63eSPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
3659c82a63eSPeter Avalos case '\\': /* Support \ path sep on Windows ONLY. */
3669c82a63eSPeter Avalos #endif
3679c82a63eSPeter Avalos ++p;
3689c82a63eSPeter Avalos break;
3699c82a63eSPeter Avalos case '\0':
3708029ab02SPeter Avalos return (NULL);
3719c82a63eSPeter Avalos default:
3729c82a63eSPeter Avalos return (p);
3739c82a63eSPeter Avalos }
3749c82a63eSPeter Avalos }
3758029ab02SPeter Avalos }
3768029ab02SPeter Avalos
3776b384f39SPeter Avalos static void
3786b384f39SPeter Avalos warn_strip_leading_char(struct bsdtar *bsdtar, const char *c)
37960b4ad09SPeter Avalos {
3806b384f39SPeter Avalos if (!bsdtar->warned_lead_slash) {
3816b384f39SPeter Avalos lafe_warnc(0,
3826b384f39SPeter Avalos "Removing leading '%c' from member names",
3836b384f39SPeter Avalos c[0]);
3846b384f39SPeter Avalos bsdtar->warned_lead_slash = 1;
3858029ab02SPeter Avalos }
3868029ab02SPeter Avalos }
3878029ab02SPeter Avalos
3886b384f39SPeter Avalos static void
3896b384f39SPeter Avalos warn_strip_drive_letter(struct bsdtar *bsdtar)
3906b384f39SPeter Avalos {
3916b384f39SPeter Avalos if (!bsdtar->warned_lead_slash) {
3926b384f39SPeter Avalos lafe_warnc(0,
3936b384f39SPeter Avalos "Removing leading drive letter from "
3946b384f39SPeter Avalos "member names");
3956b384f39SPeter Avalos bsdtar->warned_lead_slash = 1;
3966b384f39SPeter Avalos }
3976b384f39SPeter Avalos }
3986b384f39SPeter Avalos
3996b384f39SPeter Avalos /*
4006b384f39SPeter Avalos * Convert absolute path to non-absolute path by skipping leading
4016b384f39SPeter Avalos * absolute path prefixes.
4026b384f39SPeter Avalos */
4036b384f39SPeter Avalos static const char*
4046b384f39SPeter Avalos strip_absolute_path(struct bsdtar *bsdtar, const char *p)
4056b384f39SPeter Avalos {
4066b384f39SPeter Avalos const char *rp;
4078029ab02SPeter Avalos
4088029ab02SPeter Avalos /* Remove leading "//./" or "//?/" or "//?/UNC/"
4098029ab02SPeter Avalos * (absolute path prefixes used by Windows API) */
4108029ab02SPeter Avalos if ((p[0] == '/' || p[0] == '\\') &&
4118029ab02SPeter Avalos (p[1] == '/' || p[1] == '\\') &&
4128029ab02SPeter Avalos (p[2] == '.' || p[2] == '?') &&
4138029ab02SPeter Avalos (p[3] == '/' || p[3] == '\\'))
4148029ab02SPeter Avalos {
4158029ab02SPeter Avalos if (p[2] == '?' &&
4168029ab02SPeter Avalos (p[4] == 'U' || p[4] == 'u') &&
4178029ab02SPeter Avalos (p[5] == 'N' || p[5] == 'n') &&
4188029ab02SPeter Avalos (p[6] == 'C' || p[6] == 'c') &&
4198029ab02SPeter Avalos (p[7] == '/' || p[7] == '\\'))
4208029ab02SPeter Avalos p += 8;
4218029ab02SPeter Avalos else
4228029ab02SPeter Avalos p += 4;
4236b384f39SPeter Avalos warn_strip_drive_letter(bsdtar);
4248029ab02SPeter Avalos }
4256b384f39SPeter Avalos
4266b384f39SPeter Avalos /* Remove multiple leading slashes and Windows drive letters. */
4278029ab02SPeter Avalos do {
4288029ab02SPeter Avalos rp = p;
4298029ab02SPeter Avalos if (((p[0] >= 'a' && p[0] <= 'z') ||
4308029ab02SPeter Avalos (p[0] >= 'A' && p[0] <= 'Z')) &&
4318029ab02SPeter Avalos p[1] == ':') {
4328029ab02SPeter Avalos p += 2;
4336b384f39SPeter Avalos warn_strip_drive_letter(bsdtar);
4348029ab02SPeter Avalos }
4356b384f39SPeter Avalos
4366b384f39SPeter Avalos /* Remove leading "/../", "/./", "//", etc. */
4378029ab02SPeter Avalos while (p[0] == '/' || p[0] == '\\') {
4386b384f39SPeter Avalos if (p[1] == '.' &&
4396b384f39SPeter Avalos p[2] == '.' &&
4408029ab02SPeter Avalos (p[3] == '/' || p[3] == '\\')) {
4416b384f39SPeter Avalos p += 3; /* Remove "/..", leave "/" for next pass. */
4426b384f39SPeter Avalos } else if (p[1] == '.' &&
4436b384f39SPeter Avalos (p[2] == '/' || p[2] == '\\')) {
4446b384f39SPeter Avalos p += 2; /* Remove "/.", leave "/" for next pass. */
4458029ab02SPeter Avalos } else
4468029ab02SPeter Avalos p += 1; /* Remove "/". */
4476b384f39SPeter Avalos warn_strip_leading_char(bsdtar, rp);
4488029ab02SPeter Avalos }
4498029ab02SPeter Avalos } while (rp != p);
4508029ab02SPeter Avalos
4516b384f39SPeter Avalos return (p);
4528029ab02SPeter Avalos }
4538029ab02SPeter Avalos
4546b384f39SPeter Avalos /*
4556b384f39SPeter Avalos * Handle --strip-components and any future path-rewriting options.
4566b384f39SPeter Avalos * Returns non-zero if the pathname should not be extracted.
4576b384f39SPeter Avalos *
4586b384f39SPeter Avalos * Note: The rewrites are applied uniformly to pathnames and hardlink
4596b384f39SPeter Avalos * names but not to symlink bodies. This is deliberate: Symlink
4606b384f39SPeter Avalos * bodies are not necessarily filenames. Even when they are, they
4616b384f39SPeter Avalos * need to be interpreted relative to the directory containing them,
4626b384f39SPeter Avalos * so simple rewrites like this are rarely appropriate.
4636b384f39SPeter Avalos *
4646b384f39SPeter Avalos * TODO: Support pax-style regex path rewrites.
4656b384f39SPeter Avalos */
4666b384f39SPeter Avalos int
4676b384f39SPeter Avalos edit_pathname(struct bsdtar *bsdtar, struct archive_entry *entry)
4686b384f39SPeter Avalos {
4696b384f39SPeter Avalos const char *name = archive_entry_pathname(entry);
4706b384f39SPeter Avalos const char *original_name = name;
4716b384f39SPeter Avalos const char *hardlinkname = archive_entry_hardlink(entry);
4726b384f39SPeter Avalos const char *original_hardlinkname = hardlinkname;
4736b384f39SPeter Avalos #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
4746b384f39SPeter Avalos char *subst_name;
4756b384f39SPeter Avalos int r;
4766b384f39SPeter Avalos
4776b384f39SPeter Avalos /* Apply user-specified substitution to pathname. */
4786b384f39SPeter Avalos r = apply_substitution(bsdtar, name, &subst_name, 0, 0);
4796b384f39SPeter Avalos if (r == -1) {
4806b384f39SPeter Avalos lafe_warnc(0, "Invalid substitution, skipping entry");
4816b384f39SPeter Avalos return 1;
4826b384f39SPeter Avalos }
4836b384f39SPeter Avalos if (r == 1) {
4846b384f39SPeter Avalos archive_entry_copy_pathname(entry, subst_name);
4856b384f39SPeter Avalos if (*subst_name == '\0') {
4866b384f39SPeter Avalos free(subst_name);
4876b384f39SPeter Avalos return -1;
4886b384f39SPeter Avalos } else
4896b384f39SPeter Avalos free(subst_name);
4906b384f39SPeter Avalos name = archive_entry_pathname(entry);
4916b384f39SPeter Avalos original_name = name;
4926b384f39SPeter Avalos }
4936b384f39SPeter Avalos
4946b384f39SPeter Avalos /* Apply user-specified substitution to hardlink target. */
4956b384f39SPeter Avalos if (hardlinkname != NULL) {
4966b384f39SPeter Avalos r = apply_substitution(bsdtar, hardlinkname, &subst_name, 0, 1);
4976b384f39SPeter Avalos if (r == -1) {
4986b384f39SPeter Avalos lafe_warnc(0, "Invalid substitution, skipping entry");
4996b384f39SPeter Avalos return 1;
5006b384f39SPeter Avalos }
5016b384f39SPeter Avalos if (r == 1) {
5026b384f39SPeter Avalos archive_entry_copy_hardlink(entry, subst_name);
5036b384f39SPeter Avalos free(subst_name);
5046b384f39SPeter Avalos }
5056b384f39SPeter Avalos hardlinkname = archive_entry_hardlink(entry);
5066b384f39SPeter Avalos original_hardlinkname = hardlinkname;
5076b384f39SPeter Avalos }
5086b384f39SPeter Avalos
5096b384f39SPeter Avalos /* Apply user-specified substitution to symlink body. */
5106b384f39SPeter Avalos if (archive_entry_symlink(entry) != NULL) {
5116b384f39SPeter Avalos r = apply_substitution(bsdtar, archive_entry_symlink(entry), &subst_name, 1, 0);
5126b384f39SPeter Avalos if (r == -1) {
5136b384f39SPeter Avalos lafe_warnc(0, "Invalid substitution, skipping entry");
5146b384f39SPeter Avalos return 1;
5156b384f39SPeter Avalos }
5166b384f39SPeter Avalos if (r == 1) {
5176b384f39SPeter Avalos archive_entry_copy_symlink(entry, subst_name);
5186b384f39SPeter Avalos free(subst_name);
5196b384f39SPeter Avalos }
5206b384f39SPeter Avalos }
5216b384f39SPeter Avalos #endif
5226b384f39SPeter Avalos
5236b384f39SPeter Avalos /* Strip leading dir names as per --strip-components option. */
5246b384f39SPeter Avalos if (bsdtar->strip_components > 0) {
5256b384f39SPeter Avalos name = strip_components(name, bsdtar->strip_components);
5266b384f39SPeter Avalos if (name == NULL)
5276b384f39SPeter Avalos return (1);
5286b384f39SPeter Avalos
5296b384f39SPeter Avalos if (hardlinkname != NULL) {
5306b384f39SPeter Avalos hardlinkname = strip_components(hardlinkname,
5316b384f39SPeter Avalos bsdtar->strip_components);
5326b384f39SPeter Avalos if (hardlinkname == NULL)
5336b384f39SPeter Avalos return (1);
5346b384f39SPeter Avalos }
5356b384f39SPeter Avalos }
5366b384f39SPeter Avalos
537e95abc47Szrj if ((bsdtar->flags & OPTFLAG_ABSOLUTE_PATHS) == 0) {
5386b384f39SPeter Avalos /* By default, don't write or restore absolute pathnames. */
5396b384f39SPeter Avalos name = strip_absolute_path(bsdtar, name);
5406b384f39SPeter Avalos if (*name == '\0')
5418029ab02SPeter Avalos name = ".";
5426b384f39SPeter Avalos
5436b384f39SPeter Avalos if (hardlinkname != NULL) {
5446b384f39SPeter Avalos hardlinkname = strip_absolute_path(bsdtar, hardlinkname);
5456b384f39SPeter Avalos if (*hardlinkname == '\0')
5466b384f39SPeter Avalos return (1);
5476b384f39SPeter Avalos }
5488029ab02SPeter Avalos } else {
54960b4ad09SPeter Avalos /* Strip redundant leading '/' characters. */
55060b4ad09SPeter Avalos while (name[0] == '/' && name[1] == '/')
55160b4ad09SPeter Avalos name++;
55260b4ad09SPeter Avalos }
55360b4ad09SPeter Avalos
5546b384f39SPeter Avalos /* Replace name in archive_entry. */
5556b384f39SPeter Avalos if (name != original_name) {
5566b384f39SPeter Avalos archive_entry_copy_pathname(entry, name);
5576b384f39SPeter Avalos }
5586b384f39SPeter Avalos if (hardlinkname != original_hardlinkname) {
5596b384f39SPeter Avalos archive_entry_copy_hardlink(entry, hardlinkname);
56060b4ad09SPeter Avalos }
56160b4ad09SPeter Avalos return (0);
56260b4ad09SPeter Avalos }
56360b4ad09SPeter Avalos
56460b4ad09SPeter Avalos /*
5659c82a63eSPeter Avalos * It would be nice to just use printf() for formatting large numbers,
5669c82a63eSPeter Avalos * but the compatibility problems are quite a headache. Hence the
5679c82a63eSPeter Avalos * following simple utility function.
5689c82a63eSPeter Avalos */
5699c82a63eSPeter Avalos const char *
5709c82a63eSPeter Avalos tar_i64toa(int64_t n0)
5719c82a63eSPeter Avalos {
5729c82a63eSPeter Avalos static char buff[24];
573c09f92d2SPeter Avalos uint64_t n = n0 < 0 ? -n0 : n0;
5749c82a63eSPeter Avalos char *p = buff + sizeof(buff);
5759c82a63eSPeter Avalos
5769c82a63eSPeter Avalos *--p = '\0';
5779c82a63eSPeter Avalos do {
5789c82a63eSPeter Avalos *--p = '0' + (int)(n % 10);
579c09f92d2SPeter Avalos } while (n /= 10);
5809c82a63eSPeter Avalos if (n0 < 0)
5819c82a63eSPeter Avalos *--p = '-';
5829c82a63eSPeter Avalos return p;
5839c82a63eSPeter Avalos }
5849c82a63eSPeter Avalos
5859c82a63eSPeter Avalos /*
58660b4ad09SPeter Avalos * Like strcmp(), but try to be a little more aware of the fact that
58760b4ad09SPeter Avalos * we're comparing two paths. Right now, it just handles leading
58860b4ad09SPeter Avalos * "./" and trailing '/' specially, so that "a/b/" == "./a/b"
58960b4ad09SPeter Avalos *
59060b4ad09SPeter Avalos * TODO: Make this better, so that "./a//b/./c/" == "a/b/c"
59160b4ad09SPeter Avalos * TODO: After this works, push it down into libarchive.
59260b4ad09SPeter Avalos * TODO: Publish the path normalization routines in libarchive so
59360b4ad09SPeter Avalos * that bsdtar can normalize paths and use fast strcmp() instead
59460b4ad09SPeter Avalos * of this.
5959c82a63eSPeter Avalos *
5969c82a63eSPeter Avalos * Note: This is currently only used within write.c, so should
5979c82a63eSPeter Avalos * not handle \ path separators.
59860b4ad09SPeter Avalos */
59960b4ad09SPeter Avalos
60060b4ad09SPeter Avalos int
60160b4ad09SPeter Avalos pathcmp(const char *a, const char *b)
60260b4ad09SPeter Avalos {
60360b4ad09SPeter Avalos /* Skip leading './' */
60460b4ad09SPeter Avalos if (a[0] == '.' && a[1] == '/' && a[2] != '\0')
60560b4ad09SPeter Avalos a += 2;
60660b4ad09SPeter Avalos if (b[0] == '.' && b[1] == '/' && b[2] != '\0')
60760b4ad09SPeter Avalos b += 2;
60860b4ad09SPeter Avalos /* Find the first difference, or return (0) if none. */
60960b4ad09SPeter Avalos while (*a == *b) {
61060b4ad09SPeter Avalos if (*a == '\0')
61160b4ad09SPeter Avalos return (0);
61260b4ad09SPeter Avalos a++;
61360b4ad09SPeter Avalos b++;
61460b4ad09SPeter Avalos }
61560b4ad09SPeter Avalos /*
61660b4ad09SPeter Avalos * If one ends in '/' and the other one doesn't,
61760b4ad09SPeter Avalos * they're the same.
61860b4ad09SPeter Avalos */
61960b4ad09SPeter Avalos if (a[0] == '/' && a[1] == '\0' && b[0] == '\0')
62060b4ad09SPeter Avalos return (0);
62160b4ad09SPeter Avalos if (a[0] == '\0' && b[0] == '/' && b[1] == '\0')
62260b4ad09SPeter Avalos return (0);
62360b4ad09SPeter Avalos /* They're really different, return the correct sign. */
62460b4ad09SPeter Avalos return (*(const unsigned char *)a - *(const unsigned char *)b);
62560b4ad09SPeter Avalos }
6266b384f39SPeter Avalos
6276b384f39SPeter Avalos #define PPBUFF_SIZE 1024
6286b384f39SPeter Avalos const char *
6296b384f39SPeter Avalos passphrase_callback(struct archive *a, void *_client_data)
6306b384f39SPeter Avalos {
6316b384f39SPeter Avalos struct bsdtar *bsdtar = (struct bsdtar *)_client_data;
6326b384f39SPeter Avalos (void)a; /* UNUSED */
6336b384f39SPeter Avalos
6346b384f39SPeter Avalos if (bsdtar->ppbuff == NULL) {
6356b384f39SPeter Avalos bsdtar->ppbuff = malloc(PPBUFF_SIZE);
6366b384f39SPeter Avalos if (bsdtar->ppbuff == NULL)
6376b384f39SPeter Avalos lafe_errc(1, errno, "Out of memory");
6386b384f39SPeter Avalos }
6396b384f39SPeter Avalos return lafe_readpassphrase("Enter passphrase:",
6406b384f39SPeter Avalos bsdtar->ppbuff, PPBUFF_SIZE);
6416b384f39SPeter Avalos }
6426b384f39SPeter Avalos
6436b384f39SPeter Avalos void
6446b384f39SPeter Avalos passphrase_free(char *ppbuff)
6456b384f39SPeter Avalos {
6466b384f39SPeter Avalos if (ppbuff != NULL) {
6476b384f39SPeter Avalos memset(ppbuff, 0, PPBUFF_SIZE);
6486b384f39SPeter Avalos free(ppbuff);
6496b384f39SPeter Avalos }
6506b384f39SPeter Avalos }
6516b384f39SPeter Avalos
6526b384f39SPeter Avalos /*
6536b384f39SPeter Avalos * Display information about the current file.
6546b384f39SPeter Avalos *
6556b384f39SPeter Avalos * The format here roughly duplicates the output of 'ls -l'.
6566b384f39SPeter Avalos * This is based on SUSv2, where 'tar tv' is documented as
6576b384f39SPeter Avalos * listing additional information in an "unspecified format,"
6586b384f39SPeter Avalos * and 'pax -l' is documented as using the same format as 'ls -l'.
6596b384f39SPeter Avalos */
6606b384f39SPeter Avalos void
6616b384f39SPeter Avalos list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
6626b384f39SPeter Avalos {
6636b384f39SPeter Avalos char tmp[100];
6646b384f39SPeter Avalos size_t w;
6656b384f39SPeter Avalos const char *p;
6666b384f39SPeter Avalos const char *fmt;
6676b384f39SPeter Avalos time_t tim;
6686b384f39SPeter Avalos static time_t now;
669085658deSDaniel Fojt struct tm *ltime;
670085658deSDaniel Fojt #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
671085658deSDaniel Fojt struct tm tmbuf;
672085658deSDaniel Fojt #endif
673085658deSDaniel Fojt #if defined(HAVE__LOCALTIME64_S)
674085658deSDaniel Fojt errno_t terr;
675085658deSDaniel Fojt __time64_t tmptime;
676085658deSDaniel Fojt #endif
6776b384f39SPeter Avalos
6786b384f39SPeter Avalos /*
6796b384f39SPeter Avalos * We avoid collecting the entire list in memory at once by
6806b384f39SPeter Avalos * listing things as we see them. However, that also means we can't
6816b384f39SPeter Avalos * just pre-compute the field widths. Instead, we start with guesses
6826b384f39SPeter Avalos * and just widen them as necessary. These numbers are completely
6836b384f39SPeter Avalos * arbitrary.
6846b384f39SPeter Avalos */
6856b384f39SPeter Avalos if (!bsdtar->u_width) {
6866b384f39SPeter Avalos bsdtar->u_width = 6;
6876b384f39SPeter Avalos bsdtar->gs_width = 13;
6886b384f39SPeter Avalos }
6896b384f39SPeter Avalos if (!now)
6906b384f39SPeter Avalos time(&now);
6916b384f39SPeter Avalos fprintf(out, "%s %d ",
6926b384f39SPeter Avalos archive_entry_strmode(entry),
6936b384f39SPeter Avalos archive_entry_nlink(entry));
6946b384f39SPeter Avalos
6956b384f39SPeter Avalos /* Use uname if it's present, else uid. */
6966b384f39SPeter Avalos p = archive_entry_uname(entry);
6976b384f39SPeter Avalos if ((p == NULL) || (*p == '\0')) {
6986b384f39SPeter Avalos sprintf(tmp, "%lu ",
6996b384f39SPeter Avalos (unsigned long)archive_entry_uid(entry));
7006b384f39SPeter Avalos p = tmp;
7016b384f39SPeter Avalos }
7026b384f39SPeter Avalos w = strlen(p);
7036b384f39SPeter Avalos if (w > bsdtar->u_width)
7046b384f39SPeter Avalos bsdtar->u_width = w;
7056b384f39SPeter Avalos fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
7066b384f39SPeter Avalos
7076b384f39SPeter Avalos /* Use gname if it's present, else gid. */
7086b384f39SPeter Avalos p = archive_entry_gname(entry);
7096b384f39SPeter Avalos if (p != NULL && p[0] != '\0') {
7106b384f39SPeter Avalos fprintf(out, "%s", p);
7116b384f39SPeter Avalos w = strlen(p);
7126b384f39SPeter Avalos } else {
7136b384f39SPeter Avalos sprintf(tmp, "%lu",
7146b384f39SPeter Avalos (unsigned long)archive_entry_gid(entry));
7156b384f39SPeter Avalos w = strlen(tmp);
7166b384f39SPeter Avalos fprintf(out, "%s", tmp);
7176b384f39SPeter Avalos }
7186b384f39SPeter Avalos
7196b384f39SPeter Avalos /*
7206b384f39SPeter Avalos * Print device number or file size, right-aligned so as to make
7216b384f39SPeter Avalos * total width of group and devnum/filesize fields be gs_width.
7226b384f39SPeter Avalos * If gs_width is too small, grow it.
7236b384f39SPeter Avalos */
7246b384f39SPeter Avalos if (archive_entry_filetype(entry) == AE_IFCHR
7256b384f39SPeter Avalos || archive_entry_filetype(entry) == AE_IFBLK) {
7266b384f39SPeter Avalos sprintf(tmp, "%lu,%lu",
7276b384f39SPeter Avalos (unsigned long)archive_entry_rdevmajor(entry),
7286b384f39SPeter Avalos (unsigned long)archive_entry_rdevminor(entry));
7296b384f39SPeter Avalos } else {
7306b384f39SPeter Avalos strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
7316b384f39SPeter Avalos }
7326b384f39SPeter Avalos if (w + strlen(tmp) >= bsdtar->gs_width)
7336b384f39SPeter Avalos bsdtar->gs_width = w+strlen(tmp)+1;
7346b384f39SPeter Avalos fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
7356b384f39SPeter Avalos
7366b384f39SPeter Avalos /* Format the time using 'ls -l' conventions. */
7376b384f39SPeter Avalos tim = archive_entry_mtime(entry);
7386b384f39SPeter Avalos #define HALF_YEAR (time_t)365 * 86400 / 2
7396b384f39SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
7406b384f39SPeter Avalos #define DAY_FMT "%d" /* Windows' strftime function does not support %e format. */
7416b384f39SPeter Avalos #else
7426b384f39SPeter Avalos #define DAY_FMT "%e" /* Day number without leading zeros */
7436b384f39SPeter Avalos #endif
7446b384f39SPeter Avalos if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
7456b384f39SPeter Avalos fmt = bsdtar->day_first ? DAY_FMT " %b %Y" : "%b " DAY_FMT " %Y";
7466b384f39SPeter Avalos else
7476b384f39SPeter Avalos fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
748085658deSDaniel Fojt #if defined(HAVE_LOCALTIME_R)
749085658deSDaniel Fojt ltime = localtime_r(&tim, &tmbuf);
750085658deSDaniel Fojt #elif defined(HAVE__LOCALTIME64_S)
751085658deSDaniel Fojt tmptime = tim;
752085658deSDaniel Fojt terr = _localtime64_s(&tmbuf, &tmptime);
753085658deSDaniel Fojt if (terr)
754085658deSDaniel Fojt ltime = NULL;
755085658deSDaniel Fojt else
756085658deSDaniel Fojt ltime = &tmbuf;
757085658deSDaniel Fojt #else
758085658deSDaniel Fojt ltime = localtime(&tim);
759085658deSDaniel Fojt #endif
760085658deSDaniel Fojt strftime(tmp, sizeof(tmp), fmt, ltime);
7616b384f39SPeter Avalos fprintf(out, " %s ", tmp);
7626b384f39SPeter Avalos safe_fprintf(out, "%s", archive_entry_pathname(entry));
7636b384f39SPeter Avalos
7646b384f39SPeter Avalos /* Extra information for links. */
7656b384f39SPeter Avalos if (archive_entry_hardlink(entry)) /* Hard link */
7666b384f39SPeter Avalos safe_fprintf(out, " link to %s",
7676b384f39SPeter Avalos archive_entry_hardlink(entry));
7686b384f39SPeter Avalos else if (archive_entry_symlink(entry)) /* Symbolic link */
7696b384f39SPeter Avalos safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
7706b384f39SPeter Avalos }
771