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