xref: /dragonfly/contrib/libarchive/tar/util.c (revision 6b384f39)
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"
64*6b384f39SPeter 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
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. */
1438029ab02SPeter Avalos 			length = sizeof(fmtbuff_stack) - 1;
14455c601bbSPeter Avalos 			break;
14560b4ad09SPeter Avalos 		}
14660b4ad09SPeter Avalos 	}
14760b4ad09SPeter Avalos 
1488029ab02SPeter Avalos 	/* Note: mbrtowc() has a cleaner API, but mbtowc() seems a bit
1498029ab02SPeter Avalos 	 * more portable, so we use that here instead. */
150d4d8193eSPeter Avalos 	if (mbtowc(NULL, NULL, 1) == -1) { /* Reset the shift state. */
151d4d8193eSPeter Avalos 		/* mbtowc() should never fail in practice, but
152d4d8193eSPeter Avalos 		 * handle the theoretical error anyway. */
153d4d8193eSPeter Avalos 		free(fmtbuff_heap);
154d4d8193eSPeter Avalos 		return;
155d4d8193eSPeter Avalos 	}
1568029ab02SPeter Avalos 
15760b4ad09SPeter Avalos 	/* Write data, expanding unprintable characters. */
1588029ab02SPeter Avalos 	p = fmtbuff;
15960b4ad09SPeter Avalos 	i = 0;
1608029ab02SPeter Avalos 	try_wc = 1;
16160b4ad09SPeter Avalos 	while (*p != '\0') {
16260b4ad09SPeter Avalos 
1638029ab02SPeter Avalos 		/* Convert to wide char, test if the wide
1648029ab02SPeter Avalos 		 * char is printable in the current locale. */
1658029ab02SPeter Avalos 		if (try_wc && (n = mbtowc(&wc, p, length)) != -1) {
1668029ab02SPeter Avalos 			length -= n;
1678029ab02SPeter Avalos 			if (iswprint(wc) && wc != L'\\') {
1688029ab02SPeter Avalos 				/* Printable, copy the bytes through. */
1698029ab02SPeter Avalos 				while (n-- > 0)
1708029ab02SPeter Avalos 					outbuff[i++] = *p++;
1718029ab02SPeter Avalos 			} else {
1728029ab02SPeter Avalos 				/* Not printable, format the bytes. */
1738029ab02SPeter Avalos 				while (n-- > 0)
1749c82a63eSPeter Avalos 					i += (unsigned)bsdtar_expand_char(
1758029ab02SPeter Avalos 					    outbuff, i, *p++);
1768029ab02SPeter Avalos 			}
1778029ab02SPeter Avalos 		} else {
1788029ab02SPeter Avalos 			/* After any conversion failure, don't bother
1798029ab02SPeter Avalos 			 * trying to convert the rest. */
1809c82a63eSPeter Avalos 			i += (unsigned)bsdtar_expand_char(outbuff, i, *p++);
1818029ab02SPeter Avalos 			try_wc = 0;
1828029ab02SPeter Avalos 		}
1838029ab02SPeter Avalos 
1848029ab02SPeter Avalos 		/* If our output buffer is full, dump it and keep going. */
1858029ab02SPeter Avalos 		if (i > (sizeof(outbuff) - 20)) {
1869c82a63eSPeter Avalos 			outbuff[i] = '\0';
1878029ab02SPeter Avalos 			fprintf(f, "%s", outbuff);
1888029ab02SPeter Avalos 			i = 0;
1898029ab02SPeter Avalos 		}
1908029ab02SPeter Avalos 	}
1919c82a63eSPeter Avalos 	outbuff[i] = '\0';
1928029ab02SPeter Avalos 	fprintf(f, "%s", outbuff);
1938029ab02SPeter Avalos 
1948029ab02SPeter Avalos 	/* If we allocated a heap-based formatting buffer, free it now. */
1958029ab02SPeter Avalos 	free(fmtbuff_heap);
1968029ab02SPeter Avalos }
1978029ab02SPeter Avalos 
1988029ab02SPeter Avalos /*
1998029ab02SPeter Avalos  * Render an arbitrary sequence of bytes into printable ASCII characters.
2008029ab02SPeter Avalos  */
2018029ab02SPeter Avalos static size_t
2028029ab02SPeter Avalos bsdtar_expand_char(char *buff, size_t offset, char c)
2038029ab02SPeter Avalos {
2048029ab02SPeter Avalos 	size_t i = offset;
2058029ab02SPeter Avalos 
2068029ab02SPeter Avalos 	if (isprint((unsigned char)c) && c != '\\')
2078029ab02SPeter Avalos 		buff[i++] = c;
20860b4ad09SPeter Avalos 	else {
2098029ab02SPeter Avalos 		buff[i++] = '\\';
21060b4ad09SPeter Avalos 		switch (c) {
2118029ab02SPeter Avalos 		case '\a': buff[i++] = 'a'; break;
2128029ab02SPeter Avalos 		case '\b': buff[i++] = 'b'; break;
2138029ab02SPeter Avalos 		case '\f': buff[i++] = 'f'; break;
2148029ab02SPeter Avalos 		case '\n': buff[i++] = 'n'; break;
21560b4ad09SPeter Avalos #if '\r' != '\n'
21660b4ad09SPeter Avalos 		/* On some platforms, \n and \r are the same. */
2178029ab02SPeter Avalos 		case '\r': buff[i++] = 'r'; break;
21860b4ad09SPeter Avalos #endif
2198029ab02SPeter Avalos 		case '\t': buff[i++] = 't'; break;
2208029ab02SPeter Avalos 		case '\v': buff[i++] = 'v'; break;
2218029ab02SPeter Avalos 		case '\\': buff[i++] = '\\'; break;
22260b4ad09SPeter Avalos 		default:
2238029ab02SPeter Avalos 			sprintf(buff + i, "%03o", 0xFF & (int)c);
22460b4ad09SPeter Avalos 			i += 3;
22560b4ad09SPeter Avalos 		}
22660b4ad09SPeter Avalos 	}
22760b4ad09SPeter Avalos 
2288029ab02SPeter Avalos 	return (i - offset);
22960b4ad09SPeter Avalos }
23060b4ad09SPeter Avalos 
23160b4ad09SPeter Avalos int
23260b4ad09SPeter Avalos yes(const char *fmt, ...)
23360b4ad09SPeter Avalos {
23460b4ad09SPeter Avalos 	char buff[32];
23560b4ad09SPeter Avalos 	char *p;
23660b4ad09SPeter Avalos 	ssize_t l;
23760b4ad09SPeter Avalos 
23860b4ad09SPeter Avalos 	va_list ap;
23960b4ad09SPeter Avalos 	va_start(ap, fmt);
24060b4ad09SPeter Avalos 	vfprintf(stderr, fmt, ap);
24160b4ad09SPeter Avalos 	va_end(ap);
24260b4ad09SPeter Avalos 	fprintf(stderr, " (y/N)? ");
24360b4ad09SPeter Avalos 	fflush(stderr);
24460b4ad09SPeter Avalos 
24560b4ad09SPeter Avalos 	l = read(2, buff, sizeof(buff) - 1);
246c09f92d2SPeter Avalos 	if (l < 0) {
247c09f92d2SPeter Avalos 	  fprintf(stderr, "Keyboard read failed\n");
248c09f92d2SPeter Avalos 	  exit(1);
249c09f92d2SPeter Avalos 	}
250c09f92d2SPeter Avalos 	if (l == 0)
25160b4ad09SPeter Avalos 		return (0);
25260b4ad09SPeter Avalos 	buff[l] = 0;
25360b4ad09SPeter Avalos 
25460b4ad09SPeter Avalos 	for (p = buff; *p != '\0'; p++) {
2558029ab02SPeter Avalos 		if (isspace((unsigned char)*p))
25660b4ad09SPeter Avalos 			continue;
25760b4ad09SPeter Avalos 		switch(*p) {
25860b4ad09SPeter Avalos 		case 'y': case 'Y':
25960b4ad09SPeter Avalos 			return (1);
26060b4ad09SPeter Avalos 		case 'n': case 'N':
26160b4ad09SPeter Avalos 			return (0);
26260b4ad09SPeter Avalos 		default:
26360b4ad09SPeter Avalos 			return (0);
26460b4ad09SPeter Avalos 		}
26560b4ad09SPeter Avalos 	}
26660b4ad09SPeter Avalos 
26760b4ad09SPeter Avalos 	return (0);
26860b4ad09SPeter Avalos }
26960b4ad09SPeter Avalos 
27060b4ad09SPeter Avalos /*-
27160b4ad09SPeter Avalos  * The logic here for -C <dir> attempts to avoid
27260b4ad09SPeter Avalos  * chdir() as long as possible.  For example:
27360b4ad09SPeter Avalos  * "-C /foo -C /bar file"          needs chdir("/bar") but not chdir("/foo")
27460b4ad09SPeter Avalos  * "-C /foo -C bar file"           needs chdir("/foo/bar")
27560b4ad09SPeter Avalos  * "-C /foo -C bar /file1"         does not need chdir()
27660b4ad09SPeter Avalos  * "-C /foo -C bar /file1 file2"   needs chdir("/foo/bar") before file2
27760b4ad09SPeter Avalos  *
27860b4ad09SPeter Avalos  * The only correct way to handle this is to record a "pending" chdir
27960b4ad09SPeter Avalos  * request and combine multiple requests intelligently until we
28060b4ad09SPeter Avalos  * need to process a non-absolute file.  set_chdir() adds the new dir
28160b4ad09SPeter Avalos  * to the pending list; do_chdir() actually executes any pending chdir.
28260b4ad09SPeter Avalos  *
28360b4ad09SPeter Avalos  * This way, programs that build tar command lines don't have to worry
28460b4ad09SPeter Avalos  * about -C with non-existent directories; such requests will only
28560b4ad09SPeter Avalos  * fail if the directory must be accessed.
2869c82a63eSPeter Avalos  *
28760b4ad09SPeter Avalos  */
28860b4ad09SPeter Avalos void
28960b4ad09SPeter Avalos set_chdir(struct bsdtar *bsdtar, const char *newdir)
29060b4ad09SPeter Avalos {
291c09f92d2SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
292c09f92d2SPeter Avalos 	if (newdir[0] == '/' || newdir[0] == '\\' ||
293c09f92d2SPeter Avalos 	    /* Detect this type, for example, "C:\" or "C:/" */
294c09f92d2SPeter Avalos 	    (((newdir[0] >= 'a' && newdir[0] <= 'z') ||
295c09f92d2SPeter Avalos 	      (newdir[0] >= 'A' && newdir[0] <= 'Z')) &&
296c09f92d2SPeter Avalos 	    newdir[1] == ':' && (newdir[2] == '/' || newdir[2] == '\\'))) {
297c09f92d2SPeter Avalos #else
29860b4ad09SPeter Avalos 	if (newdir[0] == '/') {
299c09f92d2SPeter Avalos #endif
30060b4ad09SPeter Avalos 		/* The -C /foo -C /bar case; dump first one. */
30160b4ad09SPeter Avalos 		free(bsdtar->pending_chdir);
30260b4ad09SPeter Avalos 		bsdtar->pending_chdir = NULL;
30360b4ad09SPeter Avalos 	}
30460b4ad09SPeter Avalos 	if (bsdtar->pending_chdir == NULL)
30560b4ad09SPeter Avalos 		/* Easy case: no previously-saved dir. */
30660b4ad09SPeter Avalos 		bsdtar->pending_chdir = strdup(newdir);
30760b4ad09SPeter Avalos 	else {
30860b4ad09SPeter Avalos 		/* The -C /foo -C bar case; concatenate */
30960b4ad09SPeter Avalos 		char *old_pending = bsdtar->pending_chdir;
31060b4ad09SPeter Avalos 		size_t old_len = strlen(old_pending);
31160b4ad09SPeter Avalos 		bsdtar->pending_chdir = malloc(old_len + strlen(newdir) + 2);
31260b4ad09SPeter Avalos 		if (old_pending[old_len - 1] == '/')
31360b4ad09SPeter Avalos 			old_pending[old_len - 1] = '\0';
31460b4ad09SPeter Avalos 		if (bsdtar->pending_chdir != NULL)
31560b4ad09SPeter Avalos 			sprintf(bsdtar->pending_chdir, "%s/%s",
31660b4ad09SPeter Avalos 			    old_pending, newdir);
31760b4ad09SPeter Avalos 		free(old_pending);
31860b4ad09SPeter Avalos 	}
31960b4ad09SPeter Avalos 	if (bsdtar->pending_chdir == NULL)
3209c82a63eSPeter Avalos 		lafe_errc(1, errno, "No memory");
32160b4ad09SPeter Avalos }
32260b4ad09SPeter Avalos 
32360b4ad09SPeter Avalos void
32460b4ad09SPeter Avalos do_chdir(struct bsdtar *bsdtar)
32560b4ad09SPeter Avalos {
32660b4ad09SPeter Avalos 	if (bsdtar->pending_chdir == NULL)
32760b4ad09SPeter Avalos 		return;
32860b4ad09SPeter Avalos 
32960b4ad09SPeter Avalos 	if (chdir(bsdtar->pending_chdir) != 0) {
3309c82a63eSPeter Avalos 		lafe_errc(1, 0, "could not chdir to '%s'\n",
33160b4ad09SPeter Avalos 		    bsdtar->pending_chdir);
33260b4ad09SPeter Avalos 	}
33360b4ad09SPeter Avalos 	free(bsdtar->pending_chdir);
33460b4ad09SPeter Avalos 	bsdtar->pending_chdir = NULL;
33560b4ad09SPeter Avalos }
33660b4ad09SPeter Avalos 
3379c82a63eSPeter Avalos static const char *
3389c82a63eSPeter Avalos strip_components(const char *p, int elements)
3398029ab02SPeter Avalos {
3409c82a63eSPeter Avalos 	/* Skip as many elements as necessary. */
3418029ab02SPeter Avalos 	while (elements > 0) {
3428029ab02SPeter Avalos 		switch (*p++) {
3438029ab02SPeter Avalos 		case '/':
3449c82a63eSPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
3459c82a63eSPeter Avalos 		case '\\': /* Support \ path sep on Windows ONLY. */
3469c82a63eSPeter Avalos #endif
3478029ab02SPeter Avalos 			elements--;
3488029ab02SPeter Avalos 			break;
3498029ab02SPeter Avalos 		case '\0':
3508029ab02SPeter Avalos 			/* Path is too short, skip it. */
3518029ab02SPeter Avalos 			return (NULL);
3528029ab02SPeter Avalos 		}
3538029ab02SPeter Avalos 	}
3548029ab02SPeter Avalos 
3559c82a63eSPeter Avalos 	/* Skip any / characters.  This handles short paths that have
3569c82a63eSPeter Avalos 	 * additional / termination.  This also handles the case where
3579c82a63eSPeter Avalos 	 * the logic above stops in the middle of a duplicate //
3589c82a63eSPeter Avalos 	 * sequence (which would otherwise get converted to an
3599c82a63eSPeter Avalos 	 * absolute path). */
3609c82a63eSPeter Avalos 	for (;;) {
3619c82a63eSPeter Avalos 		switch (*p) {
3629c82a63eSPeter Avalos 		case '/':
3639c82a63eSPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
3649c82a63eSPeter Avalos 		case '\\': /* Support \ path sep on Windows ONLY. */
3659c82a63eSPeter Avalos #endif
3669c82a63eSPeter Avalos 			++p;
3679c82a63eSPeter Avalos 			break;
3689c82a63eSPeter Avalos 		case '\0':
3698029ab02SPeter Avalos 			return (NULL);
3709c82a63eSPeter Avalos 		default:
3719c82a63eSPeter Avalos 			return (p);
3729c82a63eSPeter Avalos 		}
3739c82a63eSPeter Avalos 	}
3748029ab02SPeter Avalos }
3758029ab02SPeter Avalos 
376*6b384f39SPeter Avalos static void
377*6b384f39SPeter Avalos warn_strip_leading_char(struct bsdtar *bsdtar, const char *c)
37860b4ad09SPeter Avalos {
379*6b384f39SPeter Avalos 	if (!bsdtar->warned_lead_slash) {
380*6b384f39SPeter Avalos 		lafe_warnc(0,
381*6b384f39SPeter Avalos 			   "Removing leading '%c' from member names",
382*6b384f39SPeter Avalos 			   c[0]);
383*6b384f39SPeter Avalos 		bsdtar->warned_lead_slash = 1;
3848029ab02SPeter Avalos 	}
3858029ab02SPeter Avalos }
3868029ab02SPeter Avalos 
387*6b384f39SPeter Avalos static void
388*6b384f39SPeter Avalos warn_strip_drive_letter(struct bsdtar *bsdtar)
389*6b384f39SPeter Avalos {
390*6b384f39SPeter Avalos 	if (!bsdtar->warned_lead_slash) {
391*6b384f39SPeter Avalos 		lafe_warnc(0,
392*6b384f39SPeter Avalos 			   "Removing leading drive letter from "
393*6b384f39SPeter Avalos 			   "member names");
394*6b384f39SPeter Avalos 		bsdtar->warned_lead_slash = 1;
395*6b384f39SPeter Avalos 	}
396*6b384f39SPeter Avalos }
397*6b384f39SPeter Avalos 
398*6b384f39SPeter Avalos /*
399*6b384f39SPeter Avalos  * Convert absolute path to non-absolute path by skipping leading
400*6b384f39SPeter Avalos  * absolute path prefixes.
401*6b384f39SPeter Avalos  */
402*6b384f39SPeter Avalos static const char*
403*6b384f39SPeter Avalos strip_absolute_path(struct bsdtar *bsdtar, const char *p)
404*6b384f39SPeter Avalos {
405*6b384f39SPeter Avalos 	const char *rp;
4068029ab02SPeter Avalos 
4078029ab02SPeter Avalos 	/* Remove leading "//./" or "//?/" or "//?/UNC/"
4088029ab02SPeter Avalos 	 * (absolute path prefixes used by Windows API) */
4098029ab02SPeter Avalos 	if ((p[0] == '/' || p[0] == '\\') &&
4108029ab02SPeter Avalos 	    (p[1] == '/' || p[1] == '\\') &&
4118029ab02SPeter Avalos 	    (p[2] == '.' || p[2] == '?') &&
4128029ab02SPeter Avalos 	    (p[3] == '/' || p[3] == '\\'))
4138029ab02SPeter Avalos 	{
4148029ab02SPeter Avalos 		if (p[2] == '?' &&
4158029ab02SPeter Avalos 		    (p[4] == 'U' || p[4] == 'u') &&
4168029ab02SPeter Avalos 		    (p[5] == 'N' || p[5] == 'n') &&
4178029ab02SPeter Avalos 		    (p[6] == 'C' || p[6] == 'c') &&
4188029ab02SPeter Avalos 		    (p[7] == '/' || p[7] == '\\'))
4198029ab02SPeter Avalos 			p += 8;
4208029ab02SPeter Avalos 		else
4218029ab02SPeter Avalos 			p += 4;
422*6b384f39SPeter Avalos 		warn_strip_drive_letter(bsdtar);
4238029ab02SPeter Avalos 	}
424*6b384f39SPeter Avalos 
425*6b384f39SPeter Avalos 	/* Remove multiple leading slashes and Windows drive letters. */
4268029ab02SPeter Avalos 	do {
4278029ab02SPeter Avalos 		rp = p;
4288029ab02SPeter Avalos 		if (((p[0] >= 'a' && p[0] <= 'z') ||
4298029ab02SPeter Avalos 		     (p[0] >= 'A' && p[0] <= 'Z')) &&
4308029ab02SPeter Avalos 		    p[1] == ':') {
4318029ab02SPeter Avalos 			p += 2;
432*6b384f39SPeter Avalos 			warn_strip_drive_letter(bsdtar);
4338029ab02SPeter Avalos 		}
434*6b384f39SPeter Avalos 
435*6b384f39SPeter Avalos 		/* Remove leading "/../", "/./", "//", etc. */
4368029ab02SPeter Avalos 		while (p[0] == '/' || p[0] == '\\') {
437*6b384f39SPeter Avalos 			if (p[1] == '.' &&
438*6b384f39SPeter Avalos 			    p[2] == '.' &&
4398029ab02SPeter Avalos 			    (p[3] == '/' || p[3] == '\\')) {
440*6b384f39SPeter Avalos 				p += 3; /* Remove "/..", leave "/" for next pass. */
441*6b384f39SPeter Avalos 			} else if (p[1] == '.' &&
442*6b384f39SPeter Avalos 				   (p[2] == '/' || p[2] == '\\')) {
443*6b384f39SPeter Avalos 				p += 2; /* Remove "/.", leave "/" for next pass. */
4448029ab02SPeter Avalos 			} else
4458029ab02SPeter Avalos 				p += 1; /* Remove "/". */
446*6b384f39SPeter Avalos 			warn_strip_leading_char(bsdtar, rp);
4478029ab02SPeter Avalos 		}
4488029ab02SPeter Avalos 	} while (rp != p);
4498029ab02SPeter Avalos 
450*6b384f39SPeter Avalos 	return (p);
4518029ab02SPeter Avalos }
4528029ab02SPeter Avalos 
453*6b384f39SPeter Avalos /*
454*6b384f39SPeter Avalos  * Handle --strip-components and any future path-rewriting options.
455*6b384f39SPeter Avalos  * Returns non-zero if the pathname should not be extracted.
456*6b384f39SPeter Avalos  *
457*6b384f39SPeter Avalos  * Note: The rewrites are applied uniformly to pathnames and hardlink
458*6b384f39SPeter Avalos  * names but not to symlink bodies.  This is deliberate: Symlink
459*6b384f39SPeter Avalos  * bodies are not necessarily filenames.  Even when they are, they
460*6b384f39SPeter Avalos  * need to be interpreted relative to the directory containing them,
461*6b384f39SPeter Avalos  * so simple rewrites like this are rarely appropriate.
462*6b384f39SPeter Avalos  *
463*6b384f39SPeter Avalos  * TODO: Support pax-style regex path rewrites.
464*6b384f39SPeter Avalos  */
465*6b384f39SPeter Avalos int
466*6b384f39SPeter Avalos edit_pathname(struct bsdtar *bsdtar, struct archive_entry *entry)
467*6b384f39SPeter Avalos {
468*6b384f39SPeter Avalos 	const char *name = archive_entry_pathname(entry);
469*6b384f39SPeter Avalos 	const char *original_name = name;
470*6b384f39SPeter Avalos 	const char *hardlinkname = archive_entry_hardlink(entry);
471*6b384f39SPeter Avalos 	const char *original_hardlinkname = hardlinkname;
472*6b384f39SPeter Avalos #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
473*6b384f39SPeter Avalos 	char *subst_name;
474*6b384f39SPeter Avalos 	int r;
475*6b384f39SPeter Avalos 
476*6b384f39SPeter Avalos 	/* Apply user-specified substitution to pathname. */
477*6b384f39SPeter Avalos 	r = apply_substitution(bsdtar, name, &subst_name, 0, 0);
478*6b384f39SPeter Avalos 	if (r == -1) {
479*6b384f39SPeter Avalos 		lafe_warnc(0, "Invalid substitution, skipping entry");
480*6b384f39SPeter Avalos 		return 1;
481*6b384f39SPeter Avalos 	}
482*6b384f39SPeter Avalos 	if (r == 1) {
483*6b384f39SPeter Avalos 		archive_entry_copy_pathname(entry, subst_name);
484*6b384f39SPeter Avalos 		if (*subst_name == '\0') {
485*6b384f39SPeter Avalos 			free(subst_name);
486*6b384f39SPeter Avalos 			return -1;
487*6b384f39SPeter Avalos 		} else
488*6b384f39SPeter Avalos 			free(subst_name);
489*6b384f39SPeter Avalos 		name = archive_entry_pathname(entry);
490*6b384f39SPeter Avalos 		original_name = name;
491*6b384f39SPeter Avalos 	}
492*6b384f39SPeter Avalos 
493*6b384f39SPeter Avalos 	/* Apply user-specified substitution to hardlink target. */
494*6b384f39SPeter Avalos 	if (hardlinkname != NULL) {
495*6b384f39SPeter Avalos 		r = apply_substitution(bsdtar, hardlinkname, &subst_name, 0, 1);
496*6b384f39SPeter Avalos 		if (r == -1) {
497*6b384f39SPeter Avalos 			lafe_warnc(0, "Invalid substitution, skipping entry");
498*6b384f39SPeter Avalos 			return 1;
499*6b384f39SPeter Avalos 		}
500*6b384f39SPeter Avalos 		if (r == 1) {
501*6b384f39SPeter Avalos 			archive_entry_copy_hardlink(entry, subst_name);
502*6b384f39SPeter Avalos 			free(subst_name);
503*6b384f39SPeter Avalos 		}
504*6b384f39SPeter Avalos 		hardlinkname = archive_entry_hardlink(entry);
505*6b384f39SPeter Avalos 		original_hardlinkname = hardlinkname;
506*6b384f39SPeter Avalos 	}
507*6b384f39SPeter Avalos 
508*6b384f39SPeter Avalos 	/* Apply user-specified substitution to symlink body. */
509*6b384f39SPeter Avalos 	if (archive_entry_symlink(entry) != NULL) {
510*6b384f39SPeter Avalos 		r = apply_substitution(bsdtar, archive_entry_symlink(entry), &subst_name, 1, 0);
511*6b384f39SPeter Avalos 		if (r == -1) {
512*6b384f39SPeter Avalos 			lafe_warnc(0, "Invalid substitution, skipping entry");
513*6b384f39SPeter Avalos 			return 1;
514*6b384f39SPeter Avalos 		}
515*6b384f39SPeter Avalos 		if (r == 1) {
516*6b384f39SPeter Avalos 			archive_entry_copy_symlink(entry, subst_name);
517*6b384f39SPeter Avalos 			free(subst_name);
518*6b384f39SPeter Avalos 		}
519*6b384f39SPeter Avalos 	}
520*6b384f39SPeter Avalos #endif
521*6b384f39SPeter Avalos 
522*6b384f39SPeter Avalos 	/* Strip leading dir names as per --strip-components option. */
523*6b384f39SPeter Avalos 	if (bsdtar->strip_components > 0) {
524*6b384f39SPeter Avalos 		name = strip_components(name, bsdtar->strip_components);
525*6b384f39SPeter Avalos 		if (name == NULL)
526*6b384f39SPeter Avalos 			return (1);
527*6b384f39SPeter Avalos 
528*6b384f39SPeter Avalos 		if (hardlinkname != NULL) {
529*6b384f39SPeter Avalos 			hardlinkname = strip_components(hardlinkname,
530*6b384f39SPeter Avalos 			    bsdtar->strip_components);
531*6b384f39SPeter Avalos 			if (hardlinkname == NULL)
532*6b384f39SPeter Avalos 				return (1);
533*6b384f39SPeter Avalos 		}
534*6b384f39SPeter Avalos 	}
535*6b384f39SPeter Avalos 
536*6b384f39SPeter Avalos 	if (!bsdtar->option_absolute_paths) {
537*6b384f39SPeter Avalos 		/* By default, don't write or restore absolute pathnames. */
538*6b384f39SPeter Avalos 		name = strip_absolute_path(bsdtar, name);
539*6b384f39SPeter Avalos 		if (*name == '\0')
5408029ab02SPeter Avalos 			name = ".";
541*6b384f39SPeter Avalos 
542*6b384f39SPeter Avalos 		if (hardlinkname != NULL) {
543*6b384f39SPeter Avalos 			hardlinkname = strip_absolute_path(bsdtar, hardlinkname);
544*6b384f39SPeter Avalos 			if (*hardlinkname == '\0')
545*6b384f39SPeter Avalos 				return (1);
546*6b384f39SPeter Avalos 		}
5478029ab02SPeter Avalos 	} else {
54860b4ad09SPeter Avalos 		/* Strip redundant leading '/' characters. */
54960b4ad09SPeter Avalos 		while (name[0] == '/' && name[1] == '/')
55060b4ad09SPeter Avalos 			name++;
55160b4ad09SPeter Avalos 	}
55260b4ad09SPeter Avalos 
553*6b384f39SPeter Avalos 	/* Replace name in archive_entry. */
554*6b384f39SPeter Avalos 	if (name != original_name) {
555*6b384f39SPeter Avalos 		archive_entry_copy_pathname(entry, name);
556*6b384f39SPeter Avalos 	}
557*6b384f39SPeter Avalos 	if (hardlinkname != original_hardlinkname) {
558*6b384f39SPeter Avalos 		archive_entry_copy_hardlink(entry, hardlinkname);
55960b4ad09SPeter Avalos 	}
56060b4ad09SPeter Avalos 	return (0);
56160b4ad09SPeter Avalos }
56260b4ad09SPeter Avalos 
56360b4ad09SPeter Avalos /*
5649c82a63eSPeter Avalos  * It would be nice to just use printf() for formatting large numbers,
5659c82a63eSPeter Avalos  * but the compatibility problems are quite a headache.  Hence the
5669c82a63eSPeter Avalos  * following simple utility function.
5679c82a63eSPeter Avalos  */
5689c82a63eSPeter Avalos const char *
5699c82a63eSPeter Avalos tar_i64toa(int64_t n0)
5709c82a63eSPeter Avalos {
5719c82a63eSPeter Avalos 	static char buff[24];
572c09f92d2SPeter Avalos 	uint64_t n = n0 < 0 ? -n0 : n0;
5739c82a63eSPeter Avalos 	char *p = buff + sizeof(buff);
5749c82a63eSPeter Avalos 
5759c82a63eSPeter Avalos 	*--p = '\0';
5769c82a63eSPeter Avalos 	do {
5779c82a63eSPeter Avalos 		*--p = '0' + (int)(n % 10);
578c09f92d2SPeter Avalos 	} while (n /= 10);
5799c82a63eSPeter Avalos 	if (n0 < 0)
5809c82a63eSPeter Avalos 		*--p = '-';
5819c82a63eSPeter Avalos 	return p;
5829c82a63eSPeter Avalos }
5839c82a63eSPeter Avalos 
5849c82a63eSPeter Avalos /*
58560b4ad09SPeter Avalos  * Like strcmp(), but try to be a little more aware of the fact that
58660b4ad09SPeter Avalos  * we're comparing two paths.  Right now, it just handles leading
58760b4ad09SPeter Avalos  * "./" and trailing '/' specially, so that "a/b/" == "./a/b"
58860b4ad09SPeter Avalos  *
58960b4ad09SPeter Avalos  * TODO: Make this better, so that "./a//b/./c/" == "a/b/c"
59060b4ad09SPeter Avalos  * TODO: After this works, push it down into libarchive.
59160b4ad09SPeter Avalos  * TODO: Publish the path normalization routines in libarchive so
59260b4ad09SPeter Avalos  * that bsdtar can normalize paths and use fast strcmp() instead
59360b4ad09SPeter Avalos  * of this.
5949c82a63eSPeter Avalos  *
5959c82a63eSPeter Avalos  * Note: This is currently only used within write.c, so should
5969c82a63eSPeter Avalos  * not handle \ path separators.
59760b4ad09SPeter Avalos  */
59860b4ad09SPeter Avalos 
59960b4ad09SPeter Avalos int
60060b4ad09SPeter Avalos pathcmp(const char *a, const char *b)
60160b4ad09SPeter Avalos {
60260b4ad09SPeter Avalos 	/* Skip leading './' */
60360b4ad09SPeter Avalos 	if (a[0] == '.' && a[1] == '/' && a[2] != '\0')
60460b4ad09SPeter Avalos 		a += 2;
60560b4ad09SPeter Avalos 	if (b[0] == '.' && b[1] == '/' && b[2] != '\0')
60660b4ad09SPeter Avalos 		b += 2;
60760b4ad09SPeter Avalos 	/* Find the first difference, or return (0) if none. */
60860b4ad09SPeter Avalos 	while (*a == *b) {
60960b4ad09SPeter Avalos 		if (*a == '\0')
61060b4ad09SPeter Avalos 			return (0);
61160b4ad09SPeter Avalos 		a++;
61260b4ad09SPeter Avalos 		b++;
61360b4ad09SPeter Avalos 	}
61460b4ad09SPeter Avalos 	/*
61560b4ad09SPeter Avalos 	 * If one ends in '/' and the other one doesn't,
61660b4ad09SPeter Avalos 	 * they're the same.
61760b4ad09SPeter Avalos 	 */
61860b4ad09SPeter Avalos 	if (a[0] == '/' && a[1] == '\0' && b[0] == '\0')
61960b4ad09SPeter Avalos 		return (0);
62060b4ad09SPeter Avalos 	if (a[0] == '\0' && b[0] == '/' && b[1] == '\0')
62160b4ad09SPeter Avalos 		return (0);
62260b4ad09SPeter Avalos 	/* They're really different, return the correct sign. */
62360b4ad09SPeter Avalos 	return (*(const unsigned char *)a - *(const unsigned char *)b);
62460b4ad09SPeter Avalos }
625*6b384f39SPeter Avalos 
626*6b384f39SPeter Avalos #define PPBUFF_SIZE 1024
627*6b384f39SPeter Avalos const char *
628*6b384f39SPeter Avalos passphrase_callback(struct archive *a, void *_client_data)
629*6b384f39SPeter Avalos {
630*6b384f39SPeter Avalos 	struct bsdtar *bsdtar = (struct bsdtar *)_client_data;
631*6b384f39SPeter Avalos 	(void)a; /* UNUSED */
632*6b384f39SPeter Avalos 
633*6b384f39SPeter Avalos 	if (bsdtar->ppbuff == NULL) {
634*6b384f39SPeter Avalos 		bsdtar->ppbuff = malloc(PPBUFF_SIZE);
635*6b384f39SPeter Avalos 		if (bsdtar->ppbuff == NULL)
636*6b384f39SPeter Avalos 			lafe_errc(1, errno, "Out of memory");
637*6b384f39SPeter Avalos 	}
638*6b384f39SPeter Avalos 	return lafe_readpassphrase("Enter passphrase:",
639*6b384f39SPeter Avalos 		bsdtar->ppbuff, PPBUFF_SIZE);
640*6b384f39SPeter Avalos }
641*6b384f39SPeter Avalos 
642*6b384f39SPeter Avalos void
643*6b384f39SPeter Avalos passphrase_free(char *ppbuff)
644*6b384f39SPeter Avalos {
645*6b384f39SPeter Avalos 	if (ppbuff != NULL) {
646*6b384f39SPeter Avalos 		memset(ppbuff, 0, PPBUFF_SIZE);
647*6b384f39SPeter Avalos 		free(ppbuff);
648*6b384f39SPeter Avalos 	}
649*6b384f39SPeter Avalos }
650*6b384f39SPeter Avalos 
651*6b384f39SPeter Avalos /*
652*6b384f39SPeter Avalos  * Display information about the current file.
653*6b384f39SPeter Avalos  *
654*6b384f39SPeter Avalos  * The format here roughly duplicates the output of 'ls -l'.
655*6b384f39SPeter Avalos  * This is based on SUSv2, where 'tar tv' is documented as
656*6b384f39SPeter Avalos  * listing additional information in an "unspecified format,"
657*6b384f39SPeter Avalos  * and 'pax -l' is documented as using the same format as 'ls -l'.
658*6b384f39SPeter Avalos  */
659*6b384f39SPeter Avalos void
660*6b384f39SPeter Avalos list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
661*6b384f39SPeter Avalos {
662*6b384f39SPeter Avalos 	char			 tmp[100];
663*6b384f39SPeter Avalos 	size_t			 w;
664*6b384f39SPeter Avalos 	const char		*p;
665*6b384f39SPeter Avalos 	const char		*fmt;
666*6b384f39SPeter Avalos 	time_t			 tim;
667*6b384f39SPeter Avalos 	static time_t		 now;
668*6b384f39SPeter Avalos 
669*6b384f39SPeter Avalos 	/*
670*6b384f39SPeter Avalos 	 * We avoid collecting the entire list in memory at once by
671*6b384f39SPeter Avalos 	 * listing things as we see them.  However, that also means we can't
672*6b384f39SPeter Avalos 	 * just pre-compute the field widths.  Instead, we start with guesses
673*6b384f39SPeter Avalos 	 * and just widen them as necessary.  These numbers are completely
674*6b384f39SPeter Avalos 	 * arbitrary.
675*6b384f39SPeter Avalos 	 */
676*6b384f39SPeter Avalos 	if (!bsdtar->u_width) {
677*6b384f39SPeter Avalos 		bsdtar->u_width = 6;
678*6b384f39SPeter Avalos 		bsdtar->gs_width = 13;
679*6b384f39SPeter Avalos 	}
680*6b384f39SPeter Avalos 	if (!now)
681*6b384f39SPeter Avalos 		time(&now);
682*6b384f39SPeter Avalos 	fprintf(out, "%s %d ",
683*6b384f39SPeter Avalos 	    archive_entry_strmode(entry),
684*6b384f39SPeter Avalos 	    archive_entry_nlink(entry));
685*6b384f39SPeter Avalos 
686*6b384f39SPeter Avalos 	/* Use uname if it's present, else uid. */
687*6b384f39SPeter Avalos 	p = archive_entry_uname(entry);
688*6b384f39SPeter Avalos 	if ((p == NULL) || (*p == '\0')) {
689*6b384f39SPeter Avalos 		sprintf(tmp, "%lu ",
690*6b384f39SPeter Avalos 		    (unsigned long)archive_entry_uid(entry));
691*6b384f39SPeter Avalos 		p = tmp;
692*6b384f39SPeter Avalos 	}
693*6b384f39SPeter Avalos 	w = strlen(p);
694*6b384f39SPeter Avalos 	if (w > bsdtar->u_width)
695*6b384f39SPeter Avalos 		bsdtar->u_width = w;
696*6b384f39SPeter Avalos 	fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
697*6b384f39SPeter Avalos 
698*6b384f39SPeter Avalos 	/* Use gname if it's present, else gid. */
699*6b384f39SPeter Avalos 	p = archive_entry_gname(entry);
700*6b384f39SPeter Avalos 	if (p != NULL && p[0] != '\0') {
701*6b384f39SPeter Avalos 		fprintf(out, "%s", p);
702*6b384f39SPeter Avalos 		w = strlen(p);
703*6b384f39SPeter Avalos 	} else {
704*6b384f39SPeter Avalos 		sprintf(tmp, "%lu",
705*6b384f39SPeter Avalos 		    (unsigned long)archive_entry_gid(entry));
706*6b384f39SPeter Avalos 		w = strlen(tmp);
707*6b384f39SPeter Avalos 		fprintf(out, "%s", tmp);
708*6b384f39SPeter Avalos 	}
709*6b384f39SPeter Avalos 
710*6b384f39SPeter Avalos 	/*
711*6b384f39SPeter Avalos 	 * Print device number or file size, right-aligned so as to make
712*6b384f39SPeter Avalos 	 * total width of group and devnum/filesize fields be gs_width.
713*6b384f39SPeter Avalos 	 * If gs_width is too small, grow it.
714*6b384f39SPeter Avalos 	 */
715*6b384f39SPeter Avalos 	if (archive_entry_filetype(entry) == AE_IFCHR
716*6b384f39SPeter Avalos 	    || archive_entry_filetype(entry) == AE_IFBLK) {
717*6b384f39SPeter Avalos 		sprintf(tmp, "%lu,%lu",
718*6b384f39SPeter Avalos 		    (unsigned long)archive_entry_rdevmajor(entry),
719*6b384f39SPeter Avalos 		    (unsigned long)archive_entry_rdevminor(entry));
720*6b384f39SPeter Avalos 	} else {
721*6b384f39SPeter Avalos 		strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
722*6b384f39SPeter Avalos 	}
723*6b384f39SPeter Avalos 	if (w + strlen(tmp) >= bsdtar->gs_width)
724*6b384f39SPeter Avalos 		bsdtar->gs_width = w+strlen(tmp)+1;
725*6b384f39SPeter Avalos 	fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
726*6b384f39SPeter Avalos 
727*6b384f39SPeter Avalos 	/* Format the time using 'ls -l' conventions. */
728*6b384f39SPeter Avalos 	tim = archive_entry_mtime(entry);
729*6b384f39SPeter Avalos #define	HALF_YEAR (time_t)365 * 86400 / 2
730*6b384f39SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
731*6b384f39SPeter Avalos #define	DAY_FMT  "%d"  /* Windows' strftime function does not support %e format. */
732*6b384f39SPeter Avalos #else
733*6b384f39SPeter Avalos #define	DAY_FMT  "%e"  /* Day number without leading zeros */
734*6b384f39SPeter Avalos #endif
735*6b384f39SPeter Avalos 	if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
736*6b384f39SPeter Avalos 		fmt = bsdtar->day_first ? DAY_FMT " %b  %Y" : "%b " DAY_FMT "  %Y";
737*6b384f39SPeter Avalos 	else
738*6b384f39SPeter Avalos 		fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
739*6b384f39SPeter Avalos 	strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
740*6b384f39SPeter Avalos 	fprintf(out, " %s ", tmp);
741*6b384f39SPeter Avalos 	safe_fprintf(out, "%s", archive_entry_pathname(entry));
742*6b384f39SPeter Avalos 
743*6b384f39SPeter Avalos 	/* Extra information for links. */
744*6b384f39SPeter Avalos 	if (archive_entry_hardlink(entry)) /* Hard link */
745*6b384f39SPeter Avalos 		safe_fprintf(out, " link to %s",
746*6b384f39SPeter Avalos 		    archive_entry_hardlink(entry));
747*6b384f39SPeter Avalos 	else if (archive_entry_symlink(entry)) /* Symbolic link */
748*6b384f39SPeter Avalos 		safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
749*6b384f39SPeter Avalos }
750