Home | History | Annotate | Line # | Download | only in pax
pat_rep.c revision 1.1
      1 /*-
      2  * Copyright (c) 1992 Keith Muller.
      3  * Copyright (c) 1992, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * Keith Muller of the University of California, San Diego.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 #ifndef lint
     39 static char sccsid[] = "@(#)pat_rep.c	8.2 (Berkeley) 4/18/94";
     40 #endif /* not lint */
     41 
     42 #include <sys/types.h>
     43 #include <sys/time.h>
     44 #include <sys/stat.h>
     45 #include <sys/param.h>
     46 #include <stdio.h>
     47 #include <ctype.h>
     48 #include <string.h>
     49 #include <unistd.h>
     50 #include <stdlib.h>
     51 #ifdef NET2_REGEX
     52 #include <regexp.h>
     53 #else
     54 #include <regex.h>
     55 #endif
     56 #include "pax.h"
     57 #include "pat_rep.h"
     58 #include "extern.h"
     59 
     60 /*
     61  * routines to handle pattern matching, name modification (regular expression
     62  * substitution and interactive renames), and destination name modification for
     63  * copy (-rw). Both file name and link names are adjusted as required in these
     64  * routines.
     65  */
     66 
     67 #define MAXSUBEXP	10		/* max subexpressions, DO NOT CHANGE */
     68 static PATTERN *pathead = NULL;		/* file pattern match list head */
     69 static PATTERN *pattail = NULL;		/* file pattern match list tail */
     70 static REPLACE *rephead = NULL;		/* replacement string list head */
     71 static REPLACE *reptail = NULL;		/* replacement string list tail */
     72 
     73 static int rep_name __P((char *, int *, int));
     74 static int tty_rename __P((register ARCHD *));
     75 static int fix_path __P((char *, int *, char *, int));
     76 static int fn_match __P((register char *, register char *, char **));
     77 static char * range_match __P((register char *, register int));
     78 #ifdef NET2_REGEX
     79 static int resub __P((regexp *, char *, char *, register char *));
     80 #else
     81 static int resub __P((regex_t *, regmatch_t *, char *, char *, char *));
     82 #endif
     83 
     84 /*
     85  * rep_add()
     86  *	parses the -s replacement string; compiles the regular expression
     87  *	and stores the compiled value and it's replacement string together in
     88  *	replacement string list. Input to this function is of the form:
     89  *		/old/new/pg
     90  *	The first char in the string specifies the delimiter used by this
     91  *	replacement string. "Old" is a regular expression in "ed" format which
     92  *	is compiled by regcomp() and is applied to filenames. "new" is the
     93  *	substitution string; p and g are options flags for printing and global
     94  *	replacement (over the single filename)
     95  * Return:
     96  *	0 if a proper replacement string and regular expression was added to
     97  *	the list of replacement patterns; -1 otherwise.
     98  */
     99 
    100 #if __STDC__
    101 int
    102 rep_add(register char *str)
    103 #else
    104 int
    105 rep_add(str)
    106 	register char *str;
    107 #endif
    108 {
    109 	register char *pt1;
    110 	register char *pt2;
    111 	register REPLACE *rep;
    112 #	ifndef NET2_REGEX
    113 	register int res;
    114 	char rebuf[BUFSIZ];
    115 #	endif
    116 
    117 	/*
    118 	 * throw out the bad parameters
    119 	 */
    120 	if ((str == NULL) || (*str == '\0')) {
    121 		warn(1, "Empty replacement string");
    122 		return(-1);
    123 	}
    124 
    125 	/*
    126 	 * first character in the string specifies what the delimiter is for
    127 	 * this expression
    128 	 */
    129 	if ((pt1 = strchr(str+1, *str)) == NULL) {
    130 		warn(1, "Invalid replacement string %s", str);
    131 		return(-1);
    132 	}
    133 
    134 	/*
    135 	 * allocate space for the node that handles this replacement pattern
    136 	 * and split out the regular expression and try to compile it
    137 	 */
    138 	if ((rep = (REPLACE *)malloc(sizeof(REPLACE))) == NULL) {
    139 		warn(1, "Unable to allocate memory for replacement string");
    140 		return(-1);
    141 	}
    142 
    143 	*pt1 = '\0';
    144 #	ifdef NET2_REGEX
    145 	if ((rep->rcmp = regcomp(str+1)) == NULL) {
    146 #	else
    147 	if ((res = regcomp(&(rep->rcmp), str+1, 0)) != 0) {
    148 		regerror(res, &(rep->rcmp), rebuf, sizeof(rebuf));
    149 		warn(1, "%s while compiling regular expression %s", rebuf, str);
    150 #	endif
    151 		(void)free((char *)rep);
    152 		return(-1);
    153 	}
    154 
    155 	/*
    156 	 * put the delimiter back in case we need an error message and
    157 	 * locate the delimiter at the end of the replacement string
    158 	 * we then point the node at the new substitution string
    159 	 */
    160 	*pt1++ = *str;
    161 	if ((pt2 = strchr(pt1, *str)) == NULL) {
    162 #		ifdef NET2_REGEX
    163 		(void)free((char *)rep->rcmp);
    164 #		else
    165 		regfree(&(rep->rcmp));
    166 #		endif
    167 		(void)free((char *)rep);
    168 		warn(1, "Invalid replacement string %s", str);
    169 		return(-1);
    170 	}
    171 
    172 	*pt2 = '\0';
    173 	rep->nstr = pt1;
    174 	pt1 = pt2++;
    175 	rep->flgs = 0;
    176 
    177 	/*
    178 	 * set the options if any
    179 	 */
    180 	while (*pt2 != '\0') {
    181 		switch(*pt2) {
    182 		case 'g':
    183 		case 'G':
    184 			rep->flgs  |= GLOB;
    185 			break;
    186 		case 'p':
    187 		case 'P':
    188 			rep->flgs  |= PRNT;
    189 			break;
    190 		default:
    191 #			ifdef NET2_REGEX
    192 			(void)free((char *)rep->rcmp);
    193 #			else
    194 			regfree(&(rep->rcmp));
    195 #			endif
    196 			(void)free((char *)rep);
    197 			*pt1 = *str;
    198 			warn(1, "Invalid replacement string option %s", str);
    199 			return(-1);
    200 		}
    201 		++pt2;
    202 	}
    203 
    204 	/*
    205 	 * all done, link it in at the end
    206 	 */
    207 	rep->fow = NULL;
    208 	if (rephead == NULL) {
    209 		reptail = rephead = rep;
    210 		return(0);
    211 	}
    212 	reptail->fow = rep;
    213 	reptail = rep;
    214 	return(0);
    215 }
    216 
    217 /*
    218  * pat_add()
    219  *	add a pattern match to the pattern match list. Pattern matches are used
    220  *	to select which archive members are extracted. (They appear as
    221  *	arguments to pax in the list and read modes). If no patterns are
    222  *	supplied to pax, all members in the archive will be selected (and the
    223  *	pattern match list is empty).
    224  * Return:
    225  *	0 if the pattern was added to the list, -1 otherwise
    226  */
    227 
    228 #if __STDC__
    229 int
    230 pat_add(char *str)
    231 #else
    232 int
    233 pat_add(str)
    234 	char *str;
    235 #endif
    236 {
    237 	register PATTERN *pt;
    238 
    239 	/*
    240 	 * throw out the junk
    241 	 */
    242 	if ((str == NULL) || (*str == '\0')) {
    243 		warn(1, "Empty pattern string");
    244 		return(-1);
    245 	}
    246 
    247 	/*
    248 	 * allocate space for the pattern and store the pattern. the pattern is
    249 	 * part of argv so do not bother to copy it, just point at it. Add the
    250 	 * node to the end of the pattern list
    251 	 */
    252 	if ((pt = (PATTERN *)malloc(sizeof(PATTERN))) == NULL) {
    253 		warn(1, "Unable to allocate memory for pattern string");
    254 		return(-1);
    255 	}
    256 
    257 	pt->pstr = str;
    258 	pt->pend = NULL;
    259 	pt->plen = strlen(str);
    260 	pt->fow = NULL;
    261 	pt->flgs = 0;
    262 	if (pathead == NULL) {
    263 		pattail = pathead = pt;
    264 		return(0);
    265 	}
    266 	pattail->fow = pt;
    267 	pattail = pt;
    268 	return(0);
    269 }
    270 
    271 /*
    272  * pat_chk()
    273  *	complain if any the user supplied pattern did not result in a match to
    274  *	a selected archive member.
    275  */
    276 
    277 #if __STDC__
    278 void
    279 pat_chk(void)
    280 #else
    281 void
    282 pat_chk()
    283 #endif
    284 {
    285 	register PATTERN *pt;
    286 	register int wban = 0;
    287 
    288 	/*
    289 	 * walk down the list checking the flags to make sure MTCH was set,
    290 	 * if not complain
    291 	 */
    292 	for (pt = pathead; pt != NULL; pt = pt->fow) {
    293 		if (pt->flgs & MTCH)
    294 			continue;
    295 		if (!wban) {
    296 			warn(1, "WARNING! These patterns were not matched:");
    297 			++wban;
    298 		}
    299 		(void)fprintf(stderr, "%s\n", pt->pstr);
    300 	}
    301 }
    302 
    303 /*
    304  * pat_sel()
    305  *	the archive member which matches a pattern was selected. Mark the
    306  *	pattern as having selected an archive member. arcn->pat points at the
    307  *	pattern that was matched. arcn->pat is set in pat_match()
    308  *
    309  *	NOTE: When the -c option is used, we are called when there was no match
    310  *	by pat_match() (that means we did match before the inverted sense of
    311  *	the logic). Now this seems really strange at first, but with -c  we
    312  *	need to keep track of those patterns that cause a archive member to NOT
    313  *	be selected (it found an archive member with a specified pattern)
    314  * Return:
    315  *	0 if the pattern pointed at by arcn->pat was tagged as creating a
    316  *	match, -1 otherwise.
    317  */
    318 
    319 #if __STDC__
    320 int
    321 pat_sel(register ARCHD *arcn)
    322 #else
    323 int
    324 pat_sel(arcn)
    325 	register ARCHD *arcn;
    326 #endif
    327 {
    328 	register PATTERN *pt;
    329 	register PATTERN **ppt;
    330 	register int len;
    331 
    332 	/*
    333 	 * if no patterns just return
    334 	 */
    335 	if ((pathead == NULL) || ((pt = arcn->pat) == NULL))
    336 		return(0);
    337 
    338 	/*
    339 	 * when we are NOT limited to a single match per pattern mark the
    340 	 * pattern and return
    341 	 */
    342 	if (!nflag) {
    343 		pt->flgs |= MTCH;
    344 		return(0);
    345 	}
    346 
    347 	/*
    348 	 * we reach this point only when we allow a single selected match per
    349 	 * pattern, if the pattern matches a directory and we do not have -d
    350 	 * (dflag) we are done with this pattern. We may also be handed a file
    351 	 * in the subtree of a directory. in that case when we are operating
    352 	 * with -d, this pattern was already selected and we are done
    353 	 */
    354 	if (pt->flgs & DIR_MTCH)
    355 		return(0);
    356 
    357 	if (!dflag && ((pt->pend != NULL) || (arcn->type == PAX_DIR))) {
    358 		/*
    359 		 * ok we matched a directory and we are allowing
    360 		 * subtree matches but because of the -n only its children will
    361 		 * match. This is tagged as a DIR_MTCH type.
    362 		 * WATCH IT, the code assumes that pt->pend points
    363 		 * into arcn->name and arcn->name has not been modified.
    364 		 * If not we will have a big mess. Yup this is another kludge
    365 		 */
    366 
    367 		/*
    368 		 * if this was a prefix match, remove trailing part of path
    369 		 * so we can copy it. Future matches will be exact prefix match
    370 		 */
    371 		if (pt->pend != NULL)
    372 			*pt->pend = '\0';
    373 
    374 		if ((pt->pstr = strdup(arcn->name)) == NULL) {
    375 			warn(1, "Pattern select out of memory");
    376 			if (pt->pend != NULL)
    377 				*pt->pend = '/';
    378 			pt->pend = NULL;
    379 			return(-1);
    380 		}
    381 
    382 		/*
    383 		 * put the trailing / back in the source string
    384 		 */
    385 		if (pt->pend != NULL) {
    386 			*pt->pend = '/';
    387 			pt->pend = NULL;
    388 		}
    389 		pt->plen = strlen(pt->pstr);
    390 
    391 		/*
    392 		 * strip off any trailing /, this should really never happen
    393 		 */
    394 		len = pt->plen - 1;
    395 		if (*(pt->pstr + len) == '/') {
    396 			*(pt->pstr + len) = '\0';
    397 			pt->plen = len;
    398 		}
    399 		pt->flgs = DIR_MTCH | MTCH;
    400 		arcn->pat = pt;
    401 		return(0);
    402 	}
    403 
    404 	/*
    405 	 * we are then done with this pattern, so we delete it from the list
    406 	 * because it can never be used for another match.
    407 	 * Seems kind of strange to do for a -c, but the pax spec is really
    408 	 * vague on the interaction of -c -n and -d. We assume that when -c
    409 	 * and the pattern rejects a member (i.e. it matched it) it is done.
    410 	 * In effect we place the order of the flags as having -c last.
    411 	 */
    412 	pt = pathead;
    413 	ppt = &pathead;
    414 	while ((pt != NULL) && (pt != arcn->pat)) {
    415 		ppt = &(pt->fow);
    416 		pt = pt->fow;
    417 	}
    418 
    419 	if (pt == NULL) {
    420 		/*
    421 		 * should never happen....
    422 		 */
    423 		warn(1, "Pattern list inconsistant");
    424 		return(-1);
    425 	}
    426 	*ppt = pt->fow;
    427 	(void)free((char *)pt);
    428 	arcn->pat = NULL;
    429 	return(0);
    430 }
    431 
    432 /*
    433  * pat_match()
    434  *	see if this archive member matches any supplied pattern, if a match
    435  *	is found, arcn->pat is set to point at the potential pattern. Later if
    436  *	this archive member is "selected" we process and mark the pattern as
    437  *	one which matched a selected archive member (see pat_sel())
    438  * Return:
    439  *	0 if this archive member should be processed, 1 if it should be
    440  *	skipped and -1 if we are done with all patterns (and pax should quit
    441  *	looking for more members)
    442  */
    443 
    444 #if __STDC__
    445 int
    446 pat_match(register ARCHD *arcn)
    447 #else
    448 int
    449 pat_match(arcn)
    450 	register ARCHD *arcn;
    451 #endif
    452 {
    453 	register PATTERN *pt;
    454 
    455 	arcn->pat = NULL;
    456 
    457 	/*
    458 	 * if there are no more patterns and we have -n (and not -c) we are
    459 	 * done. otherwise with no patterns to match, matches all
    460 	 */
    461 	if (pathead == NULL) {
    462 		if (nflag && !cflag)
    463 			return(-1);
    464 		return(0);
    465 	}
    466 
    467 	/*
    468 	 * have to search down the list one at a time looking for a match.
    469 	 */
    470 	pt = pathead;
    471 	while (pt != NULL) {
    472 		/*
    473 		 * check for a file name match unless we have DIR_MTCH set in
    474 		 * this pattern then we want a prefix match
    475 		 */
    476 		if (pt->flgs & DIR_MTCH) {
    477 			/*
    478 			 * this pattern was matched before to a directory
    479 			 * as we must have -n set for this (but not -d). We can
    480 			 * only match CHILDREN of that directory so we must use
    481 			 * an exact prefix match (no wildcards).
    482 			 */
    483 			if ((arcn->name[pt->plen] == '/') &&
    484 			    (strncmp(pt->pstr, arcn->name, pt->plen) == 0))
    485 				break;
    486 		} else if (fn_match(pt->pstr, arcn->name, &pt->pend) == 0)
    487 			break;
    488 		pt = pt->fow;
    489 	}
    490 
    491 	/*
    492 	 * return the result, remember that cflag (-c) inverts the sense of a
    493 	 * match
    494 	 */
    495 	if (pt == NULL)
    496 		return(cflag ? 0 : 1);
    497 
    498 	/*
    499 	 * we had a match, now when we invert the sense (-c) we reject this
    500 	 * member. However we have to tag the pattern a being successful, (in a
    501 	 * match, not in selecting a archive member) so we call pat_sel() here.
    502 	 */
    503 	arcn->pat = pt;
    504 	if (!cflag)
    505 		return(0);
    506 
    507 	if (pat_sel(arcn) < 0)
    508 		return(-1);
    509 	arcn->pat = NULL;
    510 	return(1);
    511 }
    512 
    513 /*
    514  * fn_match()
    515  * Return:
    516  *	0 if this archive member should be processed, 1 if it should be
    517  *	skipped and -1 if we are done with all patterns (and pax should quit
    518  *	looking for more members)
    519  *	Note: *pend may be changed to show where the prefix ends.
    520  */
    521 
    522 #if __STDC__
    523 static int
    524 fn_match(register char *pattern, register char *string, char **pend)
    525 #else
    526 static int
    527 fn_match(pattern, string, pend)
    528 	register char *pattern;
    529 	register char *string;
    530 	char **pend;
    531 #endif
    532 {
    533 	register char c;
    534 	char test;
    535 
    536 	*pend = NULL;
    537 	for (;;) {
    538 		switch (c = *pattern++) {
    539 		case '\0':
    540 			/*
    541 			 * Ok we found an exact match
    542 			 */
    543 			if (*string == '\0')
    544 				return(0);
    545 
    546 			/*
    547 			 * Check if it is a prefix match
    548 			 */
    549 			if ((dflag == 1) || (*string != '/'))
    550 				return(-1);
    551 
    552 			/*
    553 			 * It is a prefix match, remember where the trailing
    554 			 * / is located
    555 			 */
    556 			*pend = string;
    557 			return(0);
    558 		case '?':
    559 			if ((test = *string++) == '\0')
    560 				return (-1);
    561 			break;
    562 		case '*':
    563 			c = *pattern;
    564 			/*
    565 			 * Collapse multiple *'s.
    566 			 */
    567 			while (c == '*')
    568 				c = *++pattern;
    569 
    570 			/*
    571 			 * Optimized hack for pattern with a * at the end
    572 			 */
    573 			if (c == '\0')
    574 				return (0);
    575 
    576 			/*
    577 			 * General case, use recursion.
    578 			 */
    579 			while ((test = *string) != '\0') {
    580 				if (!fn_match(pattern, string, pend))
    581 					return (0);
    582 				++string;
    583 			}
    584 			return (-1);
    585 		case '[':
    586 			/*
    587 			 * range match
    588 			 */
    589 			if (((test = *string++) == '\0') ||
    590 			    ((pattern = range_match(pattern, test)) == NULL))
    591 				return (-1);
    592 			break;
    593 		case '\\':
    594 		default:
    595 			if (c != *string++)
    596 				return (-1);
    597 			break;
    598 		}
    599 	}
    600 	/* NOTREACHED */
    601 }
    602 
    603 #ifdef __STDC__
    604 static char *
    605 range_match(register char *pattern, register int test)
    606 #else
    607 static char *
    608 range_match(pattern, test)
    609 	register char *pattern;
    610 	register int test;
    611 #endif
    612 {
    613 	register char c;
    614 	register char c2;
    615 	int negate;
    616 	int ok = 0;
    617 
    618 	if (negate = (*pattern == '!'))
    619 		++pattern;
    620 
    621 	while ((c = *pattern++) != ']') {
    622 		/*
    623 		 * Illegal pattern
    624 		 */
    625 		if (c == '\0')
    626 			return (NULL);
    627 
    628 		if ((*pattern == '-') && ((c2 = pattern[1]) != '\0') &&
    629 		    (c2 != ']')) {
    630 			if ((c <= test) && (test <= c2))
    631 				ok = 1;
    632 			pattern += 2;
    633 		} else if (c == test)
    634 			ok = 1;
    635 	}
    636 	return (ok == negate ? NULL : pattern);
    637 }
    638 
    639 /*
    640  * mod_name()
    641  *	modify a selected file name. first attempt to apply replacement string
    642  *	expressions, then apply interactive file rename. We apply replacement
    643  *	string expressions to both filenames and file links (if we didn't the
    644  *	links would point to the wrong place, and we could never be able to
    645  *	move an archive that has a file link in it). When we rename files
    646  *	interactively, we store that mapping (old name to user input name) so
    647  *	if we spot any file links to the old file name in the future, we will
    648  *	know exactly how to fix the file link.
    649  * Return:
    650  *	0 continue to  process file, 1 skip this file, -1 pax is finished
    651  */
    652 
    653 #if __STDC__
    654 int
    655 mod_name(register ARCHD *arcn)
    656 #else
    657 int
    658 mod_name(arcn)
    659 	register ARCHD *arcn;
    660 #endif
    661 {
    662 	register int res = 0;
    663 
    664 	/*
    665 	 * IMPORTANT: We have a problem. what do we do with symlinks?
    666 	 * Modifying a hard link name makes sense, as we know the file it
    667 	 * points at should have been seen already in the archive (and if it
    668 	 * wasn't seen because of a read error or a bad archive, we lose
    669 	 * anyway). But there are no such requirements for symlinks. On one
    670 	 * hand the symlink that refers to a file in the archive will have to
    671 	 * be modified to so it will still work at its new location in the
    672 	 * file system. On the other hand a symlink that points elsewhere (and
    673 	 * should continue to do so) should not be modified. There is clearly
    674 	 * no perfect solution here. So we handle them like hardlinks. Clearly
    675 	 * a replacement made by the interactive rename mapping is very likely
    676 	 * to be correct since it applies to a single file and is an exact
    677 	 * match. The regular expression replacements are a little harder to
    678 	 * justify though. We claim that the symlink name is only likely
    679 	 * to be replaced when it points within the file tree being moved and
    680 	 * in that case it should be modified. what we really need to do is to
    681 	 * call an oracle here. :)
    682 	 */
    683 	if (rephead != NULL) {
    684 		/*
    685 		 * we have replacement strings, modify the name and the link
    686 		 * name if any.
    687 		 */
    688 		if ((res = rep_name(arcn->name, &(arcn->nlen), 1)) != 0)
    689 			return(res);
    690 
    691 		if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
    692 		    (arcn->type == PAX_HRG)) &&
    693 		    ((res = rep_name(arcn->ln_name, &(arcn->ln_nlen), 0)) != 0))
    694 			return(res);
    695 	}
    696 
    697 	if (iflag) {
    698 		/*
    699 		 * perform interactive file rename, then map the link if any
    700 		 */
    701 		if ((res = tty_rename(arcn)) != 0)
    702 			return(res);
    703 		if ((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
    704 		    (arcn->type == PAX_HRG))
    705 			sub_name(arcn->ln_name, &(arcn->ln_nlen));
    706 	}
    707 	return(res);
    708 }
    709 
    710 /*
    711  * tty_rename()
    712  *	Prompt the user for a replacement file name. A "." keeps the old name,
    713  *	a empty line skips the file, and an EOF on reading the tty, will cause
    714  *	pax to stop processing and exit. Otherwise the file name input, replaces
    715  *	the old one.
    716  * Return:
    717  *	0 process this file, 1 skip this file, -1 we need to exit pax
    718  */
    719 
    720 #if __STDC__
    721 static int
    722 tty_rename(register ARCHD *arcn)
    723 #else
    724 static int
    725 tty_rename(arcn)
    726 	register ARCHD *arcn;
    727 #endif
    728 {
    729 	char tmpname[PAXPATHLEN+2];
    730 	int res;
    731 
    732 	/*
    733 	 * prompt user for the replacement name for a file, keep trying until
    734 	 * we get some reasonable input. Archives may have more than one file
    735 	 * on them with the same name (from updates etc). We print verbose info
    736 	 * on the file so the user knows what is up.
    737 	 */
    738 	tty_prnt("\nATTENTION: %s interactive file rename operation.\n", argv0);
    739 
    740 	for (;;) {
    741 		ls_tty(arcn);
    742 		tty_prnt("Input new name, or a \".\" to keep the old name, ");
    743 		tty_prnt("or a \"return\" to skip this file.\n");
    744 		tty_prnt("Input > ");
    745 		if (tty_read(tmpname, sizeof(tmpname)) < 0)
    746 			return(-1);
    747 		if (strcmp(tmpname, "..") == 0) {
    748 			tty_prnt("Try again, illegal file name: ..\n");
    749 			continue;
    750 		}
    751 		if (strlen(tmpname) > PAXPATHLEN) {
    752 			tty_prnt("Try again, file name too long\n");
    753 			continue;
    754 		}
    755 		break;
    756 	}
    757 
    758 	/*
    759 	 * empty file name, skips this file. a "." leaves it alone
    760 	 */
    761 	if (tmpname[0] == '\0') {
    762 		tty_prnt("Skipping file.\n");
    763 		return(1);
    764 	}
    765 	if ((tmpname[0] == '.') && (tmpname[1] == '\0')) {
    766 		tty_prnt("Processing continues, name unchanged.\n");
    767 		return(0);
    768 	}
    769 
    770 	/*
    771 	 * ok the name changed. We may run into links that point at this
    772 	 * file later. we have to remember where the user sent the file
    773 	 * in order to repair any links.
    774 	 */
    775 	tty_prnt("Processing continues, name changed to: %s\n", tmpname);
    776 	res = add_name(arcn->name, arcn->nlen, tmpname);
    777 	arcn->nlen = l_strncpy(arcn->name, tmpname, PAXPATHLEN+1);
    778 	if (res < 0)
    779 		return(-1);
    780 	return(0);
    781 }
    782 
    783 /*
    784  * set_dest()
    785  *	fix up the file name and the link name (if any) so this file will land
    786  *	in the destination directory (used during copy() -rw).
    787  * Return:
    788  *	0 if ok, -1 if failure (name too long)
    789  */
    790 
    791 #if __STDC__
    792 int
    793 set_dest(register ARCHD *arcn, char *dest_dir, int dir_len)
    794 #else
    795 int
    796 set_dest(arcn, dest_dir, dir_len)
    797 	register ARCHD *arcn;
    798 	char *dest_dir;
    799 	int dir_len;
    800 #endif
    801 {
    802 	if (fix_path(arcn->name, &(arcn->nlen), dest_dir, dir_len) < 0)
    803 		return(-1);
    804 
    805 	/*
    806 	 * It is really hard to deal with symlinks here, we cannot be sure
    807 	 * if the name they point was moved (or will be moved). It is best to
    808 	 * leave them alone.
    809 	 */
    810 	if ((arcn->type != PAX_HLK) && (arcn->type != PAX_HRG))
    811 		return(0);
    812 
    813 	if (fix_path(arcn->ln_name, &(arcn->ln_nlen), dest_dir, dir_len) < 0)
    814 		return(-1);
    815 	return(0);
    816 }
    817 
    818 /*
    819  * fix_path
    820  *	concatenate dir_name and or_name and store the result in or_name (if
    821  *	it fits). This is one ugly function.
    822  * Return:
    823  *	0 if ok, -1 if the final name is too long
    824  */
    825 
    826 #if __STDC__
    827 static int
    828 fix_path( char *or_name, int *or_len, char *dir_name, int dir_len)
    829 #else
    830 static int
    831 fix_path(or_name, or_len, dir_name, dir_len)
    832 	char *or_name;
    833 	int *or_len;
    834 	char *dir_name;
    835 	int dir_len;
    836 #endif
    837 {
    838 	register char *src;
    839 	register char *dest;
    840 	register char *start;
    841 	int len;
    842 
    843 	/*
    844 	 * we shift the or_name to the right enough to tack in the dir_name
    845 	 * at the front. We make sure we have enough space for it all before
    846 	 * we start. since dest always ends in a slash, we skip of or_name
    847 	 * if it also starts with one.
    848 	 */
    849 	start = or_name;
    850 	src = start + *or_len;
    851 	dest = src + dir_len;
    852 	if (*start == '/') {
    853 		++start;
    854 		--dest;
    855 	}
    856 	if ((len = dest - or_name) > PAXPATHLEN) {
    857 		warn(1, "File name %s/%s, too long", dir_name, start);
    858 		return(-1);
    859 	}
    860 	*or_len = len;
    861 
    862 	/*
    863 	 * enough space, shift
    864 	 */
    865 	while (src >= start)
    866 		*dest-- = *src--;
    867 	src = dir_name + dir_len - 1;
    868 
    869 	/*
    870 	 * splice in the destination directory name
    871 	 */
    872 	while (src >= dir_name)
    873 		*dest-- = *src--;
    874 
    875 	*(or_name + len) = '\0';
    876 	return(0);
    877 }
    878 
    879 /*
    880  * rep_name()
    881  *	walk down the list of replacement strings applying each one in order.
    882  *	when we find one with a successful substitution, we modify the name
    883  *	as specified. if required, we print the results. if the resulting name
    884  *	is empty, we will skip this archive member. We use the regexp(3)
    885  *	routines (regexp() ought to win a prize as having the most cryptic
    886  *	library function manual page).
    887  *	--Parameters--
    888  *	name is the file name we are going to apply the regular expressions to
    889  *	(and may be modified)
    890  *	nlen is the length of this name (and is modified to hold the length of
    891  *	the final string).
    892  *	prnt is a flag that says whether to print the final result.
    893  * Return:
    894  *	0 if substitution was successful, 1 if we are to skip the file (the name
    895  *	ended up empty)
    896  */
    897 
    898 #if __STDC__
    899 static int
    900 rep_name(char *name, int *nlen, int prnt)
    901 #else
    902 static int
    903 rep_name(name, nlen, prnt)
    904 	char *name;
    905 	int *nlen;
    906 	int prnt;
    907 #endif
    908 {
    909 	register REPLACE *pt;
    910 	register char *inpt;
    911 	register char *outpt;
    912 	register char *endpt;
    913 	register char *rpt;
    914 	register int found = 0;
    915 	register int res;
    916 #	ifndef NET2_REGEX
    917 	regmatch_t pm[MAXSUBEXP];
    918 #	endif
    919 	char nname[PAXPATHLEN+1];	/* final result of all replacements */
    920 	char buf1[PAXPATHLEN+1];	/* where we work on the name */
    921 
    922 	/*
    923 	 * copy the name into buf1, where we will work on it. We need to keep
    924 	 * the orig string around so we can print out the result of the final
    925 	 * replacement. We build up the final result in nname. inpt points at
    926 	 * the string we apply the regular expression to. prnt is used to
    927 	 * suppress printing when we handle replacements on the link field
    928 	 * (the user already saw that substitution go by)
    929 	 */
    930 	pt = rephead;
    931 	(void)strcpy(buf1, name);
    932 	inpt = buf1;
    933 	outpt = nname;
    934 	endpt = outpt + PAXPATHLEN;
    935 
    936 	/*
    937 	 * try each replacement string in order
    938 	 */
    939 	while (pt != NULL) {
    940 		do {
    941 			/*
    942 			 * check for a successful substitution, if not go to
    943 			 * the next pattern, or cleanup if we were global
    944 			 */
    945 #			ifdef NET2_REGEX
    946 			if (regexec(pt->rcmp, inpt) == 0)
    947 #			else
    948 			if (regexec(&(pt->rcmp), inpt, MAXSUBEXP, pm, 0) != 0)
    949 #			endif
    950 				break;
    951 
    952 			/*
    953 			 * ok we found one. We have three parts, the prefix
    954 			 * which did not match, the section that did and the
    955 			 * tail (that also did not match). Copy the prefix to
    956 			 * the final output buffer (watching to make sure we
    957 			 * do not create a string too long).
    958 			 */
    959 			found = 1;
    960 #			ifdef NET2_REGEX
    961 			rpt = pt->rcmp->startp[0];
    962 #			else
    963 			rpt = inpt + pm[0].rm_so;
    964 #			endif
    965 
    966 			while ((inpt < rpt) && (outpt < endpt))
    967 				*outpt++ = *inpt++;
    968 			if (outpt == endpt)
    969 				break;
    970 
    971 			/*
    972 			 * for the second part (which matched the regular
    973 			 * expression) apply the substitution using the
    974 			 * replacement string and place it the prefix in the
    975 			 * final output. If we have problems, skip it.
    976 			 */
    977 #			ifdef NET2_REGEX
    978 			if ((res = resub(pt->rcmp,pt->nstr,outpt,endpt)) < 0) {
    979 #			else
    980 			if ((res = resub(&(pt->rcmp),pm,pt->nstr,outpt,endpt))
    981 			    < 0) {
    982 #			endif
    983 				if (prnt)
    984 					warn(1, "Replacement name error %s",
    985 					    name);
    986 				return(1);
    987 			}
    988 			outpt += res;
    989 
    990 			/*
    991 			 * we set up to look again starting at the first
    992 			 * character in the tail (of the input string right
    993 			 * after the last character matched by the regular
    994 			 * expression (inpt always points at the first char in
    995 			 * the string to process). If we are not doing a global
    996 			 * substitution, we will use inpt to copy the tail to
    997 			 * the final result. Make sure we do not overrun the
    998 			 * output buffer
    999 			 */
   1000 #			ifdef NET2_REGEX
   1001 			inpt = pt->rcmp->endp[0];
   1002 #			else
   1003 			inpt += pm[0].rm_eo;
   1004 #			endif
   1005 
   1006 			if ((outpt == endpt) || (*inpt == '\0'))
   1007 				break;
   1008 
   1009 			/*
   1010 			 * if the user wants global we keep trying to
   1011 			 * substitute until it fails, then we are done.
   1012 			 */
   1013 		} while (pt->flgs & GLOB);
   1014 
   1015 		if (found)
   1016 			break;
   1017 
   1018 		/*
   1019 		 * a successful substitution did NOT occur, try the next one
   1020 		 */
   1021 		pt = pt->fow;
   1022 	}
   1023 
   1024 	if (found) {
   1025 		/*
   1026 		 * we had a substitution, copy the last tail piece (if there is
   1027 		 * room) to the final result
   1028 		 */
   1029 		while ((outpt < endpt) && (*inpt != '\0'))
   1030 			*outpt++ = *inpt++;
   1031 
   1032 		*outpt = '\0';
   1033 		if ((outpt == endpt) && (*inpt != '\0')) {
   1034 			if (prnt)
   1035 				warn(1,"Replacement name too long %s >> %s",
   1036 				    name, nname);
   1037 			return(1);
   1038 		}
   1039 
   1040 		/*
   1041 		 * inform the user of the result if wanted
   1042 		 */
   1043 		if (prnt && (pt->flgs & PRNT)) {
   1044 			if (*nname == '\0')
   1045 				(void)fprintf(stderr,"%s >> <empty string>\n",
   1046 				    name);
   1047 			else
   1048 				(void)fprintf(stderr,"%s >> %s\n", name, nname);
   1049 		}
   1050 
   1051 		/*
   1052 		 * if empty inform the caller this file is to be skipped
   1053 		 * otherwise copy the new name over the orig name and return
   1054 		 */
   1055 		if (*nname == '\0')
   1056 			return(1);
   1057 		*nlen = l_strncpy(name, nname, PAXPATHLEN + 1);
   1058 	}
   1059 	return(0);
   1060 }
   1061 
   1062 #ifdef NET2_REGEX
   1063 /*
   1064  * resub()
   1065  *	apply the replacement to the matched expression. expand out the old
   1066  * 	style ed(1) subexpression expansion.
   1067  * Return:
   1068  *	-1 if error, or the number of characters added to the destination.
   1069  */
   1070 
   1071 #if __STDC__
   1072 static int
   1073 resub(regexp *prog, char *src, char *dest, register char *destend)
   1074 #else
   1075 static int
   1076 resub(prog, src, dest, destend)
   1077 	regexp *prog;
   1078 	char *src;
   1079 	char *dest;
   1080 	register char *destend;
   1081 #endif
   1082 {
   1083 	register char *spt;
   1084 	register char *dpt;
   1085 	register char c;
   1086 	register int no;
   1087 	register int len;
   1088 
   1089 	spt = src;
   1090 	dpt = dest;
   1091 	while ((dpt < destend) && ((c = *spt++) != '\0')) {
   1092 		if (c == '&')
   1093 			no = 0;
   1094 		else if ((c == '\\') && (*spt >= '0') && (*spt <= '9'))
   1095 			no = *spt++ - '0';
   1096 		else {
   1097  			if ((c == '\\') && ((*spt == '\\') || (*spt == '&')))
   1098  				c = *spt++;
   1099  			*dpt++ = c;
   1100 			continue;
   1101 		}
   1102  		if ((prog->startp[no] == NULL) || (prog->endp[no] == NULL) ||
   1103 		    ((len = prog->endp[no] - prog->startp[no]) <= 0))
   1104 			continue;
   1105 
   1106 		/*
   1107 		 * copy the subexpression to the destination.
   1108 		 * fail if we run out of space or the match string is damaged
   1109 		 */
   1110 		if (len > (destend - dpt))
   1111 			len = destend - dpt;
   1112 		if (l_strncpy(dpt, prog->startp[no], len) != len)
   1113 			return(-1);
   1114 		dpt += len;
   1115 	}
   1116 	return(dpt - dest);
   1117 }
   1118 
   1119 #else
   1120 
   1121 /*
   1122  * resub()
   1123  *	apply the replacement to the matched expression. expand out the old
   1124  * 	style ed(1) subexpression expansion.
   1125  * Return:
   1126  *	-1 if error, or the number of characters added to the destination.
   1127  */
   1128 
   1129 #if __STDC__
   1130 static int
   1131 resub(regex_t *rp, register regmatch_t *pm, char *src, char *dest,
   1132 	register char *destend)
   1133 #else
   1134 static int
   1135 resub(rp, pm, src, dest, destend)
   1136 	regex_t *rp;
   1137 	register regmatch_t *pm;
   1138 	char *src;
   1139 	char *dest;
   1140 	register char *destend;
   1141 #endif
   1142 {
   1143 	register char *spt;
   1144 	register char *dpt;
   1145 	register char c;
   1146 	register regmatch_t *pmpt;
   1147 	register int len;
   1148 	int subexcnt;
   1149 
   1150 	spt =  src;
   1151 	dpt = dest;
   1152 	subexcnt = rp->re_nsub;
   1153 	while ((dpt < destend) && ((c = *spt++) != '\0')) {
   1154 		/*
   1155 		 * see if we just have an ordinary replacement character
   1156 		 * or we refer to a subexpression.
   1157 		 */
   1158 		if (c == '&') {
   1159 			pmpt = pm;
   1160 		} else if ((c == '\\') && (*spt >= '0') && (*spt <= '9')) {
   1161 			/*
   1162 			 * make sure there is a subexpression as specified
   1163 			 */
   1164 			if ((len = *spt++ - '0') > subexcnt)
   1165 				return(-1);
   1166 			pmpt = pm + len;
   1167 		} else {
   1168  			/*
   1169 			 * Ordinary character, just copy it
   1170 			 */
   1171  			if ((c == '\\') && ((*spt == '\\') || (*spt == '&')))
   1172  				c = *spt++;
   1173  			*dpt++ = c;
   1174 			continue;
   1175 		}
   1176 
   1177 		/*
   1178 		 * continue if the subexpression is bogus
   1179 		 */
   1180 		if ((pmpt->rm_so < 0) || (pmpt->rm_eo < 0) ||
   1181 		    ((len = pmpt->rm_eo - pmpt->rm_so) <= 0))
   1182 			continue;
   1183 
   1184 		/*
   1185 		 * copy the subexpression to the destination.
   1186 		 * fail if we run out of space or the match string is damaged
   1187 		 */
   1188 		if (len > (destend - dpt))
   1189 			len = destend - dpt;
   1190 		if (l_strncpy(dpt, src + pmpt->rm_so, len) != len)
   1191 			return(-1);
   1192 		dpt += len;
   1193 	}
   1194 	return(dpt - dest);
   1195 }
   1196 #endif
   1197