Home | History | Annotate | Line # | Download | only in pax
ar_subs.c revision 1.49
      1 /*	$NetBSD: ar_subs.c,v 1.49 2006/02/11 11:04:57 dsl 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[] = "@(#)ar_subs.c	8.2 (Berkeley) 4/18/94";
     44 #else
     45 __RCSID("$NetBSD: ar_subs.c,v 1.49 2006/02/11 11:04:57 dsl 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 #include <signal.h>
     54 #include <string.h>
     55 #include <stdio.h>
     56 #include <ctype.h>
     57 #include <fcntl.h>
     58 #include <errno.h>
     59 #include <time.h>
     60 #include <unistd.h>
     61 #include <stdlib.h>
     62 #include "pax.h"
     63 #include "extern.h"
     64 
     65 static int path_check(ARCHD *, int);
     66 static int wr_archive(ARCHD *, int is_app);
     67 static int get_arc(void);
     68 static int next_head(ARCHD *);
     69 #if !HAVE_NBTOOL_CONFIG_H
     70 static int fdochroot(int);
     71 #endif
     72 extern sigset_t s_mask;
     73 
     74 /*
     75  * Routines which control the overall operation modes of pax as specified by
     76  * the user: list, append, read ...
     77  */
     78 
     79 static char hdbuf[BLKMULT];		/* space for archive header on read */
     80 u_long flcnt;				/* number of files processed */
     81 ARCHD archd;
     82 
     83 static char	cwdpath[MAXPATHLEN];	/* current working directory path */
     84 static size_t	cwdpathlen;		/* current working directory path len */
     85 
     86 int
     87 updatepath(void)
     88 {
     89 	if (getcwd(cwdpath, sizeof(cwdpath)) == NULL) {
     90 		syswarn(1, errno, "Cannot get working directory");
     91 		return -1;
     92 	}
     93 	cwdpathlen = strlen(cwdpath);
     94 	return 0;
     95 }
     96 
     97 int
     98 fdochdir(int fcwd)
     99 {
    100 	if (fchdir(fcwd) == -1) {
    101 		syswarn(1, errno, "Cannot chdir to `.'");
    102 		return -1;
    103 	}
    104 	return updatepath();
    105 }
    106 
    107 int
    108 dochdir(const char *name)
    109 {
    110 	if (chdir(name) == -1)
    111 		syswarn(1, errno, "Cannot chdir to `%s'", name);
    112 	return updatepath();
    113 }
    114 
    115 #if !HAVE_NBTOOL_CONFIG_H
    116 static int
    117 fdochroot(int fcwd)
    118 {
    119 	if (fchroot(fcwd) != 0) {
    120 		syswarn(1, errno, "Can't fchroot to \".\"");
    121 		return -1;
    122 	}
    123 	return updatepath();
    124 }
    125 #endif
    126 
    127 /*
    128  * mkdir(), but if we failed, check if someone else made it for us
    129  * already and don't error out.
    130  */
    131 int
    132 domkdir(const char *fname, mode_t mode)
    133 {
    134 	int error;
    135 	struct stat sb;
    136 
    137 	if ((error = mkdir(fname, mode)) != -1)
    138 		return error;
    139 
    140 	switch (errno) {
    141 	case EISDIR:
    142 		return 0;
    143 	case EEXIST:
    144 	case EACCES:
    145 	case ENOSYS:	/* Grr Solaris */
    146 		error = errno;
    147 		if (stat(fname, &sb) != -1 && S_ISDIR(sb.st_mode))
    148 			return 0;
    149 		errno = error;
    150 		/*FALLTHROUGH*/
    151 	default:
    152 		return -1;
    153 	}
    154 }
    155 
    156 static int
    157 path_check(ARCHD *arcn, int level)
    158 {
    159 	char buf[MAXPATHLEN];
    160 	char *p;
    161 
    162 	if ((p = strrchr(arcn->name, '/')) == NULL)
    163 		return 0;
    164 	*p = '\0';
    165 
    166 	if (realpath(arcn->name, buf) == NULL) {
    167 		int error;
    168 		error = path_check(arcn, level + 1);
    169 		*p = '/';
    170 		if (error == 0)
    171 			return 0;
    172 		if (level == 0)
    173 			syswarn(1, 0, "Cannot resolve `%s'", arcn->name);
    174 		return -1;
    175 	}
    176 	if (strncmp(buf, cwdpath, cwdpathlen) != 0) {
    177 		*p = '/';
    178 		syswarn(1, 0, "Attempt to write file `%s' that resolves into "
    179 		    "`%s/%s' outside current working directory `%s' ignored",
    180 		    arcn->name, buf, p + 1, cwdpath);
    181 		return -1;
    182 	}
    183 	*p = '/';
    184 	return 0;
    185 }
    186 
    187 /*
    188  * list()
    189  *	list the contents of an archive which match user supplied pattern(s)
    190  *	(if no pattern is supplied, list entire contents).
    191  */
    192 
    193 int
    194 list(void)
    195 {
    196 	ARCHD *arcn;
    197 	int res;
    198 	time_t now;
    199 
    200 	arcn = &archd;
    201 	/*
    202 	 * figure out archive type; pass any format specific options to the
    203 	 * archive option processing routine; call the format init routine. We
    204 	 * also save current time for ls_list() so we do not make a system
    205 	 * call for each file we need to print. If verbose (vflag) start up
    206 	 * the name and group caches.
    207 	 */
    208 	if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
    209 	    ((*frmt->st_rd)() < 0))
    210 		return 1;
    211 
    212 	now = time((time_t *)NULL);
    213 
    214 	/*
    215 	 * step through the archive until the format says it is done
    216 	 */
    217 	while (next_head(arcn) == 0) {
    218 		if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) {
    219 			/*
    220 			 * we need to read, to get the real filename
    221 			 */
    222 			off_t cnt;
    223 			if (!(*frmt->rd_data)(arcn, -arcn->type, &cnt))
    224 				(void)rd_skip(cnt + arcn->pad);
    225 			continue;
    226 		}
    227 
    228 		/*
    229 		 * check for pattern, and user specified options match.
    230 		 * When all patterns are matched we are done.
    231 		 */
    232 		if ((res = pat_match(arcn)) < 0)
    233 			break;
    234 
    235 		if ((res == 0) && (sel_chk(arcn) == 0)) {
    236 			/*
    237 			 * pattern resulted in a selected file
    238 			 */
    239 			if (pat_sel(arcn) < 0)
    240 				break;
    241 
    242 			/*
    243 			 * modify the name as requested by the user if name
    244 			 * survives modification, do a listing of the file
    245 			 */
    246 			if ((res = mod_name(arcn)) < 0)
    247 				break;
    248 			if (res == 0) {
    249 				if (arcn->name[0] == '/' && !check_Aflag()) {
    250 					memmove(arcn->name, arcn->name + 1,
    251 					    strlen(arcn->name));
    252 				}
    253 				ls_list(arcn, now, stdout);
    254 			}
    255 			/*
    256 			 * if there's an error writing to stdout then we must
    257 			 * stop now -- we're probably writing to a pipe that
    258 			 * has been closed by the reader.
    259 			 */
    260 			if (ferror(stdout)) {
    261 				syswarn(1, errno, "Listing incomplete.");
    262 				break;
    263 			}
    264 		}
    265 		/*
    266 		 * skip to next archive format header using values calculated
    267 		 * by the format header read routine
    268 		 */
    269 		if (rd_skip(arcn->skip + arcn->pad) == 1)
    270 			break;
    271 	}
    272 
    273 	/*
    274 	 * all done, let format have a chance to cleanup, and make sure that
    275 	 * the patterns supplied by the user were all matched
    276 	 */
    277 	(void)(*frmt->end_rd)();
    278 	(void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
    279 	ar_close();
    280 	pat_chk();
    281 
    282 	return 0;
    283 }
    284 
    285 /*
    286  * extract()
    287  *	extract the member(s) of an archive as specified by user supplied
    288  *	pattern(s) (no patterns extracts all members)
    289  */
    290 
    291 int
    292 extract(void)
    293 {
    294 	ARCHD *arcn;
    295 	int res;
    296 	off_t cnt;
    297 	struct stat sb;
    298 	int fd;
    299 	time_t now;
    300 
    301 	arcn = &archd;
    302 	/*
    303 	 * figure out archive type; pass any format specific options to the
    304 	 * archive option processing routine; call the format init routine;
    305 	 * start up the directory modification time and access mode database
    306 	 */
    307 	if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
    308 	    ((*frmt->st_rd)() < 0) || (dir_start() < 0))
    309 		return 1;
    310 
    311 	now = time((time_t *)NULL);
    312 #if !HAVE_NBTOOL_CONFIG_H
    313 	if (do_chroot)
    314 		(void)fdochroot(cwdfd);
    315 #endif
    316 
    317 	/*
    318 	 * When we are doing interactive rename, we store the mapping of names
    319 	 * so we can fix up hard links files later in the archive.
    320 	 */
    321 	if (iflag && (name_start() < 0))
    322 		return 1;
    323 
    324 	/*
    325 	 * step through each entry on the archive until the format read routine
    326 	 * says it is done
    327 	 */
    328 	while (next_head(arcn) == 0) {
    329 		int write_to_hard_link = 0;
    330 
    331 		if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) {
    332 			/*
    333 			 * we need to read, to get the real filename
    334 			 */
    335 			if (!(*frmt->rd_data)(arcn, -arcn->type, &cnt))
    336 				(void)rd_skip(cnt + arcn->pad);
    337 			continue;
    338 		}
    339 
    340 		/*
    341 		 * check for pattern, and user specified options match. When
    342 		 * all the patterns are matched we are done
    343 		 */
    344 		if ((res = pat_match(arcn)) < 0)
    345 			break;
    346 
    347 		if ((res > 0) || (sel_chk(arcn) != 0)) {
    348 			/*
    349 			 * file is not selected. skip past any file
    350 			 * data and padding and go back for the next
    351 			 * archive member
    352 			 */
    353 			(void)rd_skip(arcn->skip + arcn->pad);
    354 			continue;
    355 		}
    356 
    357 		if (kflag && (lstat(arcn->name, &sb) == 0)) {
    358 			(void)rd_skip(arcn->skip + arcn->pad);
    359 			continue;
    360 		}
    361 
    362 		/*
    363 		 * with -u or -D only extract when the archive member is newer
    364 		 * than the file with the same name in the file system (no
    365 		 * test of being the same type is required).
    366 		 * NOTE: this test is done BEFORE name modifications as
    367 		 * specified by pax. this operation can be confusing to the
    368 		 * user who might expect the test to be done on an existing
    369 		 * file AFTER the name mod. In honesty the pax spec is probably
    370 		 * flawed in this respect.  ignore this for GNU long links.
    371 		 */
    372 		if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
    373 			if (uflag && Dflag) {
    374 				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
    375 				    (arcn->sb.st_ctime <= sb.st_ctime)) {
    376 					(void)rd_skip(arcn->skip + arcn->pad);
    377 					continue;
    378 				}
    379 			} else if (Dflag) {
    380 				if (arcn->sb.st_ctime <= sb.st_ctime) {
    381 					(void)rd_skip(arcn->skip + arcn->pad);
    382 					continue;
    383 				}
    384 			} else if (arcn->sb.st_mtime <= sb.st_mtime) {
    385 				(void)rd_skip(arcn->skip + arcn->pad);
    386 				continue;
    387 			}
    388 		}
    389 
    390 		/*
    391 		 * this archive member is now been selected. modify the name.
    392 		 */
    393 		if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
    394 			break;
    395 		if (res > 0) {
    396 			/*
    397 			 * a bad name mod, skip and purge name from link table
    398 			 */
    399 			purg_lnk(arcn);
    400 			(void)rd_skip(arcn->skip + arcn->pad);
    401 			continue;
    402 		}
    403 
    404 		if (arcn->name[0] == '/' && !check_Aflag()) {
    405 			memmove(arcn->name, arcn->name + 1, strlen(arcn->name));
    406 		}
    407 		/*
    408 		 * Non standard -Y and -Z flag. When the existing file is
    409 		 * same age or newer skip; ignore this for GNU long links.
    410 		 */
    411 		if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
    412 			if (Yflag && Zflag) {
    413 				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
    414 				    (arcn->sb.st_ctime <= sb.st_ctime)) {
    415 					(void)rd_skip(arcn->skip + arcn->pad);
    416 					continue;
    417 				}
    418 			} else if (Yflag) {
    419 				if (arcn->sb.st_ctime <= sb.st_ctime) {
    420 					(void)rd_skip(arcn->skip + arcn->pad);
    421 					continue;
    422 				}
    423 			} else if (arcn->sb.st_mtime <= sb.st_mtime) {
    424 				(void)rd_skip(arcn->skip + arcn->pad);
    425 				continue;
    426 			}
    427 		}
    428 
    429 		if (vflag) {
    430 			if (vflag > 1)
    431 				ls_list(arcn, now, listf);
    432 			else {
    433 				(void)safe_print(arcn->name, listf);
    434 				vfpart = 1;
    435 			}
    436 		}
    437 
    438 		/*
    439 		 * if required, chdir around.
    440 		 */
    441 		if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL) &&
    442 		    !to_stdout)
    443 			dochdir(arcn->pat->chdname);
    444 
    445 		if (secure && path_check(arcn, 0) != 0) {
    446 			(void)rd_skip(arcn->skip + arcn->pad);
    447 			continue;
    448 		}
    449 
    450 
    451 		/*
    452 		 * all ok, extract this member based on type
    453 		 */
    454 		if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
    455 			/*
    456 			 * process archive members that are not regular files.
    457 			 * throw out padding and any data that might follow the
    458 			 * header (as determined by the format).
    459 			 */
    460 			if ((arcn->type == PAX_HLK) ||
    461 			    (arcn->type == PAX_HRG))
    462 				res = lnk_creat(arcn, &write_to_hard_link);
    463 			else
    464 				res = node_creat(arcn);
    465 
    466 			if (!write_to_hard_link) {
    467 				(void)rd_skip(arcn->skip + arcn->pad);
    468 				if (res < 0)
    469 					purg_lnk(arcn);
    470 
    471 				if (vflag && vfpart) {
    472 					(void)putc('\n', listf);
    473 					vfpart = 0;
    474 				}
    475 				continue;
    476 			}
    477 		}
    478 		if (to_stdout)
    479 			fd = STDOUT_FILENO;
    480 		else {
    481 			/*
    482 			 * We have a file with data here. If we cannot create
    483 			 * it, skip over the data and purge the name from hard
    484 			 * link table.
    485 			 */
    486 			if ((fd = file_creat(arcn, write_to_hard_link)) < 0) {
    487 				(void)fflush(listf);
    488 				(void)rd_skip(arcn->skip + arcn->pad);
    489 				purg_lnk(arcn);
    490 				continue;
    491 			}
    492 		}
    493 		/*
    494 		 * extract the file from the archive and skip over padding and
    495 		 * any unprocessed data
    496 		 */
    497 		res = (*frmt->rd_data)(arcn, fd, &cnt);
    498 		if (!to_stdout)
    499 			file_close(arcn, fd);
    500 		if (vflag && vfpart) {
    501 			(void)putc('\n', listf);
    502 			vfpart = 0;
    503 		}
    504 		if (!res)
    505 			(void)rd_skip(cnt + arcn->pad);
    506 
    507 		/*
    508 		 * if required, chdir around.
    509 		 */
    510 		if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
    511 			fdochdir(cwdfd);
    512 	}
    513 
    514 	/*
    515 	 * all done, restore directory modes and times as required; make sure
    516 	 * all patterns supplied by the user were matched; block off signals
    517 	 * to avoid chance for multiple entry into the cleanup code.
    518 	 */
    519 	(void)(*frmt->end_rd)();
    520 	(void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
    521 	ar_close();
    522 	proc_dir();
    523 	pat_chk();
    524 
    525 	return 0;
    526 }
    527 
    528 /*
    529  * wr_archive()
    530  *	Write an archive. used in both creating a new archive and appends on
    531  *	previously written archive.
    532  */
    533 
    534 static int
    535 wr_archive(ARCHD *arcn, int is_app)
    536 {
    537 	int res;
    538 	int hlk;
    539 	int wr_one;
    540 	off_t cnt;
    541 	int (*wrf)(ARCHD *);
    542 	int fd = -1;
    543 	time_t now;
    544 
    545 	/*
    546 	 * if this format supports hard link storage, start up the database
    547 	 * that detects them.
    548 	 */
    549 	if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
    550 		return 1;
    551 
    552 	/*
    553 	 * start up the file traversal code and format specific write
    554 	 */
    555 	if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0))
    556 		return 1;
    557 	wrf = frmt->wr;
    558 
    559 	now = time((time_t *)NULL);
    560 
    561 	/*
    562 	 * When we are doing interactive rename, we store the mapping of names
    563 	 * so we can fix up hard links files later in the archive.
    564 	 */
    565 	if (iflag && (name_start() < 0))
    566 		return 1;
    567 
    568 	/*
    569 	 * if this is not append, and there are no files, we do no write a trailer
    570 	 */
    571 	wr_one = is_app;
    572 
    573 	/*
    574 	 * while there are files to archive, process them one at at time
    575 	 */
    576 	while (next_file(arcn) == 0) {
    577 		/*
    578 		 * check if this file meets user specified options match.
    579 		 */
    580 		if (sel_chk(arcn) != 0)
    581 			continue;
    582 		fd = -1;
    583 		if (uflag) {
    584 			/*
    585 			 * only archive if this file is newer than a file with
    586 			 * the same name that is already stored on the archive
    587 			 */
    588 			if ((res = chk_ftime(arcn)) < 0)
    589 				break;
    590 			if (res > 0)
    591 				continue;
    592 		}
    593 
    594 		/*
    595 		 * this file is considered selected now. see if this is a hard
    596 		 * link to a file already stored
    597 		 */
    598 		ftree_sel(arcn);
    599 		if (hlk && (chk_lnk(arcn) < 0))
    600 			break;
    601 
    602 		if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
    603 		    (arcn->type == PAX_CTG)) {
    604 			/*
    605 			 * we will have to read this file. by opening it now we
    606 			 * can avoid writing a header to the archive for a file
    607 			 * we were later unable to read (we also purge it from
    608 			 * the link table).
    609 			 */
    610 			if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
    611 				syswarn(1, errno, "Unable to open %s to read",
    612 					arcn->org_name);
    613 				purg_lnk(arcn);
    614 				continue;
    615 			}
    616 		}
    617 
    618 		/*
    619 		 * Now modify the name as requested by the user
    620 		 */
    621 		if ((res = mod_name(arcn)) < 0) {
    622 			/*
    623 			 * name modification says to skip this file, close the
    624 			 * file and purge link table entry
    625 			 */
    626 			rdfile_close(arcn, &fd);
    627 			purg_lnk(arcn);
    628 			break;
    629 		}
    630 
    631 		if (arcn->name[0] == '/' && !check_Aflag()) {
    632 			memmove(arcn->name, arcn->name + 1, strlen(arcn->name));
    633 		}
    634 
    635 		if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
    636 			/*
    637 			 * unable to obtain the crc we need, close the file,
    638 			 * purge link table entry
    639 			 */
    640 			rdfile_close(arcn, &fd);
    641 			purg_lnk(arcn);
    642 			continue;
    643 		}
    644 
    645 		if (vflag) {
    646 			if (vflag > 1)
    647 				ls_list(arcn, now, listf);
    648 			else {
    649 				(void)safe_print(arcn->name, listf);
    650 				vfpart = 1;
    651 			}
    652 		}
    653 		++flcnt;
    654 
    655 		/*
    656 		 * looks safe to store the file, have the format specific
    657 		 * routine write routine store the file header on the archive
    658 		 */
    659 		if ((res = (*wrf)(arcn)) < 0) {
    660 			rdfile_close(arcn, &fd);
    661 			break;
    662 		}
    663 		wr_one = 1;
    664 		if (res > 0) {
    665 			/*
    666 			 * format write says no file data needs to be stored
    667 			 * so we are done messing with this file
    668 			 */
    669 			if (vflag && vfpart) {
    670 				(void)putc('\n', listf);
    671 				vfpart = 0;
    672 			}
    673 			rdfile_close(arcn, &fd);
    674 			continue;
    675 		}
    676 
    677 		/*
    678 		 * Add file data to the archive, quit on write error. if we
    679 		 * cannot write the entire file contents to the archive we
    680 		 * must pad the archive to replace the missing file data
    681 		 * (otherwise during an extract the file header for the file
    682 		 * which FOLLOWS this one will not be where we expect it to
    683 		 * be).
    684 		 */
    685 		res = (*frmt->wr_data)(arcn, fd, &cnt);
    686 		rdfile_close(arcn, &fd);
    687 		if (vflag && vfpart) {
    688 			(void)putc('\n', listf);
    689 			vfpart = 0;
    690 		}
    691 		if (res < 0)
    692 			break;
    693 
    694 		/*
    695 		 * pad as required, cnt is number of bytes not written
    696 		 */
    697 		if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
    698 		    ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
    699 			break;
    700 	}
    701 
    702 	/*
    703 	 * tell format to write trailer; pad to block boundary; reset directory
    704 	 * mode/access times, and check if all patterns supplied by the user
    705 	 * were matched. block off signals to avoid chance for multiple entry
    706 	 * into the cleanup code
    707 	 */
    708 	if (wr_one) {
    709 		(*frmt->end_wr)();
    710 		wr_fin();
    711 	}
    712 	(void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
    713 	ar_close();
    714 	if (tflag)
    715 		proc_dir();
    716 	ftree_chk();
    717 
    718 	return 0;
    719 }
    720 
    721 /*
    722  * append()
    723  *	Add file to previously written archive. Archive format specified by the
    724  *	user must agree with archive. The archive is read first to collect
    725  *	modification times (if -u) and locate the archive trailer. The archive
    726  *	is positioned in front of the record with the trailer and wr_archive()
    727  *	is called to add the new members.
    728  *	PAX IMPLEMENTATION DETAIL NOTE:
    729  *	-u is implemented by adding the new members to the end of the archive.
    730  *	Care is taken so that these do not end up as links to the older
    731  *	version of the same file already stored in the archive. It is expected
    732  *	when extraction occurs these newer versions will over-write the older
    733  *	ones stored "earlier" in the archive (this may be a bad assumption as
    734  *	it depends on the implementation of the program doing the extraction).
    735  *	It is really difficult to splice in members without either re-writing
    736  *	the entire archive (from the point were the old version was), or having
    737  *	assistance of the format specification in terms of a special update
    738  *	header that invalidates a previous archive record. The posix spec left
    739  *	the method used to implement -u unspecified. This pax is able to
    740  *	over write existing files that it creates.
    741  */
    742 
    743 int
    744 append(void)
    745 {
    746 	ARCHD *arcn;
    747 	int res;
    748 	FSUB *orgfrmt;
    749 	int udev;
    750 	off_t tlen;
    751 
    752 	arcn = &archd;
    753 	orgfrmt = frmt;
    754 
    755 	/*
    756 	 * Do not allow an append operation if the actual archive is of a
    757 	 * different format than the user specified format.
    758 	 */
    759 	if (get_arc() < 0)
    760 		return 1;
    761 	if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
    762 		tty_warn(1, "Cannot mix current archive format %s with %s",
    763 		    frmt->name, orgfrmt->name);
    764 		return 1;
    765 	}
    766 
    767 	/*
    768 	 * pass the format any options and start up format
    769 	 */
    770 	if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
    771 		return 1;
    772 
    773 	/*
    774 	 * if we only are adding members that are newer, we need to save the
    775 	 * mod times for all files we see.
    776 	 */
    777 	if (uflag && (ftime_start() < 0))
    778 		return 1;
    779 
    780 	/*
    781 	 * some archive formats encode hard links by recording the device and
    782 	 * file serial number (inode) but copy the file anyway (multiple times)
    783 	 * to the archive. When we append, we run the risk that newly added
    784 	 * files may have the same device and inode numbers as those recorded
    785 	 * on the archive but during a previous run. If this happens, when the
    786 	 * archive is extracted we get INCORRECT hard links. We avoid this by
    787 	 * remapping the device numbers so that newly added files will never
    788 	 * use the same device number as one found on the archive. remapping
    789 	 * allows new members to safely have links among themselves. remapping
    790 	 * also avoids problems with file inode (serial number) truncations
    791 	 * when the inode number is larger than storage space in the archive
    792 	 * header. See the remap routines for more details.
    793 	 */
    794 	if ((udev = frmt->udev) && (dev_start() < 0))
    795 		return 1;
    796 
    797 	/*
    798 	 * reading the archive may take a long time. If verbose tell the user
    799 	 */
    800 	if (vflag) {
    801 		(void)fprintf(listf,
    802 			"%s: Reading archive to position at the end...", argv0);
    803 		vfpart = 1;
    804 	}
    805 
    806 	/*
    807 	 * step through the archive until the format says it is done
    808 	 */
    809 	while (next_head(arcn) == 0) {
    810 		/*
    811 		 * check if this file meets user specified options.
    812 		 */
    813 		if (sel_chk(arcn) != 0) {
    814 			if (rd_skip(arcn->skip + arcn->pad) == 1)
    815 				break;
    816 			continue;
    817 		}
    818 
    819 		if (uflag) {
    820 			/*
    821 			 * see if this is the newest version of this file has
    822 			 * already been seen, if so skip.
    823 			 */
    824 			if ((res = chk_ftime(arcn)) < 0)
    825 				break;
    826 			if (res > 0) {
    827 				if (rd_skip(arcn->skip + arcn->pad) == 1)
    828 					break;
    829 				continue;
    830 			}
    831 		}
    832 
    833 		/*
    834 		 * Store this device number. Device numbers seen during the
    835 		 * read phase of append will cause newly appended files with a
    836 		 * device number seen in the old part of the archive to be
    837 		 * remapped to an unused device number.
    838 		 */
    839 		if ((udev && (add_dev(arcn) < 0)) ||
    840 		    (rd_skip(arcn->skip + arcn->pad) == 1))
    841 			break;
    842 	}
    843 
    844 	/*
    845 	 * done, finish up read and get the number of bytes to back up so we
    846 	 * can add new members. The format might have used the hard link table,
    847 	 * purge it.
    848 	 */
    849 	tlen = (*frmt->end_rd)();
    850 	lnk_end();
    851 
    852 	/*
    853 	 * try to position for write, if this fails quit. if any error occurs,
    854 	 * we will refuse to write
    855 	 */
    856 	if (appnd_start(tlen) < 0)
    857 		return 1;
    858 
    859 	/*
    860 	 * tell the user we are done reading.
    861 	 */
    862 	if (vflag && vfpart) {
    863 		(void)safe_print("done.\n", listf);
    864 		vfpart = 0;
    865 	}
    866 
    867 	/*
    868 	 * go to the writing phase to add the new members
    869 	 */
    870 	return wr_archive(arcn, 1);
    871 }
    872 
    873 /*
    874  * archive()
    875  *	write a new archive
    876  */
    877 
    878 int
    879 archive(void)
    880 {
    881 
    882 	/*
    883 	 * if we only are adding members that are newer, we need to save the
    884 	 * mod times for all files; set up for writing; pass the format any
    885 	 * options write the archive
    886 	 */
    887 	if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
    888 		return 1;
    889 	if ((*frmt->options)() < 0)
    890 		return 1;
    891 
    892 	return wr_archive(&archd, 0);
    893 }
    894 
    895 /*
    896  * copy()
    897  *	copy files from one part of the file system to another. this does not
    898  *	use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
    899  *	archive was written and then extracted in the destination directory
    900  *	(except the files are forced to be under the destination directory).
    901  */
    902 
    903 int
    904 copy(void)
    905 {
    906 	ARCHD *arcn;
    907 	int res;
    908 	int fddest;
    909 	char *dest_pt;
    910 	int dlen;
    911 	int drem;
    912 	int fdsrc = -1;
    913 	struct stat sb;
    914 	char dirbuf[PAXPATHLEN+1];
    915 
    916 	arcn = &archd;
    917 	/*
    918 	 * set up the destination dir path and make sure it is a directory. We
    919 	 * make sure we have a trailing / on the destination
    920 	 */
    921 	dlen = strlcpy(dirbuf, dirptr, sizeof(dirbuf));
    922 	if (dlen >= sizeof(dirbuf) ||
    923 	    (dlen == sizeof(dirbuf) - 1 && dirbuf[dlen - 1] != '/')) {
    924 		tty_warn(1, "directory name is too long %s", dirptr);
    925 		return 1;
    926 	}
    927 	dest_pt = dirbuf + dlen;
    928 	if (*(dest_pt-1) != '/') {
    929 		*dest_pt++ = '/';
    930 		++dlen;
    931 	}
    932 	*dest_pt = '\0';
    933 	drem = PAXPATHLEN - dlen;
    934 
    935 	if (stat(dirptr, &sb) < 0) {
    936 		syswarn(1, errno, "Cannot access destination directory %s",
    937 			dirptr);
    938 		return 1;
    939 	}
    940 	if (!S_ISDIR(sb.st_mode)) {
    941 		tty_warn(1, "Destination is not a directory %s", dirptr);
    942 		return 1;
    943 	}
    944 
    945 	/*
    946 	 * start up the hard link table; file traversal routines and the
    947 	 * modification time and access mode database
    948 	 */
    949 	if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
    950 		return 1;
    951 
    952 	/*
    953 	 * When we are doing interactive rename, we store the mapping of names
    954 	 * so we can fix up hard links files later in the archive.
    955 	 */
    956 	if (iflag && (name_start() < 0))
    957 		return 1;
    958 
    959 	/*
    960 	 * set up to cp file trees
    961 	 */
    962 	cp_start();
    963 
    964 	/*
    965 	 * while there are files to archive, process them
    966 	 */
    967 	while (next_file(arcn) == 0) {
    968 		fdsrc = -1;
    969 
    970 		/*
    971 		 * check if this file meets user specified options
    972 		 */
    973 		if (sel_chk(arcn) != 0)
    974 			continue;
    975 
    976 		/*
    977 		 * if there is already a file in the destination directory with
    978 		 * the same name and it is newer, skip the one stored on the
    979 		 * archive.
    980 		 * NOTE: this test is done BEFORE name modifications as
    981 		 * specified by pax. this can be confusing to the user who
    982 		 * might expect the test to be done on an existing file AFTER
    983 		 * the name mod. In honesty the pax spec is probably flawed in
    984 		 * this respect
    985 		 */
    986 		if (uflag || Dflag) {
    987 			/*
    988 			 * create the destination name
    989 			 */
    990 			if (strlcpy(dest_pt, arcn->name + (*arcn->name == '/'),
    991 			    drem + 1) > drem) {
    992 				tty_warn(1, "Destination pathname too long %s",
    993 					arcn->name);
    994 				continue;
    995 			}
    996 
    997 			/*
    998 			 * if existing file is same age or newer skip
    999 			 */
   1000 			res = lstat(dirbuf, &sb);
   1001 			*dest_pt = '\0';
   1002 
   1003 			if (res == 0) {
   1004 				if (uflag && Dflag) {
   1005 					if ((arcn->sb.st_mtime<=sb.st_mtime) &&
   1006 					    (arcn->sb.st_ctime<=sb.st_ctime))
   1007 						continue;
   1008 				} else if (Dflag) {
   1009 					if (arcn->sb.st_ctime <= sb.st_ctime)
   1010 						continue;
   1011 				} else if (arcn->sb.st_mtime <= sb.st_mtime)
   1012 					continue;
   1013 			}
   1014 		}
   1015 
   1016 		/*
   1017 		 * this file is considered selected. See if this is a hard link
   1018 		 * to a previous file; modify the name as requested by the
   1019 		 * user; set the final destination.
   1020 		 */
   1021 		ftree_sel(arcn);
   1022 		if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
   1023 			break;
   1024 		if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
   1025 			/*
   1026 			 * skip file, purge from link table
   1027 			 */
   1028 			purg_lnk(arcn);
   1029 			continue;
   1030 		}
   1031 
   1032 		/*
   1033 		 * Non standard -Y and -Z flag. When the exisiting file is
   1034 		 * same age or newer skip
   1035 		 */
   1036 		if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
   1037 			if (Yflag && Zflag) {
   1038 				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
   1039 				    (arcn->sb.st_ctime <= sb.st_ctime))
   1040 					continue;
   1041 			} else if (Yflag) {
   1042 				if (arcn->sb.st_ctime <= sb.st_ctime)
   1043 					continue;
   1044 			} else if (arcn->sb.st_mtime <= sb.st_mtime)
   1045 				continue;
   1046 		}
   1047 
   1048 		if (vflag) {
   1049 			(void)safe_print(arcn->name, listf);
   1050 			vfpart = 1;
   1051 		}
   1052 		++flcnt;
   1053 
   1054 		/*
   1055 		 * try to create a hard link to the src file if requested
   1056 		 * but make sure we are not trying to overwrite ourselves.
   1057 		 */
   1058 		if (lflag)
   1059 			res = cross_lnk(arcn);
   1060 		else
   1061 			res = chk_same(arcn);
   1062 		if (res <= 0) {
   1063 			if (vflag && vfpart) {
   1064 				(void)putc('\n', listf);
   1065 				vfpart = 0;
   1066 			}
   1067 			continue;
   1068 		}
   1069 
   1070 		/*
   1071 		 * have to create a new file
   1072 		 */
   1073 		if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
   1074 			/*
   1075 			 * create a link or special file
   1076 			 */
   1077 			if ((arcn->type == PAX_HLK) ||
   1078 			    (arcn->type == PAX_HRG)) {
   1079 				int payload;
   1080 
   1081 				res = lnk_creat(arcn, &payload);
   1082 			} else {
   1083 				res = node_creat(arcn);
   1084 			}
   1085 			if (res < 0)
   1086 				purg_lnk(arcn);
   1087 			if (vflag && vfpart) {
   1088 				(void)putc('\n', listf);
   1089 				vfpart = 0;
   1090 			}
   1091 			continue;
   1092 		}
   1093 
   1094 		/*
   1095 		 * have to copy a regular file to the destination directory.
   1096 		 * first open source file and then create the destination file
   1097 		 */
   1098 		if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
   1099 			syswarn(1, errno, "Unable to open %s to read",
   1100 			    arcn->org_name);
   1101 			purg_lnk(arcn);
   1102 			continue;
   1103 		}
   1104 		if ((fddest = file_creat(arcn, 0)) < 0) {
   1105 			rdfile_close(arcn, &fdsrc);
   1106 			purg_lnk(arcn);
   1107 			continue;
   1108 		}
   1109 
   1110 		/*
   1111 		 * copy source file data to the destination file
   1112 		 */
   1113 		cp_file(arcn, fdsrc, fddest);
   1114 		file_close(arcn, fddest);
   1115 		rdfile_close(arcn, &fdsrc);
   1116 
   1117 		if (vflag && vfpart) {
   1118 			(void)putc('\n', listf);
   1119 			vfpart = 0;
   1120 		}
   1121 	}
   1122 
   1123 	/*
   1124 	 * restore directory modes and times as required; make sure all
   1125 	 * patterns were selected block off signals to avoid chance for
   1126 	 * multiple entry into the cleanup code.
   1127 	 */
   1128 	(void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
   1129 	ar_close();
   1130 	proc_dir();
   1131 	ftree_chk();
   1132 
   1133 	return 0;
   1134 }
   1135 
   1136 /*
   1137  * next_head()
   1138  *	try to find a valid header in the archive. Uses format specific
   1139  *	routines to extract the header and id the trailer. Trailers may be
   1140  *	located within a valid header or in an invalid header (the location
   1141  *	is format specific. The inhead field from the option table tells us
   1142  *	where to look for the trailer).
   1143  *	We keep reading (and resyncing) until we get enough contiguous data
   1144  *	to check for a header. If we cannot find one, we shift by a byte
   1145  *	add a new byte from the archive to the end of the buffer and try again.
   1146  *	If we get a read error, we throw out what we have (as we must have
   1147  *	contiguous data) and start over again.
   1148  *	ASSUMED: headers fit within a BLKMULT header.
   1149  * Return:
   1150  *	0 if we got a header, -1 if we are unable to ever find another one
   1151  *	(we reached the end of input, or we reached the limit on retries. see
   1152  *	the specs for rd_wrbuf() for more details)
   1153  */
   1154 
   1155 static int
   1156 next_head(ARCHD *arcn)
   1157 {
   1158 	int ret;
   1159 	char *hdend;
   1160 	int res;
   1161 	int shftsz;
   1162 	int hsz;
   1163 	int in_resync = 0;		/* set when we are in resync mode */
   1164 	int cnt = 0;			/* counter for trailer function */
   1165 	int first = 1;			/* on 1st read, EOF isn't premature. */
   1166 
   1167 	/*
   1168 	 * set up initial conditions, we want a whole frmt->hsz block as we
   1169 	 * have no data yet.
   1170 	 */
   1171 	res = hsz = frmt->hsz;
   1172 	hdend = hdbuf;
   1173 	shftsz = hsz - 1;
   1174 	for(;;) {
   1175 		/*
   1176 		 * keep looping until we get a contiguous FULL buffer
   1177 		 * (frmt->hsz is the proper size)
   1178 		 */
   1179 		for (;;) {
   1180 			if ((ret = rd_wrbuf(hdend, res)) == res)
   1181 				break;
   1182 
   1183 			/*
   1184 			 * If we read 0 bytes (EOF) from an archive when we
   1185 			 * expect to find a header, we have stepped upon
   1186 			 * an archive without the customary block of zeroes
   1187 			 * end marker.  It's just stupid to error out on
   1188 			 * them, so exit gracefully.
   1189 			 */
   1190 			if (first && ret == 0)
   1191 				return -1;
   1192 			first = 0;
   1193 
   1194 			/*
   1195 			 * some kind of archive read problem, try to resync the
   1196 			 * storage device, better give the user the bad news.
   1197 			 */
   1198 			if ((ret == 0) || (rd_sync() < 0)) {
   1199 				tty_warn(1,
   1200 				    "Premature end of file on archive read");
   1201 				return -1;
   1202 			}
   1203 			if (!in_resync) {
   1204 				if (act == APPND) {
   1205 					tty_warn(1,
   1206 					  "Archive I/O error, cannot continue");
   1207 					return -1;
   1208 				}
   1209 				tty_warn(1,
   1210 				    "Archive I/O error. Trying to recover.");
   1211 				++in_resync;
   1212 			}
   1213 
   1214 			/*
   1215 			 * oh well, throw it all out and start over
   1216 			 */
   1217 			res = hsz;
   1218 			hdend = hdbuf;
   1219 		}
   1220 
   1221 		/*
   1222 		 * ok we have a contiguous buffer of the right size. Call the
   1223 		 * format read routine. If this was not a valid header and this
   1224 		 * format stores trailers outside of the header, call the
   1225 		 * format specific trailer routine to check for a trailer. We
   1226 		 * have to watch out that we do not mis-identify file data or
   1227 		 * block padding as a header or trailer. Format specific
   1228 		 * trailer functions must NOT check for the trailer while we
   1229 		 * are running in resync mode. Some trailer functions may tell
   1230 		 * us that this block cannot contain a valid header either, so
   1231 		 * we then throw out the entire block and start over.
   1232 		 */
   1233 		if ((*frmt->rd)(arcn, hdbuf) == 0)
   1234 			break;
   1235 
   1236 		if (!frmt->inhead) {
   1237 			/*
   1238 			 * this format has trailers outside of valid headers
   1239 			 */
   1240 			if ((ret = (*frmt->trail)(hdbuf,in_resync,&cnt)) == 0){
   1241 				/*
   1242 				 * valid trailer found, drain input as required
   1243 				 */
   1244 				ar_drain();
   1245 				return -1;
   1246 			}
   1247 
   1248 			if (ret == 1) {
   1249 				/*
   1250 				 * we are in resync and we were told to throw
   1251 				 * the whole block out because none of the
   1252 				 * bytes in this block can be used to form a
   1253 				 * valid header
   1254 				 */
   1255 				res = hsz;
   1256 				hdend = hdbuf;
   1257 				continue;
   1258 			}
   1259 		}
   1260 
   1261 		/*
   1262 		 * Brute force section.
   1263 		 * not a valid header. We may be able to find a header yet. So
   1264 		 * we shift over by one byte, and set up to read one byte at a
   1265 		 * time from the archive and place it at the end of the buffer.
   1266 		 * We will keep moving byte at a time until we find a header or
   1267 		 * get a read error and have to start over.
   1268 		 */
   1269 		if (!in_resync) {
   1270 			if (act == APPND) {
   1271 				tty_warn(1,
   1272 				    "Unable to append, archive header flaw");
   1273 				return -1;
   1274 			}
   1275 			tty_warn(1,
   1276 			    "Invalid header, starting valid header search.");
   1277 			++in_resync;
   1278 		}
   1279 		memmove(hdbuf, hdbuf+1, shftsz);
   1280 		res = 1;
   1281 		hdend = hdbuf + shftsz;
   1282 	}
   1283 
   1284 	/*
   1285 	 * ok got a valid header, check for trailer if format encodes it in the
   1286 	 * the header. NOTE: the parameters are different than trailer routines
   1287 	 * which encode trailers outside of the header!
   1288 	 */
   1289 	if (frmt->inhead && ((*frmt->subtrail)(arcn) == 0)) {
   1290 		/*
   1291 		 * valid trailer found, drain input as required
   1292 		 */
   1293 		ar_drain();
   1294 		return -1;
   1295 	}
   1296 
   1297 	++flcnt;
   1298 	return 0;
   1299 }
   1300 
   1301 /*
   1302  * get_arc()
   1303  *	Figure out what format an archive is. Handles archive with flaws by
   1304  *	brute force searches for a legal header in any supported format. The
   1305  *	format id routines have to be careful to NOT mis-identify a format.
   1306  *	ASSUMED: headers fit within a BLKMULT header.
   1307  * Return:
   1308  *	0 if archive found -1 otherwise
   1309  */
   1310 
   1311 static int
   1312 get_arc(void)
   1313 {
   1314 	int i;
   1315 	int hdsz = 0;
   1316 	int res;
   1317 	int minhd = BLKMULT;
   1318 	char *hdend;
   1319 	int notice = 0;
   1320 
   1321 	/*
   1322 	 * find the smallest header size in all archive formats and then set up
   1323 	 * to read the archive.
   1324 	 */
   1325 	for (i = 0; ford[i] >= 0; ++i) {
   1326 		if (fsub[ford[i]].hsz < minhd)
   1327 			minhd = fsub[ford[i]].hsz;
   1328 	}
   1329 	if (rd_start() < 0)
   1330 		return -1;
   1331 	res = BLKMULT;
   1332 	hdsz = 0;
   1333 	hdend = hdbuf;
   1334 	for(;;) {
   1335 		for (;;) {
   1336 			/*
   1337 			 * fill the buffer with at least the smallest header
   1338 			 */
   1339 			i = rd_wrbuf(hdend, res);
   1340 			if (i > 0)
   1341 				hdsz += i;
   1342 			if (hdsz >= minhd)
   1343 				break;
   1344 
   1345 			/*
   1346 			 * if we cannot recover from a read error quit
   1347 			 */
   1348 			if ((i == 0) || (rd_sync() < 0))
   1349 				goto out;
   1350 
   1351 			/*
   1352 			 * when we get an error none of the data we already
   1353 			 * have can be used to create a legal header (we just
   1354 			 * got an error in the middle), so we throw it all out
   1355 			 * and refill the buffer with fresh data.
   1356 			 */
   1357 			res = BLKMULT;
   1358 			hdsz = 0;
   1359 			hdend = hdbuf;
   1360 			if (!notice) {
   1361 				if (act == APPND)
   1362 					return -1;
   1363 				tty_warn(1,
   1364 				    "Cannot identify format. Searching...");
   1365 				++notice;
   1366 			}
   1367 		}
   1368 
   1369 		/*
   1370 		 * we have at least the size of the smallest header in any
   1371 		 * archive format. Look to see if we have a match. The array
   1372 		 * ford[] is used to specify the header id order to reduce the
   1373 		 * chance of incorrectly id'ing a valid header (some formats
   1374 		 * may be subsets of each other and the order would then be
   1375 		 * important).
   1376 		 */
   1377 		for (i = 0; ford[i] >= 0; ++i) {
   1378 			if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
   1379 				continue;
   1380 			frmt = &(fsub[ford[i]]);
   1381 			/*
   1382 			 * yuck, to avoid slow special case code in the extract
   1383 			 * routines, just push this header back as if it was
   1384 			 * not seen. We have left extra space at start of the
   1385 			 * buffer for this purpose. This is a bit ugly, but
   1386 			 * adding all the special case code is far worse.
   1387 			 */
   1388 			pback(hdbuf, hdsz);
   1389 			return 0;
   1390 		}
   1391 
   1392 		/*
   1393 		 * We have a flawed archive, no match. we start searching, but
   1394 		 * we never allow additions to flawed archives
   1395 		 */
   1396 		if (!notice) {
   1397 			if (act == APPND)
   1398 				return -1;
   1399 			tty_warn(1, "Cannot identify format. Searching...");
   1400 			++notice;
   1401 		}
   1402 
   1403 		/*
   1404 		 * brute force search for a header that we can id.
   1405 		 * we shift through byte at a time. this is slow, but we cannot
   1406 		 * determine the nature of the flaw in the archive in a
   1407 		 * portable manner
   1408 		 */
   1409 		if (--hdsz > 0) {
   1410 			memmove(hdbuf, hdbuf+1, hdsz);
   1411 			res = BLKMULT - hdsz;
   1412 			hdend = hdbuf + hdsz;
   1413 		} else {
   1414 			res = BLKMULT;
   1415 			hdend = hdbuf;
   1416 			hdsz = 0;
   1417 		}
   1418 	}
   1419 
   1420     out:
   1421 	/*
   1422 	 * we cannot find a header, bow, apologize and quit
   1423 	 */
   1424 	tty_warn(1, "Sorry, unable to determine archive format.");
   1425 	return -1;
   1426 }
   1427