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