Home | History | Annotate | Line # | Download | only in pax
tar.c revision 1.30
      1 /*	$NetBSD: tar.c,v 1.30 2002/10/17 01:08:22 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.30 2002/10/17 01:08:22 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 *);
     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 link */
     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 	if (hd->linkflag != LONGLINKTYPE && hd->linkflag != LONGNAMETYPE) {
    418 		arcn->nlen = expandname(arcn->name, sizeof(arcn->name),
    419 		    &gnu_name_string, hd->name);
    420 		arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name),
    421 		    &gnu_link_string, hd->linkname);
    422 	}
    423 	arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) &
    424 	    0xfff);
    425 	arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
    426 	arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
    427 	arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
    428 	arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
    429 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
    430 
    431 	/*
    432 	 * have to look at the last character, it may be a '/' and that is used
    433 	 * to encode this as a directory
    434 	 */
    435 	pt = &(arcn->name[arcn->nlen - 1]);
    436 	arcn->pad = 0;
    437 	arcn->skip = 0;
    438 	switch(hd->linkflag) {
    439 	case SYMTYPE:
    440 		/*
    441 		 * symbolic link, need to get the link name and set the type in
    442 		 * the st_mode so -v printing will look correct.
    443 		 */
    444 		arcn->type = PAX_SLK;
    445 		arcn->sb.st_mode |= S_IFLNK;
    446 		break;
    447 	case LNKTYPE:
    448 		/*
    449 		 * hard link, need to get the link name, set the type in the
    450 		 * st_mode and st_nlink so -v printing will look better.
    451 		 */
    452 		arcn->type = PAX_HLK;
    453 		arcn->sb.st_nlink = 2;
    454 
    455 		/*
    456 		 * no idea of what type this thing really points at, but
    457 		 * we set something for printing only.
    458 		 */
    459 		arcn->sb.st_mode |= S_IFREG;
    460 		break;
    461 	case LONGLINKTYPE:
    462 		arcn->type = PAX_GLL;
    463 		/* FALLTHROUGH */
    464 	case LONGNAMETYPE:
    465 		/*
    466 		 * GNU long link/file; we tag these here and let the
    467 		 * pax internals deal with it -- too ugly otherwise.
    468 		 */
    469 		if (hd->linkflag != LONGLINKTYPE)
    470 			arcn->type = PAX_GLF;
    471 		arcn->pad = TAR_PAD(arcn->sb.st_size);
    472 		arcn->skip = arcn->sb.st_size;
    473 		break;
    474 	case AREGTYPE:
    475 	case REGTYPE:
    476 	case DIRTYPE:	/* see below */
    477 	default:
    478 		/*
    479 		 * If we have a trailing / this is a directory and NOT a file.
    480 		 * Note: V7 tar doesn't actually have DIRTYPE, but it was
    481 		 * reported that V7 archives using USTAR directories do exist.
    482 		 */
    483 		if (*pt == '/' || hd->linkflag == DIRTYPE) {
    484 			/*
    485 			 * it is a directory, set the mode for -v printing
    486 			 */
    487 			arcn->type = PAX_DIR;
    488 			arcn->sb.st_mode |= S_IFDIR;
    489 			arcn->sb.st_nlink = 2;
    490 		} else {
    491 			/*
    492 			 * have a file that will be followed by data. Set the
    493 			 * skip value to the size field and calculate the size
    494 			 * of the padding.
    495 			 */
    496 			arcn->type = PAX_REG;
    497 			arcn->sb.st_mode |= S_IFREG;
    498 			arcn->pad = TAR_PAD(arcn->sb.st_size);
    499 			arcn->skip = arcn->sb.st_size;
    500 		}
    501 		break;
    502 	}
    503 
    504 	/*
    505 	 * strip off any trailing slash.
    506 	 */
    507 	if (*pt == '/') {
    508 		*pt = '\0';
    509 		--arcn->nlen;
    510 	}
    511 	return(0);
    512 }
    513 
    514 /*
    515  * tar_wr()
    516  *	write a tar header for the file specified in the ARCHD to the archive.
    517  *	Have to check for file types that cannot be stored and file names that
    518  *	are too long. Be careful of the term (last arg) to ul_oct, each field
    519  *	of tar has it own spec for the termination character(s).
    520  *	ASSUMED: space after header in header block is zero filled
    521  * Return:
    522  *	0 if file has data to be written after the header, 1 if file has NO
    523  *	data to write after the header, -1 if archive write failed
    524  */
    525 
    526 int
    527 tar_wr(ARCHD *arcn)
    528 {
    529 	HD_TAR *hd;
    530 	int len;
    531 	char hdblk[sizeof(HD_TAR)];
    532 
    533 	/*
    534 	 * check for those file system types which tar cannot store
    535 	 */
    536 	switch(arcn->type) {
    537 	case PAX_DIR:
    538 		/*
    539 		 * user asked that dirs not be written to the archive
    540 		 */
    541 		if (tar_nodir)
    542 			return(1);
    543 		break;
    544 	case PAX_CHR:
    545 		tty_warn(1, "Tar cannot archive a character device %s",
    546 		    arcn->org_name);
    547 		return(1);
    548 	case PAX_BLK:
    549 		tty_warn(1,
    550 		    "Tar cannot archive a block device %s", arcn->org_name);
    551 		return(1);
    552 	case PAX_SCK:
    553 		tty_warn(1, "Tar cannot archive a socket %s", arcn->org_name);
    554 		return(1);
    555 	case PAX_FIF:
    556 		tty_warn(1, "Tar cannot archive a fifo %s", arcn->org_name);
    557 		return(1);
    558 	case PAX_SLK:
    559 	case PAX_HLK:
    560 	case PAX_HRG:
    561 		if (arcn->ln_nlen > sizeof(hd->linkname)) {
    562 			tty_warn(1,"Link name too long for tar %s",
    563 			    arcn->ln_name);
    564 			return(1);
    565 		}
    566 		break;
    567 	case PAX_REG:
    568 	case PAX_CTG:
    569 	default:
    570 		break;
    571 	}
    572 
    573 	/*
    574 	 * check file name len, remember extra char for dirs (the / at the end)
    575 	 */
    576 	len = arcn->nlen;
    577 	if (arcn->type == PAX_DIR)
    578 		++len;
    579 	if (len >= sizeof(hd->name)) {
    580 		tty_warn(1, "File name too long for tar %s", arcn->name);
    581 		return(1);
    582 	}
    583 
    584 	/*
    585 	 * copy the data out of the ARCHD into the tar header based on the type
    586 	 * of the file. Remember many tar readers want the unused fields to be
    587 	 * padded with zero. We set the linkflag field (type), the linkname
    588 	 * (or zero if not used),the size, and set the padding (if any) to be
    589 	 * added after the file data (0 for all other types, as they only have
    590 	 * a header)
    591 	 */
    592 	memset(hdblk, 0, sizeof(hdblk));
    593 	hd = (HD_TAR *)hdblk;
    594 	strlcpy(hd->name, arcn->name, sizeof(hd->name));
    595 	arcn->pad = 0;
    596 
    597 	if (arcn->type == PAX_DIR) {
    598 		/*
    599 		 * directories are the same as files, except have a filename
    600 		 * that ends with a /, we add the slash here. No data follows,
    601 		 * dirs, so no pad.
    602 		 */
    603 		hd->linkflag = AREGTYPE;
    604 		hd->name[len-1] = '/';
    605 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
    606 			goto out;
    607 	} else if (arcn->type == PAX_SLK) {
    608 		/*
    609 		 * no data follows this file, so no pad
    610 		 */
    611 		hd->linkflag = SYMTYPE;
    612 		strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
    613 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
    614 			goto out;
    615 	} else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
    616 		/*
    617 		 * no data follows this file, so no pad
    618 		 */
    619 		hd->linkflag = LNKTYPE;
    620 		strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
    621 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
    622 			goto out;
    623 	} else {
    624 		/*
    625 		 * data follows this file, so set the pad
    626 		 */
    627 		hd->linkflag = AREGTYPE;
    628 		if (OFFT_OCT(arcn->sb.st_size, hd->size, sizeof(hd->size), 1)) {
    629 			tty_warn(1,"File is too large for tar %s",
    630 			    arcn->org_name);
    631 			return(1);
    632 		}
    633 		arcn->pad = TAR_PAD(arcn->sb.st_size);
    634 	}
    635 
    636 	/*
    637 	 * copy those fields that are independent of the type
    638 	 */
    639 	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
    640 	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
    641 	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
    642 	    ul_oct((u_long)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1))
    643 		goto out;
    644 
    645 	/*
    646 	 * calculate and add the checksum, then write the header. A return of
    647 	 * 0 tells the caller to now write the file data, 1 says no data needs
    648 	 * to be written
    649 	 */
    650 	if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum,
    651 	    sizeof(hd->chksum), 3))
    652 		goto out;			/* XXX Something's wrong here
    653 						 * because a zero-byte file can
    654 						 * cause this to be done and
    655 						 * yet the resulting warning
    656 						 * seems incorrect */
    657 
    658 	if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0)
    659 		return(-1);
    660 	if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
    661 		return(-1);
    662 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
    663 		return(0);
    664 	return(1);
    665 
    666     out:
    667 	/*
    668 	 * header field is out of range
    669 	 */
    670 	tty_warn(1, "Tar header field is too small for %s", arcn->org_name);
    671 	return(1);
    672 }
    673 
    674 /*
    675  * Routines for POSIX ustar
    676  */
    677 
    678 /*
    679  * ustar_strd()
    680  *	initialization for ustar read
    681  * Return:
    682  *	0 if ok, -1 otherwise
    683  */
    684 
    685 int
    686 ustar_strd(void)
    687 {
    688 	return(0);
    689 }
    690 
    691 /*
    692  * ustar_stwr()
    693  *	initialization for ustar write
    694  * Return:
    695  *	0 if ok, -1 otherwise
    696  */
    697 
    698 int
    699 ustar_stwr(void)
    700 {
    701 	return(0);
    702 }
    703 
    704 /*
    705  * ustar_id()
    706  *	determine if a block given to us is a valid ustar header. We have to
    707  *	be on the lookout for those pesky blocks of all zero's
    708  * Return:
    709  *	0 if a ustar header, -1 otherwise
    710  */
    711 
    712 int
    713 ustar_id(char *blk, int size)
    714 {
    715 	HD_USTAR *hd;
    716 
    717 	if (size < BLKMULT)
    718 		return(-1);
    719 	hd = (HD_USTAR *)blk;
    720 
    721 	/*
    722 	 * check for block of zero's first, a simple and fast test then check
    723 	 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
    724 	 * programs are fouled up and create archives missing the \0. Last we
    725 	 * check the checksum. If ok we have to assume it is a valid header.
    726 	 */
    727 	if (hd->name[0] == '\0')
    728 		return(-1);
    729 	if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
    730 		return(-1);
    731 	/* This is GNU tar */
    732 	if (strncmp(hd->magic, "ustar  ", 8) == 0 && !is_gnutar)
    733 		tty_warn(1,
    734 		    "Trying to read GNU tar archive with extensions off");
    735 	if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
    736 		return(-1);
    737 	return(0);
    738 }
    739 
    740 /*
    741  * ustar_rd()
    742  *	extract the values out of block already determined to be a ustar header.
    743  *	store the values in the ARCHD parameter.
    744  * Return:
    745  *	0
    746  */
    747 
    748 int
    749 ustar_rd(ARCHD *arcn, char *buf)
    750 {
    751 	HD_USTAR *hd;
    752 	char *dest;
    753 	int cnt;
    754 	dev_t devmajor;
    755 	dev_t devminor;
    756 
    757 	/*
    758 	 * we only get proper sized buffers
    759 	 */
    760 	if (ustar_id(buf, BLKMULT) < 0)
    761 		return(-1);
    762 
    763 	memset(arcn, 0, sizeof(*arcn));
    764 	arcn->org_name = arcn->name;
    765 	arcn->pat = NULL;
    766 	arcn->sb.st_nlink = 1;
    767 	hd = (HD_USTAR *)buf;
    768 
    769 	/*
    770 	 * see if the filename is split into two parts. if, so joint the parts.
    771 	 * we copy the prefix first and add a / between the prefix and name.
    772 	 */
    773 	dest = arcn->name;
    774 	if (*(hd->prefix) != '\0') {
    775 		cnt = strlcpy(arcn->name, hd->prefix, sizeof(arcn->name));
    776 		dest += cnt;
    777 		*dest++ = '/';
    778 		cnt++;
    779 	} else {
    780 		cnt = 0;
    781 	}
    782 
    783 	if (hd->typeflag != LONGLINKTYPE && hd->typeflag != LONGNAMETYPE) {
    784 		arcn->nlen = expandname(dest, sizeof(arcn->name) - cnt,
    785 		    &gnu_name_string, hd->name);
    786 		arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name),
    787 		    &gnu_link_string, hd->linkname);
    788 	}
    789 
    790 	/*
    791 	 * follow the spec to the letter. we should only have mode bits, strip
    792 	 * off all other crud we may be passed.
    793 	 */
    794 	arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) &
    795 	    0xfff);
    796 	arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
    797 	arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
    798 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
    799 
    800 	/*
    801 	 * If we can find the ascii names for gname and uname in the password
    802 	 * and group files we will use the uid's and gid they bind. Otherwise
    803 	 * we use the uid and gid values stored in the header. (This is what
    804 	 * the posix spec wants).
    805 	 */
    806 	hd->gname[sizeof(hd->gname) - 1] = '\0';
    807 	if (gid_from_group(hd->gname, &(arcn->sb.st_gid)) < 0)
    808 		arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
    809 	hd->uname[sizeof(hd->uname) - 1] = '\0';
    810 	if (uid_from_user(hd->uname, &(arcn->sb.st_uid)) < 0)
    811 		arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
    812 
    813 	/*
    814 	 * set the defaults, these may be changed depending on the file type
    815 	 */
    816 	arcn->pad = 0;
    817 	arcn->skip = 0;
    818 	arcn->sb.st_rdev = (dev_t)0;
    819 
    820 	/*
    821 	 * set the mode and PAX type according to the typeflag in the header
    822 	 */
    823 	switch(hd->typeflag) {
    824 	case FIFOTYPE:
    825 		arcn->type = PAX_FIF;
    826 		arcn->sb.st_mode |= S_IFIFO;
    827 		break;
    828 	case DIRTYPE:
    829 		arcn->type = PAX_DIR;
    830 		arcn->sb.st_mode |= S_IFDIR;
    831 		arcn->sb.st_nlink = 2;
    832 
    833 		/*
    834 		 * Some programs that create ustar archives append a '/'
    835 		 * to the pathname for directories. This clearly violates
    836 		 * ustar specs, but we will silently strip it off anyway.
    837 		 */
    838 		if (arcn->name[arcn->nlen - 1] == '/')
    839 			arcn->name[--arcn->nlen] = '\0';
    840 		break;
    841 	case BLKTYPE:
    842 	case CHRTYPE:
    843 		/*
    844 		 * this type requires the rdev field to be set.
    845 		 */
    846 		if (hd->typeflag == BLKTYPE) {
    847 			arcn->type = PAX_BLK;
    848 			arcn->sb.st_mode |= S_IFBLK;
    849 		} else {
    850 			arcn->type = PAX_CHR;
    851 			arcn->sb.st_mode |= S_IFCHR;
    852 		}
    853 		devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
    854 		devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
    855 		arcn->sb.st_rdev = TODEV(devmajor, devminor);
    856 		break;
    857 	case SYMTYPE:
    858 	case LNKTYPE:
    859 		if (hd->typeflag == SYMTYPE) {
    860 			arcn->type = PAX_SLK;
    861 			arcn->sb.st_mode |= S_IFLNK;
    862 		} else {
    863 			arcn->type = PAX_HLK;
    864 			/*
    865 			 * so printing looks better
    866 			 */
    867 			arcn->sb.st_mode |= S_IFREG;
    868 			arcn->sb.st_nlink = 2;
    869 		}
    870 		break;
    871 	case LONGLINKTYPE:
    872 		if (is_gnutar)
    873 			arcn->type = PAX_GLL;
    874 		/* FALLTHROUGH */
    875 	case LONGNAMETYPE:
    876 		if (is_gnutar) {
    877 			/*
    878 			 * GNU long link/file; we tag these here and let the
    879 			 * pax internals deal with it -- too ugly otherwise.
    880 			 */
    881 			if (hd->typeflag != LONGLINKTYPE)
    882 				arcn->type = PAX_GLF;
    883 			arcn->pad = TAR_PAD(arcn->sb.st_size);
    884 			arcn->skip = arcn->sb.st_size;
    885 		} else {
    886 			tty_warn(1, "GNU Long %s found in posix ustar archive.",
    887 			    hd->typeflag == LONGLINKTYPE ? "Link" : "File");
    888 		}
    889 		break;
    890 	case CONTTYPE:
    891 	case AREGTYPE:
    892 	case REGTYPE:
    893 	default:
    894 		/*
    895 		 * these types have file data that follows. Set the skip and
    896 		 * pad fields.
    897 		 */
    898 		arcn->type = PAX_REG;
    899 		arcn->pad = TAR_PAD(arcn->sb.st_size);
    900 		arcn->skip = arcn->sb.st_size;
    901 		arcn->sb.st_mode |= S_IFREG;
    902 		break;
    903 	}
    904 	return(0);
    905 }
    906 
    907 static int
    908 expandname(char *buf, size_t len,  char **gnu_name, const char *name)
    909 {
    910 	if (*gnu_name) {
    911 		len = strlcpy(buf, *gnu_name, len);
    912 		free(*gnu_name);
    913 		*gnu_name = NULL;
    914 	} else {
    915 		len = strlcpy(buf, name, len);
    916 	}
    917 	return len;
    918 }
    919 
    920 static void
    921 longlink(ARCHD *arcn)
    922 {
    923 	ARCHD larc;
    924 
    925 	memset(&larc, 0, sizeof(larc));
    926 
    927 	switch (arcn->type) {
    928 	case PAX_SLK:
    929 	case PAX_HRG:
    930 	case PAX_HLK:
    931 		larc.type = PAX_GLL;
    932 		larc.ln_nlen = strlcpy(larc.ln_name, "././@LongLink",
    933 		    sizeof(larc.ln_name));
    934 		gnu_hack_string = arcn->ln_name;
    935 		gnu_hack_len = arcn->ln_nlen + 1;
    936 		break;
    937 	default:
    938 		larc.nlen = strlcpy(larc.name, "././@LongLink",
    939 		    sizeof(larc.name));
    940 		gnu_hack_string = arcn->name;
    941 		gnu_hack_len = arcn->nlen + 1;
    942 		larc.type = PAX_GLF;
    943 	}
    944 	/*
    945 	 * We need a longlink now.
    946 	 */
    947 	ustar_wr(&larc);
    948 }
    949 
    950 /*
    951  * ustar_wr()
    952  *	write a ustar header for the file specified in the ARCHD to the archive
    953  *	Have to check for file types that cannot be stored and file names that
    954  *	are too long. Be careful of the term (last arg) to ul_oct, we only use
    955  *	'\0' for the termination character (this is different than picky tar)
    956  *	ASSUMED: space after header in header block is zero filled
    957  * Return:
    958  *	0 if file has data to be written after the header, 1 if file has NO
    959  *	data to write after the header, -1 if archive write failed
    960  */
    961 
    962 int
    963 ustar_wr(ARCHD *arcn)
    964 {
    965 	HD_USTAR *hd;
    966 	char *pt;
    967 	char hdblk[sizeof(HD_USTAR)];
    968 	const char *user, *group;
    969 
    970 	/*
    971 	 * check for those file system types ustar cannot store
    972 	 */
    973 	if (arcn->type == PAX_SCK) {
    974 		tty_warn(1, "Ustar cannot archive a socket %s", arcn->org_name);
    975 		return(1);
    976 	}
    977 
    978 	/*
    979 	 * check the length of the linkname
    980 	 */
    981 	if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
    982 	    (arcn->type == PAX_HRG)) &&
    983 	    (arcn->ln_nlen >= sizeof(hd->linkname))){
    984 		if (is_gnutar) {
    985 			longlink(arcn);
    986 		} else {
    987 			tty_warn(1, "Link name too long for ustar %s",
    988 			    arcn->ln_name);
    989 			return(1);
    990 		}
    991 	}
    992 
    993 	/*
    994 	 * split the path name into prefix and name fields (if needed). if
    995 	 * pt != arcn->name, the name has to be split
    996 	 */
    997 	if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
    998 		if (is_gnutar) {
    999 			longlink(arcn);
   1000 			pt = arcn->name;
   1001 		} else {
   1002 			tty_warn(1, "File name too long for ustar %s",
   1003 			    arcn->name);
   1004 			return(1);
   1005 		}
   1006 	}
   1007 
   1008 	/*
   1009 	 * zero out the header so we don't have to worry about zero fill below
   1010 	 */
   1011 	memset(hdblk, 0, sizeof(hdblk));
   1012 	hd = (HD_USTAR *)hdblk;
   1013 	arcn->pad = 0L;
   1014 
   1015 	/*
   1016 	 * split the name, or zero out the prefix
   1017 	 */
   1018 	if (pt != arcn->name) {
   1019 		/*
   1020 		 * name was split, pt points at the / where the split is to
   1021 		 * occur, we remove the / and copy the first part to the prefix
   1022 		 */
   1023 		*pt = '\0';
   1024 		strlcpy(hd->prefix, arcn->name, sizeof(hd->prefix));
   1025 		*pt++ = '/';
   1026 	}
   1027 
   1028 	/*
   1029 	 * copy the name part. this may be the whole path or the part after
   1030 	 * the prefix
   1031 	 */
   1032 	strlcpy(hd->name, pt, sizeof(hd->name));
   1033 
   1034 	/*
   1035 	 * set the fields in the header that are type dependent
   1036 	 */
   1037 	switch(arcn->type) {
   1038 	case PAX_DIR:
   1039 		hd->typeflag = DIRTYPE;
   1040 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
   1041 			goto out;
   1042 		break;
   1043 	case PAX_CHR:
   1044 	case PAX_BLK:
   1045 		if (arcn->type == PAX_CHR)
   1046 			hd->typeflag = CHRTYPE;
   1047 		else
   1048 			hd->typeflag = BLKTYPE;
   1049 		if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
   1050 		   sizeof(hd->devmajor), 3) ||
   1051 		   ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
   1052 		   sizeof(hd->devminor), 3) ||
   1053 		   ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
   1054 			goto out;
   1055 		break;
   1056 	case PAX_FIF:
   1057 		hd->typeflag = FIFOTYPE;
   1058 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
   1059 			goto out;
   1060 		break;
   1061 	case PAX_GLL:
   1062 	case PAX_SLK:
   1063 	case PAX_HLK:
   1064 	case PAX_HRG:
   1065 		if (arcn->type == PAX_SLK)
   1066 			hd->typeflag = SYMTYPE;
   1067 		else if (arcn->type == PAX_GLL)
   1068 			hd->typeflag = LONGLINKTYPE;
   1069 		else
   1070 			hd->typeflag = LNKTYPE;
   1071 		strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
   1072 		if (ul_oct((u_long)gnu_hack_len, hd->size,
   1073 		    sizeof(hd->size), 3))
   1074 			goto out;
   1075 		break;
   1076 	case PAX_GLF:
   1077 	case PAX_REG:
   1078 	case PAX_CTG:
   1079 	default:
   1080 		/*
   1081 		 * file data with this type, set the padding
   1082 		 */
   1083 		if (arcn->type == PAX_GLF) {
   1084 			hd->typeflag = LONGNAMETYPE;
   1085 			arcn->pad = TAR_PAD(gnu_hack_len);
   1086 			if (OFFT_OCT((u_long)gnu_hack_len, hd->size,
   1087 			    sizeof(hd->size), 3)) {
   1088 				tty_warn(1,"File is too long for ustar %s",
   1089 				    arcn->org_name);
   1090 				return(1);
   1091 			}
   1092 		} else {
   1093 			if (arcn->type == PAX_CTG)
   1094 				hd->typeflag = CONTTYPE;
   1095 			else
   1096 				hd->typeflag = REGTYPE;
   1097 			arcn->pad = TAR_PAD(arcn->sb.st_size);
   1098 			if (OFFT_OCT(arcn->sb.st_size, hd->size,
   1099 			    sizeof(hd->size), 3)) {
   1100 				tty_warn(1,"File is too long for ustar %s",
   1101 				    arcn->org_name);
   1102 				return(1);
   1103 			}
   1104 		}
   1105 		break;
   1106 	}
   1107 
   1108 	strncpy(hd->magic, TMAGIC, TMAGLEN);
   1109 	if (is_gnutar)
   1110 		hd->magic[TMAGLEN - 1] = hd->magic[TMAGLEN] = ' ';
   1111 	else
   1112 		strncpy(hd->version, TVERSION, TVERSLEN);
   1113 
   1114 	/*
   1115 	 * set the remaining fields. Some versions want all 16 bits of mode
   1116 	 * we better humor them (they really do not meet spec though)....
   1117 	 */
   1118 	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
   1119 	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3)  ||
   1120 	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) ||
   1121 	    ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
   1122 		goto out;
   1123 	user = user_from_uid(arcn->sb.st_uid, 1);
   1124 	group = group_from_gid(arcn->sb.st_gid, 1);
   1125 	strncpy(hd->uname, user ? user : "", sizeof(hd->uname));
   1126 	strncpy(hd->gname, group ? group : "", sizeof(hd->gname));
   1127 
   1128 	/*
   1129 	 * calculate and store the checksum write the header to the archive
   1130 	 * return 0 tells the caller to now write the file data, 1 says no data
   1131 	 * needs to be written
   1132 	 */
   1133 	if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum,
   1134 	   sizeof(hd->chksum), 3))
   1135 		goto out;
   1136 	if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0)
   1137 		return(-1);
   1138 	if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
   1139 		return(-1);
   1140 	if (gnu_hack_string) {
   1141 		int res = wr_rdbuf(gnu_hack_string, gnu_hack_len);
   1142 		int pad = gnu_hack_len;
   1143 		gnu_hack_string = NULL;
   1144 		gnu_hack_len = 0;
   1145 		if (res < 0)
   1146 			return(-1);
   1147 		if (wr_skip((off_t)(BLKMULT - (pad % BLKMULT))) < 0)
   1148 			return(-1);
   1149 	}
   1150 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
   1151 		return(0);
   1152 	return(1);
   1153 
   1154     out:
   1155 	/*
   1156 	 * header field is out of range
   1157 	 */
   1158 	tty_warn(1, "Ustar header field is too small for %s", arcn->org_name);
   1159 	return(1);
   1160 }
   1161 
   1162 /*
   1163  * name_split()
   1164  *	see if the name has to be split for storage in a ustar header. We try
   1165  *	to fit the entire name in the name field without splitting if we can.
   1166  *	The split point is always at a /
   1167  * Return
   1168  *	character pointer to split point (always the / that is to be removed
   1169  *	if the split is not needed, the points is set to the start of the file
   1170  *	name (it would violate the spec to split there). A NULL is returned if
   1171  *	the file name is too long
   1172  */
   1173 
   1174 static char *
   1175 name_split(char *name, int len)
   1176 {
   1177 	char *start;
   1178 
   1179 	/*
   1180 	 * check to see if the file name is small enough to fit in the name
   1181 	 * field. if so just return a pointer to the name.
   1182 	 */
   1183 	if (len < TNMSZ)
   1184 		return(name);
   1185 	if (len > (TPFSZ + TNMSZ))
   1186 		return(NULL);
   1187 
   1188 	/*
   1189 	 * we start looking at the biggest sized piece that fits in the name
   1190 	 * field. We walk forward looking for a slash to split at. The idea is
   1191 	 * to find the biggest piece to fit in the name field (or the smallest
   1192 	 * prefix we can find) (the -1 is correct the biggest piece would
   1193 	 * include the slash between the two parts that gets thrown away)
   1194 	 */
   1195 	start = name + len - TNMSZ;
   1196 	while ((*start != '\0') && (*start != '/'))
   1197 		++start;
   1198 
   1199 	/*
   1200 	 * if we hit the end of the string, this name cannot be split, so we
   1201 	 * cannot store this file.
   1202 	 */
   1203 	if (*start == '\0')
   1204 		return(NULL);
   1205 	len = start - name;
   1206 
   1207 	/*
   1208 	 * NOTE: /str where the length of str == TNMSZ can not be stored under
   1209 	 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
   1210 	 * the file would then expand on extract to //str. The len == 0 below
   1211 	 * makes this special case follow the spec to the letter.
   1212 	 */
   1213 	if ((len >= TPFSZ) || (len == 0))
   1214 		return(NULL);
   1215 
   1216 	/*
   1217 	 * ok have a split point, return it to the caller
   1218 	 */
   1219 	return(start);
   1220 }
   1221 
   1222 /*
   1223  * deal with GNU tar -X switch.  basically, we go through each line of
   1224  * the file, building a string from the "glob" lines in the file into
   1225  * RE lines, of the form `/^RE$//', which we pass to rep_add(), which
   1226  * will add a empty replacement (exclusion), for the named files.
   1227  */
   1228 int
   1229 tar_gnutar_X_compat(path)
   1230 	const char *path;
   1231 {
   1232 	char *line, sbuf[MAXPATHLEN * 2 + 1 + 5];
   1233 	FILE *fp;
   1234 	int lineno = 0, i, j;
   1235 	size_t len;
   1236 
   1237 	fp = fopen(path, "r");
   1238 	if (fp == NULL) {
   1239 		tty_warn(1, "can not open %s: %s", path,
   1240 		    strerror(errno));
   1241 		return(-1);
   1242 	}
   1243 
   1244 	while ((line = fgetln(fp, &len))) {
   1245 		lineno++;
   1246 		if (len > MAXPATHLEN) {
   1247 			tty_warn(0, "pathname too long, line %d of %s",
   1248 			    lineno, path);
   1249 		}
   1250 		if (line[len - 1] == '\n')
   1251 			len--;
   1252 		for (i = 0, j = 2; i < len; i++) {
   1253 			/*
   1254 			 * convert glob to regexp, escaping everything
   1255 			 */
   1256 			if (line[i] == '*')
   1257 				sbuf[j++] = '.';
   1258 			else if (line[i] == '?')
   1259 				line[i] = '.';
   1260 			else if (!isalnum(line[i]) && !isblank(line[i]))
   1261 				sbuf[j++] = '\\';
   1262 			sbuf[j++] = line[i];
   1263 		}
   1264 		sbuf[0] = sbuf[j + 1] = sbuf[j + 2] = '/';
   1265 		sbuf[1] = '^';
   1266 		sbuf[j] = '$';
   1267 		sbuf[j + 3] = '\0';
   1268 		if (rep_add(sbuf) < 0)
   1269 			return (-1);
   1270 	}
   1271 	return (0);
   1272 }
   1273