Home | History | Annotate | Line # | Download | only in pax
tar.c revision 1.47.2.5
      1  1.47.2.5      tron /*	$NetBSD: tar.c,v 1.47.2.5 2004/06/18 09:14:00 tron Exp $	*/
      2       1.5       cgd 
      3       1.1       jtc /*-
      4      1.44       agc  * Copyright (c) 1992 Keith Muller.
      5       1.1       jtc  * Copyright (c) 1992, 1993
      6       1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      7       1.1       jtc  *
      8       1.1       jtc  * This code is derived from software contributed to Berkeley by
      9       1.1       jtc  * Keith Muller of the University of California, San Diego.
     10       1.1       jtc  *
     11       1.1       jtc  * Redistribution and use in source and binary forms, with or without
     12       1.1       jtc  * modification, are permitted provided that the following conditions
     13       1.1       jtc  * are met:
     14       1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     15       1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     16       1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     18       1.1       jtc  *    documentation and/or other materials provided with the distribution.
     19      1.43       agc  * 3. Neither the name of the University nor the names of its contributors
     20      1.43       agc  *    may be used to endorse or promote products derived from this software
     21      1.43       agc  *    without specific prior written permission.
     22      1.43       agc  *
     23      1.43       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24      1.43       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25      1.43       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26      1.43       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27      1.43       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28      1.43       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29      1.43       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30      1.43       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31      1.43       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32      1.43       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33      1.43       agc  * SUCH DAMAGE.
     34      1.43       agc  */
     35      1.43       agc 
     36      1.45     lukem #if HAVE_NBTOOL_CONFIG_H
     37      1.45     lukem #include "nbtool_config.h"
     38      1.45     lukem #endif
     39      1.45     lukem 
     40       1.9  christos #include <sys/cdefs.h>
     41      1.45     lukem #if !defined(lint)
     42       1.5       cgd #if 0
     43       1.5       cgd static char sccsid[] = "@(#)tar.c	8.2 (Berkeley) 4/18/94";
     44       1.5       cgd #else
     45  1.47.2.5      tron __RCSID("$NetBSD: tar.c,v 1.47.2.5 2004/06/18 09:14:00 tron Exp $");
     46       1.5       cgd #endif
     47       1.1       jtc #endif /* not lint */
     48       1.1       jtc 
     49       1.1       jtc #include <sys/types.h>
     50       1.1       jtc #include <sys/time.h>
     51       1.1       jtc #include <sys/stat.h>
     52       1.1       jtc #include <sys/param.h>
     53      1.10   mycroft 
     54      1.10   mycroft #include <ctype.h>
     55      1.13       mrg #include <errno.h>
     56      1.10   mycroft #include <grp.h>
     57      1.10   mycroft #include <pwd.h>
     58      1.10   mycroft #include <stdio.h>
     59      1.10   mycroft #include <stdlib.h>
     60       1.1       jtc #include <string.h>
     61       1.1       jtc #include <unistd.h>
     62      1.10   mycroft 
     63       1.1       jtc #include "pax.h"
     64       1.1       jtc #include "extern.h"
     65       1.1       jtc #include "tar.h"
     66       1.1       jtc 
     67       1.1       jtc /*
     68       1.1       jtc  * Routines for reading, writing and header identify of various versions of tar
     69       1.1       jtc  */
     70       1.1       jtc 
     71      1.39  christos static int expandname(char *, size_t,  char **, const char *, size_t);
     72  1.47.2.5      tron static void longlink(ARCHD *, int);
     73      1.19     lukem static u_long tar_chksm(char *, int);
     74      1.19     lukem static char *name_split(char *, int);
     75      1.19     lukem static int ul_oct(u_long, char *, int, int);
     76      1.24       mrg #if !defined(NET2_STAT) && !defined(_LP64)
     77      1.20     lukem static int ull_oct(unsigned long long, char *, int, int);
     78       1.1       jtc #endif
     79      1.33       mrg static int tar_gnutar_exclude_one(const char *, size_t);
     80      1.46      matt static int check_sum(char *, size_t, char *, size_t, int);
     81       1.1       jtc 
     82       1.1       jtc /*
     83       1.1       jtc  * Routines common to all versions of tar
     84       1.1       jtc  */
     85       1.1       jtc 
     86       1.1       jtc static int tar_nodir;			/* do not write dirs under old tar */
     87      1.28  christos int is_gnutar;				/* behave like gnu tar; enable gnu
     88      1.26  christos 					 * extensions and skip end-ofvolume
     89      1.26  christos 					 * checks
     90      1.26  christos 					 */
     91      1.31  christos static int seen_gnu_warning;		/* Have we warned yet? */
     92      1.29  christos static char *gnu_hack_string;		/* ././@LongLink hackery */
     93      1.29  christos static int gnu_hack_len;		/* len of gnu_hack_string */
     94      1.29  christos char *gnu_name_string;			/* ././@LongLink hackery name */
     95      1.30  christos char *gnu_link_string;			/* ././@LongLink hackery link */
     96  1.47.2.4     grant static int gnu_short_trailer;		/* gnu short trailer */
     97       1.1       jtc 
     98  1.47.2.5      tron static const char LONG_LINK[] = "././@LongLink";
     99  1.47.2.5      tron 
    100      1.35  christos static int
    101      1.46      matt check_sum(char *hd, size_t hdlen, char *bl, size_t bllen, int quiet)
    102      1.35  christos {
    103      1.35  christos 	u_long hdck, blck;
    104      1.35  christos 
    105      1.35  christos 	hdck = asc_ul(hd, hdlen, OCT);
    106      1.35  christos 	blck = tar_chksm(bl, bllen);
    107      1.35  christos 
    108      1.35  christos 	if (hdck != blck) {
    109      1.46      matt 		if (!quiet)
    110      1.46      matt 			tty_warn(0, "Header checksum %lo does not match %lo",
    111      1.46      matt 			    hdck, blck);
    112      1.35  christos 		return(-1);
    113      1.35  christos 	}
    114      1.35  christos 	return(0);
    115      1.35  christos }
    116      1.35  christos 
    117      1.35  christos 
    118       1.1       jtc /*
    119       1.1       jtc  * tar_endwr()
    120       1.1       jtc  *	add the tar trailer of two null blocks
    121       1.1       jtc  * Return:
    122       1.1       jtc  *	0 if ok, -1 otherwise (what wr_skip returns)
    123       1.1       jtc  */
    124       1.1       jtc 
    125       1.1       jtc int
    126       1.1       jtc tar_endwr(void)
    127       1.1       jtc {
    128  1.47.2.4     grant 	return(wr_skip((off_t)(NULLCNT * BLKMULT)));
    129       1.1       jtc }
    130       1.1       jtc 
    131       1.1       jtc /*
    132       1.1       jtc  * tar_endrd()
    133       1.1       jtc  *	no cleanup needed here, just return size of trailer (for append)
    134       1.1       jtc  * Return:
    135      1.47  christos  *	size of trailer BLKMULT
    136       1.1       jtc  */
    137       1.1       jtc 
    138       1.1       jtc off_t
    139       1.1       jtc tar_endrd(void)
    140       1.1       jtc {
    141  1.47.2.4     grant 	return((off_t)((gnu_short_trailer ? 1 : NULLCNT) * BLKMULT));
    142       1.1       jtc }
    143       1.1       jtc 
    144       1.1       jtc /*
    145       1.1       jtc  * tar_trail()
    146       1.1       jtc  *	Called to determine if a header block is a valid trailer. We are passed
    147       1.1       jtc  *	the block, the in_sync flag (which tells us we are in resync mode;
    148       1.1       jtc  *	looking for a valid header), and cnt (which starts at zero) which is
    149       1.1       jtc  *	used to count the number of empty blocks we have seen so far.
    150       1.1       jtc  * Return:
    151       1.1       jtc  *	0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
    152       1.1       jtc  *	could never contain a header.
    153       1.1       jtc  */
    154       1.1       jtc 
    155       1.1       jtc int
    156       1.6       tls tar_trail(char *buf, int in_resync, int *cnt)
    157       1.1       jtc {
    158       1.6       tls 	int i;
    159       1.1       jtc 
    160  1.47.2.4     grant 	gnu_short_trailer = 0;
    161       1.1       jtc 	/*
    162       1.1       jtc 	 * look for all zero, trailer is two consecutive blocks of zero
    163       1.1       jtc 	 */
    164       1.1       jtc 	for (i = 0; i < BLKMULT; ++i) {
    165      1.36  christos 		if (buf[i] != '\0')
    166       1.1       jtc 			break;
    167       1.1       jtc 	}
    168       1.1       jtc 
    169       1.1       jtc 	/*
    170       1.1       jtc 	 * if not all zero it is not a trailer, but MIGHT be a header.
    171       1.1       jtc 	 */
    172       1.1       jtc 	if (i != BLKMULT)
    173       1.1       jtc 		return(-1);
    174       1.1       jtc 
    175       1.1       jtc 	/*
    176       1.1       jtc 	 * When given a zero block, we must be careful!
    177       1.1       jtc 	 * If we are not in resync mode, check for the trailer. Have to watch
    178       1.1       jtc 	 * out that we do not mis-identify file data as the trailer, so we do
    179       1.1       jtc 	 * NOT try to id a trailer during resync mode. During resync mode we
    180       1.1       jtc 	 * might as well throw this block out since a valid header can NEVER be
    181       1.1       jtc 	 * a block of all 0 (we must have a valid file name).
    182       1.1       jtc 	 */
    183      1.37  christos 	if (!in_resync) {
    184      1.37  christos 		++*cnt;
    185      1.37  christos 		/*
    186      1.37  christos 		 * old GNU tar (up through 1.13) only writes one block of
    187      1.37  christos 		 * trailers, so we pretend we got another
    188      1.37  christos 		 */
    189  1.47.2.4     grant 		if (is_gnutar) {
    190  1.47.2.4     grant 			gnu_short_trailer = 1;
    191      1.37  christos 			++*cnt;
    192  1.47.2.4     grant 		}
    193      1.37  christos 		if (*cnt >= NULLCNT)
    194      1.37  christos 			return(0);
    195      1.37  christos 	}
    196       1.1       jtc 	return(1);
    197       1.1       jtc }
    198       1.1       jtc 
    199       1.1       jtc /*
    200       1.1       jtc  * ul_oct()
    201       1.1       jtc  *	convert an unsigned long to an octal string. many oddball field
    202       1.1       jtc  *	termination characters are used by the various versions of tar in the
    203       1.7    kleink  *	different fields. term selects which kind to use. str is '0' padded
    204       1.1       jtc  *	at the front to len. we are unable to use only one format as many old
    205       1.1       jtc  *	tar readers are very cranky about this.
    206       1.1       jtc  * Return:
    207       1.1       jtc  *	0 if the number fit into the string, -1 otherwise
    208       1.1       jtc  */
    209       1.1       jtc 
    210       1.1       jtc static int
    211       1.6       tls ul_oct(u_long val, char *str, int len, int term)
    212       1.1       jtc {
    213       1.6       tls 	char *pt;
    214      1.18     itohy 
    215       1.1       jtc 	/*
    216       1.1       jtc 	 * term selects the appropriate character(s) for the end of the string
    217       1.1       jtc 	 */
    218       1.1       jtc 	pt = str + len - 1;
    219       1.1       jtc 	switch(term) {
    220       1.1       jtc 	case 3:
    221       1.1       jtc 		*pt-- = '\0';
    222       1.1       jtc 		break;
    223       1.1       jtc 	case 2:
    224       1.1       jtc 		*pt-- = ' ';
    225       1.1       jtc 		*pt-- = '\0';
    226       1.1       jtc 		break;
    227       1.1       jtc 	case 1:
    228       1.1       jtc 		*pt-- = ' ';
    229       1.1       jtc 		break;
    230       1.1       jtc 	case 0:
    231       1.1       jtc 	default:
    232       1.1       jtc 		*pt-- = '\0';
    233       1.1       jtc 		*pt-- = ' ';
    234       1.1       jtc 		break;
    235       1.1       jtc 	}
    236       1.1       jtc 
    237       1.1       jtc 	/*
    238       1.1       jtc 	 * convert and blank pad if there is space
    239       1.1       jtc 	 */
    240       1.1       jtc 	while (pt >= str) {
    241       1.1       jtc 		*pt-- = '0' + (char)(val & 0x7);
    242       1.1       jtc 		if ((val = val >> 3) == (u_long)0)
    243       1.1       jtc 			break;
    244       1.1       jtc 	}
    245       1.1       jtc 
    246       1.1       jtc 	while (pt >= str)
    247       1.7    kleink 		*pt-- = '0';
    248       1.1       jtc 	if (val != (u_long)0)
    249       1.1       jtc 		return(-1);
    250       1.1       jtc 	return(0);
    251       1.1       jtc }
    252       1.1       jtc 
    253      1.24       mrg #if !defined(NET2_STAT) && !defined(_LP64)
    254       1.1       jtc /*
    255      1.19     lukem  * ull_oct()
    256      1.20     lukem  *	convert an unsigned long long to an octal string. one of many oddball
    257      1.20     lukem  *	field termination characters are used by the various versions of tar
    258      1.20     lukem  *	in the different fields. term selects which kind to use. str is '0'
    259      1.20     lukem  *	padded at the front to len. we are unable to use only one format as
    260      1.20     lukem  *	many old tar readers are very cranky about this.
    261       1.1       jtc  * Return:
    262       1.1       jtc  *	0 if the number fit into the string, -1 otherwise
    263       1.1       jtc  */
    264       1.1       jtc 
    265       1.1       jtc static int
    266      1.20     lukem ull_oct(unsigned long long val, char *str, int len, int term)
    267       1.1       jtc {
    268       1.6       tls 	char *pt;
    269      1.18     itohy 
    270       1.1       jtc 	/*
    271       1.1       jtc 	 * term selects the appropriate character(s) for the end of the string
    272       1.1       jtc 	 */
    273       1.1       jtc 	pt = str + len - 1;
    274       1.1       jtc 	switch(term) {
    275       1.1       jtc 	case 3:
    276       1.1       jtc 		*pt-- = '\0';
    277       1.1       jtc 		break;
    278       1.1       jtc 	case 2:
    279       1.1       jtc 		*pt-- = ' ';
    280       1.1       jtc 		*pt-- = '\0';
    281       1.1       jtc 		break;
    282       1.1       jtc 	case 1:
    283       1.1       jtc 		*pt-- = ' ';
    284       1.1       jtc 		break;
    285       1.1       jtc 	case 0:
    286       1.1       jtc 	default:
    287       1.1       jtc 		*pt-- = '\0';
    288       1.1       jtc 		*pt-- = ' ';
    289       1.1       jtc 		break;
    290       1.1       jtc 	}
    291       1.1       jtc 
    292       1.1       jtc 	/*
    293       1.1       jtc 	 * convert and blank pad if there is space
    294       1.1       jtc 	 */
    295       1.1       jtc 	while (pt >= str) {
    296       1.1       jtc 		*pt-- = '0' + (char)(val & 0x7);
    297       1.1       jtc 		if ((val = val >> 3) == 0)
    298       1.1       jtc 			break;
    299       1.1       jtc 	}
    300       1.1       jtc 
    301       1.1       jtc 	while (pt >= str)
    302       1.7    kleink 		*pt-- = '0';
    303      1.20     lukem 	if (val != (unsigned long long)0)
    304       1.1       jtc 		return(-1);
    305       1.1       jtc 	return(0);
    306       1.1       jtc }
    307       1.1       jtc #endif
    308       1.1       jtc 
    309       1.1       jtc /*
    310       1.1       jtc  * tar_chksm()
    311       1.1       jtc  *	calculate the checksum for a tar block counting the checksum field as
    312      1.18     itohy  *	all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
    313       1.1       jtc  *	NOTE: we use len to short circuit summing 0's on write since we ALWAYS
    314       1.1       jtc  *	pad headers with 0.
    315       1.1       jtc  * Return:
    316       1.1       jtc  *	unsigned long checksum
    317       1.1       jtc  */
    318       1.1       jtc 
    319       1.1       jtc static u_long
    320       1.6       tls tar_chksm(char *blk, int len)
    321       1.1       jtc {
    322       1.6       tls 	char *stop;
    323       1.6       tls 	char *pt;
    324      1.18     itohy 	u_long chksm = BLNKSUM;	/* initial value is checksum field sum */
    325       1.1       jtc 
    326       1.1       jtc 	/*
    327       1.1       jtc 	 * add the part of the block before the checksum field
    328       1.1       jtc 	 */
    329       1.1       jtc 	pt = blk;
    330       1.1       jtc 	stop = blk + CHK_OFFSET;
    331      1.18     itohy 	while (pt < stop)
    332      1.18     itohy 		chksm += (u_long)(*pt++ & 0xff);
    333       1.1       jtc 	/*
    334       1.1       jtc 	 * move past the checksum field and keep going, spec counts the
    335       1.1       jtc 	 * checksum field as the sum of 8 blanks (which is pre-computed as
    336       1.1       jtc 	 * BLNKSUM).
    337       1.1       jtc 	 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
    338       1.1       jtc 	 * starts, no point in summing zero's)
    339       1.1       jtc 	 */
    340       1.1       jtc 	pt += CHK_LEN;
    341       1.1       jtc 	stop = blk + len;
    342       1.1       jtc 	while (pt < stop)
    343      1.18     itohy 		chksm += (u_long)(*pt++ & 0xff);
    344       1.1       jtc 	return(chksm);
    345       1.1       jtc }
    346       1.1       jtc 
    347       1.1       jtc /*
    348       1.1       jtc  * Routines for old BSD style tar (also made portable to sysV tar)
    349       1.1       jtc  */
    350       1.1       jtc 
    351       1.1       jtc /*
    352       1.1       jtc  * tar_id()
    353       1.1       jtc  *	determine if a block given to us is a valid tar header (and not a USTAR
    354       1.1       jtc  *	header). We have to be on the lookout for those pesky blocks of	all
    355       1.1       jtc  *	zero's.
    356       1.1       jtc  * Return:
    357       1.1       jtc  *	0 if a tar header, -1 otherwise
    358       1.1       jtc  */
    359       1.1       jtc 
    360       1.1       jtc int
    361       1.6       tls tar_id(char *blk, int size)
    362       1.1       jtc {
    363       1.6       tls 	HD_TAR *hd;
    364       1.6       tls 	HD_USTAR *uhd;
    365       1.1       jtc 
    366       1.1       jtc 	if (size < BLKMULT)
    367       1.1       jtc 		return(-1);
    368       1.1       jtc 	hd = (HD_TAR *)blk;
    369       1.1       jtc 	uhd = (HD_USTAR *)blk;
    370       1.1       jtc 
    371       1.1       jtc 	/*
    372       1.1       jtc 	 * check for block of zero's first, a simple and fast test, then make
    373       1.1       jtc 	 * sure this is not a ustar header by looking for the ustar magic
    374       1.1       jtc 	 * cookie. We should use TMAGLEN, but some USTAR archive programs are
    375       1.1       jtc 	 * wrong and create archives missing the \0. Last we check the
    376       1.1       jtc 	 * checksum. If this is ok we have to assume it is a valid header.
    377       1.1       jtc 	 */
    378       1.1       jtc 	if (hd->name[0] == '\0')
    379       1.1       jtc 		return(-1);
    380       1.1       jtc 	if (strncmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0)
    381       1.1       jtc 		return(-1);
    382      1.46      matt 	return check_sum(hd->chksum, sizeof(hd->chksum), blk, BLKMULT, 1);
    383       1.1       jtc }
    384       1.1       jtc 
    385       1.1       jtc /*
    386       1.1       jtc  * tar_opt()
    387       1.1       jtc  *	handle tar format specific -o options
    388       1.1       jtc  * Return:
    389       1.1       jtc  *	0 if ok -1 otherwise
    390       1.1       jtc  */
    391       1.1       jtc 
    392       1.1       jtc int
    393       1.1       jtc tar_opt(void)
    394       1.1       jtc {
    395       1.1       jtc 	OPLIST *opt;
    396       1.1       jtc 
    397       1.1       jtc 	while ((opt = opt_next()) != NULL) {
    398       1.1       jtc 		if (strcmp(opt->name, TAR_OPTION) ||
    399       1.1       jtc 		    strcmp(opt->value, TAR_NODIR)) {
    400       1.9  christos 			tty_warn(1,
    401       1.9  christos 			    "Unknown tar format -o option/value pair %s=%s",
    402       1.1       jtc 			    opt->name, opt->value);
    403       1.9  christos 			tty_warn(1,
    404       1.9  christos 			    "%s=%s is the only supported tar format option",
    405       1.1       jtc 			    TAR_OPTION, TAR_NODIR);
    406       1.1       jtc 			return(-1);
    407       1.1       jtc 		}
    408       1.1       jtc 
    409       1.1       jtc 		/*
    410       1.1       jtc 		 * we only support one option, and only when writing
    411       1.1       jtc 		 */
    412       1.1       jtc 		if ((act != APPND) && (act != ARCHIVE)) {
    413       1.9  christos 			tty_warn(1, "%s=%s is only supported when writing.",
    414       1.1       jtc 			    opt->name, opt->value);
    415       1.1       jtc 			return(-1);
    416       1.1       jtc 		}
    417       1.1       jtc 		tar_nodir = 1;
    418       1.1       jtc 	}
    419       1.1       jtc 	return(0);
    420       1.1       jtc }
    421       1.1       jtc 
    422       1.1       jtc 
    423       1.1       jtc /*
    424       1.1       jtc  * tar_rd()
    425       1.1       jtc  *	extract the values out of block already determined to be a tar header.
    426       1.1       jtc  *	store the values in the ARCHD parameter.
    427       1.1       jtc  * Return:
    428       1.1       jtc  *	0
    429       1.1       jtc  */
    430       1.1       jtc 
    431       1.1       jtc int
    432       1.6       tls tar_rd(ARCHD *arcn, char *buf)
    433       1.1       jtc {
    434       1.6       tls 	HD_TAR *hd;
    435       1.6       tls 	char *pt;
    436       1.1       jtc 
    437       1.1       jtc 	/*
    438       1.1       jtc 	 * we only get proper sized buffers passed to us
    439       1.1       jtc 	 */
    440       1.1       jtc 	if (tar_id(buf, BLKMULT) < 0)
    441       1.1       jtc 		return(-1);
    442      1.29  christos 	memset(arcn, 0, sizeof(*arcn));
    443       1.1       jtc 	arcn->org_name = arcn->name;
    444      1.29  christos 	arcn->pat = NULL;
    445       1.1       jtc 	arcn->sb.st_nlink = 1;
    446       1.1       jtc 
    447       1.1       jtc 	/*
    448       1.1       jtc 	 * copy out the name and values in the stat buffer
    449       1.1       jtc 	 */
    450       1.1       jtc 	hd = (HD_TAR *)buf;
    451      1.30  christos 	if (hd->linkflag != LONGLINKTYPE && hd->linkflag != LONGNAMETYPE) {
    452      1.30  christos 		arcn->nlen = expandname(arcn->name, sizeof(arcn->name),
    453      1.39  christos 		    &gnu_name_string, hd->name, sizeof(hd->name));
    454      1.30  christos 		arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name),
    455      1.39  christos 		    &gnu_link_string, hd->linkname, sizeof(hd->linkname));
    456      1.30  christos 	}
    457       1.1       jtc 	arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) &
    458       1.1       jtc 	    0xfff);
    459       1.1       jtc 	arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
    460       1.1       jtc 	arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
    461      1.19     lukem 	arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
    462       1.1       jtc 	arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
    463       1.1       jtc 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
    464       1.1       jtc 
    465       1.1       jtc 	/*
    466       1.1       jtc 	 * have to look at the last character, it may be a '/' and that is used
    467       1.1       jtc 	 * to encode this as a directory
    468       1.1       jtc 	 */
    469       1.1       jtc 	pt = &(arcn->name[arcn->nlen - 1]);
    470       1.1       jtc 	arcn->pad = 0;
    471       1.1       jtc 	arcn->skip = 0;
    472       1.1       jtc 	switch(hd->linkflag) {
    473       1.1       jtc 	case SYMTYPE:
    474       1.1       jtc 		/*
    475       1.1       jtc 		 * symbolic link, need to get the link name and set the type in
    476       1.1       jtc 		 * the st_mode so -v printing will look correct.
    477       1.1       jtc 		 */
    478       1.1       jtc 		arcn->type = PAX_SLK;
    479       1.1       jtc 		arcn->sb.st_mode |= S_IFLNK;
    480       1.1       jtc 		break;
    481       1.1       jtc 	case LNKTYPE:
    482       1.1       jtc 		/*
    483       1.1       jtc 		 * hard link, need to get the link name, set the type in the
    484       1.1       jtc 		 * st_mode and st_nlink so -v printing will look better.
    485       1.1       jtc 		 */
    486       1.1       jtc 		arcn->type = PAX_HLK;
    487       1.1       jtc 		arcn->sb.st_nlink = 2;
    488       1.1       jtc 
    489       1.1       jtc 		/*
    490       1.1       jtc 		 * no idea of what type this thing really points at, but
    491       1.1       jtc 		 * we set something for printing only.
    492       1.1       jtc 		 */
    493       1.1       jtc 		arcn->sb.st_mode |= S_IFREG;
    494      1.16       mrg 		break;
    495      1.16       mrg 	case LONGLINKTYPE:
    496      1.16       mrg 	case LONGNAMETYPE:
    497      1.16       mrg 		/*
    498      1.16       mrg 		 * GNU long link/file; we tag these here and let the
    499      1.16       mrg 		 * pax internals deal with it -- too ugly otherwise.
    500      1.16       mrg 		 */
    501      1.16       mrg 		if (hd->linkflag != LONGLINKTYPE)
    502      1.16       mrg 			arcn->type = PAX_GLF;
    503  1.47.2.5      tron 		else
    504  1.47.2.5      tron 			arcn->type = PAX_GLL;
    505      1.16       mrg 		arcn->pad = TAR_PAD(arcn->sb.st_size);
    506      1.16       mrg 		arcn->skip = arcn->sb.st_size;
    507       1.1       jtc 		break;
    508       1.1       jtc 	case AREGTYPE:
    509       1.1       jtc 	case REGTYPE:
    510      1.15    kleink 	case DIRTYPE:	/* see below */
    511       1.1       jtc 	default:
    512       1.1       jtc 		/*
    513       1.1       jtc 		 * If we have a trailing / this is a directory and NOT a file.
    514      1.15    kleink 		 * Note: V7 tar doesn't actually have DIRTYPE, but it was
    515      1.15    kleink 		 * reported that V7 archives using USTAR directories do exist.
    516       1.1       jtc 		 */
    517      1.15    kleink 		if (*pt == '/' || hd->linkflag == DIRTYPE) {
    518       1.1       jtc 			/*
    519       1.1       jtc 			 * it is a directory, set the mode for -v printing
    520       1.1       jtc 			 */
    521       1.1       jtc 			arcn->type = PAX_DIR;
    522       1.1       jtc 			arcn->sb.st_mode |= S_IFDIR;
    523       1.1       jtc 			arcn->sb.st_nlink = 2;
    524       1.1       jtc 		} else {
    525       1.1       jtc 			/*
    526       1.1       jtc 			 * have a file that will be followed by data. Set the
    527      1.18     itohy 			 * skip value to the size field and calculate the size
    528       1.1       jtc 			 * of the padding.
    529       1.1       jtc 			 */
    530       1.1       jtc 			arcn->type = PAX_REG;
    531       1.1       jtc 			arcn->sb.st_mode |= S_IFREG;
    532       1.1       jtc 			arcn->pad = TAR_PAD(arcn->sb.st_size);
    533       1.1       jtc 			arcn->skip = arcn->sb.st_size;
    534       1.1       jtc 		}
    535       1.1       jtc 		break;
    536       1.1       jtc 	}
    537       1.1       jtc 
    538       1.1       jtc 	/*
    539       1.1       jtc 	 * strip off any trailing slash.
    540       1.1       jtc 	 */
    541       1.1       jtc 	if (*pt == '/') {
    542      1.18     itohy 		*pt = '\0';
    543       1.1       jtc 		--arcn->nlen;
    544       1.1       jtc 	}
    545       1.1       jtc 	return(0);
    546       1.1       jtc }
    547       1.1       jtc 
    548       1.1       jtc /*
    549       1.1       jtc  * tar_wr()
    550       1.1       jtc  *	write a tar header for the file specified in the ARCHD to the archive.
    551       1.1       jtc  *	Have to check for file types that cannot be stored and file names that
    552       1.1       jtc  *	are too long. Be careful of the term (last arg) to ul_oct, each field
    553       1.1       jtc  *	of tar has it own spec for the termination character(s).
    554       1.1       jtc  *	ASSUMED: space after header in header block is zero filled
    555       1.1       jtc  * Return:
    556       1.1       jtc  *	0 if file has data to be written after the header, 1 if file has NO
    557       1.1       jtc  *	data to write after the header, -1 if archive write failed
    558       1.1       jtc  */
    559       1.1       jtc 
    560       1.1       jtc int
    561       1.6       tls tar_wr(ARCHD *arcn)
    562       1.1       jtc {
    563       1.6       tls 	HD_TAR *hd;
    564       1.1       jtc 	int len;
    565       1.1       jtc 	char hdblk[sizeof(HD_TAR)];
    566       1.1       jtc 
    567       1.1       jtc 	/*
    568       1.1       jtc 	 * check for those file system types which tar cannot store
    569       1.1       jtc 	 */
    570       1.1       jtc 	switch(arcn->type) {
    571       1.1       jtc 	case PAX_DIR:
    572       1.1       jtc 		/*
    573       1.1       jtc 		 * user asked that dirs not be written to the archive
    574       1.1       jtc 		 */
    575       1.1       jtc 		if (tar_nodir)
    576       1.1       jtc 			return(1);
    577       1.1       jtc 		break;
    578       1.1       jtc 	case PAX_CHR:
    579       1.9  christos 		tty_warn(1, "Tar cannot archive a character device %s",
    580       1.1       jtc 		    arcn->org_name);
    581       1.1       jtc 		return(1);
    582       1.1       jtc 	case PAX_BLK:
    583       1.9  christos 		tty_warn(1,
    584       1.9  christos 		    "Tar cannot archive a block device %s", arcn->org_name);
    585       1.1       jtc 		return(1);
    586       1.1       jtc 	case PAX_SCK:
    587       1.9  christos 		tty_warn(1, "Tar cannot archive a socket %s", arcn->org_name);
    588       1.1       jtc 		return(1);
    589       1.1       jtc 	case PAX_FIF:
    590       1.9  christos 		tty_warn(1, "Tar cannot archive a fifo %s", arcn->org_name);
    591       1.1       jtc 		return(1);
    592       1.1       jtc 	case PAX_SLK:
    593       1.1       jtc 	case PAX_HLK:
    594       1.1       jtc 	case PAX_HRG:
    595       1.1       jtc 		if (arcn->ln_nlen > sizeof(hd->linkname)) {
    596       1.9  christos 			tty_warn(1,"Link name too long for tar %s",
    597       1.9  christos 			    arcn->ln_name);
    598       1.1       jtc 			return(1);
    599       1.1       jtc 		}
    600       1.1       jtc 		break;
    601       1.1       jtc 	case PAX_REG:
    602       1.1       jtc 	case PAX_CTG:
    603       1.1       jtc 	default:
    604       1.1       jtc 		break;
    605       1.1       jtc 	}
    606       1.1       jtc 
    607       1.1       jtc 	/*
    608       1.1       jtc 	 * check file name len, remember extra char for dirs (the / at the end)
    609       1.1       jtc 	 */
    610       1.1       jtc 	len = arcn->nlen;
    611       1.1       jtc 	if (arcn->type == PAX_DIR)
    612       1.1       jtc 		++len;
    613      1.22  christos 	if (len >= sizeof(hd->name)) {
    614       1.9  christos 		tty_warn(1, "File name too long for tar %s", arcn->name);
    615       1.1       jtc 		return(1);
    616       1.1       jtc 	}
    617       1.1       jtc 
    618       1.1       jtc 	/*
    619       1.1       jtc 	 * copy the data out of the ARCHD into the tar header based on the type
    620       1.1       jtc 	 * of the file. Remember many tar readers want the unused fields to be
    621       1.1       jtc 	 * padded with zero. We set the linkflag field (type), the linkname
    622       1.1       jtc 	 * (or zero if not used),the size, and set the padding (if any) to be
    623       1.1       jtc 	 * added after the file data (0 for all other types, as they only have
    624       1.1       jtc 	 * a header)
    625       1.1       jtc 	 */
    626      1.22  christos 	memset(hdblk, 0, sizeof(hdblk));
    627       1.1       jtc 	hd = (HD_TAR *)hdblk;
    628      1.22  christos 	strlcpy(hd->name, arcn->name, sizeof(hd->name));
    629       1.1       jtc 	arcn->pad = 0;
    630       1.1       jtc 
    631       1.1       jtc 	if (arcn->type == PAX_DIR) {
    632       1.1       jtc 		/*
    633       1.1       jtc 		 * directories are the same as files, except have a filename
    634       1.1       jtc 		 * that ends with a /, we add the slash here. No data follows,
    635       1.1       jtc 		 * dirs, so no pad.
    636       1.1       jtc 		 */
    637       1.1       jtc 		hd->linkflag = AREGTYPE;
    638       1.1       jtc 		hd->name[len-1] = '/';
    639       1.1       jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
    640       1.1       jtc 			goto out;
    641       1.1       jtc 	} else if (arcn->type == PAX_SLK) {
    642       1.1       jtc 		/*
    643       1.1       jtc 		 * no data follows this file, so no pad
    644       1.1       jtc 		 */
    645       1.1       jtc 		hd->linkflag = SYMTYPE;
    646      1.22  christos 		strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
    647       1.1       jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
    648       1.1       jtc 			goto out;
    649       1.1       jtc 	} else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
    650       1.1       jtc 		/*
    651       1.1       jtc 		 * no data follows this file, so no pad
    652       1.1       jtc 		 */
    653       1.1       jtc 		hd->linkflag = LNKTYPE;
    654      1.22  christos 		strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
    655       1.1       jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
    656       1.1       jtc 			goto out;
    657       1.1       jtc 	} else {
    658       1.1       jtc 		/*
    659       1.1       jtc 		 * data follows this file, so set the pad
    660       1.1       jtc 		 */
    661       1.1       jtc 		hd->linkflag = AREGTYPE;
    662      1.19     lukem 		if (OFFT_OCT(arcn->sb.st_size, hd->size, sizeof(hd->size), 1)) {
    663       1.9  christos 			tty_warn(1,"File is too large for tar %s",
    664       1.9  christos 			    arcn->org_name);
    665       1.1       jtc 			return(1);
    666       1.1       jtc 		}
    667       1.1       jtc 		arcn->pad = TAR_PAD(arcn->sb.st_size);
    668       1.1       jtc 	}
    669       1.1       jtc 
    670       1.1       jtc 	/*
    671       1.1       jtc 	 * copy those fields that are independent of the type
    672       1.1       jtc 	 */
    673       1.1       jtc 	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
    674       1.1       jtc 	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
    675       1.1       jtc 	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
    676       1.1       jtc 	    ul_oct((u_long)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1))
    677       1.1       jtc 		goto out;
    678       1.1       jtc 
    679       1.1       jtc 	/*
    680       1.1       jtc 	 * calculate and add the checksum, then write the header. A return of
    681       1.1       jtc 	 * 0 tells the caller to now write the file data, 1 says no data needs
    682       1.1       jtc 	 * to be written
    683       1.1       jtc 	 */
    684       1.1       jtc 	if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum,
    685      1.22  christos 	    sizeof(hd->chksum), 3))
    686      1.23  christos 		goto out;			/* XXX Something's wrong here
    687      1.23  christos 						 * because a zero-byte file can
    688      1.23  christos 						 * cause this to be done and
    689      1.23  christos 						 * yet the resulting warning
    690      1.23  christos 						 * seems incorrect */
    691      1.23  christos 
    692       1.1       jtc 	if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0)
    693       1.1       jtc 		return(-1);
    694       1.1       jtc 	if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
    695       1.1       jtc 		return(-1);
    696       1.1       jtc 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
    697       1.1       jtc 		return(0);
    698       1.1       jtc 	return(1);
    699       1.1       jtc 
    700       1.1       jtc     out:
    701       1.1       jtc 	/*
    702       1.1       jtc 	 * header field is out of range
    703       1.1       jtc 	 */
    704       1.9  christos 	tty_warn(1, "Tar header field is too small for %s", arcn->org_name);
    705       1.1       jtc 	return(1);
    706       1.1       jtc }
    707       1.1       jtc 
    708       1.1       jtc /*
    709       1.1       jtc  * Routines for POSIX ustar
    710       1.1       jtc  */
    711       1.1       jtc 
    712       1.1       jtc /*
    713       1.1       jtc  * ustar_strd()
    714       1.1       jtc  *	initialization for ustar read
    715       1.1       jtc  * Return:
    716       1.1       jtc  *	0 if ok, -1 otherwise
    717       1.1       jtc  */
    718       1.1       jtc 
    719       1.1       jtc int
    720       1.1       jtc ustar_strd(void)
    721       1.1       jtc {
    722       1.1       jtc 	return(0);
    723       1.1       jtc }
    724       1.1       jtc 
    725       1.1       jtc /*
    726       1.1       jtc  * ustar_stwr()
    727       1.1       jtc  *	initialization for ustar write
    728       1.1       jtc  * Return:
    729       1.1       jtc  *	0 if ok, -1 otherwise
    730       1.1       jtc  */
    731       1.1       jtc 
    732       1.1       jtc int
    733       1.1       jtc ustar_stwr(void)
    734       1.1       jtc {
    735       1.1       jtc 	return(0);
    736       1.1       jtc }
    737       1.1       jtc 
    738       1.1       jtc /*
    739       1.1       jtc  * ustar_id()
    740       1.1       jtc  *	determine if a block given to us is a valid ustar header. We have to
    741       1.1       jtc  *	be on the lookout for those pesky blocks of all zero's
    742       1.1       jtc  * Return:
    743       1.1       jtc  *	0 if a ustar header, -1 otherwise
    744       1.1       jtc  */
    745       1.1       jtc 
    746       1.1       jtc int
    747       1.1       jtc ustar_id(char *blk, int size)
    748       1.1       jtc {
    749       1.6       tls 	HD_USTAR *hd;
    750       1.1       jtc 
    751      1.36  christos 	if (size < BLKMULT)
    752       1.1       jtc 		return(-1);
    753       1.1       jtc 	hd = (HD_USTAR *)blk;
    754       1.1       jtc 
    755       1.1       jtc 	/*
    756       1.1       jtc 	 * check for block of zero's first, a simple and fast test then check
    757       1.1       jtc 	 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
    758       1.1       jtc 	 * programs are fouled up and create archives missing the \0. Last we
    759       1.1       jtc 	 * check the checksum. If ok we have to assume it is a valid header.
    760       1.1       jtc 	 */
    761      1.36  christos 	if (hd->name[0] == '\0')
    762       1.1       jtc 		return(-1);
    763      1.36  christos 	if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
    764       1.1       jtc 		return(-1);
    765      1.26  christos 	/* This is GNU tar */
    766      1.31  christos 	if (strncmp(hd->magic, "ustar  ", 8) == 0 && !is_gnutar &&
    767      1.31  christos 	    !seen_gnu_warning) {
    768      1.31  christos 		seen_gnu_warning = 1;
    769      1.32  christos 		tty_warn(0,
    770      1.27  christos 		    "Trying to read GNU tar archive with extensions off");
    771      1.31  christos 	}
    772      1.46      matt 	return check_sum(hd->chksum, sizeof(hd->chksum), blk, BLKMULT, 0);
    773       1.1       jtc }
    774       1.1       jtc 
    775       1.1       jtc /*
    776       1.1       jtc  * ustar_rd()
    777       1.1       jtc  *	extract the values out of block already determined to be a ustar header.
    778       1.1       jtc  *	store the values in the ARCHD parameter.
    779       1.1       jtc  * Return:
    780       1.1       jtc  *	0
    781       1.1       jtc  */
    782       1.1       jtc 
    783       1.1       jtc int
    784       1.6       tls ustar_rd(ARCHD *arcn, char *buf)
    785       1.1       jtc {
    786       1.6       tls 	HD_USTAR *hd;
    787       1.6       tls 	char *dest;
    788      1.12   mycroft 	int cnt;
    789       1.1       jtc 	dev_t devmajor;
    790       1.1       jtc 	dev_t devminor;
    791       1.1       jtc 
    792       1.1       jtc 	/*
    793       1.1       jtc 	 * we only get proper sized buffers
    794       1.1       jtc 	 */
    795       1.1       jtc 	if (ustar_id(buf, BLKMULT) < 0)
    796       1.1       jtc 		return(-1);
    797      1.29  christos 
    798      1.29  christos 	memset(arcn, 0, sizeof(*arcn));
    799       1.1       jtc 	arcn->org_name = arcn->name;
    800      1.29  christos 	arcn->pat = NULL;
    801       1.1       jtc 	arcn->sb.st_nlink = 1;
    802       1.1       jtc 	hd = (HD_USTAR *)buf;
    803       1.1       jtc 
    804       1.1       jtc 	/*
    805       1.1       jtc 	 * see if the filename is split into two parts. if, so joint the parts.
    806       1.1       jtc 	 * we copy the prefix first and add a / between the prefix and name.
    807       1.1       jtc 	 */
    808       1.1       jtc 	dest = arcn->name;
    809       1.1       jtc 	if (*(hd->prefix) != '\0') {
    810      1.22  christos 		cnt = strlcpy(arcn->name, hd->prefix, sizeof(arcn->name));
    811      1.12   mycroft 		dest += cnt;
    812       1.1       jtc 		*dest++ = '/';
    813      1.22  christos 		cnt++;
    814      1.30  christos 	} else {
    815      1.30  christos 		cnt = 0;
    816       1.1       jtc 	}
    817      1.30  christos 
    818      1.30  christos 	if (hd->typeflag != LONGLINKTYPE && hd->typeflag != LONGNAMETYPE) {
    819      1.30  christos 		arcn->nlen = expandname(dest, sizeof(arcn->name) - cnt,
    820  1.47.2.3       jmc 		    &gnu_name_string, hd->name, sizeof(hd->name)) + cnt;
    821  1.47.2.3       jmc 		arcn->ln_nlen = expandname(arcn->ln_name,
    822  1.47.2.3       jmc 		    sizeof(arcn->ln_name), &gnu_link_string, hd->linkname,
    823  1.47.2.3       jmc 		    sizeof(hd->linkname));
    824      1.26  christos 	}
    825       1.1       jtc 
    826       1.1       jtc 	/*
    827       1.1       jtc 	 * follow the spec to the letter. we should only have mode bits, strip
    828       1.1       jtc 	 * off all other crud we may be passed.
    829       1.1       jtc 	 */
    830       1.1       jtc 	arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) &
    831       1.1       jtc 	    0xfff);
    832      1.19     lukem 	arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
    833       1.1       jtc 	arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
    834       1.1       jtc 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
    835       1.1       jtc 
    836       1.1       jtc 	/*
    837       1.1       jtc 	 * If we can find the ascii names for gname and uname in the password
    838       1.1       jtc 	 * and group files we will use the uid's and gid they bind. Otherwise
    839       1.1       jtc 	 * we use the uid and gid values stored in the header. (This is what
    840       1.1       jtc 	 * the posix spec wants).
    841       1.1       jtc 	 */
    842       1.1       jtc 	hd->gname[sizeof(hd->gname) - 1] = '\0';
    843      1.10   mycroft 	if (gid_from_group(hd->gname, &(arcn->sb.st_gid)) < 0)
    844       1.1       jtc 		arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
    845       1.1       jtc 	hd->uname[sizeof(hd->uname) - 1] = '\0';
    846      1.10   mycroft 	if (uid_from_user(hd->uname, &(arcn->sb.st_uid)) < 0)
    847       1.1       jtc 		arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
    848       1.1       jtc 
    849       1.1       jtc 	/*
    850       1.1       jtc 	 * set the defaults, these may be changed depending on the file type
    851       1.1       jtc 	 */
    852       1.1       jtc 	arcn->pad = 0;
    853       1.1       jtc 	arcn->skip = 0;
    854       1.1       jtc 	arcn->sb.st_rdev = (dev_t)0;
    855       1.1       jtc 
    856       1.1       jtc 	/*
    857       1.1       jtc 	 * set the mode and PAX type according to the typeflag in the header
    858       1.1       jtc 	 */
    859       1.1       jtc 	switch(hd->typeflag) {
    860       1.1       jtc 	case FIFOTYPE:
    861       1.1       jtc 		arcn->type = PAX_FIF;
    862       1.1       jtc 		arcn->sb.st_mode |= S_IFIFO;
    863       1.1       jtc 		break;
    864       1.1       jtc 	case DIRTYPE:
    865       1.1       jtc 		arcn->type = PAX_DIR;
    866       1.1       jtc 		arcn->sb.st_mode |= S_IFDIR;
    867       1.1       jtc 		arcn->sb.st_nlink = 2;
    868       1.1       jtc 
    869       1.1       jtc 		/*
    870       1.1       jtc 		 * Some programs that create ustar archives append a '/'
    871       1.1       jtc 		 * to the pathname for directories. This clearly violates
    872       1.1       jtc 		 * ustar specs, but we will silently strip it off anyway.
    873       1.1       jtc 		 */
    874       1.1       jtc 		if (arcn->name[arcn->nlen - 1] == '/')
    875       1.1       jtc 			arcn->name[--arcn->nlen] = '\0';
    876       1.1       jtc 		break;
    877       1.1       jtc 	case BLKTYPE:
    878       1.1       jtc 	case CHRTYPE:
    879       1.1       jtc 		/*
    880       1.1       jtc 		 * this type requires the rdev field to be set.
    881       1.1       jtc 		 */
    882       1.1       jtc 		if (hd->typeflag == BLKTYPE) {
    883       1.1       jtc 			arcn->type = PAX_BLK;
    884       1.1       jtc 			arcn->sb.st_mode |= S_IFBLK;
    885       1.1       jtc 		} else {
    886       1.1       jtc 			arcn->type = PAX_CHR;
    887       1.1       jtc 			arcn->sb.st_mode |= S_IFCHR;
    888       1.1       jtc 		}
    889       1.1       jtc 		devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
    890       1.1       jtc 		devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
    891       1.1       jtc 		arcn->sb.st_rdev = TODEV(devmajor, devminor);
    892       1.1       jtc 		break;
    893       1.1       jtc 	case SYMTYPE:
    894       1.1       jtc 	case LNKTYPE:
    895       1.1       jtc 		if (hd->typeflag == SYMTYPE) {
    896       1.1       jtc 			arcn->type = PAX_SLK;
    897       1.1       jtc 			arcn->sb.st_mode |= S_IFLNK;
    898       1.1       jtc 		} else {
    899       1.1       jtc 			arcn->type = PAX_HLK;
    900       1.1       jtc 			/*
    901       1.1       jtc 			 * so printing looks better
    902       1.1       jtc 			 */
    903       1.1       jtc 			arcn->sb.st_mode |= S_IFREG;
    904       1.1       jtc 			arcn->sb.st_nlink = 2;
    905       1.1       jtc 		}
    906       1.1       jtc 		break;
    907      1.26  christos 	case LONGLINKTYPE:
    908      1.26  christos 	case LONGNAMETYPE:
    909      1.26  christos 		if (is_gnutar) {
    910      1.26  christos 			/*
    911      1.26  christos 			 * GNU long link/file; we tag these here and let the
    912      1.26  christos 			 * pax internals deal with it -- too ugly otherwise.
    913      1.26  christos 			 */
    914      1.26  christos 			if (hd->typeflag != LONGLINKTYPE)
    915      1.26  christos 				arcn->type = PAX_GLF;
    916  1.47.2.5      tron 			else
    917  1.47.2.5      tron 				arcn->type = PAX_GLL;
    918      1.26  christos 			arcn->pad = TAR_PAD(arcn->sb.st_size);
    919      1.26  christos 			arcn->skip = arcn->sb.st_size;
    920      1.26  christos 		} else {
    921      1.26  christos 			tty_warn(1, "GNU Long %s found in posix ustar archive.",
    922      1.26  christos 			    hd->typeflag == LONGLINKTYPE ? "Link" : "File");
    923      1.26  christos 		}
    924      1.26  christos 		break;
    925       1.1       jtc 	case CONTTYPE:
    926       1.1       jtc 	case AREGTYPE:
    927       1.1       jtc 	case REGTYPE:
    928       1.1       jtc 	default:
    929       1.1       jtc 		/*
    930       1.1       jtc 		 * these types have file data that follows. Set the skip and
    931       1.1       jtc 		 * pad fields.
    932       1.1       jtc 		 */
    933       1.1       jtc 		arcn->type = PAX_REG;
    934       1.1       jtc 		arcn->pad = TAR_PAD(arcn->sb.st_size);
    935       1.1       jtc 		arcn->skip = arcn->sb.st_size;
    936       1.1       jtc 		arcn->sb.st_mode |= S_IFREG;
    937       1.1       jtc 		break;
    938       1.1       jtc 	}
    939       1.1       jtc 	return(0);
    940       1.1       jtc }
    941       1.1       jtc 
    942      1.30  christos static int
    943      1.39  christos expandname(char *buf, size_t len, char **gnu_name, const char *name,
    944      1.39  christos     size_t nlen)
    945      1.29  christos {
    946      1.30  christos 	if (*gnu_name) {
    947      1.30  christos 		len = strlcpy(buf, *gnu_name, len);
    948      1.30  christos 		free(*gnu_name);
    949      1.30  christos 		*gnu_name = NULL;
    950      1.29  christos 	} else {
    951      1.39  christos 		if (len > ++nlen)
    952      1.39  christos 			len = nlen;
    953      1.30  christos 		len = strlcpy(buf, name, len);
    954      1.29  christos 	}
    955      1.30  christos 	return len;
    956      1.29  christos }
    957      1.26  christos 
    958      1.26  christos static void
    959  1.47.2.5      tron longlink(ARCHD *arcn, int type)
    960      1.26  christos {
    961      1.26  christos 	ARCHD larc;
    962      1.26  christos 
    963  1.47.2.5      tron 	(void)memset(&larc, 0, sizeof(larc));
    964      1.26  christos 
    965  1.47.2.5      tron 	larc.type = type;
    966  1.47.2.5      tron 	larc.nlen = strlcpy(larc.name, LONG_LINK, sizeof(larc.name));
    967  1.47.2.5      tron 
    968  1.47.2.5      tron 	switch (type) {
    969  1.47.2.5      tron 	case PAX_GLL:
    970      1.26  christos 		gnu_hack_string = arcn->ln_name;
    971      1.26  christos 		gnu_hack_len = arcn->ln_nlen + 1;
    972      1.26  christos 		break;
    973  1.47.2.5      tron 	case PAX_GLF:
    974      1.26  christos 		gnu_hack_string = arcn->name;
    975      1.26  christos 		gnu_hack_len = arcn->nlen + 1;
    976  1.47.2.5      tron 		break;
    977  1.47.2.5      tron 	default:
    978  1.47.2.5      tron 		errx(1, "Invalid type in GNU longlink %d\n", type);
    979      1.26  christos 	}
    980  1.47.2.5      tron 
    981      1.26  christos 	/*
    982      1.26  christos 	 * We need a longlink now.
    983      1.26  christos 	 */
    984      1.26  christos 	ustar_wr(&larc);
    985      1.26  christos }
    986      1.26  christos 
    987       1.1       jtc /*
    988       1.1       jtc  * ustar_wr()
    989       1.1       jtc  *	write a ustar header for the file specified in the ARCHD to the archive
    990       1.1       jtc  *	Have to check for file types that cannot be stored and file names that
    991       1.1       jtc  *	are too long. Be careful of the term (last arg) to ul_oct, we only use
    992       1.1       jtc  *	'\0' for the termination character (this is different than picky tar)
    993       1.1       jtc  *	ASSUMED: space after header in header block is zero filled
    994       1.1       jtc  * Return:
    995       1.1       jtc  *	0 if file has data to be written after the header, 1 if file has NO
    996       1.1       jtc  *	data to write after the header, -1 if archive write failed
    997       1.1       jtc  */
    998       1.1       jtc 
    999       1.1       jtc int
   1000       1.6       tls ustar_wr(ARCHD *arcn)
   1001       1.1       jtc {
   1002       1.6       tls 	HD_USTAR *hd;
   1003       1.6       tls 	char *pt;
   1004       1.1       jtc 	char hdblk[sizeof(HD_USTAR)];
   1005      1.10   mycroft 	const char *user, *group;
   1006       1.1       jtc 
   1007  1.47.2.5      tron 	switch (arcn->type) {
   1008  1.47.2.5      tron 	case PAX_SCK:
   1009  1.47.2.5      tron 		/*
   1010  1.47.2.5      tron 		 * check for those file system types ustar cannot store
   1011  1.47.2.5      tron 		 */
   1012      1.38  christos 		if (!is_gnutar)
   1013      1.38  christos 			tty_warn(1, "Ustar cannot archive a socket %s",
   1014      1.38  christos 			    arcn->org_name);
   1015       1.1       jtc 		return(1);
   1016       1.1       jtc 
   1017  1.47.2.5      tron 	case PAX_SLK:
   1018  1.47.2.5      tron 	case PAX_HLK:
   1019  1.47.2.5      tron 	case PAX_HRG:
   1020  1.47.2.5      tron 		/*
   1021  1.47.2.5      tron 		 * check the length of the linkname
   1022  1.47.2.5      tron 		 */
   1023  1.47.2.5      tron 		if (arcn->ln_nlen >= sizeof(hd->linkname)) {
   1024  1.47.2.5      tron 			if (is_gnutar) {
   1025  1.47.2.5      tron 				longlink(arcn, PAX_GLL);
   1026  1.47.2.5      tron 			} else {
   1027  1.47.2.5      tron 				tty_warn(1, "Link name too long for ustar %s",
   1028  1.47.2.5      tron 				    arcn->ln_name);
   1029  1.47.2.5      tron 				return(1);
   1030  1.47.2.5      tron 			}
   1031      1.26  christos 		}
   1032  1.47.2.5      tron 		break;
   1033  1.47.2.5      tron 	default:
   1034  1.47.2.5      tron 		break;
   1035       1.1       jtc 	}
   1036       1.1       jtc 
   1037       1.1       jtc 	/*
   1038       1.1       jtc 	 * split the path name into prefix and name fields (if needed). if
   1039       1.1       jtc 	 * pt != arcn->name, the name has to be split
   1040       1.1       jtc 	 */
   1041       1.1       jtc 	if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
   1042      1.26  christos 		if (is_gnutar) {
   1043  1.47.2.5      tron 			longlink(arcn, PAX_GLF);
   1044      1.26  christos 			pt = arcn->name;
   1045      1.26  christos 		} else {
   1046      1.26  christos 			tty_warn(1, "File name too long for ustar %s",
   1047      1.26  christos 			    arcn->name);
   1048      1.26  christos 			return(1);
   1049      1.26  christos 		}
   1050       1.1       jtc 	}
   1051      1.22  christos 
   1052      1.22  christos 	/*
   1053      1.22  christos 	 * zero out the header so we don't have to worry about zero fill below
   1054      1.22  christos 	 */
   1055      1.22  christos 	memset(hdblk, 0, sizeof(hdblk));
   1056       1.1       jtc 	hd = (HD_USTAR *)hdblk;
   1057       1.1       jtc 	arcn->pad = 0L;
   1058       1.1       jtc 
   1059       1.1       jtc 	/*
   1060       1.1       jtc 	 * split the name, or zero out the prefix
   1061       1.1       jtc 	 */
   1062       1.1       jtc 	if (pt != arcn->name) {
   1063       1.1       jtc 		/*
   1064       1.1       jtc 		 * name was split, pt points at the / where the split is to
   1065       1.1       jtc 		 * occur, we remove the / and copy the first part to the prefix
   1066       1.1       jtc 		 */
   1067       1.1       jtc 		*pt = '\0';
   1068      1.22  christos 		strlcpy(hd->prefix, arcn->name, sizeof(hd->prefix));
   1069       1.1       jtc 		*pt++ = '/';
   1070      1.22  christos 	}
   1071       1.1       jtc 
   1072       1.1       jtc 	/*
   1073       1.1       jtc 	 * copy the name part. this may be the whole path or the part after
   1074       1.1       jtc 	 * the prefix
   1075       1.1       jtc 	 */
   1076      1.22  christos 	strlcpy(hd->name, pt, sizeof(hd->name));
   1077       1.1       jtc 
   1078      1.18     itohy 	/*
   1079       1.1       jtc 	 * set the fields in the header that are type dependent
   1080       1.1       jtc 	 */
   1081       1.1       jtc 	switch(arcn->type) {
   1082       1.1       jtc 	case PAX_DIR:
   1083       1.1       jtc 		hd->typeflag = DIRTYPE;
   1084       1.1       jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
   1085       1.1       jtc 			goto out;
   1086       1.1       jtc 		break;
   1087       1.1       jtc 	case PAX_CHR:
   1088       1.1       jtc 	case PAX_BLK:
   1089       1.1       jtc 		if (arcn->type == PAX_CHR)
   1090       1.1       jtc 			hd->typeflag = CHRTYPE;
   1091       1.1       jtc 		else
   1092       1.1       jtc 			hd->typeflag = BLKTYPE;
   1093       1.1       jtc 		if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
   1094       1.1       jtc 		   sizeof(hd->devmajor), 3) ||
   1095       1.1       jtc 		   ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
   1096       1.1       jtc 		   sizeof(hd->devminor), 3) ||
   1097       1.1       jtc 		   ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
   1098       1.1       jtc 			goto out;
   1099       1.1       jtc 		break;
   1100       1.1       jtc 	case PAX_FIF:
   1101       1.1       jtc 		hd->typeflag = FIFOTYPE;
   1102       1.1       jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
   1103       1.1       jtc 			goto out;
   1104       1.1       jtc 		break;
   1105      1.26  christos 	case PAX_GLL:
   1106       1.1       jtc 	case PAX_SLK:
   1107       1.1       jtc 	case PAX_HLK:
   1108       1.1       jtc 	case PAX_HRG:
   1109       1.1       jtc 		if (arcn->type == PAX_SLK)
   1110       1.1       jtc 			hd->typeflag = SYMTYPE;
   1111      1.26  christos 		else if (arcn->type == PAX_GLL)
   1112      1.26  christos 			hd->typeflag = LONGLINKTYPE;
   1113       1.1       jtc 		else
   1114       1.1       jtc 			hd->typeflag = LNKTYPE;
   1115      1.22  christos 		strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
   1116      1.26  christos 		if (ul_oct((u_long)gnu_hack_len, hd->size,
   1117      1.26  christos 		    sizeof(hd->size), 3))
   1118       1.1       jtc 			goto out;
   1119       1.1       jtc 		break;
   1120      1.26  christos 	case PAX_GLF:
   1121       1.1       jtc 	case PAX_REG:
   1122       1.1       jtc 	case PAX_CTG:
   1123       1.1       jtc 	default:
   1124       1.1       jtc 		/*
   1125       1.1       jtc 		 * file data with this type, set the padding
   1126       1.1       jtc 		 */
   1127      1.26  christos 		if (arcn->type == PAX_GLF) {
   1128      1.26  christos 			hd->typeflag = LONGNAMETYPE;
   1129      1.26  christos 			arcn->pad = TAR_PAD(gnu_hack_len);
   1130      1.26  christos 			if (OFFT_OCT((u_long)gnu_hack_len, hd->size,
   1131      1.26  christos 			    sizeof(hd->size), 3)) {
   1132      1.26  christos 				tty_warn(1,"File is too long for ustar %s",
   1133      1.26  christos 				    arcn->org_name);
   1134      1.26  christos 				return(1);
   1135      1.26  christos 			}
   1136      1.26  christos 		} else {
   1137      1.26  christos 			if (arcn->type == PAX_CTG)
   1138      1.26  christos 				hd->typeflag = CONTTYPE;
   1139      1.26  christos 			else
   1140      1.26  christos 				hd->typeflag = REGTYPE;
   1141      1.26  christos 			arcn->pad = TAR_PAD(arcn->sb.st_size);
   1142      1.26  christos 			if (OFFT_OCT(arcn->sb.st_size, hd->size,
   1143      1.26  christos 			    sizeof(hd->size), 3)) {
   1144      1.26  christos 				tty_warn(1,"File is too long for ustar %s",
   1145      1.26  christos 				    arcn->org_name);
   1146      1.26  christos 				return(1);
   1147      1.26  christos 			}
   1148       1.1       jtc 		}
   1149       1.1       jtc 		break;
   1150       1.1       jtc 	}
   1151       1.1       jtc 
   1152      1.22  christos 	strncpy(hd->magic, TMAGIC, TMAGLEN);
   1153      1.26  christos 	if (is_gnutar)
   1154      1.26  christos 		hd->magic[TMAGLEN - 1] = hd->magic[TMAGLEN] = ' ';
   1155      1.26  christos 	else
   1156      1.26  christos 		strncpy(hd->version, TVERSION, TVERSLEN);
   1157       1.1       jtc 
   1158       1.1       jtc 	/*
   1159       1.1       jtc 	 * set the remaining fields. Some versions want all 16 bits of mode
   1160       1.1       jtc 	 * we better humor them (they really do not meet spec though)....
   1161       1.1       jtc 	 */
   1162       1.1       jtc 	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
   1163       1.1       jtc 	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3)  ||
   1164       1.1       jtc 	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) ||
   1165       1.1       jtc 	    ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
   1166       1.1       jtc 		goto out;
   1167      1.42     grant 	user = user_from_uid(arcn->sb.st_uid, 1);
   1168      1.42     grant 	group = group_from_gid(arcn->sb.st_gid, 1);
   1169      1.22  christos 	strncpy(hd->uname, user ? user : "", sizeof(hd->uname));
   1170      1.22  christos 	strncpy(hd->gname, group ? group : "", sizeof(hd->gname));
   1171       1.1       jtc 
   1172       1.1       jtc 	/*
   1173       1.1       jtc 	 * calculate and store the checksum write the header to the archive
   1174       1.1       jtc 	 * return 0 tells the caller to now write the file data, 1 says no data
   1175       1.1       jtc 	 * needs to be written
   1176       1.1       jtc 	 */
   1177       1.1       jtc 	if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum,
   1178       1.1       jtc 	   sizeof(hd->chksum), 3))
   1179       1.1       jtc 		goto out;
   1180       1.1       jtc 	if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0)
   1181       1.1       jtc 		return(-1);
   1182       1.1       jtc 	if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
   1183       1.1       jtc 		return(-1);
   1184      1.26  christos 	if (gnu_hack_string) {
   1185      1.26  christos 		int res = wr_rdbuf(gnu_hack_string, gnu_hack_len);
   1186      1.26  christos 		int pad = gnu_hack_len;
   1187      1.26  christos 		gnu_hack_string = NULL;
   1188      1.26  christos 		gnu_hack_len = 0;
   1189      1.26  christos 		if (res < 0)
   1190      1.26  christos 			return(-1);
   1191      1.26  christos 		if (wr_skip((off_t)(BLKMULT - (pad % BLKMULT))) < 0)
   1192      1.26  christos 			return(-1);
   1193      1.26  christos 	}
   1194       1.1       jtc 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
   1195       1.1       jtc 		return(0);
   1196       1.1       jtc 	return(1);
   1197       1.1       jtc 
   1198       1.1       jtc     out:
   1199      1.18     itohy 	/*
   1200       1.1       jtc 	 * header field is out of range
   1201       1.1       jtc 	 */
   1202       1.9  christos 	tty_warn(1, "Ustar header field is too small for %s", arcn->org_name);
   1203       1.1       jtc 	return(1);
   1204       1.1       jtc }
   1205       1.1       jtc 
   1206       1.1       jtc /*
   1207       1.1       jtc  * name_split()
   1208       1.1       jtc  *	see if the name has to be split for storage in a ustar header. We try
   1209       1.1       jtc  *	to fit the entire name in the name field without splitting if we can.
   1210       1.1       jtc  *	The split point is always at a /
   1211       1.1       jtc  * Return
   1212       1.1       jtc  *	character pointer to split point (always the / that is to be removed
   1213       1.1       jtc  *	if the split is not needed, the points is set to the start of the file
   1214       1.1       jtc  *	name (it would violate the spec to split there). A NULL is returned if
   1215       1.1       jtc  *	the file name is too long
   1216       1.1       jtc  */
   1217       1.1       jtc 
   1218       1.1       jtc static char *
   1219       1.6       tls name_split(char *name, int len)
   1220       1.1       jtc {
   1221       1.6       tls 	char *start;
   1222       1.1       jtc 
   1223       1.1       jtc 	/*
   1224       1.1       jtc 	 * check to see if the file name is small enough to fit in the name
   1225       1.1       jtc 	 * field. if so just return a pointer to the name.
   1226       1.1       jtc 	 */
   1227      1.22  christos 	if (len < TNMSZ)
   1228       1.1       jtc 		return(name);
   1229      1.22  christos 	if (len > (TPFSZ + TNMSZ))
   1230       1.1       jtc 		return(NULL);
   1231       1.1       jtc 
   1232       1.1       jtc 	/*
   1233       1.1       jtc 	 * we start looking at the biggest sized piece that fits in the name
   1234      1.18     itohy 	 * field. We walk forward looking for a slash to split at. The idea is
   1235       1.1       jtc 	 * to find the biggest piece to fit in the name field (or the smallest
   1236       1.1       jtc 	 * prefix we can find) (the -1 is correct the biggest piece would
   1237       1.1       jtc 	 * include the slash between the two parts that gets thrown away)
   1238       1.1       jtc 	 */
   1239      1.22  christos 	start = name + len - TNMSZ;
   1240       1.1       jtc 	while ((*start != '\0') && (*start != '/'))
   1241       1.1       jtc 		++start;
   1242       1.1       jtc 
   1243       1.1       jtc 	/*
   1244       1.1       jtc 	 * if we hit the end of the string, this name cannot be split, so we
   1245       1.1       jtc 	 * cannot store this file.
   1246       1.1       jtc 	 */
   1247       1.1       jtc 	if (*start == '\0')
   1248       1.1       jtc 		return(NULL);
   1249       1.1       jtc 	len = start - name;
   1250       1.1       jtc 
   1251       1.1       jtc 	/*
   1252      1.40     grant 	 * NOTE: /str where the length of str == TNMSZ cannot be stored under
   1253       1.1       jtc 	 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
   1254       1.1       jtc 	 * the file would then expand on extract to //str. The len == 0 below
   1255       1.1       jtc 	 * makes this special case follow the spec to the letter.
   1256       1.1       jtc 	 */
   1257      1.22  christos 	if ((len >= TPFSZ) || (len == 0))
   1258       1.1       jtc 		return(NULL);
   1259       1.1       jtc 
   1260       1.1       jtc 	/*
   1261       1.1       jtc 	 * ok have a split point, return it to the caller
   1262       1.1       jtc 	 */
   1263       1.1       jtc 	return(start);
   1264      1.13       mrg }
   1265      1.13       mrg 
   1266  1.47.2.1       jmc /*
   1267  1.47.2.1       jmc  * convert a glob into a RE, and add it to the list.  we convert to
   1268  1.47.2.1       jmc  * four different RE's (because we're using BRE's and can't use |
   1269  1.47.2.1       jmc  * alternation :-() with this padding:
   1270  1.47.2.1       jmc  *	.*\/ and $
   1271  1.47.2.1       jmc  *	.*\/ and \/.*
   1272  1.47.2.1       jmc  *	^ and $
   1273  1.47.2.1       jmc  *	^ and \/.*
   1274  1.47.2.1       jmc  */
   1275      1.33       mrg static int
   1276      1.33       mrg tar_gnutar_exclude_one(const char *line, size_t len)
   1277      1.33       mrg {
   1278  1.47.2.2       jmc 	/* 2 * buffer len + nul */
   1279  1.47.2.2       jmc 	char sbuf[MAXPATHLEN * 2 + 1];
   1280  1.47.2.2       jmc 	/* + / + // + .*""/\/ + \/.* */
   1281  1.47.2.2       jmc 	char rabuf[MAXPATHLEN * 2 + 1 + 1 + 2 + 4 + 4];
   1282      1.33       mrg 	int i, j;
   1283      1.33       mrg 
   1284      1.33       mrg 	if (line[len - 1] == '\n')
   1285      1.33       mrg 		len--;
   1286  1.47.2.2       jmc 	strncpy(sbuf, ".*" "\\/", j = 4);
   1287  1.47.2.2       jmc 	for (i = 0; i < len; i++) {
   1288      1.33       mrg 		/*
   1289      1.33       mrg 		 * convert glob to regexp, escaping everything
   1290      1.33       mrg 		 */
   1291      1.33       mrg 		if (line[i] == '*')
   1292      1.33       mrg 			sbuf[j++] = '.';
   1293      1.33       mrg 		else if (line[i] == '?') {
   1294      1.33       mrg 			sbuf[j++] = '.';
   1295      1.33       mrg 			continue;
   1296      1.33       mrg 		} else if (!isalnum(line[i]) && !isblank(line[i]))
   1297      1.33       mrg 			sbuf[j++] = '\\';
   1298      1.33       mrg 		sbuf[j++] = line[i];
   1299      1.33       mrg 	}
   1300  1.47.2.2       jmc 	/* don't need the .*\/ ones if we start with /, i guess */
   1301  1.47.2.2       jmc 	if (line[0] != '/') {
   1302  1.47.2.3       jmc 		(void)snprintf(rabuf, sizeof rabuf, "/.*\\/%s$//", sbuf);
   1303  1.47.2.2       jmc 		if (rep_add(rabuf) < 0)
   1304  1.47.2.2       jmc 			return (-1);
   1305  1.47.2.3       jmc 		(void)snprintf(rabuf, sizeof rabuf, "/.*\\/%s\\/.*//", sbuf);
   1306  1.47.2.2       jmc 		if (rep_add(rabuf) < 0)
   1307  1.47.2.2       jmc 			return (-1);
   1308  1.47.2.2       jmc 	}
   1309      1.33       mrg 
   1310  1.47.2.3       jmc 	(void)snprintf(rabuf, sizeof rabuf, "/^%s$//", sbuf);
   1311  1.47.2.2       jmc 	if (rep_add(rabuf) < 0)
   1312  1.47.2.1       jmc 		return (-1);
   1313  1.47.2.3       jmc 	(void)snprintf(rabuf, sizeof rabuf, "/^%s\\/.*//", sbuf);
   1314  1.47.2.2       jmc 	if (rep_add(rabuf) < 0)
   1315  1.47.2.1       jmc 		return (-1);
   1316  1.47.2.1       jmc 
   1317      1.33       mrg 	return (0);
   1318      1.33       mrg }
   1319      1.33       mrg 
   1320      1.13       mrg /*
   1321      1.33       mrg  * deal with GNU tar -X/--exclude-from & --exclude switchs.  basically,
   1322      1.33       mrg  * we go through each line of the file, building a string from the "glob"
   1323      1.33       mrg  * lines in the file into RE lines, of the form `/^RE$//', which we pass
   1324      1.33       mrg  * to rep_add(), which will add a empty replacement (exclusion), for the
   1325      1.33       mrg  * named files.
   1326      1.13       mrg  */
   1327      1.13       mrg int
   1328      1.33       mrg tar_gnutar_minus_minus_exclude(path)
   1329      1.33       mrg 	const char *path;
   1330      1.33       mrg {
   1331      1.33       mrg 	size_t	len = strlen(path);
   1332      1.33       mrg 
   1333      1.33       mrg 	if (len > MAXPATHLEN)
   1334      1.33       mrg 		tty_warn(0, "pathname too long: %s", path);
   1335      1.33       mrg 
   1336      1.33       mrg 	return (tar_gnutar_exclude_one(path, len));
   1337      1.33       mrg }
   1338      1.33       mrg 
   1339      1.33       mrg int
   1340      1.13       mrg tar_gnutar_X_compat(path)
   1341      1.13       mrg 	const char *path;
   1342      1.13       mrg {
   1343      1.33       mrg 	char *line;
   1344      1.13       mrg 	FILE *fp;
   1345      1.33       mrg 	int lineno = 0;
   1346      1.14   mycroft 	size_t len;
   1347      1.13       mrg 
   1348      1.13       mrg 	fp = fopen(path, "r");
   1349      1.13       mrg 	if (fp == NULL) {
   1350      1.40     grant 		tty_warn(1, "cannot open %s: %s", path,
   1351      1.13       mrg 		    strerror(errno));
   1352      1.13       mrg 		return(-1);
   1353      1.13       mrg 	}
   1354      1.13       mrg 
   1355      1.13       mrg 	while ((line = fgetln(fp, &len))) {
   1356      1.13       mrg 		lineno++;
   1357      1.13       mrg 		if (len > MAXPATHLEN) {
   1358      1.13       mrg 			tty_warn(0, "pathname too long, line %d of %s",
   1359      1.13       mrg 			    lineno, path);
   1360      1.13       mrg 		}
   1361      1.33       mrg 		if (tar_gnutar_exclude_one(line, len))
   1362      1.13       mrg 			return (-1);
   1363      1.13       mrg 	}
   1364      1.13       mrg 	return (0);
   1365       1.1       jtc }
   1366