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