]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - contrib/cpio/src/copyin.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / contrib / cpio / src / copyin.c
1 /* $FreeBSD$ */
2
3 /* copyin.c - extract or list a cpio archive
4    Copyright (C) 1990,1991,1992,2001,2002,2003,2004,
5    2005, 2006 Free Software Foundation, Inc.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public
18    License along with this program; if not, write to the Free
19    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301 USA.  */
21
22 #include <system.h>
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include "filetypes.h"
28 #include "cpiohdr.h"
29 #include "dstring.h"
30 #include "extern.h"
31 #include "defer.h"
32 #include "dirname.h"
33 #include <rmt.h>
34 #ifndef FNM_PATHNAME
35 # include <fnmatch.h>
36 #endif
37 #include <langinfo.h>
38
39 #ifndef HAVE_LCHOWN
40 # define lchown(f,u,g) 0
41 #endif
42
43 static void copyin_regular_file(struct cpio_file_stat* file_hdr,
44                                 int in_file_des);
45
46 void
47 warn_junk_bytes (long bytes_skipped)
48 {
49   error (0, 0, ngettext ("warning: skipped %ld byte of junk",
50                          "warning: skipped %ld bytes of junk", bytes_skipped),
51          bytes_skipped);
52 }
53
54 \f
55 static int
56 query_rename(struct cpio_file_stat* file_hdr, FILE *tty_in, FILE *tty_out,
57              FILE *rename_in)
58 {
59   char *str_res;                /* Result for string function.  */
60   static dynamic_string new_name;       /* New file name for rename option.  */
61   static int initialized_new_name = false;
62   if (!initialized_new_name)
63   {
64     ds_init (&new_name, 128);
65     initialized_new_name = true;
66   }
67
68   if (rename_flag)
69     {
70       fprintf (tty_out, _("rename %s -> "), file_hdr->c_name);
71       fflush (tty_out);
72       str_res = ds_fgets (tty_in, &new_name);
73     }
74   else
75     {
76       str_res = ds_fgetstr (rename_in, &new_name, '\n');
77     }
78   if (str_res == NULL || str_res[0] == 0)
79     {
80       return -1;
81     }
82   else
83   /* Debian hack: file_hrd.c_name is sometimes set to
84      point to static memory by code in tar.c.  This
85      causes a segfault.  This has been fixed and an
86      additional check to ensure that the file name
87      is not too long has been added.  (Reported by
88      Horst Knobloch.)  This bug has been reported to
89      "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM */
90     {
91       if (archive_format != arf_tar && archive_format != arf_ustar)
92         {
93           free (file_hdr->c_name);
94           file_hdr->c_name = xstrdup (new_name.ds_string);
95         }
96       else
97         {
98           if (is_tar_filename_too_long (new_name.ds_string))
99             error (0, 0, _("%s: file name too long"),
100                    new_name.ds_string);
101           else
102             strcpy (file_hdr->c_name, new_name.ds_string);
103         }
104     }
105   return 0;
106 }
107 \f
108 /* Skip the padding on IN_FILE_DES after a header or file,
109    up to the next header.
110    The number of bytes skipped is based on OFFSET -- the current offset
111    from the last start of a header (or file) -- and the current
112    header type.  */
113
114 static void
115 tape_skip_padding (int in_file_des, int offset)
116 {
117   int pad;
118
119   if (archive_format == arf_crcascii || archive_format == arf_newascii)
120     pad = (4 - (offset % 4)) % 4;
121   else if (archive_format == arf_binary || archive_format == arf_hpbinary)
122     pad = (2 - (offset % 2)) % 2;
123   else if (archive_format == arf_tar || archive_format == arf_ustar)
124     pad = (512 - (offset % 512)) % 512;
125   else
126     pad = 0;
127
128   if (pad != 0)
129     tape_toss_input (in_file_des, pad);
130 }
131
132 \f
133 static void
134 list_file(struct cpio_file_stat* file_hdr, int in_file_des)
135 {
136   if (verbose_flag)
137     {
138 #ifdef CP_IFLNK
139       if ((file_hdr->c_mode & CP_IFMT) == CP_IFLNK)
140         {
141           if (archive_format != arf_tar && archive_format != arf_ustar)
142             {
143               char *link_name = NULL;   /* Name of hard and symbolic links.  */
144
145               link_name = (char *) xmalloc ((unsigned int) file_hdr->c_filesize + 1);
146               link_name[file_hdr->c_filesize] = '\0';
147               tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
148               long_format (file_hdr, link_name);
149               free (link_name);
150               tape_skip_padding (in_file_des, file_hdr->c_filesize);
151               return;
152             }
153           else
154             {
155               long_format (file_hdr, file_hdr->c_tar_linkname);
156               return;
157             }
158         }
159       else
160 #endif
161         long_format (file_hdr, (char *) 0);
162     }
163   else
164     {
165       /* Debian hack: Modified to print a list of filenames
166          terminiated by a null character when the -t and -0
167          flags are used.  This has been submitted as a
168          suggestion to "bug-gnu-utils@prep.ai.mit.edu".  -BEM */
169       printf ("%s%c", file_hdr->c_name, name_end);
170     }
171
172   crc = 0;
173   tape_toss_input (in_file_des, file_hdr->c_filesize);
174   tape_skip_padding (in_file_des, file_hdr->c_filesize);
175   if (only_verify_crc_flag)
176     {
177 #ifdef CP_IFLNK
178       if ((file_hdr->c_mode & CP_IFMT) == CP_IFLNK)
179         {
180           return;   /* links don't have a checksum */
181         }
182 #endif
183       if (crc != file_hdr->c_chksum)
184         {
185           error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
186                  file_hdr->c_name, crc, file_hdr->c_chksum);
187         }
188     }
189 }
190 \f
191 static int
192 try_existing_file (struct cpio_file_stat* file_hdr, int in_file_des,
193                    int *existing_dir)
194 {
195   struct stat file_stat;
196
197   *existing_dir = false;
198   if (lstat (file_hdr->c_name, &file_stat) == 0)
199     {
200       if (S_ISDIR (file_stat.st_mode)
201           && ((file_hdr->c_mode & CP_IFMT) == CP_IFDIR))
202         {
203           /* If there is already a directory there that
204              we are trying to create, don't complain about
205              it.  */
206           *existing_dir = true;
207           return 0;
208         }
209       else if (!unconditional_flag
210                && file_hdr->c_mtime <= file_stat.st_mtime)
211         {
212           error (0, 0, _("%s not created: newer or same age version exists"),
213                  file_hdr->c_name);
214           tape_toss_input (in_file_des, file_hdr->c_filesize);
215           tape_skip_padding (in_file_des, file_hdr->c_filesize);
216           return -1;    /* Go to the next file.  */
217         }
218       else if (S_ISDIR (file_stat.st_mode) 
219                 ? rmdir (file_hdr->c_name)
220                 : unlink (file_hdr->c_name))
221         {
222           error (0, errno, _("cannot remove current %s"),
223                  file_hdr->c_name);
224           tape_toss_input (in_file_des, file_hdr->c_filesize);
225           tape_skip_padding (in_file_des, file_hdr->c_filesize);
226           return -1;    /* Go to the next file.  */
227         }
228     }
229   return 0;
230 }
231 \f
232 /* The newc and crc formats store multiply linked copies of the same file 
233    in the archive only once.  The actual data is attached to the last link 
234    in the archive, and the other links all have a filesize of 0.  When a 
235    file in the archive has multiple links and a filesize of 0, its data is 
236    probably "attatched" to another file in the archive, so we can't create
237    it right away.  We have to "defer" creating it until we have created
238    the file that has the data "attatched" to it.  We keep a list of the
239    "defered" links on deferments.  */
240
241 struct deferment *deferments = NULL;
242
243 /* Add a file header to the deferments list.  For now they all just
244    go on one list, although we could optimize this if necessary.  */
245
246 static void
247 defer_copyin (struct cpio_file_stat *file_hdr)
248 {
249   struct deferment *d;
250   d = create_deferment (file_hdr);
251   d->next = deferments;
252   deferments = d;
253   return;
254 }
255
256 /* We just created a file that (probably) has some other links to it
257    which have been defered.  Go through all of the links on the deferments
258    list and create any which are links to this file.  */
259
260 static void
261 create_defered_links (struct cpio_file_stat *file_hdr)
262 {
263   struct deferment *d;
264   struct deferment *d_prev;
265   int   ino;
266   int   maj;
267   int   min;
268   int   link_res;
269   ino = file_hdr->c_ino;
270   maj = file_hdr->c_dev_maj;
271   min = file_hdr->c_dev_min;
272   d = deferments;
273   d_prev = NULL;
274   while (d != NULL)
275     {
276       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
277           && (d->header.c_dev_min == min) )
278         {
279           struct deferment *d_free;
280           link_res = link_to_name (d->header.c_name, file_hdr->c_name);
281           if (link_res < 0)
282             {
283               error (0, errno, _("cannot link %s to %s"),
284                      d->header.c_name, file_hdr->c_name);
285             }
286           if (d_prev != NULL)
287             d_prev->next = d->next;
288           else
289             deferments = d->next;
290           d_free = d;
291           d = d->next;
292           free_deferment (d_free);
293         }
294       else
295         {
296           d_prev = d;
297           d = d->next;
298         }
299     }
300 }
301
302 /* We are skipping a file but there might be other links to it that we
303    did not skip, so we have to copy its data for the other links.  Find
304    the first link that we didn't skip and try to create that.  That will
305    then create the other deferred links.  */
306
307 static int
308 create_defered_links_to_skipped (struct cpio_file_stat *file_hdr,
309                                  int in_file_des)
310 {
311   struct deferment *d;
312   struct deferment *d_prev;
313   int   ino;
314   int   maj;
315   int   min;
316   if (file_hdr->c_filesize == 0)
317     {
318       /* The file doesn't have any data attached to it so we don't have
319          to bother.  */
320       return -1;
321     }
322   ino = file_hdr->c_ino;
323   maj = file_hdr->c_dev_maj;
324   min = file_hdr->c_dev_min;
325   d = deferments;
326   d_prev = NULL;
327   while (d != NULL)
328     {
329       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
330           && (d->header.c_dev_min == min) )
331         {
332           if (d_prev != NULL)
333             d_prev->next = d->next;
334           else
335             deferments = d->next;
336           free (file_hdr->c_name);
337           file_hdr->c_name = xstrdup(d->header.c_name);
338           free_deferment (d);
339           copyin_regular_file(file_hdr, in_file_des);
340           return 0;
341         }
342       else
343         {
344           d_prev = d;
345           d = d->next;
346         }
347     }
348   return -1;
349 }
350
351 /* If we had a multiply linked file that really was empty then we would
352    have defered all of its links, since we never found any with data
353    "attached", and they will still be on the deferment list even when
354    we are done reading the whole archive.  Write out all of these
355    empty links that are still on the deferments list.  */
356
357 static void
358 create_final_defers ()
359 {
360   struct deferment *d;
361   int   link_res;
362   int   out_file_des;
363
364   for (d = deferments; d != NULL; d = d->next)
365     {
366       /* Debian hack: A line, which could cause an endless loop, was
367          removed (97/1/2).  It was reported by Ronald F. Guilmette to
368          the upstream maintainers. -BEM */
369       /* Debian hack:  This was reported by Horst Knobloch. This bug has
370          been reported to "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM
371          */
372       link_res = link_to_maj_min_ino (d->header.c_name, 
373                     d->header.c_dev_maj, d->header.c_dev_min,
374                     d->header.c_ino);
375       if (link_res == 0)
376         {
377           continue;
378         }
379       out_file_des = open (d->header.c_name,
380                            O_CREAT | O_WRONLY | O_BINARY, 0600);
381       if (out_file_des < 0 && create_dir_flag)
382         {
383           create_all_directories (d->header.c_name);
384           out_file_des = open (d->header.c_name,
385                                O_CREAT | O_WRONLY | O_BINARY,
386                                0600);
387         }
388       if (out_file_des < 0)
389         {
390           open_error (d->header.c_name);
391           continue;
392         }
393
394       set_perms (out_file_des, &d->header);
395
396       if (close (out_file_des) < 0)
397         close_error (d->header.c_name);
398
399     }
400 }
401 \f
402 static void
403 copyin_regular_file (struct cpio_file_stat* file_hdr, int in_file_des)
404 {
405   int out_file_des;             /* Output file descriptor.  */
406
407   if (to_stdout_option)
408     out_file_des = STDOUT_FILENO;
409   else
410     {
411       /* Can the current file be linked to a previously copied file? */
412       if (file_hdr->c_nlink > 1
413           && (archive_format == arf_newascii
414               || archive_format == arf_crcascii) )
415         {
416           int link_res;
417           if (file_hdr->c_filesize == 0)
418             {
419               /* The newc and crc formats store multiply linked copies
420                  of the same file in the archive only once.  The
421                  actual data is attached to the last link in the
422                  archive, and the other links all have a filesize
423                  of 0.  Since this file has multiple links and a
424                  filesize of 0, its data is probably attatched to
425                  another file in the archive.  Save the link, and
426                  process it later when we get the actual data.  We
427                  can't just create it with length 0 and add the
428                  data later, in case the file is readonly.  We still
429                  lose if its parent directory is readonly (and we aren't
430                  running as root), but there's nothing we can do about
431                  that.  */
432               defer_copyin (file_hdr);
433               tape_toss_input (in_file_des, file_hdr->c_filesize);
434               tape_skip_padding (in_file_des, file_hdr->c_filesize);
435               return;
436             }
437           /* If the file has data (filesize != 0), then presumably
438              any other links have already been defer_copyin'ed(),
439              but GNU cpio version 2.0-2.2 didn't do that, so we
440              still have to check for links here (and also in case
441              the archive was created and later appeneded to). */
442           /* Debian hack: (97/1/2) This was reported by Ronald
443              F. Guilmette to the upstream maintainers. -BEM */
444           link_res = link_to_maj_min_ino (file_hdr->c_name, 
445                     file_hdr->c_dev_maj, file_hdr->c_dev_min,
446                                           file_hdr->c_ino);
447           if (link_res == 0)
448             {
449               tape_toss_input (in_file_des, file_hdr->c_filesize);
450               tape_skip_padding (in_file_des, file_hdr->c_filesize);
451               return;
452             }
453         }
454       else if (file_hdr->c_nlink > 1
455                && archive_format != arf_tar
456                && archive_format != arf_ustar)
457         {
458           int link_res;
459           /* Debian hack: (97/1/2) This was reported by Ronald
460              F. Guilmette to the upstream maintainers. -BEM */
461           link_res = link_to_maj_min_ino (file_hdr->c_name, 
462                                           file_hdr->c_dev_maj,
463                                           file_hdr->c_dev_min,
464                                           file_hdr->c_ino);
465           if (link_res == 0)
466             {
467               tape_toss_input (in_file_des, file_hdr->c_filesize);
468               tape_skip_padding (in_file_des, file_hdr->c_filesize);
469               return;
470             }
471         }
472       else if ((archive_format == arf_tar || archive_format == arf_ustar)
473                && file_hdr->c_tar_linkname
474                && file_hdr->c_tar_linkname[0] != '\0')
475         {
476           int   link_res;
477           link_res = link_to_name (file_hdr->c_name, file_hdr->c_tar_linkname);
478           if (link_res < 0)
479             {
480               error (0, errno, _("cannot link %s to %s"),
481                      file_hdr->c_tar_linkname, file_hdr->c_name);
482             }
483           return;
484         }
485     
486       /* If not linked, copy the contents of the file.  */
487       out_file_des = open (file_hdr->c_name,
488                            O_CREAT | O_WRONLY | O_BINARY, 0600);
489   
490       if (out_file_des < 0 && create_dir_flag)
491         {
492           create_all_directories (file_hdr->c_name);
493           out_file_des = open (file_hdr->c_name,
494                                O_CREAT | O_WRONLY | O_BINARY,
495                                0600);
496         }
497       
498       if (out_file_des < 0)
499         {
500           open_error (file_hdr->c_name);
501           tape_toss_input (in_file_des, file_hdr->c_filesize);
502           tape_skip_padding (in_file_des, file_hdr->c_filesize);
503           return;
504         }
505     }
506   
507   crc = 0;
508   if (swap_halfwords_flag)
509     {
510       if ((file_hdr->c_filesize % 4) == 0)
511         swapping_halfwords = true;
512       else
513         error (0, 0, _("cannot swap halfwords of %s: odd number of halfwords"),
514                file_hdr->c_name);
515     }
516   if (swap_bytes_flag)
517     {
518       if ((file_hdr->c_filesize % 2) == 0)
519         swapping_bytes = true;
520       else
521         error (0, 0, _("cannot swap bytes of %s: odd number of bytes"),
522                file_hdr->c_name);
523     }
524   copy_files_tape_to_disk (in_file_des, out_file_des, file_hdr->c_filesize);
525   disk_empty_output_buffer (out_file_des);
526   
527   if (to_stdout_option)
528     {
529       if (archive_format == arf_crcascii)
530         {
531           if (crc != file_hdr->c_chksum)
532             error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
533                    file_hdr->c_name, crc, file_hdr->c_chksum);
534         }
535       tape_skip_padding (in_file_des, file_hdr->c_filesize);
536       return;
537     }
538       
539   /* Debian hack to fix a bug in the --sparse option.
540      This bug has been reported to
541      "bug-gnu-utils@prep.ai.mit.edu".  (96/7/10) -BEM */
542   if (delayed_seek_count > 0)
543     {
544       lseek (out_file_des, delayed_seek_count-1, SEEK_CUR);
545       write (out_file_des, "", 1);
546       delayed_seek_count = 0;
547     }
548
549   set_perms (out_file_des, file_hdr);
550
551   if (close (out_file_des) < 0)
552     close_error (file_hdr->c_name);
553
554   if (archive_format == arf_crcascii)
555     {
556       if (crc != file_hdr->c_chksum)
557         error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
558                file_hdr->c_name, crc, file_hdr->c_chksum);
559     }
560
561   tape_skip_padding (in_file_des, file_hdr->c_filesize);
562   if (file_hdr->c_nlink > 1
563       && (archive_format == arf_newascii || archive_format == arf_crcascii) )
564     {
565       /* (see comment above for how the newc and crc formats 
566          store multiple links).  Now that we have the data 
567          for this file, create any other links to it which
568          we defered.  */
569       create_defered_links (file_hdr);
570     }
571 }
572 \f
573 static void
574 copyin_directory (struct cpio_file_stat *file_hdr, int existing_dir)
575 {
576   int res;                      /* Result of various function calls.  */
577 #ifdef HPUX_CDF
578   int cdf_flag;                 /* True if file is a CDF.  */
579   int cdf_char;                 /* Index of `+' char indicating a CDF.  */
580 #endif
581
582   if (to_stdout_option)
583     return;
584   
585   /* Strip any trailing `/'s off the filename; tar puts
586      them on.  We might as well do it here in case anybody
587      else does too, since they cause strange things to happen.  */
588   strip_trailing_slashes (file_hdr->c_name);
589
590   /* Ignore the current directory.  It must already exist,
591      and we don't want to change its permission, ownership
592      or time.  */
593   if (file_hdr->c_name[0] == '.' && file_hdr->c_name[1] == '\0')
594     {
595       return;
596     }
597
598 #ifdef HPUX_CDF
599   cdf_flag = 0;
600 #endif
601   if (!existing_dir)
602
603     {
604 #ifdef HPUX_CDF
605       /* If the directory name ends in a + and is SUID,
606          then it is a CDF.  Strip the trailing + from
607          the name before creating it.  */
608       cdf_char = strlen (file_hdr->c_name) - 1;
609       if ( (cdf_char > 0) &&
610            (file_hdr->c_mode & 04000) && 
611            (file_hdr->c_name [cdf_char] == '+') )
612         {
613           file_hdr->c_name [cdf_char] = '\0';
614           cdf_flag = 1;
615         }
616 #endif
617       res = mkdir (file_hdr->c_name, file_hdr->c_mode);
618     }
619   else
620     res = 0;
621   if (res < 0 && create_dir_flag)
622     {
623       create_all_directories (file_hdr->c_name);
624       res = mkdir (file_hdr->c_name, file_hdr->c_mode);
625     }
626   if (res < 0)
627     {
628       /* In some odd cases where the file_hdr->c_name includes `.',
629          the directory may have actually been created by
630          create_all_directories(), so the mkdir will fail
631          because the directory exists.  If that's the case,
632          don't complain about it.  */
633       struct stat file_stat;
634       if (errno != EEXIST)
635         {
636           mkdir_error (file_hdr->c_name);
637           return;
638         }
639       if (lstat (file_hdr->c_name, &file_stat))
640         {
641           stat_error (file_hdr->c_name);
642           return;
643         }
644       if (!(S_ISDIR (file_stat.st_mode)))
645         {
646           error (0, 0, _("%s is not a directory"),
647                  quotearg_colon (file_hdr->c_name));
648           return;
649         }
650     }
651
652   set_perms (-1, file_hdr); 
653 }
654 \f
655 static void
656 copyin_device (struct cpio_file_stat* file_hdr)
657 {
658   int res;                      /* Result of various function calls.  */
659
660   if (to_stdout_option)
661     return;
662
663   if (file_hdr->c_nlink > 1 && archive_format != arf_tar
664       && archive_format != arf_ustar)
665     {
666       int link_res;
667       /* Debian hack:  This was reported by Horst
668          Knobloch. This bug has been reported to
669          "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM */
670       link_res = link_to_maj_min_ino (file_hdr->c_name, 
671                     file_hdr->c_dev_maj, file_hdr->c_dev_min,
672                     file_hdr->c_ino);
673       if (link_res == 0)
674         {
675           return;
676         }
677     }
678   else if (archive_format == arf_ustar &&
679            file_hdr->c_tar_linkname && 
680            file_hdr->c_tar_linkname [0] != '\0')
681     {
682       int       link_res;
683       link_res = link_to_name (file_hdr->c_name,
684                                file_hdr->c_tar_linkname);
685       if (link_res < 0)
686         {
687           error (0, errno, _("cannot link %s to %s"),
688                  file_hdr->c_tar_linkname, file_hdr->c_name);
689           /* Something must be wrong, because we couldn't
690              find the file to link to.  But can we assume
691              that the device maj/min numbers are correct
692              and fall through to the mknod?  It's probably
693              safer to just return, rather than possibly
694              creating a bogus device file.  */
695         }
696       return;
697     }
698   
699 #ifdef CP_IFIFO
700   if ((file_hdr->c_mode & CP_IFMT) == CP_IFIFO)
701     res = mkfifo (file_hdr->c_name, file_hdr->c_mode);
702   else
703 #endif
704     res = mknod (file_hdr->c_name, file_hdr->c_mode,
705               makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min));
706   if (res < 0 && create_dir_flag)
707     {
708       create_all_directories (file_hdr->c_name);
709 #ifdef CP_IFIFO
710       if ((file_hdr->c_mode & CP_IFMT) == CP_IFIFO)
711         res = mkfifo (file_hdr->c_name, file_hdr->c_mode);
712       else
713 #endif
714         res = mknod (file_hdr->c_name, file_hdr->c_mode,
715               makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min));
716     }
717   if (res < 0)
718     {
719       mknod_error (file_hdr->c_name);
720       return;
721     }
722   if (!no_chown_flag)
723     {
724       uid_t uid = set_owner_flag ? set_owner : file_hdr->c_uid;
725       gid_t gid = set_group_flag ? set_group : file_hdr->c_gid;
726       if ((chown (file_hdr->c_name, uid, gid) < 0)
727           && errno != EPERM)
728         chown_error_details (file_hdr->c_name, uid, gid);
729     }
730   /* chown may have turned off some permissions we wanted. */
731   if (chmod (file_hdr->c_name, file_hdr->c_mode) < 0)
732     chmod_error_details (file_hdr->c_name, file_hdr->c_mode);
733   if (retain_time_flag)
734     set_file_times (-1, file_hdr->c_name, file_hdr->c_mtime,
735                     file_hdr->c_mtime);
736 }
737 \f
738 static void
739 copyin_link(struct cpio_file_stat *file_hdr, int in_file_des)
740 {
741   char *link_name = NULL;       /* Name of hard and symbolic links.  */
742   int res;                      /* Result of various function calls.  */
743
744   if (to_stdout_option)
745     return;
746
747   if (archive_format != arf_tar && archive_format != arf_ustar)
748     {
749       link_name = (char *) xmalloc ((unsigned int) file_hdr->c_filesize + 1);
750       link_name[file_hdr->c_filesize] = '\0';
751       tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
752       tape_skip_padding (in_file_des, file_hdr->c_filesize);
753     }
754   else
755     {
756       link_name = xstrdup (file_hdr->c_tar_linkname);
757     }
758
759   res = UMASKED_SYMLINK (link_name, file_hdr->c_name,
760                          file_hdr->c_mode);
761   if (res < 0 && create_dir_flag)
762     {
763       create_all_directories (file_hdr->c_name);
764       res = UMASKED_SYMLINK (link_name, file_hdr->c_name,
765                              file_hdr->c_mode);
766     }
767   if (res < 0)
768     {
769       error (0, errno, _("%s: Cannot symlink to %s"),
770              quotearg_colon (link_name), quote_n (1, file_hdr->c_name));
771       free (link_name);
772       return;
773     }
774   if (!no_chown_flag)
775     {
776       uid_t uid = set_owner_flag ? set_owner : file_hdr->c_uid;
777       gid_t gid = set_group_flag ? set_group : file_hdr->c_gid;
778       if ((lchown (file_hdr->c_name, uid, gid) < 0)
779           && errno != EPERM)
780         chown_error_details (file_hdr->c_name, uid, gid);
781     }
782   free (link_name);
783 }
784 \f
785 static void
786 copyin_file (struct cpio_file_stat* file_hdr, int in_file_des)
787 {
788   int existing_dir;
789
790   if (!to_stdout_option
791       && try_existing_file (file_hdr, in_file_des, &existing_dir) < 0)
792     return;
793
794   /* Do the real copy or link.  */
795   switch (file_hdr->c_mode & CP_IFMT)
796     {
797     case CP_IFREG:
798       copyin_regular_file (file_hdr, in_file_des);
799       break;
800
801     case CP_IFDIR:
802       copyin_directory (file_hdr, existing_dir);
803       break;
804
805     case CP_IFCHR:
806     case CP_IFBLK:
807 #ifdef CP_IFSOCK
808     case CP_IFSOCK:
809 #endif
810 #ifdef CP_IFIFO
811     case CP_IFIFO:
812 #endif
813       copyin_device (file_hdr);
814       break;
815
816 #ifdef CP_IFLNK
817     case CP_IFLNK:
818       copyin_link (file_hdr, in_file_des);
819       break;
820 #endif
821
822     default:
823       error (0, 0, _("%s: unknown file type"), file_hdr->c_name);
824       tape_toss_input (in_file_des, file_hdr->c_filesize);
825       tape_skip_padding (in_file_des, file_hdr->c_filesize);
826     }
827 }
828 \f
829
830 /* Current time for verbose table.  */
831 static time_t current_time;
832
833
834 /* Print the file described by FILE_HDR in long format.
835    If LINK_NAME is nonzero, it is the name of the file that
836    this file is a symbolic link to.  */
837
838 void
839 long_format (struct cpio_file_stat *file_hdr, char *link_name)
840 {
841   char mbuf[11];
842   char tbuf[40];
843   time_t when;
844   char *ptbuf;
845   static int d_first = -1;
846
847   mode_string (file_hdr->c_mode, mbuf);
848   mbuf[10] = '\0';
849
850   /* Get time values ready to print.  */
851   when = file_hdr->c_mtime;
852   if (d_first < 0)
853     d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
854   if (current_time - when > 6L * 30L * 24L * 60L * 60L
855       || current_time - when < 0L)
856         ptbuf = d_first ? "%e %b  %Y" : "%b %e  %Y";
857   else
858         ptbuf = d_first ? "%e %b %R" : "%b %e %R";
859   strftime(tbuf, sizeof(tbuf), ptbuf, localtime(&when));
860   ptbuf = tbuf;
861
862   printf ("%s %3lu ", mbuf, file_hdr->c_nlink);
863
864   if (numeric_uid)
865     printf ("%-8u %-8u ", (unsigned int) file_hdr->c_uid,
866             (unsigned int) file_hdr->c_gid);
867   else
868     printf ("%-8.8s %-8.8s ", getuser (file_hdr->c_uid),
869             getgroup (file_hdr->c_gid));
870
871   if ((file_hdr->c_mode & CP_IFMT) == CP_IFCHR
872       || (file_hdr->c_mode & CP_IFMT) == CP_IFBLK)
873     printf ("%3lu, %3lu ", file_hdr->c_rdev_maj,
874             file_hdr->c_rdev_min);
875   else
876     printf ("%8"PRIuMAX" ", (uintmax_t) file_hdr->c_filesize);
877
878   printf ("%s ", ptbuf);
879
880   print_name_with_quoting (file_hdr->c_name);
881   if (link_name)
882     {
883       printf (" -> ");
884       print_name_with_quoting (link_name);
885     }
886   putc ('\n', stdout);
887 }
888
889 void
890 print_name_with_quoting (register char *p)
891 {
892   register unsigned char c;
893
894   while ( (c = *p++) )
895     {
896       switch (c)
897         {
898         case '\\':
899           printf ("\\\\");
900           break;
901
902         case '\n':
903           printf ("\\n");
904           break;
905
906         case '\b':
907           printf ("\\b");
908           break;
909
910         case '\r':
911           printf ("\\r");
912           break;
913
914         case '\t':
915           printf ("\\t");
916           break;
917
918         case '\f':
919           printf ("\\f");
920           break;
921
922         case ' ':
923           printf ("\\ ");
924           break;
925
926         case '"':
927           printf ("\\\"");
928           break;
929
930         default:
931           if (isprint (c))
932             putchar (c);
933           else
934             printf ("\\%03o", (unsigned int) c);
935         }
936     }
937 }
938
939 /* Read a pattern file (for the -E option).  Put a list of
940    `num_patterns' elements in `save_patterns'.  Any patterns that were
941    already in `save_patterns' (from the command line) are preserved.  */
942
943 static void
944 read_pattern_file ()
945 {
946   int max_new_patterns;
947   char **new_save_patterns;
948   int new_num_patterns;
949   int i;
950   dynamic_string pattern_name;
951   FILE *pattern_fp;
952
953   if (num_patterns < 0)
954     num_patterns = 0;
955   max_new_patterns = 1 + num_patterns;
956   new_save_patterns = (char **) xmalloc (max_new_patterns * sizeof (char *));
957   new_num_patterns = num_patterns;
958   ds_init (&pattern_name, 128);
959
960   pattern_fp = fopen (pattern_file_name, "r");
961   if (pattern_fp == NULL)
962     open_error (pattern_file_name);
963   while (ds_fgetstr (pattern_fp, &pattern_name, '\n') != NULL)
964     {
965       if (new_num_patterns >= max_new_patterns)
966         {
967           max_new_patterns += 1;
968           new_save_patterns = (char **)
969             xrealloc ((char *) new_save_patterns,
970                       max_new_patterns * sizeof (char *));
971         }
972       new_save_patterns[new_num_patterns] = xstrdup (pattern_name.ds_string);
973       ++new_num_patterns;
974     }
975   if (ferror (pattern_fp) || fclose (pattern_fp) == EOF)
976     close_error (pattern_file_name);
977
978   for (i = 0; i < num_patterns; ++i)
979     new_save_patterns[i] = save_patterns[i];
980
981   save_patterns = new_save_patterns;
982   num_patterns = new_num_patterns;
983 }
984
985 \f
986 uintmax_t
987 from_ascii (char const *where, size_t digs, unsigned logbase)
988 {
989   uintmax_t value = 0;
990   char const *buf = where;
991   char const *end = buf + digs;
992   int overflow = 0;
993   static char codetab[] = "0123456789ABCDEF";
994
995   for (; *buf == ' '; buf++)
996     {
997       if (buf == end)
998         return 0;
999     }
1000
1001   if (buf == end || *buf == 0)
1002     return 0;
1003   while (1)
1004     {
1005       unsigned d;
1006       
1007       char *p = strchr (codetab, toupper (*buf));
1008       if (!p)
1009         {
1010           error (0, 0, _("Malformed number %.*s"), digs, where);
1011           break;
1012         }
1013       
1014       d = p - codetab;
1015       if ((d >> logbase) > 1)
1016         {
1017           error (0, 0, _("Malformed number %.*s"), digs, where);
1018           break;
1019         }
1020       value += d;
1021       if (++buf == end || *buf == 0)
1022         break;
1023       overflow |= value ^ (value << logbase >> logbase);
1024       value <<= logbase;
1025     }
1026   if (overflow)
1027     error (0, 0, _("Archive value %.*s is out of range"),
1028            digs, where);
1029   return value;
1030 }
1031
1032 \f
1033
1034 /* Return 16-bit integer I with the bytes swapped.  */
1035 #define swab_short(i) ((((i) << 8) & 0xff00) | (((i) >> 8) & 0x00ff))
1036
1037 /* Read the header, including the name of the file, from file
1038    descriptor IN_DES into FILE_HDR.  */
1039
1040 void
1041 read_in_header (struct cpio_file_stat *file_hdr, int in_des)
1042 {
1043   union {
1044     char str[6];
1045     unsigned short num;
1046     struct old_cpio_header old_header;
1047   } magic;
1048   long bytes_skipped = 0;       /* Bytes of junk found before magic number.  */
1049
1050   /* Search for a valid magic number.  */
1051
1052   if (archive_format == arf_unknown)
1053     {
1054       char tmpbuf[512];
1055       int check_tar;
1056       int peeked_bytes;
1057
1058       while (archive_format == arf_unknown)
1059         {
1060           peeked_bytes = tape_buffered_peek (tmpbuf, in_des, 512);
1061           if (peeked_bytes < 6)
1062             error (1, 0, _("premature end of archive"));
1063
1064           if (!strncmp (tmpbuf, "070701", 6))
1065             archive_format = arf_newascii;
1066           else if (!strncmp (tmpbuf, "070707", 6))
1067             archive_format = arf_oldascii;
1068           else if (!strncmp (tmpbuf, "070702", 6))
1069             {
1070               archive_format = arf_crcascii;
1071               crc_i_flag = true;
1072             }
1073           else if ((*((unsigned short *) tmpbuf) == 070707) ||
1074                    (*((unsigned short *) tmpbuf) == swab_short ((unsigned short) 070707)))
1075             archive_format = arf_binary;
1076           else if (peeked_bytes >= 512
1077                    && (check_tar = is_tar_header (tmpbuf)))
1078             {
1079               if (check_tar == 2)
1080                 archive_format = arf_ustar;
1081               else
1082                 archive_format = arf_tar;
1083             }
1084           else
1085             {
1086               tape_buffered_read ((char *) tmpbuf, in_des, 1L);
1087               ++bytes_skipped;
1088             }
1089         }
1090     }
1091
1092   if (archive_format == arf_tar || archive_format == arf_ustar)
1093     {
1094       if (append_flag)
1095         last_header_start = input_bytes - io_block_size +
1096           (in_buff - input_buffer);
1097       if (bytes_skipped > 0)
1098         warn_junk_bytes (bytes_skipped);
1099
1100       read_in_tar_header (file_hdr, in_des);
1101       return;
1102     }
1103
1104   file_hdr->c_tar_linkname = NULL;
1105
1106   tape_buffered_read (magic.str, in_des, 6L);
1107   while (1)
1108     {
1109       if (append_flag)
1110         last_header_start = input_bytes - io_block_size
1111           + (in_buff - input_buffer) - 6;
1112       if (archive_format == arf_newascii
1113           && !strncmp (magic.str, "070701", 6))
1114         {
1115           if (bytes_skipped > 0)
1116             warn_junk_bytes (bytes_skipped);
1117           file_hdr->c_magic = 070701;
1118           read_in_new_ascii (file_hdr, in_des);
1119           break;
1120         }
1121       if (archive_format == arf_crcascii
1122           && !strncmp (magic.str, "070702", 6))
1123         {
1124           if (bytes_skipped > 0)
1125             warn_junk_bytes (bytes_skipped);
1126           file_hdr->c_magic = 070702;
1127           read_in_new_ascii (file_hdr, in_des);
1128           break;
1129         }
1130       if ( (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
1131           && !strncmp (magic.str, "070707", 6))
1132         {
1133           if (bytes_skipped > 0)
1134             warn_junk_bytes (bytes_skipped);
1135           file_hdr->c_magic = 070707;
1136           read_in_old_ascii (file_hdr, in_des);
1137           break;
1138         }
1139       if ( (archive_format == arf_binary || archive_format == arf_hpbinary)
1140           && (magic.num == 070707
1141               || magic.num == swab_short ((unsigned short) 070707)))
1142         {
1143           /* Having to skip 1 byte because of word alignment is normal.  */
1144           if (bytes_skipped > 0)
1145             warn_junk_bytes (bytes_skipped);
1146           file_hdr->c_magic = 070707;
1147           read_in_binary (file_hdr, &magic.old_header, in_des);
1148           break;
1149         }
1150       bytes_skipped++;
1151       memmove (magic.str, magic.str + 1, 5);
1152       tape_buffered_read (magic.str, in_des, 1L);
1153     }
1154 }
1155
1156 /* Fill in FILE_HDR by reading an old-format ASCII format cpio header from
1157    file descriptor IN_DES, except for the magic number, which is
1158    already filled in.  */
1159
1160 void
1161 read_in_old_ascii (struct cpio_file_stat *file_hdr, int in_des)
1162 {
1163   struct old_ascii_header ascii_header;
1164   unsigned long dev;
1165
1166   tape_buffered_read (ascii_header.c_dev, in_des,
1167                       sizeof ascii_header - sizeof ascii_header.c_magic);
1168   dev = FROM_OCTAL (ascii_header.c_dev);
1169   file_hdr->c_dev_maj = major (dev);
1170   file_hdr->c_dev_min = minor (dev);
1171
1172   file_hdr->c_ino = FROM_OCTAL (ascii_header.c_ino);
1173   file_hdr->c_mode = FROM_OCTAL (ascii_header.c_mode);
1174   file_hdr->c_uid = FROM_OCTAL (ascii_header.c_uid);
1175   file_hdr->c_gid = FROM_OCTAL (ascii_header.c_gid);
1176   file_hdr->c_nlink = FROM_OCTAL (ascii_header.c_nlink);
1177   dev = FROM_OCTAL (ascii_header.c_rdev);
1178   file_hdr->c_rdev_maj = major (dev);
1179   file_hdr->c_rdev_min = minor (dev);
1180
1181   file_hdr->c_mtime = FROM_OCTAL (ascii_header.c_mtime);
1182   file_hdr->c_namesize = FROM_OCTAL (ascii_header.c_namesize);
1183   file_hdr->c_filesize = FROM_OCTAL (ascii_header.c_filesize);
1184   
1185   /* Read file name from input.  */
1186   if (file_hdr->c_name != NULL)
1187     free (file_hdr->c_name);
1188   file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize + 1);
1189   tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
1190
1191   /* HP/UX cpio creates archives that look just like ordinary archives,
1192      but for devices it sets major = 0, minor = 1, and puts the
1193      actual major/minor number in the filesize field.  See if this
1194      is an HP/UX cpio archive, and if so fix it.  We have to do this
1195      here because process_copy_in() assumes filesize is always 0
1196      for devices.  */
1197   switch (file_hdr->c_mode & CP_IFMT)
1198     {
1199       case CP_IFCHR:
1200       case CP_IFBLK:
1201 #ifdef CP_IFSOCK
1202       case CP_IFSOCK:
1203 #endif
1204 #ifdef CP_IFIFO
1205       case CP_IFIFO:
1206 #endif
1207         if (file_hdr->c_filesize != 0
1208             && file_hdr->c_rdev_maj == 0
1209             && file_hdr->c_rdev_min == 1)
1210           {
1211             file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
1212             file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
1213             file_hdr->c_filesize = 0;
1214           }
1215         break;
1216       default:
1217         break;
1218     }
1219 }
1220
1221 /* Fill in FILE_HDR by reading a new-format ASCII format cpio header from
1222    file descriptor IN_DES, except for the magic number, which is
1223    already filled in.  */
1224
1225 void
1226 read_in_new_ascii (struct cpio_file_stat *file_hdr, int in_des)
1227 {
1228   struct new_ascii_header ascii_header;
1229
1230   tape_buffered_read (ascii_header.c_ino, in_des,
1231                       sizeof ascii_header - sizeof ascii_header.c_magic);
1232
1233   file_hdr->c_ino = FROM_HEX (ascii_header.c_ino);
1234   file_hdr->c_mode = FROM_HEX (ascii_header.c_mode);
1235   file_hdr->c_uid = FROM_HEX (ascii_header.c_uid);
1236   file_hdr->c_gid = FROM_HEX (ascii_header.c_gid);
1237   file_hdr->c_nlink = FROM_HEX (ascii_header.c_nlink);
1238   file_hdr->c_mtime = FROM_HEX (ascii_header.c_mtime);
1239   file_hdr->c_filesize = FROM_HEX (ascii_header.c_filesize);
1240   file_hdr->c_dev_maj = FROM_HEX (ascii_header.c_dev_maj);
1241   file_hdr->c_dev_min = FROM_HEX (ascii_header.c_dev_min);
1242   file_hdr->c_rdev_maj = FROM_HEX (ascii_header.c_rdev_maj);
1243   file_hdr->c_rdev_min = FROM_HEX (ascii_header.c_rdev_min);
1244   file_hdr->c_namesize = FROM_HEX (ascii_header.c_namesize);
1245   file_hdr->c_chksum = FROM_HEX (ascii_header.c_chksum);
1246   
1247   /* Read file name from input.  */
1248   if (file_hdr->c_name != NULL)
1249     free (file_hdr->c_name);
1250   file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
1251   tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
1252
1253   /* In SVR4 ASCII format, the amount of space allocated for the header
1254      is rounded up to the next long-word, so we might need to drop
1255      1-3 bytes.  */
1256   tape_skip_padding (in_des, file_hdr->c_namesize + 110);
1257 }
1258
1259 /* Fill in FILE_HDR by reading a binary format cpio header from
1260    file descriptor IN_DES, except for the first 6 bytes (the magic
1261    number, device, and inode number), which are already filled in.  */
1262
1263 void
1264 read_in_binary (struct cpio_file_stat *file_hdr,
1265                 struct old_cpio_header *short_hdr,
1266                 int in_des)
1267 {
1268   file_hdr->c_magic = short_hdr->c_magic;
1269
1270   tape_buffered_read (((char *) short_hdr) + 6, in_des,
1271                       sizeof *short_hdr - 6 /* = 20 */);
1272
1273   /* If the magic number is byte swapped, fix the header.  */
1274   if (file_hdr->c_magic == swab_short ((unsigned short) 070707))
1275     {
1276       static int warned = 0;
1277
1278       /* Alert the user that they might have to do byte swapping on
1279          the file contents.  */
1280       if (warned == 0)
1281         {
1282           error (0, 0, _("warning: archive header has reverse byte-order"));
1283           warned = 1;
1284         }
1285       swab_array ((char *) &short_hdr, 13);
1286     }
1287
1288   file_hdr->c_dev_maj = major (short_hdr->c_dev);
1289   file_hdr->c_dev_min = minor (short_hdr->c_dev);
1290   file_hdr->c_ino = short_hdr->c_ino;
1291   file_hdr->c_mode = short_hdr->c_mode;
1292   file_hdr->c_uid = short_hdr->c_uid;
1293   file_hdr->c_gid = short_hdr->c_gid;
1294   file_hdr->c_nlink = short_hdr->c_nlink;
1295   file_hdr->c_rdev_maj = major (short_hdr->c_rdev);
1296   file_hdr->c_rdev_min = minor (short_hdr->c_rdev);
1297   file_hdr->c_mtime = (unsigned long) short_hdr->c_mtimes[0] << 16
1298                       | short_hdr->c_mtimes[1];
1299
1300   file_hdr->c_namesize = short_hdr->c_namesize;
1301   file_hdr->c_filesize = (unsigned long) short_hdr->c_filesizes[0] << 16
1302                       | short_hdr->c_filesizes[1];
1303
1304   /* Read file name from input.  */
1305   if (file_hdr->c_name != NULL)
1306     free (file_hdr->c_name);
1307   file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
1308   tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
1309
1310   /* In binary mode, the amount of space allocated in the header for
1311      the filename is `c_namesize' rounded up to the next short-word,
1312      so we might need to drop a byte.  */
1313   if (file_hdr->c_namesize % 2)
1314     tape_toss_input (in_des, 1L);
1315
1316   /* HP/UX cpio creates archives that look just like ordinary archives,
1317      but for devices it sets major = 0, minor = 1, and puts the
1318      actual major/minor number in the filesize field.  See if this
1319      is an HP/UX cpio archive, and if so fix it.  We have to do this
1320      here because process_copy_in() assumes filesize is always 0
1321      for devices.  */
1322   switch (file_hdr->c_mode & CP_IFMT)
1323     {
1324       case CP_IFCHR:
1325       case CP_IFBLK:
1326 #ifdef CP_IFSOCK
1327       case CP_IFSOCK:
1328 #endif
1329 #ifdef CP_IFIFO
1330       case CP_IFIFO:
1331 #endif
1332         if (file_hdr->c_filesize != 0
1333             && file_hdr->c_rdev_maj == 0
1334             && file_hdr->c_rdev_min == 1)
1335           {
1336             file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
1337             file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
1338             file_hdr->c_filesize = 0;
1339           }
1340         break;
1341       default:
1342         break;
1343     }
1344 }
1345
1346 /* Exchange the bytes of each element of the array of COUNT shorts
1347    starting at PTR.  */
1348
1349 void
1350 swab_array (char *ptr, int count)
1351 {
1352   char tmp;
1353
1354   while (count-- > 0)
1355     {
1356       tmp = *ptr;
1357       *ptr = *(ptr + 1);
1358       ++ptr;
1359       *ptr = tmp;
1360       ++ptr;
1361     }
1362 }
1363
1364 #if 0   /* Now in util.c, but different */
1365 /* Return a safer suffix of FILE_NAME, or "." if it has no safer
1366    suffix.  Check for fully specified file names and other atrocities.  */
1367
1368 static const char *
1369 safer_name_suffix (char const *file_name)
1370 {
1371   char const *p;
1372
1373   /* Skip file system prefixes, leading file name components that contain
1374      "..", and leading slashes.  */
1375
1376   size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (file_name);
1377
1378   for (p = file_name + prefix_len; *p;)
1379     {
1380       if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
1381         prefix_len = p + 2 - file_name;
1382
1383       do
1384         {
1385           char c = *p++;
1386           if (ISSLASH (c))
1387             break;
1388         }
1389       while (*p);
1390     }
1391
1392   for (p = file_name + prefix_len; ISSLASH (*p); p++)
1393     continue;
1394   prefix_len = p - file_name;
1395
1396   if (prefix_len)
1397     {
1398       char *prefix = alloca (prefix_len + 1);
1399       memcpy (prefix, file_name, prefix_len);
1400       prefix[prefix_len] = '\0';
1401
1402
1403       error (0, 0, _("Removing leading `%s' from member names"), prefix);
1404     }
1405
1406   if (!*p)
1407     p = ".";
1408
1409   return p;
1410 }
1411 #endif
1412
1413 /* Read the collection from standard input and create files
1414    in the file system.  */
1415
1416 void
1417 process_copy_in ()
1418 {
1419   char done = false;            /* True if trailer reached.  */
1420   FILE *tty_in = NULL;          /* Interactive file for rename option.  */
1421   FILE *tty_out = NULL;         /* Interactive file for rename option.  */
1422   FILE *rename_in = NULL;       /* Batch file for rename option.  */
1423   struct stat file_stat;        /* Output file stat record.  */
1424   struct cpio_file_stat file_hdr;       /* Output header information.  */
1425   int in_file_des;              /* Input file descriptor.  */
1426   char skip_file;               /* Flag for use with patterns.  */
1427   int i;                        /* Loop index variable.  */
1428
1429   umask (0);                    /* Reset umask to preserve modes of
1430                                    created files  */
1431   
1432   /* Initialize the copy in.  */
1433   if (pattern_file_name)
1434     {
1435       read_pattern_file ();
1436     }
1437   file_hdr.c_name = NULL;
1438
1439   if (rename_batch_file)
1440     {
1441       rename_in = fopen (rename_batch_file, "r");
1442       if (rename_in == NULL)
1443         {
1444           error (2, errno, TTY_NAME);
1445         }
1446     }
1447   else if (rename_flag)
1448     {
1449       /* Open interactive file pair for rename operation.  */
1450       tty_in = fopen (TTY_NAME, "r");
1451       if (tty_in == NULL)
1452         {
1453           error (2, errno, TTY_NAME);
1454         }
1455       tty_out = fopen (TTY_NAME, "w");
1456       if (tty_out == NULL)
1457         {
1458           error (2, errno, TTY_NAME);
1459         }
1460     }
1461
1462   /* Get date and time if needed for processing the table option.  */
1463   if (table_flag && verbose_flag)
1464     {
1465       time (&current_time);
1466     }
1467
1468   /* Check whether the input file might be a tape.  */
1469   in_file_des = archive_des;
1470   if (_isrmt (in_file_des))
1471     {
1472       input_is_special = 1;
1473       input_is_seekable = 0;
1474     }
1475   else
1476     {
1477       if (fstat (in_file_des, &file_stat))
1478         error (1, errno, _("standard input is closed"));
1479       input_is_special =
1480 #ifdef S_ISBLK
1481         S_ISBLK (file_stat.st_mode) ||
1482 #endif
1483         S_ISCHR (file_stat.st_mode);
1484       input_is_seekable = S_ISREG (file_stat.st_mode);
1485     }
1486   output_is_seekable = true;
1487
1488   /* While there is more input in the collection, process the input.  */
1489   while (!done)
1490     {
1491       swapping_halfwords = swapping_bytes = false;
1492
1493       /* Start processing the next file by reading the header.  */
1494       read_in_header (&file_hdr, in_file_des);
1495
1496 #ifdef DEBUG_CPIO
1497       if (debug_flag)
1498         {
1499           struct cpio_file_stat *h;
1500           h = &file_hdr;
1501           fprintf (stderr, 
1502                 "magic = 0%o, ino = %d, mode = 0%o, uid = %d, gid = %d\n",
1503                 h->c_magic, h->c_ino, h->c_mode, h->c_uid, h->c_gid);
1504           fprintf (stderr, 
1505                 "nlink = %d, mtime = %d, filesize = %d, dev_maj = 0x%x\n",
1506                 h->c_nlink, h->c_mtime, h->c_filesize, h->c_dev_maj);
1507           fprintf (stderr, 
1508                 "dev_min = 0x%x, rdev_maj = 0x%x, rdev_min = 0x%x, namesize = %d\n",
1509                 h->c_dev_min, h->c_rdev_maj, h->c_rdev_min, h->c_namesize);
1510           fprintf (stderr, 
1511                 "chksum = %d, name = \"%s\", tar_linkname = \"%s\"\n",
1512                 h->c_chksum, h->c_name, 
1513                 h->c_tar_linkname ? h->c_tar_linkname : "(null)" );
1514
1515         }
1516 #endif
1517       /* Is this the header for the TRAILER file?  */
1518       if (strcmp (CPIO_TRAILER_NAME, file_hdr.c_name) == 0)
1519         {
1520           done = true;
1521           break;
1522         }
1523
1524       cpio_safer_name_suffix (file_hdr.c_name, false, abs_paths_flag,
1525                               false);
1526       
1527       /* Does the file name match one of the given patterns?  */
1528       if (num_patterns <= 0)
1529         skip_file = false;
1530       else
1531         {
1532           skip_file = copy_matching_files;
1533           for (i = 0; i < num_patterns
1534                && skip_file == copy_matching_files; i++)
1535             {
1536               if (fnmatch (save_patterns[i], file_hdr.c_name, 0) == 0)
1537                 skip_file = !copy_matching_files;
1538             }
1539         }
1540
1541       if (skip_file)
1542         {
1543           /* If we're skipping a file with links, there might be other
1544              links that we didn't skip, and this file might have the
1545              data for the links.  If it does, we'll copy in the data
1546              to the links, but not to this file.  */
1547           if (file_hdr.c_nlink > 1 && (archive_format == arf_newascii
1548               || archive_format == arf_crcascii) )
1549             {
1550               if (create_defered_links_to_skipped(&file_hdr, in_file_des) < 0)
1551                 {
1552                   tape_toss_input (in_file_des, file_hdr.c_filesize);
1553                   tape_skip_padding (in_file_des, file_hdr.c_filesize);
1554                 }
1555             }
1556           else
1557             {
1558               tape_toss_input (in_file_des, file_hdr.c_filesize);
1559               tape_skip_padding (in_file_des, file_hdr.c_filesize);
1560             }
1561         }
1562       else if (table_flag)
1563         {
1564           list_file(&file_hdr, in_file_des);
1565         }
1566       else if (append_flag)
1567         {
1568           tape_toss_input (in_file_des, file_hdr.c_filesize);
1569           tape_skip_padding (in_file_des, file_hdr.c_filesize);
1570         }
1571       else if (only_verify_crc_flag)
1572         {
1573 #ifdef CP_IFLNK
1574           if ((file_hdr.c_mode & CP_IFMT) == CP_IFLNK)
1575             {
1576               if (archive_format != arf_tar && archive_format != arf_ustar)
1577                 {
1578                   tape_toss_input (in_file_des, file_hdr.c_filesize);
1579                   tape_skip_padding (in_file_des, file_hdr.c_filesize);
1580                   continue;
1581                 }
1582             }
1583 #endif
1584             crc = 0;
1585             tape_toss_input (in_file_des, file_hdr.c_filesize);
1586             tape_skip_padding (in_file_des, file_hdr.c_filesize);
1587             if (crc != file_hdr.c_chksum)
1588               {
1589                 error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
1590                        file_hdr.c_name, crc, file_hdr.c_chksum);
1591               }
1592          /* Debian hack: -v and -V now work with --only-verify-crc.
1593             (99/11/10) -BEM */
1594             if (verbose_flag)
1595               {
1596                 fprintf (stderr, "%s\n", file_hdr.c_name);
1597               }
1598             if (dot_flag)
1599               {
1600                 fputc ('.', stderr);
1601               }
1602         }
1603       else
1604         {
1605           /* Copy the input file into the directory structure.  */
1606
1607           /* Do we need to rename the file? */
1608           if (rename_flag || rename_batch_file)
1609             {
1610               if (query_rename(&file_hdr, tty_in, tty_out, rename_in) < 0)
1611                 {
1612                   tape_toss_input (in_file_des, file_hdr.c_filesize);
1613                   tape_skip_padding (in_file_des, file_hdr.c_filesize);
1614                   continue;
1615                 }
1616             }
1617
1618           copyin_file(&file_hdr, in_file_des);
1619
1620           if (verbose_flag)
1621             fprintf (stderr, "%s\n", file_hdr.c_name);
1622           if (dot_flag)
1623             fputc ('.', stderr);
1624         }
1625     }
1626
1627   if (dot_flag)
1628     fputc ('\n', stderr);
1629
1630   if (append_flag)
1631     return;
1632
1633   if (archive_format == arf_newascii || archive_format == arf_crcascii)
1634     {
1635       create_final_defers ();
1636     }
1637   if (!quiet_flag)
1638     {
1639       int blocks;
1640       blocks = (input_bytes + io_block_size - 1) / io_block_size;
1641       fprintf (stderr, ngettext ("%d block\n", "%d blocks\n", blocks), blocks);
1642     }
1643 }
1644