]> Dogcows Code - chaz/tar/blob - src/tar.c
*** empty log message ***
[chaz/tar] / src / tar.c
1 /* Tar -- a tape archiver.
2 Copyright (C) 1988, 1992 Free Software Foundation
3
4 This file is part of GNU Tar.
5
6 GNU Tar is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Tar is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Tar; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /*
21 * A tar (tape archiver) program.
22 *
23 * Written by John Gilmore, ihnp4!hoptoad!gnu, starting 25 Aug 85.
24 */
25
26 #include <stdio.h>
27 #include <sys/types.h> /* Needed for typedefs in tar.h */
28 #include "getopt.h"
29 #include "regex.h"
30
31 /*
32 * The following causes "tar.h" to produce definitions of all the
33 * global variables, rather than just "extern" declarations of them.
34 */
35 #define TAR_EXTERN /**/
36 #include "tar.h"
37
38 #include "port.h"
39
40 #if defined(_POSIX_VERSION) || defined(DIRENT)
41 #include <dirent.h>
42 #ifdef direct
43 #undef direct
44 #endif /* direct */
45 #define direct dirent
46 #define DP_NAMELEN(x) strlen((x)->d_name)
47 #endif /* _POSIX_VERSION or DIRENT */
48 #if !defined(_POSIX_VERSION) && !defined(DIRENT) && defined(BSD42)
49 #include <sys/dir.h>
50 #define DP_NAMELEN(x) (x)->d_namlen
51 #endif /* not _POSIX_VERSION and BSD42 */
52 #ifdef __MSDOS__
53 #include "msd_dir.h"
54 #define DP_NAMELEN(x) (x)->d_namlen
55 #define direct dirent
56 #endif
57 #if defined(USG) && !defined(_POSIX_VERSION) && !defined(DIRENT)
58 #include <ndir.h>
59 #define DP_NAMELEN(x) strlen((x)->d_name)
60 #endif /* USG and not _POSIX_VERSION and not DIRENT */
61
62 /*
63 * We should use a conversion routine that does reasonable error
64 * checking -- atoi doesn't. For now, punt. FIXME.
65 */
66 #define intconv atoi
67 PTR ck_malloc();
68 PTR ck_realloc();
69 extern int getoldopt();
70 extern void read_and();
71 extern void list_archive();
72 extern void extract_archive();
73 extern void diff_archive();
74 extern void create_archive();
75 extern void update_archive();
76 extern void junk_archive();
77
78 /* JF */
79 extern time_t get_date();
80
81 time_t new_time;
82
83 static FILE *namef; /* File to read names from */
84 static char **n_argv; /* Argv used by name routines */
85 static int n_argc; /* Argc used by name routines */
86 static char **n_ind; /* Store an array of names */
87 static int n_indalloc; /* How big is the array? */
88 static int n_indused; /* How many entries does it have? */
89 static int n_indscan; /* How many of the entries have we scanned? */
90
91
92 extern FILE *msg_file;
93
94 int check_exclude();
95 void add_exclude();
96 void add_exclude_file();
97 void addname();
98 void describe();
99 void diff_init();
100 void extr_init();
101 int is_regex();
102 void name_add();
103 void name_init();
104 void options();
105 char *un_quote_string();
106 int wildmat();
107
108 #ifndef S_ISLNK
109 #define lstat stat
110 #endif
111
112 #ifndef DEFBLOCKING
113 #define DEFBLOCKING 20
114 #endif
115
116 #ifndef DEF_AR_FILE
117 #define DEF_AR_FILE "tar.out"
118 #endif
119
120 /* For long options that unconditionally set a single flag, we have getopt
121 do it. For the others, we share the code for the equivalent short
122 named option, the name of which is stored in the otherwise-unused `val'
123 field of the `struct option'; for long options that have no equivalent
124 short option, we use nongraphic characters as pseudo short option
125 characters, starting (for no particular reason) with character 10. */
126
127 struct option long_options[] =
128 {
129 {"create", 0, 0, 'c'},
130 {"append", 0, 0, 'r'},
131 {"extract", 0, 0, 'x'},
132 {"get", 0, 0, 'x'},
133 {"list", 0, 0, 't'},
134 {"update", 0, 0, 'u'},
135 {"catenate", 0, 0, 'A'},
136 {"concatenate", 0, 0, 'A'},
137 {"compare", 0, 0, 'd'},
138 {"diff", 0, 0, 'd'},
139 {"delete", 0, 0, 14},
140 {"help", 0, 0, 12},
141
142 {"null", 0, 0, 16},
143 {"directory", 1, 0, 'C'},
144 {"record-number", 0, &f_sayblock, 1},
145 {"files-from", 1, 0, 'T'},
146 {"label", 1, 0, 'V'},
147 {"exclude-from", 1, 0, 'X'},
148 {"exclude", 1, 0, 15},
149 {"file", 1, 0, 'f'},
150 {"block-size", 1, 0, 'b'},
151 {"version", 0, 0, 11},
152 {"verbose", 0, 0, 'v'},
153 {"totals", 0, &f_totals, 1},
154
155 {"read-full-blocks", 0, &f_reblock, 1},
156 {"starting-file", 1, 0, 'K'},
157 {"to-stdout", 0, &f_exstdout, 1},
158 {"ignore-zeros", 0, &f_ignorez, 1},
159 {"keep-old-files", 0, 0, 'k'},
160 {"uncompress", 0, &f_compress, 1},
161 {"same-permissions", 0, &f_use_protection, 1},
162 {"preserve-permissions",0, &f_use_protection, 1},
163 {"modification-time", 0, &f_modified, 1},
164 {"preserve", 0, 0, 10},
165 {"same-order", 0, &f_sorted_names, 1},
166 {"same-owner", 0, &f_do_chown, 1},
167 {"preserve-order", 0, &f_sorted_names, 1},
168
169 {"newer", 1, 0, 'N'},
170 {"after-date", 1, 0, 'N'},
171 {"newer-mtime", 1, 0, 13},
172 {"incremental", 0, 0, 'G'},
173 {"listed-incremental", 1, 0, 'g'},
174 {"multi-volume", 0, &f_multivol, 1},
175 {"info-script", 1, 0, 'F'},
176 {"absolute-paths", 0, &f_absolute_paths, 1},
177 {"interactive", 0, &f_confirm, 1},
178 {"confirmation", 0, &f_confirm, 1},
179
180 {"verify", 0, &f_verify, 1},
181 {"dereference", 0, &f_follow_links, 1},
182 {"one-file-system", 0, &f_local_filesys, 1},
183 {"old-archive", 0, 0, 'o'},
184 {"portability", 0, 0, 'o'},
185 {"compress", 0, &f_compress, 1},
186 {"compress-block", 0, &f_compress, 2},
187 {"sparse", 0, &f_sparse_files, 1},
188 {"tape-length", 1, 0, 'L'},
189 {"remove-files", 0, &f_remove_files, 1},
190 {"ignore-failed-read", 0, &f_ignore_failed_read, 1},
191 {"checkpoint", 0, &f_checkpoint, 1},
192 {"show-omitted-dirs", 0, &f_show_omitted_dirs, 1},
193 {"volno-file", 1, 0, 17},
194 {"force-local", 0, &f_force_local, 1},
195 {"atime-preserve", 0, &f_atime_preserve, 1},
196
197 {0, 0, 0, 0}
198 };
199
200 /*
201 * Main routine for tar.
202 */
203 void
204 main(argc, argv)
205 int argc;
206 char **argv;
207 {
208 extern char version_string[];
209
210 tar = argv[0]; /* JF: was "tar" Set program name */
211 filename_terminator = '\n';
212 errors = 0;
213
214 options(argc, argv);
215
216 if(!n_argv)
217 name_init(argc, argv);
218
219 if (f_volno_file)
220 init_volume_number ();
221
222 switch(cmd_mode) {
223 case CMD_CAT:
224 case CMD_UPDATE:
225 case CMD_APPEND:
226 update_archive();
227 break;
228 case CMD_DELETE:
229 junk_archive();
230 break;
231 case CMD_CREATE:
232 create_archive();
233 if (f_totals)
234 fprintf (stderr, "Total bytes written: %d\n", tot_written);
235 break;
236 case CMD_EXTRACT:
237 if (f_volhdr) {
238 const char *err;
239 label_pattern = (struct re_pattern_buffer *)
240 ck_malloc (sizeof *label_pattern);
241 err = re_compile_pattern (f_volhdr, strlen (f_volhdr),
242 label_pattern);
243 if (err) {
244 fprintf (stderr,"Bad regular expression: %s\n",
245 err);
246 errors++;
247 break;
248 }
249
250 }
251 extr_init();
252 read_and(extract_archive);
253 break;
254 case CMD_LIST:
255 if (f_volhdr) {
256 const char *err;
257 label_pattern = (struct re_pattern_buffer *)
258 ck_malloc (sizeof *label_pattern);
259 err = re_compile_pattern (f_volhdr, strlen (f_volhdr),
260 label_pattern);
261 if (err) {
262 fprintf (stderr,"Bad regular expression: %s\n",
263 err);
264 errors++;
265 break;
266 }
267 }
268 read_and(list_archive);
269 #if 0
270 if (!errors)
271 errors = different;
272 #endif
273 break;
274 case CMD_DIFF:
275 diff_init();
276 read_and(diff_archive);
277 break;
278 case CMD_VERSION:
279 fprintf(stderr,"%s\n",version_string);
280 break;
281 case CMD_NONE:
282 msg("you must specify exactly one of the r, c, t, x, or d options\n");
283 fprintf(stderr,"For more information, type ``%s +help''.\n",tar);
284 exit(EX_ARGSBAD);
285 }
286 if (f_volno_file)
287 closeout_volume_number ();
288 exit(errors);
289 /* NOTREACHED */
290 }
291
292
293 /*
294 * Parse the options for tar.
295 */
296 void
297 options(argc, argv)
298 int argc;
299 char **argv;
300 {
301 register int c; /* Option letter */
302 int ind = -1;
303
304 /* Set default option values */
305 blocking = DEFBLOCKING; /* From Makefile */
306 ar_files = malloc (sizeof (char *) * 10);
307 ar_files_len = 10;
308 n_ar_files = 0;
309 cur_ar_file = 0;
310
311 /* Parse options */
312 while ((c = getoldopt(argc, argv,
313 "-01234567Ab:BcC:df:F:g:GhikK:lL:mMN:oOpPrRsStT:uvV:wWxX:zZ",
314 long_options, &ind)) != EOF) {
315 switch (c) {
316 case 0: /* long options that set a single flag */
317 break;
318 case 1:
319 /* File name or non-parsed option */
320 name_add(optarg);
321 break;
322 case 'C':
323 name_add("-C");
324 name_add(optarg);
325 break;
326 case 10: /* preserve */
327 f_use_protection = f_sorted_names = 1;
328 break;
329 case 11:
330 if(cmd_mode!=CMD_NONE)
331 goto badopt;
332 cmd_mode=CMD_VERSION;
333 break;
334 case 12: /* help */
335 printf("This is GNU tar, the tape archiving program.\n");
336 describe();
337 exit(1);
338 case 13:
339 f_new_files++;
340 goto get_newer;
341
342 case 14: /* Delete in the archive */
343 if(cmd_mode!=CMD_NONE)
344 goto badopt;
345 cmd_mode=CMD_DELETE;
346 break;
347
348 case 15:
349 f_exclude++;
350 add_exclude(optarg);
351 break;
352
353 case 16: /* -T reads null terminated filenames. */
354 filename_terminator = '\0';
355 break;
356
357 case 17:
358 f_volno_file = optarg;
359 break;
360
361 case 'g': /* We are making a GNU dump; save
362 directories at the beginning of
363 the archive, and include in each
364 directory its contents */
365 if(f_oldarch)
366 goto badopt;
367 f_gnudump++;
368 gnu_dumpfile=optarg;
369 break;
370
371
372 case '0':
373 case '1':
374 case '2':
375 case '3':
376 case '4':
377 case '5':
378 case '6':
379 case '7':
380 {
381 /* JF this'll have to be modified for other
382 systems, of course! */
383 int d,add;
384 static char buf[50];
385
386 d=getoldopt(argc,argv,"lmh");
387 #ifdef MAYBEDEF
388 sprintf(buf,"/dev/rmt/%d%c",c,d);
389 #else
390 #ifndef LOW_NUM
391 #define LOW_NUM 0
392 #define MID_NUM 8
393 #define HGH_NUM 16
394 #endif
395 if(d=='l') add=LOW_NUM;
396 else if(d=='m') add=MID_NUM;
397 else if(d=='h') add=HGH_NUM;
398 else goto badopt;
399
400 sprintf(buf,"/dev/rmt%d",add+c-'0');
401 #endif
402 if (n_ar_files == ar_files_len)
403 ar_files
404 = (sizeof (char *)
405 * (ar_files_len *= 2));
406 ar_files[n_ar_files++]=buf;
407 }
408 break;
409
410 case 'A': /* Arguments are tar files,
411 just cat them onto the end
412 of the archive. */
413 if(cmd_mode!=CMD_NONE)
414 goto badopt;
415 cmd_mode=CMD_CAT;
416 break;
417
418 case 'b': /* Set blocking factor */
419 blocking = intconv(optarg);
420 break;
421
422 case 'B': /* Try to reblock input */
423 f_reblock++; /* For reading 4.2BSD pipes */
424 break;
425
426 case 'c': /* Create an archive */
427 if(cmd_mode!=CMD_NONE)
428 goto badopt;
429 cmd_mode=CMD_CREATE;
430 break;
431
432 #if 0
433 case 'C':
434 if(chdir(optarg)<0)
435 msg_perror("Can't change directory to %d",optarg);
436 break;
437 #endif
438
439 case 'd': /* Find difference tape/disk */
440 if(cmd_mode!=CMD_NONE)
441 goto badopt;
442 cmd_mode=CMD_DIFF;
443 break;
444
445 case 'f': /* Use ar_file for the archive */
446 if (n_ar_files == ar_files_len)
447 ar_files
448 = (sizeof (char *)
449 * (ar_files_len *= 2));
450
451 ar_files[n_ar_files++] = optarg;
452 break;
453
454 case 'F':
455 /* Since -F is only useful with -M , make it implied */
456 f_run_script_at_end++; /* run this script at the end */
457 info_script = optarg; /* of each tape */
458 f_multivol++;
459 break;
460
461 case 'G': /* We are making a GNU dump; save
462 directories at the beginning of
463 the archive, and include in each
464 directory its contents */
465 if(f_oldarch)
466 goto badopt;
467 f_gnudump++;
468 gnu_dumpfile=0;
469 break;
470
471 case 'h':
472 f_follow_links++; /* follow symbolic links */
473 break;
474
475 case 'i':
476 f_ignorez++; /* Ignore zero records (eofs) */
477 /*
478 * This can't be the default, because Unix tar
479 * writes two records of zeros, then pads out the
480 * block with garbage.
481 */
482 break;
483
484 case 'k': /* Don't overwrite files */
485 #ifdef NO_OPEN3
486 msg("can't keep old files on this system");
487 exit(EX_ARGSBAD);
488 #else
489 f_keep++;
490 #endif
491 break;
492
493 case 'K':
494 f_startfile++;
495 addname(optarg);
496 break;
497
498 case 'l': /* When dumping directories, don't
499 dump files/subdirectories that are
500 on other filesystems. */
501 f_local_filesys++;
502 break;
503
504 case 'L':
505 tape_length = intconv (optarg);
506 f_multivol++;
507 break;
508 case 'm':
509 f_modified++;
510 break;
511
512 case 'M': /* Make Multivolume archive:
513 When we can't write any more
514 into the archive, re-open it,
515 and continue writing */
516 f_multivol++;
517 break;
518
519 case 'N': /* Only write files newer than X */
520 get_newer:
521 f_new_files++;
522 new_time=get_date(optarg, (PTR) 0);
523 if (new_time == (time_t) -1) {
524 msg("invalid date format `%s'", optarg);
525 exit(EX_ARGSBAD);
526 }
527 break;
528
529 case 'o': /* Generate old archive */
530 if(f_gnudump /* || f_dironly */)
531 goto badopt;
532 f_oldarch++;
533 break;
534
535 case 'O':
536 f_exstdout++;
537 break;
538
539 case 'p':
540 f_use_protection++;
541 break;
542
543 case 'P':
544 f_absolute_paths++;
545 break;
546
547 case 'r': /* Append files to the archive */
548 if(cmd_mode!=CMD_NONE)
549 goto badopt;
550 cmd_mode=CMD_APPEND;
551 break;
552
553 case 'R':
554 f_sayblock++; /* Print block #s for debug */
555 break; /* of bad tar archives */
556
557 case 's':
558 f_sorted_names++; /* Names to extr are sorted */
559 break;
560
561 case 'S': /* deal with sparse files */
562 f_sparse_files++;
563 break;
564 case 't':
565 if(cmd_mode!=CMD_NONE)
566 goto badopt;
567 cmd_mode=CMD_LIST;
568 f_verbose++; /* "t" output == "cv" or "xv" */
569 break;
570
571 case 'T':
572 name_file = optarg;
573 f_namefile++;
574 break;
575
576 case 'u': /* Append files to the archive that
577 aren't there, or are newer than the
578 copy in the archive */
579 if(cmd_mode!=CMD_NONE)
580 goto badopt;
581 cmd_mode=CMD_UPDATE;
582 break;
583
584 case 'v':
585 f_verbose++;
586 break;
587
588 case 'V':
589 f_volhdr=optarg;
590 break;
591
592 case 'w':
593 f_confirm++;
594 break;
595
596 case 'W':
597 f_verify++;
598 break;
599
600 case 'x': /* Extract files from the archive */
601 if(cmd_mode!=CMD_NONE)
602 goto badopt;
603 cmd_mode=CMD_EXTRACT;
604 break;
605
606 case 'X':
607 f_exclude++;
608 add_exclude_file(optarg);
609 break;
610
611 case 'z': /* Easy to type */
612 case 'Z': /* Like the filename extension .Z */
613 f_compress++;
614 break;
615
616 case '?':
617 badopt:
618 msg("Unknown option. Use '%s +help' for a complete list of options.", tar);
619 exit(EX_ARGSBAD);
620
621 }
622 }
623
624 blocksize = blocking * RECORDSIZE;
625 if (n_ar_files == 0)
626 {
627 n_ar_files = 1;
628 ar_files[0] = getenv("TAPE"); /* From environment, or */
629 if (ar_files[0] == 0)
630 ar_files[0] = DEF_AR_FILE; /* From Makefile */
631 }
632 if (n_ar_files > 1 && !f_multivol)
633 {
634 msg ("Multiple archive files requires --multi-volume\n");
635 exit (EX_ARGSBAD);
636 }
637 }
638
639
640 /*
641 * Print as much help as the user's gonna get.
642 *
643 * We have to sprinkle in the KLUDGE lines because too many compilers
644 * cannot handle character strings longer than about 512 bytes. Yuk!
645 * In particular, MS-DOS and Xenix MSC and PDP-11 V7 Unix have this
646 * problem.
647 */
648 void
649 describe()
650 {
651 puts("choose one of the following:");
652 fputs("\
653 -A, --catenate,\n\
654 --concatenate append tar files to an archive\n\
655 -c, --create create a new archive\n\
656 -d, --diff,\n\
657 --compare find differences between archive and file system\n\
658 --delete delete from the archive (not for use on mag tapes!)\n\
659 -r, --append append files to the end of an archive\n\
660 -t, --list list the contents of an archive\n\
661 -u, --update only append files that are newer than copy in archive\n\
662 -x, --extract,\n\
663 --get extract files from an archive\n",stdout);
664
665 fprintf(stdout, "\
666 Other options:\n\
667 --atime-preserve don't change access times on dumped files\n\
668 -b, --block-size N block size of Nx512 bytes (default N=%d)\n", DEFBLOCKING);
669 fputs ("\
670 -B, --read-full-blocks reblock as we read (for reading 4.2BSD pipes)\n\
671 -C, --directory DIR change to directory DIR\n\
672 --checkpoint print directory names while reading the archive\n\
673 ", stdout); /* KLUDGE */ fprintf(stdout, "\
674 -f, --file [HOSTNAME:]F use archive file or device F (default %s)\n",
675 DEF_AR_FILE); fputs("\
676 --force-local archive file is local even if has a colon\n\
677 -F, --info-script F run script at end of each tape (implies -M)\n\
678 -G, --incremental create/list/extract old GNU-format incremental backup\n\
679 -g, --listed-incremental F create/list/extract new GNU-format incremental backup\n\
680 -h, --dereference don't dump symlinks; dump the files they point to\n\
681 -i, --ignore-zeros ignore blocks of zeros in archive (normally mean EOF)\n\
682 --ignore-failed-read don't exit with non-zero status on unreadable files\n\
683 -k, --keep-old-files keep existing files; don't overwrite them from archive\n\
684 -K, --starting-file FILE begin at FILE in the archive\n\
685 -l, --one-file-system stay in local file system when creating an archive\n\
686 -L, --tape-length LENGTH change tapes after writing LENGTH\n\
687 ", stdout); /* KLUDGE */ fputs("\
688 -m, --modification-time don't extract file modified time\n\
689 -M, --multi-volume create/list/extract multi-volume archive\n\
690 -N, --after-date DATE,\n\
691 --newer DATE only store files newer than DATE\n\
692 -o, --old-archive,\n\
693 --portability write a V7 format archive, rather than ANSI format\n\
694 -O, --to-stdout extract files to standard output\n\
695 -p, --same-permissions,\n\
696 --preserve-permissions extract all protection information\n\
697 -P, --absolute-paths don't strip leading `/'s from file names\n\
698 --preserve like -p -s\n\
699 ", stdout); /* KLUDGE */ fputs("\
700 -R, --record-number show record number within archive with each message\n\
701 --remove-files remove files after adding them to the archive\n\
702 -s, --same-order,\n\
703 --preserve-order list of names to extract is sorted to match archive\n\
704 --same-owner create extracted files with the same ownership \n\
705 -S, --sparse handle sparse files efficiently\n\
706 -T, --files-from F get names to extract or create from file F\n\
707 --null -T reads null-terminated names, disable -C\n\
708 --totals print total bytes written with --create\n\
709 -v, --verbose verbosely list files processed\n\
710 -V, --label NAME create archive with volume name NAME\n\
711 --version print tar program version number\n\
712 -w, --interactive,\n\
713 --confirmation ask for confirmation for every action\n\
714 ", stdout); /* KLUDGE */ fputs("\
715 -W, --verify attempt to verify the archive after writing it\n\
716 --exclude FILE exclude file FILE\n\
717 -X, --exclude-from FILE exclude files listed in FILE\n\
718 -z, -Z, --compress,\n\
719 --uncompress filter the archive through compress\n\
720 -[0-7][lmh] specify drive and density\n\
721 ", stdout);
722 }
723
724 void
725 name_add(name)
726 char *name;
727 {
728 if(n_indalloc==n_indused) {
729 n_indalloc+=10;
730 n_ind=(char **)(n_indused ? ck_realloc(n_ind,n_indalloc*sizeof(char *)) : ck_malloc(n_indalloc*sizeof(char *)));
731 }
732 n_ind[n_indused++]=name;
733 }
734
735 /*
736 * Set up to gather file names for tar.
737 *
738 * They can either come from stdin or from argv.
739 */
740 void
741 name_init(argc, argv)
742 int argc;
743 char **argv;
744 {
745
746 if (f_namefile) {
747 if (optind < argc) {
748 msg("too many args with -T option");
749 exit(EX_ARGSBAD);
750 }
751 if (!strcmp(name_file, "-")) {
752 namef = stdin;
753 } else {
754 namef = fopen(name_file, "r");
755 if (namef == NULL) {
756 msg_perror("can't open file %s",name_file);
757 exit(EX_BADFILE);
758 }
759 }
760 } else {
761 /* Get file names from argv, after options. */
762 n_argc = argc;
763 n_argv = argv;
764 }
765 }
766
767 /* Read the next filename read from STREAM and null-terminate it.
768 Put it into BUFFER, reallocating and adjusting *PBUFFER_SIZE if necessary.
769 Return the new value for BUFFER, or NULL at end of file. */
770
771 char *
772 read_name_from_file (buffer, pbuffer_size, stream)
773 char *buffer;
774 size_t *pbuffer_size;
775 FILE *stream;
776 {
777 register int c;
778 register int indx = 0;
779 register size_t buffer_size = *pbuffer_size;
780
781 while ((c = getc (stream)) != EOF && c != filename_terminator)
782 {
783 if (indx == buffer_size)
784 {
785 buffer_size += NAMSIZ;
786 buffer = ck_realloc (buffer, buffer_size + 2);
787 }
788 buffer[indx++] = c;
789 }
790 if (indx == 0 && c == EOF)
791 return NULL;
792 if (indx == buffer_size)
793 {
794 buffer_size += NAMSIZ;
795 buffer = ck_realloc (buffer, buffer_size + 2);
796 }
797 buffer[indx] = '\0';
798 *pbuffer_size = buffer_size;
799 return buffer;
800 }
801
802 /*
803 * Get the next name from argv or the name file.
804 *
805 * Result is in static storage and can't be relied upon across two calls.
806 *
807 * If CHANGE_DIRS is non-zero, treat a filename of the form "-C" as
808 * meaning that the next filename is the name of a directory to change to.
809 * If `filename_terminator' is '\0', CHANGE_DIRS is effectively always 0.
810 */
811
812 char *
813 name_next(change_dirs)
814 int change_dirs;
815 {
816 static char *buffer; /* Holding pattern */
817 static int buffer_siz;
818 register char *p;
819 register char *q = 0;
820 register int next_name_is_dir = 0;
821 extern char *un_quote_string();
822
823 if(buffer_siz==0) {
824 buffer=ck_malloc(NAMSIZ+2);
825 buffer_siz=NAMSIZ;
826 }
827 if (filename_terminator == '\0')
828 change_dirs = 0;
829 tryagain:
830 if (namef == NULL) {
831 if(n_indscan<n_indused)
832 p=n_ind[n_indscan++];
833 else if (optind < n_argc)
834 /* Names come from argv, after options */
835 p=n_argv[optind++];
836 else {
837 if(q)
838 msg("Missing filename after -C");
839 return NULL;
840 }
841
842 /* JF trivial support for -C option. I don't know if
843 chdir'ing at this point is dangerous or not.
844 It seems to work, which is all I ask. */
845 if(change_dirs && !q && p[0]=='-' && p[1]=='C' && p[2]=='\0') {
846 q=p;
847 goto tryagain;
848 }
849 if(q) {
850 if(chdir(p)<0)
851 msg_perror("Can't chdir to %s",p);
852 q=0;
853 goto tryagain;
854 }
855 /* End of JF quick -C hack */
856
857 if(f_exclude && check_exclude(p))
858 goto tryagain;
859 return un_quote_string(p);
860 }
861 while (p = read_name_from_file (buffer, &buffer_siz, namef)) {
862 buffer = p;
863 if (*p == '\0')
864 continue; /* Ignore empty lines. */
865 q = p + strlen (p) - 1;
866 while (q > p && *q == '/') /* Zap trailing "/"s. */
867 *q-- = '\0';
868 if (change_dirs && next_name_is_dir == 0
869 && p[0] == '-' && p[1] == 'C' && p[2] == '\0') {
870 next_name_is_dir = 1;
871 goto tryagain;
872 }
873 if (next_name_is_dir) {
874 if (chdir (p) < 0)
875 msg_perror ("Can't change to directory %s", p);
876 next_name_is_dir = 0;
877 goto tryagain;
878 }
879 if(f_exclude && check_exclude(p))
880 goto tryagain;
881 return un_quote_string(p);
882 }
883 return NULL;
884 }
885
886
887 /*
888 * Close the name file, if any.
889 */
890 void
891 name_close()
892 {
893
894 if (namef != NULL && namef != stdin) fclose(namef);
895 }
896
897
898 /*
899 * Gather names in a list for scanning.
900 * Could hash them later if we really care.
901 *
902 * If the names are already sorted to match the archive, we just
903 * read them one by one. name_gather reads the first one, and it
904 * is called by name_match as appropriate to read the next ones.
905 * At EOF, the last name read is just left in the buffer.
906 * This option lets users of small machines extract an arbitrary
907 * number of files by doing "tar t" and editing down the list of files.
908 */
909 void
910 name_gather()
911 {
912 register char *p;
913 static struct name *namebuf; /* One-name buffer */
914 static namelen;
915 static char *chdir_name;
916
917 if (f_sorted_names) {
918 if(!namelen) {
919 namelen=NAMSIZ;
920 namebuf=(struct name *)ck_malloc(sizeof(struct name)+NAMSIZ);
921 }
922 p = name_next(0);
923 if (p) {
924 if(*p=='-' && p[1]=='C' && p[2]=='\0') {
925 chdir_name=name_next(0);
926 p=name_next(0);
927 if(!p) {
928 msg("Missing file name after -C");
929 exit(EX_ARGSBAD);
930 }
931 namebuf->change_dir=chdir_name;
932 }
933 namebuf->length = strlen(p);
934 if (namebuf->length >= namelen) {
935 namebuf=(struct name *)ck_realloc(namebuf,sizeof(struct name)+namebuf->length);
936 namelen=namebuf->length;
937 }
938 strncpy(namebuf->name, p, namebuf->length);
939 namebuf->name[ namebuf->length ] = 0;
940 namebuf->next = (struct name *)NULL;
941 namebuf->found = 0;
942 namelist = namebuf;
943 namelast = namelist;
944 }
945 return;
946 }
947
948 /* Non sorted names -- read them all in */
949 while (p = name_next(0))
950 addname(p);
951 }
952
953 /*
954 * Add a name to the namelist.
955 */
956 void
957 addname(name)
958 char *name; /* pointer to name */
959 {
960 register int i; /* Length of string */
961 register struct name *p; /* Current struct pointer */
962 static char *chdir_name;
963 char *new_name();
964
965 if(name[0]=='-' && name[1]=='C' && name[2]=='\0') {
966 chdir_name=name_next(0);
967 name=name_next(0);
968 if(!chdir_name) {
969 msg("Missing file name after -C");
970 exit(EX_ARGSBAD);
971 }
972 if(chdir_name[0]!='/') {
973 char *path = ck_malloc(PATH_MAX);
974 #if defined(__MSDOS__) || defined(USG) || defined(_POSIX_VERSION)
975 if(!getcwd(path,PATH_MAX))
976 msg("Couldn't get current directory.");
977 exit(EX_SYSTEM);
978 #else
979 char *getwd();
980
981 if(!getwd(path)) {
982 msg("Couldn't get current directory: %s",path);
983 exit(EX_SYSTEM);
984 }
985 #endif
986 chdir_name=new_name(path,chdir_name);
987 free(path);
988 }
989 }
990
991 if (name)
992 {
993 i = strlen(name);
994 /*NOSTRICT*/
995 p = (struct name *)malloc((unsigned)(sizeof(struct name) + i));
996 }
997 else
998 p = (struct name *)malloc ((unsigned)(sizeof (struct name)));
999 if (!p) {
1000 if (name)
1001 msg("cannot allocate mem for name '%s'.",name);
1002 else
1003 msg("cannot allocate mem for chdir record.");
1004 exit(EX_SYSTEM);
1005 }
1006 p->next = (struct name *)NULL;
1007 if (name)
1008 {
1009 p->fake = 0;
1010 p->length = i;
1011 strncpy(p->name, name, i);
1012 p->name[i] = '\0'; /* Null term */
1013 }
1014 else
1015 p->fake = 1;
1016 p->found = 0;
1017 p->regexp = 0; /* Assume not a regular expression */
1018 p->firstch = 1; /* Assume first char is literal */
1019 p->change_dir=chdir_name;
1020 p->dir_contents = 0; /* JF */
1021 if (name)
1022 {
1023 if (index(name, '*') || index(name, '[') || index(name, '?')) {
1024 p->regexp = 1; /* No, it's a regexp */
1025 if (name[0] == '*' || name[0] == '[' || name[0] == '?')
1026 p->firstch = 0; /* Not even 1st char literal */
1027 }
1028 }
1029
1030 if (namelast) namelast->next = p;
1031 namelast = p;
1032 if (!namelist) namelist = p;
1033 }
1034
1035 /*
1036 * Return nonzero if name P (from an archive) matches any name from
1037 * the namelist, zero if not.
1038 */
1039 int
1040 name_match(p)
1041 register char *p;
1042 {
1043 register struct name *nlp;
1044 register int len;
1045
1046 again:
1047 if (0 == (nlp = namelist)) /* Empty namelist is easy */
1048 return 1;
1049 if (nlp->fake)
1050 {
1051 if (nlp->change_dir && chdir (nlp->change_dir))
1052 msg_perror ("Can't change to directory %d", nlp->change_dir);
1053 namelist = 0;
1054 return 1;
1055 }
1056 len = strlen(p);
1057 for (; nlp != 0; nlp = nlp->next) {
1058 /* If first chars don't match, quick skip */
1059 if (nlp->firstch && nlp->name[0] != p[0])
1060 continue;
1061
1062 /* Regular expressions (shell globbing, actually). */
1063 if (nlp->regexp) {
1064 if (wildmat(p, nlp->name)) {
1065 nlp->found = 1; /* Remember it matched */
1066 if(f_startfile) {
1067 free((void *)namelist);
1068 namelist=0;
1069 }
1070 if(nlp->change_dir && chdir(nlp->change_dir))
1071 msg_perror("Can't change to directory %s",nlp->change_dir);
1072 return 1; /* We got a match */
1073 }
1074 continue;
1075 }
1076
1077 /* Plain Old Strings */
1078 if (nlp->length <= len /* Archive len >= specified */
1079 && (p[nlp->length] == '\0' || p[nlp->length] == '/')
1080 /* Full match on file/dirname */
1081 && strncmp(p, nlp->name, nlp->length) == 0) /* Name compare */
1082 {
1083 nlp->found = 1; /* Remember it matched */
1084 if(f_startfile) {
1085 free((void *)namelist);
1086 namelist = 0;
1087 }
1088 if(nlp->change_dir && chdir(nlp->change_dir))
1089 msg_perror("Can't change to directory %s",nlp->change_dir);
1090 return 1; /* We got a match */
1091 }
1092 }
1093
1094 /*
1095 * Filename from archive not found in namelist.
1096 * If we have the whole namelist here, just return 0.
1097 * Otherwise, read the next name in and compare it.
1098 * If this was the last name, namelist->found will remain on.
1099 * If not, we loop to compare the newly read name.
1100 */
1101 if (f_sorted_names && namelist->found) {
1102 name_gather(); /* Read one more */
1103 if (!namelist->found) goto again;
1104 }
1105 return 0;
1106 }
1107
1108
1109 /*
1110 * Print the names of things in the namelist that were not matched.
1111 */
1112 void
1113 names_notfound()
1114 {
1115 register struct name *nlp,*next;
1116 register char *p;
1117
1118 for (nlp = namelist; nlp != 0; nlp = next) {
1119 next=nlp->next;
1120 if (!nlp->found)
1121 msg("%s not found in archive",nlp->name);
1122
1123 /*
1124 * We could free() the list, but the process is about
1125 * to die anyway, so save some CPU time. Amigas and
1126 * other similarly broken software will need to waste
1127 * the time, though.
1128 */
1129 #ifdef amiga
1130 if (!f_sorted_names)
1131 free(nlp);
1132 #endif
1133 }
1134 namelist = (struct name *)NULL;
1135 namelast = (struct name *)NULL;
1136
1137 if (f_sorted_names) {
1138 while (0 != (p = name_next(1)))
1139 msg("%s not found in archive", p);
1140 }
1141 }
1142
1143 /* These next routines were created by JF */
1144
1145 void
1146 name_expand()
1147 {
1148 ;
1149 }
1150
1151 /* This is like name_match(), except that it returns a pointer to the name
1152 it matched, and doesn't set ->found The caller will have to do that
1153 if it wants to. Oh, and if the namelist is empty, it returns 0, unlike
1154 name_match(), which returns TRUE */
1155
1156 struct name *
1157 name_scan(p)
1158 register char *p;
1159 {
1160 register struct name *nlp;
1161 register int len;
1162
1163 again:
1164 if (0 == (nlp = namelist)) /* Empty namelist is easy */
1165 return 0;
1166 len = strlen(p);
1167 for (; nlp != 0; nlp = nlp->next) {
1168 /* If first chars don't match, quick skip */
1169 if (nlp->firstch && nlp->name[0] != p[0])
1170 continue;
1171
1172 /* Regular expressions */
1173 if (nlp->regexp) {
1174 if (wildmat(p, nlp->name))
1175 return nlp; /* We got a match */
1176 continue;
1177 }
1178
1179 /* Plain Old Strings */
1180 if (nlp->length <= len /* Archive len >= specified */
1181 && (p[nlp->length] == '\0' || p[nlp->length] == '/')
1182 /* Full match on file/dirname */
1183 && strncmp(p, nlp->name, nlp->length) == 0) /* Name compare */
1184 return nlp; /* We got a match */
1185 }
1186
1187 /*
1188 * Filename from archive not found in namelist.
1189 * If we have the whole namelist here, just return 0.
1190 * Otherwise, read the next name in and compare it.
1191 * If this was the last name, namelist->found will remain on.
1192 * If not, we loop to compare the newly read name.
1193 */
1194 if (f_sorted_names && namelist->found) {
1195 name_gather(); /* Read one more */
1196 if (!namelist->found) goto again;
1197 }
1198 return (struct name *) 0;
1199 }
1200
1201 /* This returns a name from the namelist which doesn't have ->found set.
1202 It sets ->found before returning, so successive calls will find and return
1203 all the non-found names in the namelist */
1204
1205 struct name *gnu_list_name;
1206
1207 char *
1208 name_from_list()
1209 {
1210 if(!gnu_list_name)
1211 gnu_list_name = namelist;
1212 while(gnu_list_name && gnu_list_name->found)
1213 gnu_list_name=gnu_list_name->next;
1214 if(gnu_list_name) {
1215 gnu_list_name->found++;
1216 if(gnu_list_name->change_dir)
1217 if(chdir(gnu_list_name->change_dir)<0)
1218 msg_perror("can't chdir to %s",gnu_list_name->change_dir);
1219 return gnu_list_name->name;
1220 }
1221 return (char *)0;
1222 }
1223
1224 void
1225 blank_name_list()
1226 {
1227 struct name *n;
1228
1229 gnu_list_name = 0;
1230 for(n=namelist;n;n=n->next)
1231 n->found = 0;
1232 }
1233
1234 char *
1235 new_name(path,name)
1236 char *path,*name;
1237 {
1238 char *path_buf;
1239
1240 path_buf=(char *)malloc(strlen(path)+strlen(name)+2);
1241 if(path_buf==0) {
1242 msg("Can't allocate memory for name '%s/%s",path,name);
1243 exit(EX_SYSTEM);
1244 }
1245 (void) sprintf(path_buf,"%s/%s",path,name);
1246 return path_buf;
1247 }
1248
1249 /* returns non-zero if the luser typed 'y' or 'Y', zero otherwise. */
1250
1251 int
1252 confirm(action,file)
1253 char *action, *file;
1254 {
1255 int c,nl;
1256 static FILE *confirm_file = 0;
1257 extern FILE *msg_file;
1258 extern char TTY_NAME[];
1259
1260 fprintf(msg_file,"%s %s?", action, file);
1261 fflush(msg_file);
1262 if(!confirm_file) {
1263 confirm_file = (archive == 0) ? fopen(TTY_NAME, "r") : stdin;
1264 if(!confirm_file) {
1265 msg("Can't read confirmation from user");
1266 exit(EX_SYSTEM);
1267 }
1268 }
1269 c=getc(confirm_file);
1270 for(nl = c; nl != '\n' && nl != EOF; nl = getc(confirm_file))
1271 ;
1272 return (c=='y' || c=='Y');
1273 }
1274
1275 char *x_buffer = 0;
1276 int size_x_buffer;
1277 int free_x_buffer;
1278
1279 char **exclude = 0;
1280 int size_exclude = 0;
1281 int free_exclude = 0;
1282
1283 char **re_exclude = 0;
1284 int size_re_exclude = 0;
1285 int free_re_exclude = 0;
1286
1287 void
1288 add_exclude(name)
1289 char *name;
1290 {
1291 /* char *rname;*/
1292 /* char **tmp_ptr;*/
1293 int size_buf;
1294
1295 un_quote_string(name);
1296 size_buf = strlen(name);
1297
1298 if(x_buffer==0) {
1299 x_buffer = (char *)ck_malloc(size_buf+1024);
1300 free_x_buffer=1024;
1301 } else if(free_x_buffer<=size_buf) {
1302 char *old_x_buffer;
1303 char **tmp_ptr;
1304
1305 old_x_buffer = x_buffer;
1306 x_buffer = (char *)ck_realloc(x_buffer,size_x_buffer+1024);
1307 free_x_buffer = 1024;
1308 for(tmp_ptr=exclude;tmp_ptr<exclude+size_exclude;tmp_ptr++)
1309 *tmp_ptr= x_buffer + ((*tmp_ptr) - old_x_buffer);
1310 for(tmp_ptr=re_exclude;tmp_ptr<re_exclude+size_re_exclude;tmp_ptr++)
1311 *tmp_ptr= x_buffer + ((*tmp_ptr) - old_x_buffer);
1312 }
1313
1314 if(is_regex(name)) {
1315 if(free_re_exclude==0) {
1316 re_exclude= (char **)(re_exclude ? ck_realloc(re_exclude,(size_re_exclude+32)*sizeof(char *)) : ck_malloc(sizeof(char *)*32));
1317 free_re_exclude+=32;
1318 }
1319 re_exclude[size_re_exclude]=x_buffer+size_x_buffer;
1320 size_re_exclude++;
1321 free_re_exclude--;
1322 } else {
1323 if(free_exclude==0) {
1324 exclude=(char **)(exclude ? ck_realloc(exclude,(size_exclude+32)*sizeof(char *)) : ck_malloc(sizeof(char *)*32));
1325 free_exclude+=32;
1326 }
1327 exclude[size_exclude]=x_buffer+size_x_buffer;
1328 size_exclude++;
1329 free_exclude--;
1330 }
1331 strcpy(x_buffer+size_x_buffer,name);
1332 size_x_buffer+=size_buf+1;
1333 free_x_buffer-=size_buf+1;
1334 }
1335
1336 void
1337 add_exclude_file(file)
1338 char *file;
1339 {
1340 FILE *fp;
1341 char buf[1024];
1342 extern char *rindex();
1343
1344 if(strcmp(file, "-"))
1345 fp=fopen(file,"r");
1346 else
1347 /* Let's hope the person knows what they're doing. */
1348 /* Using -X - -T - -f - will get you *REALLY* strange
1349 results. . . */
1350 fp=stdin;
1351
1352 if(!fp) {
1353 msg_perror("can't open %s",file);
1354 exit(2);
1355 }
1356 while(fgets(buf,1024,fp)) {
1357 /* int size_buf;*/
1358 char *end_str;
1359
1360 end_str=rindex(buf,'\n');
1361 if(end_str)
1362 *end_str='\0';
1363 add_exclude(buf);
1364
1365 }
1366 fclose(fp);
1367 }
1368
1369 int
1370 is_regex(str)
1371 char *str;
1372 {
1373 return index(str,'*') || index(str,'[') || index(str,'?');
1374 }
1375
1376 /* Returns non-zero if the file 'name' should not be added/extracted */
1377 int
1378 check_exclude(name)
1379 char *name;
1380 {
1381 int n;
1382 char *str;
1383 extern char *strstr();
1384
1385 for(n=0;n<size_re_exclude;n++) {
1386 if(wildmat(name,re_exclude[n]))
1387 return 1;
1388 }
1389 for(n=0;n<size_exclude;n++) {
1390 /* Accept the output from strstr only if it is the last
1391 part of the string. There is certainly a faster way to
1392 do this. . . */
1393 if( (str=strstr(name,exclude[n]))
1394 && (str==name || str[-1]=='/')
1395 && str[strlen(exclude[n])]=='\0')
1396 return 1;
1397 }
1398 return 0;
1399 }
This page took 0.09289 seconds and 5 git commands to generate.