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