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