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