]>
git.cameronkatri.com Git - apple_cmds.git/blob - file_cmds/pax/pat_rep.c
1 /* $OpenBSD: pat_rep.c,v 1.30 2005/08/05 08:30:10 djm Exp $ */
2 /* $NetBSD: pat_rep.c,v 1.4 1995/03/21 09:07:33 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
40 static const char sccsid
[] = "@(#)pat_rep.c 8.2 (Berkeley) 4/18/94";
42 __used
static const char rcsid
[] = "$OpenBSD: pat_rep.c,v 1.30 2005/08/05 08:30:10 djm Exp $";
46 #include <sys/types.h>
49 #include <sys/param.h>
61 * routines to handle pattern matching, name modification (regular expression
62 * substitution and interactive renames), and destination name modification for
63 * copy (-rw). Both file name and link names are adjusted as required in these
67 #define MAXSUBEXP 10 /* max subexpressions, DO NOT CHANGE */
68 static PATTERN
*pathead
= NULL
; /* file pattern match list head */
69 static PATTERN
*pattail
= NULL
; /* file pattern match list tail */
70 static REPLACE
*rephead
= NULL
; /* replacement string list head */
71 static REPLACE
*reptail
= NULL
; /* replacement string list tail */
73 static int rep_name(char *, size_t, int *, int);
74 int tty_rename(ARCHD
*);
75 static int fix_path(char *, int *, char *, int);
76 static int fn_match(char *, char *, char **);
78 static char* extract_equiv_pat(char *, char **);
79 static int regex_match(char *, char *, char **);
81 static char * range_match(char *, int);
82 static int resub(regex_t
*, regmatch_t
*, char *, char *, char *, char *);
86 * parses the -s replacement string; compiles the regular expression
87 * and stores the compiled value and it's replacement string together in
88 * replacement string list. Input to this function is of the form:
90 * The first char in the string specifies the delimiter used by this
91 * replacement string. "Old" is a regular expression in "ed" format which
92 * is compiled by regcomp() and is applied to filenames. "new" is the
93 * substitution string; p and g are options flags for printing and global
94 * replacement (over the single filename)
96 * 0 if a proper replacement string and regular expression was added to
97 * the list of replacement patterns; -1 otherwise.
110 * throw out the bad parameters
112 if ((str
== NULL
) || (*str
== '\0')) {
113 paxwarn(1, "Empty replacement string");
118 * first character in the string specifies what the delimiter is for
121 for (pt1
= str
+1; *pt1
; pt1
++) {
130 paxwarn(1, "Invalid replacement string %s", str
);
135 * allocate space for the node that handles this replacement pattern
136 * and split out the regular expression and try to compile it
138 if ((rep
= (REPLACE
*)malloc(sizeof(REPLACE
))) == NULL
) {
139 paxwarn(1, "Unable to allocate memory for replacement string");
144 if ((res
= regcomp(&(rep
->rcmp
), str
+1, 0)) != 0) {
145 regerror(res
, &(rep
->rcmp
), rebuf
, sizeof(rebuf
));
146 paxwarn(1, "%s while compiling regular expression %s", rebuf
, str
);
147 (void)free((char *)rep
);
152 * put the delimiter back in case we need an error message and
153 * locate the delimiter at the end of the replacement string
154 * we then point the node at the new substitution string
157 for (pt2
= pt1
; *pt2
; pt2
++) {
166 regfree(&(rep
->rcmp
));
167 (void)free((char *)rep
);
168 paxwarn(1, "Invalid replacement string %s", str
);
178 * set the options if any
180 while (*pt2
!= '\0') {
191 regfree(&(rep
->rcmp
));
192 (void)free((char *)rep
);
194 paxwarn(1, "Invalid replacement string option %s", str
);
201 * all done, link it in at the end
204 if (rephead
== NULL
) {
205 reptail
= rephead
= rep
;
215 * add a pattern match to the pattern match list. Pattern matches are used
216 * to select which archive members are extracted. (They appear as
217 * arguments to pax in the list and read modes). If no patterns are
218 * supplied to pax, all members in the archive will be selected (and the
219 * pattern match list is empty).
221 * 0 if the pattern was added to the list, -1 otherwise
225 pat_add(char *str
, char *chdname
)
232 if ((str
== NULL
) || (*str
== '\0')) {
233 paxwarn(1, "Empty pattern string");
238 * allocate space for the pattern and store the pattern. the pattern is
239 * part of argv so do not bother to copy it, just point at it. Add the
240 * node to the end of the pattern list
242 if ((pt
= (PATTERN
*)malloc(sizeof(PATTERN
))) == NULL
) {
243 paxwarn(1, "Unable to allocate memory for pattern string");
249 pt
->plen
= strlen(str
);
252 pt
->chdname
= chdname
;
254 if (pathead
== NULL
) {
255 pattail
= pathead
= pt
;
265 * complain if any the user supplied pattern did not result in a match to
266 * a selected archive member.
276 * walk down the list checking the flags to make sure MTCH was set,
279 for (pt
= pathead
; pt
!= NULL
; pt
= pt
->fow
) {
283 paxwarn(1, "WARNING! These patterns were not matched:");
286 (void)fprintf(stderr
, "%s\n", pt
->pstr
);
292 * the archive member which matches a pattern was selected. Mark the
293 * pattern as having selected an archive member. arcn->pat points at the
294 * pattern that was matched. arcn->pat is set in pat_match()
296 * NOTE: When the -c option is used, we are called when there was no match
297 * by pat_match() (that means we did match before the inverted sense of
298 * the logic). Now this seems really strange at first, but with -c we
299 * need to keep track of those patterns that cause an archive member to NOT
300 * be selected (it found an archive member with a specified pattern)
302 * 0 if the pattern pointed at by arcn->pat was tagged as creating a
303 * match, -1 otherwise.
314 * if no patterns just return
316 if ((pathead
== NULL
) || ((pt
= arcn
->pat
) == NULL
))
320 * when we are NOT limited to a single match per pattern mark the
329 * we reach this point only when we allow a single selected match per
330 * pattern, if the pattern matches a directory and we do not have -d
331 * (dflag) we are done with this pattern. We may also be handed a file
332 * in the subtree of a directory. in that case when we are operating
333 * with -d, this pattern was already selected and we are done
335 if (pt
->flgs
& DIR_MTCH
)
338 if (!dflag
&& ((pt
->pend
!= NULL
) || (arcn
->type
== PAX_DIR
))) {
340 * ok we matched a directory and we are allowing
341 * subtree matches but because of the -n only its children will
342 * match. This is tagged as a DIR_MTCH type.
343 * WATCH IT, the code assumes that pt->pend points
344 * into arcn->name and arcn->name has not been modified.
345 * If not we will have a big mess. Yup this is another kludge
349 * if this was a prefix match, remove trailing part of path
350 * so we can copy it. Future matches will be exact prefix match
352 if (pt
->pend
!= NULL
)
355 if ((pt
->pstr
= strdup(arcn
->name
)) == NULL
) {
356 paxwarn(1, "Pattern select out of memory");
357 if (pt
->pend
!= NULL
)
364 * put the trailing / back in the source string
366 if (pt
->pend
!= NULL
) {
370 pt
->plen
= strlen(pt
->pstr
);
373 * strip off any trailing /, this should really never happen
376 if (*(pt
->pstr
+ len
) == '/') {
377 *(pt
->pstr
+ len
) = '\0';
380 pt
->flgs
= DIR_MTCH
| MTCH
;
386 * we are then done with this pattern, so we delete it from the list
387 * because it can never be used for another match.
388 * Seems kind of strange to do for a -c, but the pax spec is really
389 * vague on the interaction of -c, -n and -d. We assume that when -c
390 * and the pattern rejects a member (i.e. it matched it) it is done.
391 * In effect we place the order of the flags as having -c last.
395 while ((pt
!= NULL
) && (pt
!= arcn
->pat
)) {
402 * should never happen....
404 paxwarn(1, "Pattern list inconsistent");
408 (void)free((char *)pt
);
415 * see if this archive member matches any supplied pattern, if a match
416 * is found, arcn->pat is set to point at the potential pattern. Later if
417 * this archive member is "selected" we process and mark the pattern as
418 * one which matched a selected archive member (see pat_sel())
420 * 0 if this archive member should be processed, 1 if it should be
421 * skipped and -1 if we are done with all patterns (and pax should quit
422 * looking for more members)
426 pat_match(ARCHD
*arcn
)
433 * if there are no more patterns and we have -n (and not -c) we are
434 * done. otherwise with no patterns to match, matches all
436 if (pathead
== NULL
) {
443 * have to search down the list one at a time looking for a match.
448 * check for a file name match unless we have DIR_MTCH set in
449 * this pattern then we want a prefix match
451 if (pt
->flgs
& DIR_MTCH
) {
453 * this pattern was matched before to a directory
454 * as we must have -n set for this (but not -d). We can
455 * only match CHILDREN of that directory so we must use
456 * an exact prefix match (no wildcards).
458 if ((arcn
->name
[pt
->plen
] == '/') &&
459 (strncmp(pt
->pstr
, arcn
->name
, pt
->plen
) == 0))
461 } else if (fn_match(pt
->pstr
, arcn
->name
, &pt
->pend
) == 0)
467 * return the result, remember that cflag (-c) inverts the sense of a
471 return(cflag
? 0 : 1);
474 * we had a match, now when we invert the sense (-c) we reject this
475 * member. However we have to tag the pattern a being successful, (in a
476 * match, not in selecting an archive member) so we call pat_sel() here.
482 if (pat_sel(arcn
) < 0)
491 * 0 if this archive member should be processed, 1 if it should be
492 * skipped and -1 if we are done with all patterns (and pax should quit
493 * looking for more members)
494 * Note: *pend may be changed to show where the prefix ends.
498 fn_match(char *pattern
, char *string
, char **pend
)
502 #ifdef _HAVE_REGCOMP_
504 char *pat_pend
= NULL
;
509 switch (c
= *pattern
++) {
512 * Ok we found an exact match
518 * Check if it is a prefix match
520 if ((dflag
== 1) || (*string
!= '/'))
524 * It is a prefix match, remember where the trailing
530 if ((*string
++) == '\0')
536 * Collapse multiple *'s.
542 * Optimized hack for pattern with a * at the end
548 * General case, use recursion.
550 while ((*string
) != '\0') {
551 if (!fn_match(pattern
, string
, pend
))
560 #ifdef _HAVE_REGCOMP_
562 * Check for equivalence class and use regex_match to
563 * handle this case. Note pattern should include the
564 * opening bracket '['
566 equiv_pat
= extract_equiv_pat(pattern
-1, &pat_pend
);
568 if (regex_match(equiv_pat
, string
, &string
) == -1) {
576 * Update the pattern string
582 if (((test
= *string
++) == '\0') ||
583 ((pattern
= range_match(pattern
, test
)) == NULL
))
597 #ifdef _HAVE_REGCOMP_
599 extract_equiv_pat(char *pattern
, char **pend
)
603 int is_double_bracket
= 0;
604 char* equiv_pat
= NULL
;
606 if (*pattern
== '\0' || pattern
[1] == '\0' || pattern
[2] == '\0')
610 * check if the pattern is
611 * "[= =]", "[[= =][= =]]", "[: :]", or "[[: :][: :]]"
612 * note that the full "pattern" string needs to be passed in
614 is_double_bracket
= (*pattern
== '[' && pattern
[1] == '[');
615 if (!(*pattern
== '[') && !is_double_bracket
) {
621 if (is_double_bracket
) {
626 if (!(*pattern
== ':') && !(*pattern
== '=')) {
633 for(; *pattern
!= '\0'; pat_len
++, pattern
++) {
634 if (!is_double_bracket
) {
635 if ((*pattern
== '=' || *pattern
== ':')
636 && pattern
[1] == ']') {
644 if ((*pattern
== '=' || *pattern
== ':')
645 && pattern
[1] == ']' && pattern
[2] == ']') {
659 equiv_pat
= strndup(pattern
-pat_len
, pat_len
);
661 if (equiv_pat
== NULL
) {
662 paxwarn(1, "Out of memory");
667 * set pend to the remaining pattern to be matched
677 regex_match(char *pattern
, char *string
, char **pend
)
684 if ((res
= regcomp(&(preg
), pattern
, REG_EXTENDED
)) != 0) {
685 regerror(res
, &(preg
), rebuf
, sizeof(rebuf
));
686 paxwarn(1, "%s while compiling pattern %s", rebuf
, pattern
);
690 if (regexec(&(preg
), string
, 1, &(pmatch
), 0) != 0) {
698 * starting position of the match must be 0
700 if (pmatch
.rm_so
!= 0) {
705 * set pend to the remaining string to be matched
708 *pend
= string
+ pmatch
.rm_eo
;
716 range_match(char *pattern
, int test
)
723 if ((negate
= (*pattern
== '!')) != 0)
726 while ((c
= *pattern
++) != ']') {
733 if ((*pattern
== '-') && ((c2
= pattern
[1]) != '\0') &&
735 if ((c
<= test
) && (test
<= c2
))
738 } else if (c
== test
)
741 return (ok
== negate
? NULL
: pattern
);
746 * modify a selected file name. first attempt to apply replacement string
747 * expressions, then apply interactive file rename. We apply replacement
748 * string expressions to both filenames and file links (if we didn't the
749 * links would point to the wrong place, and we could never be able to
750 * move an archive that has a file link in it). When we rename files
751 * interactively, we store that mapping (old name to user input name) so
752 * if we spot any file links to the old file name in the future, we will
753 * know exactly how to fix the file link.
755 * 0 continue to process file, 1 skip this file, -1 pax is finished
759 mod_name(ARCHD
*arcn
)
764 * Strip off leading '/' if appropriate.
765 * Currently, this option is only set for the tar format.
767 while (rmleadslash
&& arcn
->name
[0] == '/') {
768 if (arcn
->name
[1] == '\0') {
771 (void)memmove(arcn
->name
, &arcn
->name
[1],
775 if (rmleadslash
< 2) {
777 paxwarn(0, "Removing leading / from absolute path names in the archive");
780 while (rmleadslash
&& arcn
->ln_name
[0] == '/' &&
781 (arcn
->type
== PAX_HLK
|| arcn
->type
== PAX_HRG
)) {
782 if (arcn
->ln_name
[1] == '\0') {
783 arcn
->ln_name
[0] = '.';
785 (void)memmove(arcn
->ln_name
, &arcn
->ln_name
[1],
786 strlen(arcn
->ln_name
));
789 if (rmleadslash
< 2) {
791 paxwarn(0, "Removing leading / from absolute path names in the archive");
796 * IMPORTANT: We have a problem. what do we do with symlinks?
797 * Modifying a hard link name makes sense, as we know the file it
798 * points at should have been seen already in the archive (and if it
799 * wasn't seen because of a read error or a bad archive, we lose
800 * anyway). But there are no such requirements for symlinks. On one
801 * hand the symlink that refers to a file in the archive will have to
802 * be modified to so it will still work at its new location in the
803 * file system. On the other hand a symlink that points elsewhere (and
804 * should continue to do so) should not be modified. There is clearly
805 * no perfect solution here. So we handle them like hardlinks. Clearly
806 * a replacement made by the interactive rename mapping is very likely
807 * to be correct since it applies to a single file and is an exact
808 * match. The regular expression replacements are a little harder to
809 * justify though. We claim that the symlink name is only likely
810 * to be replaced when it points within the file tree being moved and
811 * in that case it should be modified. what we really need to do is to
812 * call an oracle here. :)
814 if (rephead
!= NULL
) {
816 * we have replacement strings, modify the name and the link
819 if ((res
= rep_name(arcn
->name
, sizeof(arcn
->name
), &(arcn
->nlen
), 1)) != 0)
822 if (((arcn
->type
== PAX_SLK
) || (arcn
->type
== PAX_HLK
) ||
823 (arcn
->type
== PAX_HRG
)) &&
824 ((res
= rep_name(arcn
->ln_name
, sizeof(arcn
->ln_name
), &(arcn
->ln_nlen
), 0)) != 0))
830 * perform interactive file rename, then map the link if any
832 if ((res
= tty_rename(arcn
)) != 0)
834 if ((arcn
->type
== PAX_SLK
) || (arcn
->type
== PAX_HLK
) ||
835 (arcn
->type
== PAX_HRG
))
836 sub_name(arcn
->ln_name
, &(arcn
->ln_nlen
), sizeof(arcn
->ln_name
));
843 * Prompt the user for a replacement file name. A "." keeps the old name,
844 * a empty line skips the file, and an EOF on reading the tty, will cause
845 * pax to stop processing and exit. Otherwise the file name input, replaces
848 * 0 process this file, 1 skip this file, -1 we need to exit pax
852 tty_rename(ARCHD
*arcn
)
854 char tmpname
[PAXPATHLEN
+2];
858 * prompt user for the replacement name for a file, keep trying until
859 * we get some reasonable input. Archives may have more than one file
860 * on them with the same name (from updates etc). We print verbose info
861 * on the file so the user knows what is up.
863 tty_prnt("\nATTENTION: %s interactive file rename operation.\n", argv0
);
867 tty_prnt("Input new name, or a \".\" to keep the old name, ");
868 tty_prnt("or a \"return\" to skip this file.\n");
869 tty_prnt("Input > ");
870 if (tty_read(tmpname
, sizeof(tmpname
)) < 0)
872 if (strcmp(tmpname
, "..") == 0) {
873 tty_prnt("Try again, illegal file name: ..\n");
876 if (strlen(tmpname
) > PAXPATHLEN
) {
877 tty_prnt("Try again, file name too long\n");
884 * empty file name, skips this file. a "." leaves it alone
886 if (tmpname
[0] == '\0') {
887 tty_prnt("Skipping file.\n");
890 if ((tmpname
[0] == '.') && (tmpname
[1] == '\0')) {
891 tty_prnt("Processing continues, name unchanged.\n");
896 * ok the name changed. We may run into links that point at this
897 * file later. we have to remember where the user sent the file
898 * in order to repair any links.
900 tty_prnt("Processing continues, name changed to: %s\n", tmpname
);
901 res
= add_name(arcn
->name
, arcn
->nlen
, tmpname
);
902 arcn
->nlen
= strlcpy(arcn
->name
, tmpname
, sizeof(arcn
->name
));
903 if (arcn
->nlen
>= sizeof(arcn
->name
))
904 arcn
->nlen
= sizeof(arcn
->name
) - 1; /* XXX truncate? */
912 * fix up the file name and the link name (if any) so this file will land
913 * in the destination directory (used during copy() -rw).
915 * 0 if ok, -1 if failure (name too long)
919 set_dest(ARCHD
*arcn
, char *dest_dir
, int dir_len
)
921 if (fix_path(arcn
->name
, &(arcn
->nlen
), dest_dir
, dir_len
) < 0)
925 * It is really hard to deal with symlinks here, we cannot be sure
926 * if the name they point was moved (or will be moved). It is best to
929 if ((arcn
->type
!= PAX_HLK
) && (arcn
->type
!= PAX_HRG
))
932 if (fix_path(arcn
->ln_name
, &(arcn
->ln_nlen
), dest_dir
, dir_len
) < 0)
939 * concatenate dir_name and or_name and store the result in or_name (if
940 * it fits). This is one ugly function.
942 * 0 if ok, -1 if the final name is too long
946 fix_path(char *or_name
, int *or_len
, char *dir_name
, int dir_len
)
954 * we shift the or_name to the right enough to tack in the dir_name
955 * at the front. We make sure we have enough space for it all before
956 * we start. since dest always ends in a slash, we skip of or_name
957 * if it also starts with one.
960 src
= start
+ *or_len
;
961 dest
= src
+ dir_len
;
966 if ((len
= dest
- or_name
) > PAXPATHLEN
) {
967 paxwarn(1, "File name %s/%s, too long", dir_name
, start
);
973 * enough space, shift
977 src
= dir_name
+ dir_len
- 1;
980 * splice in the destination directory name
982 while (src
>= dir_name
)
985 *(or_name
+ len
) = '\0';
991 * walk down the list of replacement strings applying each one in order.
992 * when we find one with a successful substitution, we modify the name
993 * as specified. if required, we print the results. if the resulting name
994 * is empty, we will skip this archive member. We use the regexp(3)
995 * routines (regexp() ought to win a prize as having the most cryptic
996 * library function manual page).
998 * name is the file name we are going to apply the regular expressions to
999 * (and may be modified)
1000 * nsize is the size of the name buffer.
1001 * nlen is the length of this name (and is modified to hold the length of
1002 * the final string).
1003 * prnt is a flag that says whether to print the final result.
1005 * 0 if substitution was successful, 1 if we are to skip the file (the name
1010 rep_name(char *name
, size_t nsize
, int *nlen
, int prnt
)
1019 regmatch_t pm
[MAXSUBEXP
];
1020 char nname
[PAXPATHLEN
+1]; /* final result of all replacements */
1021 char buf1
[PAXPATHLEN
+1]; /* where we work on the name */
1024 * copy the name into buf1, where we will work on it. We need to keep
1025 * the orig string around so we can print out the result of the final
1026 * replacement. We build up the final result in nname. inpt points at
1027 * the string we apply the regular expression to. prnt is used to
1028 * suppress printing when we handle replacements on the link field
1029 * (the user already saw that substitution go by)
1032 (void)strlcpy(buf1
, name
, sizeof(buf1
));
1035 endpt
= outpt
+ PAXPATHLEN
;
1038 * try each replacement string in order
1040 while (pt
!= NULL
) {
1044 * check for a successful substitution, if not go to
1045 * the next pattern, or cleanup if we were global
1047 if (regexec(&(pt
->rcmp
), inpt
, MAXSUBEXP
, pm
, 0) != 0)
1051 * ok we found one. We have three parts, the prefix
1052 * which did not match, the section that did and the
1053 * tail (that also did not match). Copy the prefix to
1054 * the final output buffer (watching to make sure we
1055 * do not create a string too long).
1058 rpt
= inpt
+ pm
[0].rm_so
;
1060 while ((inpt
< rpt
) && (outpt
< endpt
))
1066 * for the second part (which matched the regular
1067 * expression) apply the substitution using the
1068 * replacement string and place it the prefix in the
1069 * final output. If we have problems, skip it.
1071 if ((res
= resub(&(pt
->rcmp
),pm
,pt
->nstr
,oinpt
,outpt
,endpt
))
1074 paxwarn(1, "Replacement name error %s",
1081 * we set up to look again starting at the first
1082 * character in the tail (of the input string right
1083 * after the last character matched by the regular
1084 * expression (inpt always points at the first char in
1085 * the string to process). If we are not doing a global
1086 * substitution, we will use inpt to copy the tail to
1087 * the final result. Make sure we do not overrun the
1090 inpt
+= pm
[0].rm_eo
- pm
[0].rm_so
;
1092 if ((outpt
== endpt
) || (*inpt
== '\0'))
1096 * if the user wants global we keep trying to
1097 * substitute until it fails, then we are done.
1099 } while (pt
->flgs
& GLOB
);
1105 * a successful substitution did NOT occur, try the next one
1112 * we had a substitution, copy the last tail piece (if there is
1113 * room) to the final result
1115 while ((outpt
< endpt
) && (*inpt
!= '\0'))
1119 if ((outpt
== endpt
) && (*inpt
!= '\0')) {
1121 paxwarn(1,"Replacement name too long %s >> %s",
1127 * inform the user of the result if wanted
1129 if (prnt
&& (pt
->flgs
& PRNT
)) {
1131 (void)fprintf(stderr
,"%s >> <empty string>\n",
1134 (void)fprintf(stderr
,"%s >> %s\n", name
, nname
);
1138 * if empty inform the caller this file is to be skipped
1139 * otherwise copy the new name over the orig name and return
1143 *nlen
= strlcpy(name
, nname
, nsize
);
1150 * apply the replacement to the matched expression. expand out the old
1151 * style ed(1) subexpression expansion.
1153 * -1 if error, or the number of characters added to the destination.
1157 resub(regex_t
*rp
, regmatch_t
*pm
, char *src
, char *inpt
, char *dest
,
1169 subexcnt
= rp
->re_nsub
;
1170 while ((dpt
< destend
) && ((c
= *spt
++) != '\0')) {
1172 * see if we just have an ordinary replacement character
1173 * or we refer to a subexpression.
1177 } else if ((c
== '\\') && (*spt
>= '0') && (*spt
<= '9')) {
1179 * make sure there is a subexpression as specified
1181 if ((len
= *spt
++ - '0') > subexcnt
)
1186 * Ordinary character, just copy it
1188 if ((c
== '\\') && (*spt
!= '\0'))
1195 * continue if the subexpression is bogus
1197 if ((pmpt
->rm_so
< 0) || (pmpt
->rm_eo
< 0) ||
1198 ((len
= (int)(pmpt
->rm_eo
- pmpt
->rm_so
)) <= 0))
1202 * copy the subexpression to the destination.
1203 * fail if we run out of space or the match string is damaged
1205 if (len
> (destend
- dpt
))
1206 len
= destend
- dpt
;
1207 strncpy(dpt
, inpt
+ pmpt
->rm_so
, len
);