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