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