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