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