]>
Dogcows Code - chaz/tar/blob - src/tar.c
1 /* Tar -- a tape archiver.
2 Copyright (C) 1988, 1992, 1993 Free Software Foundation
4 This file is part of GNU Tar.
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)
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.
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. */
21 * A tar (tape archiver) program.
23 * Written by John Gilmore, ihnp4!hoptoad!gnu, starting 25 Aug 85.
27 #include <sys/types.h> /* Needed for typedefs in tar.h */
32 * The following causes "tar.h" to produce definitions of all the
33 * global variables, rather than just "extern" declarations of them.
35 #define TAR_EXTERN /**/
42 * We should use a conversion routine that does reasonable error
43 * checking -- atoi doesn't. For now, punt. FIXME.
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 ();
60 extern time_t get_date ();
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? */
73 extern FILE *msg_file
;
77 void add_exclude_file ();
86 char *un_quote_string ();
93 #define DEFBLOCKING 20
97 #define DEF_AR_FILE "tar.out"
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. */
107 struct option long_options
[] =
109 {"create", 0, 0, 'c'},
110 {"append", 0, 0, 'r'},
111 {"extract", 0, 0, 'x'},
114 {"update", 0, 0, 'u'},
115 {"catenate", 0, 0, 'A'},
116 {"concatenate", 0, 0, 'A'},
117 {"compare", 0, 0, 'd'},
119 {"delete", 0, 0, 14},
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},
130 {"block-size", 1, 0, 'b'},
131 {"version", 0, 0, 11},
132 {"verbose", 0, 0, 'v'},
133 {"totals", 0, &f_totals
, 1},
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 {"same-permissions", 0, &f_use_protection
, 1},
141 {"preserve-permissions", 0, &f_use_protection
, 1},
142 {"modification-time", 0, &f_modified
, 1},
143 {"preserve", 0, 0, 10},
144 {"same-order", 0, &f_sorted_names
, 1},
145 {"same-owner", 0, &f_do_chown
, 1},
146 {"preserve-order", 0, &f_sorted_names
, 1},
148 {"newer", 1, 0, 'N'},
149 {"after-date", 1, 0, 'N'},
150 {"newer-mtime", 1, 0, 13},
151 {"incremental", 0, 0, 'G'},
152 {"listed-incremental", 1, 0, 'g'},
153 {"multi-volume", 0, &f_multivol
, 1},
154 {"info-script", 1, 0, 'F'},
155 {"new-volume-script", 1, 0, 'F'},
156 {"absolute-paths", 0, &f_absolute_paths
, 1},
157 {"interactive", 0, &f_confirm
, 1},
158 {"confirmation", 0, &f_confirm
, 1},
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, 0, 'Z'},
166 {"uncompress", 0, 0, 'Z'},
167 {"block-compress", 0, &f_compress_block
, 1},
169 {"ungzip", 0, 0, 'z'},
170 {"use-compress-program", 1, 0, 18},
173 {"same-permissions", 0, &f_use_protection
, 1},
174 {"sparse", 0, &f_sparse_files
, 1},
175 {"tape-length", 1, 0, 'L'},
176 {"remove-files", 0, &f_remove_files
, 1},
177 {"ignore-failed-read", 0, &f_ignore_failed_read
, 1},
178 {"checkpoint", 0, &f_checkpoint
, 1},
179 {"show-omitted-dirs", 0, &f_show_omitted_dirs
, 1},
180 {"volno-file", 1, 0, 17},
181 {"force-local", 0, &f_force_local
, 1},
182 {"atime-preserve", 0, &f_atime_preserve
, 1},
188 * Main routine for tar.
195 extern char version_string
[];
197 tar
= argv
[0]; /* JF: was "tar" Set program name */
198 filename_terminator
= '\n';
201 options (argc
, argv
);
204 name_init (argc
, argv
);
207 init_volume_number ();
222 fprintf (stderr
, "Total bytes written: %d\n", tot_written
);
228 label_pattern
= (struct re_pattern_buffer
*)
229 ck_malloc (sizeof *label_pattern
);
230 err
= re_compile_pattern (f_volhdr
, strlen (f_volhdr
),
234 fprintf (stderr
, "Bad regular expression: %s\n",
242 read_and (extract_archive
);
248 label_pattern
= (struct re_pattern_buffer
*)
249 ck_malloc (sizeof *label_pattern
);
250 err
= re_compile_pattern (f_volhdr
, strlen (f_volhdr
),
254 fprintf (stderr
, "Bad regular expression: %s\n",
260 read_and (list_archive
);
268 read_and (diff_archive
);
271 fprintf (stderr
, "%s\n", version_string
);
274 msg ("you must specify exactly one of the r, c, t, x, or d options\n");
275 fprintf (stderr
, "For more information, type ``%s --help''.\n", tar
);
279 closeout_volume_number ();
286 * Parse the options for tar.
293 register int c
; /* Option letter */
296 /* Set default option values */
297 blocking
= DEFBLOCKING
; /* From Makefile */
298 ar_files
= (char **) ck_malloc (sizeof (char *) * 10);
304 while ((c
= getoldopt (argc
, argv
,
305 "-01234567Ab:BcC:df:F:g:GhikK:lL:mMN:oOpPrRsStT:uvV:wWxX:zZ",
306 long_options
, &ind
)) != EOF
)
310 case 0: /* long options that set a single flag */
313 /* File name or non-parsed option */
320 case 10: /* preserve */
321 f_use_protection
= f_sorted_names
= 1;
324 if (cmd_mode
!= CMD_NONE
)
326 cmd_mode
= CMD_VERSION
;
329 printf ("This is GNU tar, the tape archiving program.\n");
336 case 14: /* Delete in the archive */
337 if (cmd_mode
!= CMD_NONE
)
339 cmd_mode
= CMD_DELETE
;
344 add_exclude (optarg
);
347 case 16: /* -T reads null terminated filenames. */
348 filename_terminator
= '\0';
352 f_volno_file
= optarg
;
358 msg ("Only one compression option permitted\n");
361 f_compressprog
= optarg
;
364 case 'g': /* We are making a GNU dump; save
365 directories at the beginning of
366 the archive, and include in each
367 directory its contents */
371 gnu_dumpfile
= optarg
;
384 /* JF this'll have to be modified for other
385 systems, of course! */
389 d
= getoldopt (argc
, argv
, "lmh");
391 sprintf (buf
, "/dev/rmt/%d%c", c
, d
);
407 sprintf (buf
, "/dev/rmt%d", add
+ c
- '0');
409 if (n_ar_files
== ar_files_len
)
412 ck_malloc (sizeof (char *)
413 * (ar_files_len
*= 2));
414 ar_files
[n_ar_files
++] = buf
;
418 case 'A': /* Arguments are tar files,
419 just cat them onto the end
421 if (cmd_mode
!= CMD_NONE
)
426 case 'b': /* Set blocking factor */
427 blocking
= intconv (optarg
);
430 case 'B': /* Try to reblock input */
431 f_reblock
++; /* For reading 4.2BSD pipes */
434 case 'c': /* Create an archive */
435 if (cmd_mode
!= CMD_NONE
)
437 cmd_mode
= CMD_CREATE
;
442 if (chdir (optarg
) < 0)
443 msg_perror ("Can't change directory to %d", optarg
);
447 case 'd': /* Find difference tape/disk */
448 if (cmd_mode
!= CMD_NONE
)
453 case 'f': /* Use ar_file for the archive */
454 if (n_ar_files
== ar_files_len
)
456 = (char **) ck_malloc (sizeof (char *)
457 * (ar_files_len
*= 2));
459 ar_files
[n_ar_files
++] = optarg
;
463 /* Since -F is only useful with -M , make it implied */
464 f_run_script_at_end
++;/* run this script at the end */
465 info_script
= optarg
; /* of each tape */
469 case 'G': /* We are making a GNU dump; save
470 directories at the beginning of
471 the archive, and include in each
472 directory its contents */
480 f_follow_links
++; /* follow symbolic links */
484 f_ignorez
++; /* Ignore zero records (eofs) */
486 * This can't be the default, because Unix tar
487 * writes two records of zeros, then pads out the
488 * block with garbage.
492 case 'k': /* Don't overwrite files */
494 msg ("can't keep old files on this system");
506 case 'l': /* When dumping directories, don't
507 dump files/subdirectories that are
508 on other filesystems. */
513 tape_length
= intconv (optarg
);
520 case 'M': /* Make Multivolume archive:
521 When we can't write any more
522 into the archive, re-open it,
523 and continue writing */
527 case 'N': /* Only write files newer than X */
530 new_time
= get_date (optarg
, (PTR
) 0);
531 if (new_time
== (time_t) - 1)
533 msg ("invalid date format `%s'", optarg
);
538 case 'o': /* Generate old archive */
539 if (f_gnudump
/* || f_dironly */ )
556 case 'r': /* Append files to the archive */
557 if (cmd_mode
!= CMD_NONE
)
559 cmd_mode
= CMD_APPEND
;
563 f_sayblock
++; /* Print block #s for debug */
564 break; /* of bad tar archives */
567 f_sorted_names
++; /* Names to extr are sorted */
570 case 'S': /* deal with sparse files */
574 if (cmd_mode
!= CMD_NONE
)
577 f_verbose
++; /* "t" output == "cv" or "xv" */
585 case 'u': /* Append files to the archive that
586 aren't there, or are newer than the
587 copy in the archive */
588 if (cmd_mode
!= CMD_NONE
)
590 cmd_mode
= CMD_UPDATE
;
609 case 'x': /* Extract files from the archive */
610 if (cmd_mode
!= CMD_NONE
)
612 cmd_mode
= CMD_EXTRACT
;
617 add_exclude_file (optarg
);
623 msg ("Only one compression option permitted\n");
626 f_compressprog
= "gzip";
632 msg ("Only one compression option permitted\n");
635 f_compressprog
= "compress";
640 msg ("Unknown option. Use '%s --help' for a complete list of options.", tar
);
646 blocksize
= blocking
* RECORDSIZE
;
650 ar_files
[0] = getenv ("TAPE"); /* From environment, or */
651 if (ar_files
[0] == 0)
652 ar_files
[0] = DEF_AR_FILE
; /* From Makefile */
654 if (n_ar_files
> 1 && !f_multivol
)
656 msg ("Multiple archive files requires --multi-volume\n");
659 if (f_compress_block
&& !f_compressprog
)
661 msg ("You must use a compression option (--gzip, --compress\n\
662 or --use-compress-program) with --block-compress.\n");
669 * Print as much help as the user's gonna get.
671 * We have to sprinkle in the KLUDGE lines because too many compilers
672 * cannot handle character strings longer than about 512 bytes. Yuk!
673 * In particular, MS-DOS and Xenix MSC and PDP-11 V7 Unix have this
679 puts ("choose one of the following:");
682 --concatenate append tar files to an archive\n\
683 -c, --create create a new archive\n\
685 --compare find differences between archive and file system\n\
686 --delete delete from the archive (not for use on mag tapes!)\n\
687 -r, --append append files to the end of an archive\n\
688 -t, --list list the contents of an archive\n\
689 -u, --update only append files that are newer than copy in archive\n\
691 --get extract files from an archive\n", stdout
);
695 --atime-preserve don't change access times on dumped files\n\
696 -b, --block-size N block size of Nx512 bytes (default N=%d)\n", DEFBLOCKING
);
698 -B, --read-full-blocks reblock as we read (for reading 4.2BSD pipes)\n\
699 -C, --directory DIR change to directory DIR\n\
700 --checkpoint print directory names while reading the archive\n\
701 ", stdout
); /* KLUDGE */
703 -f, --file [HOSTNAME:]F use archive file or device F (default %s)\n",
706 --force-local archive file is local even if has a colon\n\
707 -F, --info-script F\n\
708 --new-volume-script F run script at end of each tape (implies -M)\n\
709 -G, --incremental create/list/extract old GNU-format incremental backup\n\
710 -g, --listed-incremental F create/list/extract new GNU-format incremental backup\n\
711 -h, --dereference don't dump symlinks; dump the files they point to\n\
712 -i, --ignore-zeros ignore blocks of zeros in archive (normally mean EOF)\n\
713 --ignore-failed-read don't exit with non-zero status on unreadable files\n\
714 -k, --keep-old-files keep existing files; don't overwrite them from archive\n\
715 -K, --starting-file F begin at file F in the archive\n\
716 -l, --one-file-system stay in local file system when creating an archive\n\
717 -L, --tape-length N change tapes after writing N*1024 bytes\n\
718 ", stdout
); /* KLUDGE */
720 -m, --modification-time don't extract file modified time\n\
721 -M, --multi-volume create/list/extract multi-volume archive\n\
722 -N, --after-date DATE,\n\
723 --newer DATE only store files newer than DATE\n\
724 -o, --old-archive,\n\
725 --portability write a V7 format archive, rather than ANSI format\n\
726 -O, --to-stdout extract files to standard output\n\
727 -p, --same-permissions,\n\
728 --preserve-permissions extract all protection information\n\
729 -P, --absolute-paths don't strip leading `/'s from file names\n\
730 --preserve like -p -s\n\
731 ", stdout
); /* KLUDGE */
733 -R, --record-number show record number within archive with each message\n\
734 --remove-files remove files after adding them to the archive\n\
736 --preserve-order list of names to extract is sorted to match archive\n\
737 --same-owner create extracted files with the same ownership \n\
738 -S, --sparse handle sparse files efficiently\n\
739 -T, --files-from F get names to extract or create from file F\n\
740 --null -T reads null-terminated names, disable -C\n\
741 --totals print total bytes written with --create\n\
742 -v, --verbose verbosely list files processed\n\
743 -V, --label NAME create archive with volume name NAME\n\
744 --version print tar program version number\n\
745 -w, --interactive,\n\
746 --confirmation ask for confirmation for every action\n\
747 ", stdout
); /* KLUDGE */
749 -W, --verify attempt to verify the archive after writing it\n\
750 --exclude FILE exclude file FILE\n\
751 -X, --exclude-from FILE exclude files listed in FILE\n\
753 --uncompress filter the archive through compress\n\
755 --ungzip filter the archive through gzip\n\
756 --use-compress-program PROG\n\
757 filter the archive through PROG (which must accept -d)\n\
758 --block-compress block the output of compression program for tapes\n\
759 -[0-7][lmh] specify drive and density\n\
767 if (n_indalloc
== n_indused
)
770 n_ind
= (char **) (n_indused
? ck_realloc (n_ind
, n_indalloc
* sizeof (char *)): ck_malloc (n_indalloc
* sizeof (char *)));
772 n_ind
[n_indused
++] = name
;
776 * Set up to gather file names for tar.
778 * They can either come from stdin or from argv.
781 name_init (argc
, argv
)
790 msg ("too many args with -T option");
793 if (!strcmp (name_file
, "-"))
799 namef
= fopen (name_file
, "r");
802 msg_perror ("can't open file %s", name_file
);
809 /* Get file names from argv, after options. */
815 /* Read the next filename read from STREAM and null-terminate it.
816 Put it into BUFFER, reallocating and adjusting *PBUFFER_SIZE if necessary.
817 Return the new value for BUFFER, or NULL at end of file. */
820 read_name_from_file (buffer
, pbuffer_size
, stream
)
822 size_t *pbuffer_size
;
826 register int indx
= 0;
827 register size_t buffer_size
= *pbuffer_size
;
829 while ((c
= getc (stream
)) != EOF
&& c
!= filename_terminator
)
831 if (indx
== buffer_size
)
833 buffer_size
+= NAMSIZ
;
834 buffer
= ck_realloc (buffer
, buffer_size
+ 2);
838 if (indx
== 0 && c
== EOF
)
840 if (indx
== buffer_size
)
842 buffer_size
+= NAMSIZ
;
843 buffer
= ck_realloc (buffer
, buffer_size
+ 2);
846 *pbuffer_size
= buffer_size
;
851 * Get the next name from argv or the name file.
853 * Result is in static storage and can't be relied upon across two calls.
855 * If CHANGE_DIRS is non-zero, treat a filename of the form "-C" as
856 * meaning that the next filename is the name of a directory to change to.
857 * If `filename_terminator' is '\0', CHANGE_DIRS is effectively always 0.
861 name_next (change_dirs
)
864 static char *buffer
; /* Holding pattern */
865 static int buffer_siz
;
867 register char *q
= 0;
868 register int next_name_is_dir
= 0;
869 extern char *un_quote_string ();
873 buffer
= ck_malloc (NAMSIZ
+ 2);
876 if (filename_terminator
== '\0')
881 if (n_indscan
< n_indused
)
882 p
= n_ind
[n_indscan
++];
883 else if (optind
< n_argc
)
884 /* Names come from argv, after options */
885 p
= n_argv
[optind
++];
889 msg ("Missing filename after -C");
893 /* JF trivial support for -C option. I don't know if
894 chdir'ing at this point is dangerous or not.
895 It seems to work, which is all I ask. */
896 if (change_dirs
&& !q
&& p
[0] == '-' && p
[1] == 'C' && p
[2] == '\0')
904 msg_perror ("Can't chdir to %s", p
);
908 /* End of JF quick -C hack */
911 if (f_exclude
&& check_exclude (p
))
914 return un_quote_string (p
);
916 while (p
= read_name_from_file (buffer
, &buffer_siz
, namef
))
920 continue; /* Ignore empty lines. */
921 q
= p
+ strlen (p
) - 1;
922 while (q
> p
&& *q
== '/')/* Zap trailing "/"s. */
924 if (change_dirs
&& next_name_is_dir
== 0
925 && p
[0] == '-' && p
[1] == 'C' && p
[2] == '\0')
927 next_name_is_dir
= 1;
930 if (next_name_is_dir
)
933 msg_perror ("Can't change to directory %s", p
);
934 next_name_is_dir
= 0;
938 if (f_exclude
&& check_exclude (p
))
941 return un_quote_string (p
);
948 * Close the name file, if any.
954 if (namef
!= NULL
&& namef
!= stdin
)
960 * Gather names in a list for scanning.
961 * Could hash them later if we really care.
963 * If the names are already sorted to match the archive, we just
964 * read them one by one. name_gather reads the first one, and it
965 * is called by name_match as appropriate to read the next ones.
966 * At EOF, the last name read is just left in the buffer.
967 * This option lets users of small machines extract an arbitrary
968 * number of files by doing "tar t" and editing down the list of files.
974 static struct name
*namebuf
; /* One-name buffer */
976 static char *chdir_name
;
983 namebuf
= (struct name
*) ck_malloc (sizeof (struct name
) + NAMSIZ
);
988 if (*p
== '-' && p
[1] == 'C' && p
[2] == '\0')
990 chdir_name
= name_next (0);
994 msg ("Missing file name after -C");
997 namebuf
->change_dir
= chdir_name
;
999 namebuf
->length
= strlen (p
);
1000 if (namebuf
->length
>= namelen
)
1002 namebuf
= (struct name
*) ck_realloc (namebuf
, sizeof (struct name
) + namebuf
->length
);
1003 namelen
= namebuf
->length
;
1005 strncpy (namebuf
->name
, p
, namebuf
->length
);
1006 namebuf
->name
[namebuf
->length
] = 0;
1007 namebuf
->next
= (struct name
*) NULL
;
1010 namelast
= namelist
;
1015 /* Non sorted names -- read them all in */
1016 while (p
= name_next (0))
1021 * Add a name to the namelist.
1025 char *name
; /* pointer to name */
1027 register int i
; /* Length of string */
1028 register struct name
*p
; /* Current struct pointer */
1029 static char *chdir_name
;
1032 if (name
[0] == '-' && name
[1] == 'C' && name
[2] == '\0')
1034 chdir_name
= name_next (0);
1035 name
= name_next (0);
1038 msg ("Missing file name after -C");
1041 if (chdir_name
[0] != '/')
1043 char *path
= ck_malloc (PATH_MAX
);
1044 #if defined(__MSDOS__) || defined(HAVE_GETCWD) || defined(_POSIX_VERSION)
1045 if (!getcwd (path
, PATH_MAX
))
1047 msg ("Couldn't get current directory.");
1055 msg ("Couldn't get current directory: %s", path
);
1059 chdir_name
= new_name (path
, chdir_name
);
1068 p
= (struct name
*) malloc ((unsigned) (sizeof (struct name
) + i
));
1071 p
= (struct name
*) malloc ((unsigned) (sizeof (struct name
)));
1075 msg ("cannot allocate mem for name '%s'.", name
);
1077 msg ("cannot allocate mem for chdir record.");
1080 p
->next
= (struct name
*) NULL
;
1085 strncpy (p
->name
, name
, i
);
1086 p
->name
[i
] = '\0'; /* Null term */
1091 p
->regexp
= 0; /* Assume not a regular expression */
1092 p
->firstch
= 1; /* Assume first char is literal */
1093 p
->change_dir
= chdir_name
;
1094 p
->dir_contents
= 0; /* JF */
1097 if (index (name
, '*') || index (name
, '[') || index (name
, '?'))
1099 p
->regexp
= 1; /* No, it's a regexp */
1100 if (name
[0] == '*' || name
[0] == '[' || name
[0] == '?')
1101 p
->firstch
= 0; /* Not even 1st char literal */
1113 * Return nonzero if name P (from an archive) matches any name from
1114 * the namelist, zero if not.
1120 register struct name
*nlp
;
1124 if (0 == (nlp
= namelist
)) /* Empty namelist is easy */
1128 if (nlp
->change_dir
&& chdir (nlp
->change_dir
))
1129 msg_perror ("Can't change to directory %d", nlp
->change_dir
);
1134 for (; nlp
!= 0; nlp
= nlp
->next
)
1136 /* If first chars don't match, quick skip */
1137 if (nlp
->firstch
&& nlp
->name
[0] != p
[0])
1140 /* Regular expressions (shell globbing, actually). */
1143 if (fnmatch (nlp
->name
, p
, FNM_LEADING_DIR
) == 0)
1145 nlp
->found
= 1; /* Remember it matched */
1148 free ((void *) namelist
);
1151 if (nlp
->change_dir
&& chdir (nlp
->change_dir
))
1152 msg_perror ("Can't change to directory %s", nlp
->change_dir
);
1153 return 1; /* We got a match */
1158 /* Plain Old Strings */
1159 if (nlp
->length
<= len
/* Archive len >= specified */
1160 && (p
[nlp
->length
] == '\0' || p
[nlp
->length
] == '/')
1161 /* Full match on file/dirname */
1162 && strncmp (p
, nlp
->name
, nlp
->length
) == 0) /* Name compare */
1164 nlp
->found
= 1; /* Remember it matched */
1167 free ((void *) namelist
);
1170 if (nlp
->change_dir
&& chdir (nlp
->change_dir
))
1171 msg_perror ("Can't change to directory %s", nlp
->change_dir
);
1172 return 1; /* We got a match */
1177 * Filename from archive not found in namelist.
1178 * If we have the whole namelist here, just return 0.
1179 * Otherwise, read the next name in and compare it.
1180 * If this was the last name, namelist->found will remain on.
1181 * If not, we loop to compare the newly read name.
1183 if (f_sorted_names
&& namelist
->found
)
1185 name_gather (); /* Read one more */
1186 if (!namelist
->found
)
1194 * Print the names of things in the namelist that were not matched.
1199 register struct name
*nlp
, *next
;
1202 for (nlp
= namelist
; nlp
!= 0; nlp
= next
)
1206 msg ("%s not found in archive", nlp
->name
);
1209 * We could free() the list, but the process is about
1210 * to die anyway, so save some CPU time. Amigas and
1211 * other similarly broken software will need to waste
1215 if (!f_sorted_names
)
1219 namelist
= (struct name
*) NULL
;
1220 namelast
= (struct name
*) NULL
;
1224 while (0 != (p
= name_next (1)))
1225 msg ("%s not found in archive", p
);
1229 /* These next routines were created by JF */
1237 /* This is like name_match(), except that it returns a pointer to the name
1238 it matched, and doesn't set ->found The caller will have to do that
1239 if it wants to. Oh, and if the namelist is empty, it returns 0, unlike
1240 name_match(), which returns TRUE */
1246 register struct name
*nlp
;
1250 if (0 == (nlp
= namelist
)) /* Empty namelist is easy */
1253 for (; nlp
!= 0; nlp
= nlp
->next
)
1255 /* If first chars don't match, quick skip */
1256 if (nlp
->firstch
&& nlp
->name
[0] != p
[0])
1259 /* Regular expressions */
1262 if (fnmatch (nlp
->name
, p
, FNM_LEADING_DIR
) == 0)
1263 return nlp
; /* We got a match */
1267 /* Plain Old Strings */
1268 if (nlp
->length
<= len
/* Archive len >= specified */
1269 && (p
[nlp
->length
] == '\0' || p
[nlp
->length
] == '/')
1270 /* Full match on file/dirname */
1271 && strncmp (p
, nlp
->name
, nlp
->length
) == 0) /* Name compare */
1272 return nlp
; /* We got a match */
1276 * Filename from archive not found in namelist.
1277 * If we have the whole namelist here, just return 0.
1278 * Otherwise, read the next name in and compare it.
1279 * If this was the last name, namelist->found will remain on.
1280 * If not, we loop to compare the newly read name.
1282 if (f_sorted_names
&& namelist
->found
)
1284 name_gather (); /* Read one more */
1285 if (!namelist
->found
)
1288 return (struct name
*) 0;
1291 /* This returns a name from the namelist which doesn't have ->found set.
1292 It sets ->found before returning, so successive calls will find and return
1293 all the non-found names in the namelist */
1295 struct name
*gnu_list_name
;
1301 gnu_list_name
= namelist
;
1302 while (gnu_list_name
&& gnu_list_name
->found
)
1303 gnu_list_name
= gnu_list_name
->next
;
1306 gnu_list_name
->found
++;
1307 if (gnu_list_name
->change_dir
)
1308 if (chdir (gnu_list_name
->change_dir
) < 0)
1309 msg_perror ("can't chdir to %s", gnu_list_name
->change_dir
);
1310 return gnu_list_name
->name
;
1321 for (n
= namelist
; n
; n
= n
->next
)
1326 new_name (path
, name
)
1331 path_buf
= (char *) malloc (strlen (path
) + strlen (name
) + 2);
1334 msg ("Can't allocate memory for name '%s/%s", path
, name
);
1337 (void) sprintf (path_buf
, "%s/%s", path
, name
);
1341 /* returns non-zero if the luser typed 'y' or 'Y', zero otherwise. */
1344 confirm (action
, file
)
1345 char *action
, *file
;
1348 static FILE *confirm_file
= 0;
1349 extern FILE *msg_file
;
1350 extern char TTY_NAME
[];
1352 fprintf (msg_file
, "%s %s?", action
, file
);
1356 confirm_file
= (archive
== 0) ? fopen (TTY_NAME
, "r") : stdin
;
1359 msg ("Can't read confirmation from user");
1363 c
= getc (confirm_file
);
1364 for (nl
= c
; nl
!= '\n' && nl
!= EOF
; nl
= getc (confirm_file
))
1366 return (c
== 'y' || c
== 'Y');
1374 int size_exclude
= 0;
1375 int free_exclude
= 0;
1377 char **re_exclude
= 0;
1378 int size_re_exclude
= 0;
1379 int free_re_exclude
= 0;
1386 /* char **tmp_ptr;*/
1389 un_quote_string (name
);
1390 size_buf
= strlen (name
);
1394 x_buffer
= (char *) ck_malloc (size_buf
+ 1024);
1395 free_x_buffer
= 1024;
1397 else if (free_x_buffer
<= size_buf
)
1402 old_x_buffer
= x_buffer
;
1403 x_buffer
= (char *) ck_realloc (x_buffer
, size_x_buffer
+ 1024);
1404 free_x_buffer
= 1024;
1405 for (tmp_ptr
= exclude
; tmp_ptr
< exclude
+ size_exclude
; tmp_ptr
++)
1406 *tmp_ptr
= x_buffer
+ ((*tmp_ptr
) - old_x_buffer
);
1407 for (tmp_ptr
= re_exclude
; tmp_ptr
< re_exclude
+ size_re_exclude
; tmp_ptr
++)
1408 *tmp_ptr
= x_buffer
+ ((*tmp_ptr
) - old_x_buffer
);
1411 if (is_regex (name
))
1413 if (free_re_exclude
== 0)
1415 re_exclude
= (char **) (re_exclude
? ck_realloc (re_exclude
, (size_re_exclude
+ 32) * sizeof (char *)): ck_malloc (sizeof (char *) * 32));
1416 free_re_exclude
+= 32;
1418 re_exclude
[size_re_exclude
] = x_buffer
+ size_x_buffer
;
1424 if (free_exclude
== 0)
1426 exclude
= (char **) (exclude
? ck_realloc (exclude
, (size_exclude
+ 32) * sizeof (char *)): ck_malloc (sizeof (char *) * 32));
1429 exclude
[size_exclude
] = x_buffer
+ size_x_buffer
;
1433 strcpy (x_buffer
+ size_x_buffer
, name
);
1434 size_x_buffer
+= size_buf
+ 1;
1435 free_x_buffer
-= size_buf
+ 1;
1439 add_exclude_file (file
)
1445 if (strcmp (file
, "-"))
1446 fp
= fopen (file
, "r");
1448 /* Let's hope the person knows what they're doing. */
1449 /* Using -X - -T - -f - will get you *REALLY* strange
1455 msg_perror ("can't open %s", file
);
1458 while (fgets (buf
, 1024, fp
))
1463 end_str
= rindex (buf
, '\n');
1476 return index (str
, '*') || index (str
, '[') || index (str
, '?');
1479 /* Returns non-zero if the file 'name' should not be added/extracted */
1481 check_exclude (name
)
1486 extern char *strstr ();
1488 for (n
= 0; n
< size_re_exclude
; n
++)
1490 if (fnmatch (re_exclude
[n
], name
, FNM_LEADING_DIR
) == 0)
1493 for (n
= 0; n
< size_exclude
; n
++)
1495 /* Accept the output from strstr only if it is the last
1496 part of the string. There is certainly a faster way to
1498 if ((str
= strstr (name
, exclude
[n
]))
1499 && (str
== name
|| str
[-1] == '/')
1500 && str
[strlen (exclude
[n
])] == '\0')
This page took 0.097927 seconds and 5 git commands to generate.