Add a new option -s. With this option pkg_search(1) will display the description
[dragonfly.git] / contrib / cpio / util.c
blob216041fd6c08dc56f9e0f316d6b26dfc447d5fc9
1 /* util.c - Several utility routines for cpio.
2 Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
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.
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.
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. */
18 #include <stdio.h>
19 #include <sys/types.h>
20 #ifdef HPUX_CDF
21 #include <sys/stat.h>
22 #endif
23 #include "system.h"
24 #include "cpiohdr.h"
25 #include "dstring.h"
26 #include "extern.h"
27 #include "rmt.h"
29 #ifndef __MSDOS__
30 #include <sys/ioctl.h>
31 #else
32 #include <io.h>
33 #endif
35 #ifdef HAVE_SYS_MTIO_H
36 #ifdef HAVE_SYS_IO_TRIOCTL_H
37 #include <sys/io/trioctl.h>
38 #endif
39 #include <sys/mtio.h>
40 #endif
42 static void tape_fill_input_buffer P_((int in_des, int num_bytes));
43 static int disk_fill_input_buffer P_((int in_des, int num_bytes));
44 static void hash_insert ();
45 static void write_nuls_to_file P_((long num_bytes, int out_des));
47 /* Write `output_size' bytes of `output_buffer' to file
48 descriptor OUT_DES and reset `output_size' and `out_buff'. */
50 void
51 tape_empty_output_buffer (out_des)
52 int out_des;
54 int bytes_written;
56 #ifdef BROKEN_LONG_TAPE_DRIVER
57 static long output_bytes_before_lseek = 0;
59 /* Some tape drivers seem to have a signed internal seek pointer and
60 they lose if it overflows and becomes negative (e.g. when writing
61 tapes > 2Gb). Doing an lseek (des, 0, SEEK_SET) seems to reset the
62 seek pointer and prevent it from overflowing. */
63 if (output_is_special
64 && ( (output_bytes_before_lseek += output_size) >= 1073741824L) )
66 lseek(out_des, 0L, SEEK_SET);
67 output_bytes_before_lseek = 0;
69 #endif
71 bytes_written = rmtwrite (out_des, output_buffer, output_size);
72 if (bytes_written != output_size)
74 int rest_bytes_written;
75 int rest_output_size;
77 if (output_is_special
78 && (bytes_written >= 0
79 || (bytes_written < 0
80 && (errno == ENOSPC || errno == EIO || errno == ENXIO))))
82 get_next_reel (out_des);
83 if (bytes_written > 0)
84 rest_output_size = output_size - bytes_written;
85 else
86 rest_output_size = output_size;
87 rest_bytes_written = rmtwrite (out_des, output_buffer,
88 rest_output_size);
89 if (rest_bytes_written != rest_output_size)
90 error (1, errno, "write error");
92 else
93 error (1, errno, "write error");
95 output_bytes += output_size;
96 out_buff = output_buffer;
97 output_size = 0;
100 /* Write `output_size' bytes of `output_buffer' to file
101 descriptor OUT_DES and reset `output_size' and `out_buff'.
102 If `swapping_halfwords' or `swapping_bytes' is set,
103 do the appropriate swapping first. Our callers have
104 to make sure to only set these flags if `output_size'
105 is appropriate (a multiple of 4 for `swapping_halfwords',
106 2 for `swapping_bytes'). The fact that DISK_IO_BLOCK_SIZE
107 must always be a multiple of 4 helps us (and our callers)
108 insure this. */
110 void
111 disk_empty_output_buffer (out_des)
112 int out_des;
114 int bytes_written;
116 if (swapping_halfwords || swapping_bytes)
118 if (swapping_halfwords)
120 int complete_words;
121 complete_words = output_size / 4;
122 swahw_array (output_buffer, complete_words);
123 if (swapping_bytes)
124 swab_array (output_buffer, 2 * complete_words);
126 else
128 int complete_halfwords;
129 complete_halfwords = output_size /2;
130 swab_array (output_buffer, complete_halfwords);
134 if (sparse_flag)
135 bytes_written = sparse_write (out_des, output_buffer, output_size);
136 else
137 bytes_written = write (out_des, output_buffer, output_size);
139 if (bytes_written != output_size)
141 error (1, errno, "write error");
143 output_bytes += output_size;
144 out_buff = output_buffer;
145 output_size = 0;
148 /* Exchange the halfwords of each element of the array of COUNT longs
149 starting at PTR. PTR does not have to be aligned at a word
150 boundary. */
152 void
153 swahw_array (ptr, count)
154 char *ptr;
155 int count;
157 char tmp;
159 for (; count > 0; --count)
161 tmp = *ptr;
162 *ptr = *(ptr + 2);
163 *(ptr + 2) = tmp;
164 ++ptr;
165 tmp = *ptr;
166 *ptr = *(ptr + 2);
167 *(ptr + 2) = tmp;
168 ptr += 3;
172 /* Read at most NUM_BYTES or `io_block_size' bytes, whichever is smaller,
173 into the start of `input_buffer' from file descriptor IN_DES.
174 Set `input_size' to the number of bytes read and reset `in_buff'.
175 Exit with an error if end of file is reached. */
177 #ifdef BROKEN_LONG_TAPE_DRIVER
178 static long input_bytes_before_lseek = 0;
179 #endif
181 static void
182 tape_fill_input_buffer (in_des, num_bytes)
183 int in_des;
184 int num_bytes;
186 #ifdef BROKEN_LONG_TAPE_DRIVER
187 /* Some tape drivers seem to have a signed internal seek pointer and
188 they lose if it overflows and becomes negative (e.g. when writing
189 tapes > 4Gb). Doing an lseek (des, 0, SEEK_SET) seems to reset the
190 seek pointer and prevent it from overflowing. */
191 if (input_is_special
192 && ( (input_bytes_before_lseek += num_bytes) >= 1073741824L) )
194 lseek(in_des, 0L, SEEK_SET);
195 input_bytes_before_lseek = 0;
197 #endif
198 in_buff = input_buffer;
199 num_bytes = (num_bytes < io_block_size) ? num_bytes : io_block_size;
200 input_size = rmtread (in_des, input_buffer, num_bytes);
201 if (input_size == 0 && input_is_special)
203 get_next_reel (in_des);
204 input_size = rmtread (in_des, input_buffer, num_bytes);
206 if (input_size < 0)
207 error (1, errno, "read error");
208 if (input_size == 0)
210 error (0, 0, "premature end of file");
211 exit (1);
213 input_bytes += input_size;
216 /* Read at most NUM_BYTES or `DISK_IO_BLOCK_SIZE' bytes, whichever is smaller,
217 into the start of `input_buffer' from file descriptor IN_DES.
218 Set `input_size' to the number of bytes read and reset `in_buff'.
219 Exit with an error if end of file is reached. */
221 static int
222 disk_fill_input_buffer (in_des, num_bytes)
223 int in_des;
224 int num_bytes;
226 in_buff = input_buffer;
227 num_bytes = (num_bytes < DISK_IO_BLOCK_SIZE) ? num_bytes : DISK_IO_BLOCK_SIZE;
228 input_size = read (in_des, input_buffer, num_bytes);
229 if (input_size < 0)
231 input_size = 0;
232 return (-1);
234 else if (input_size == 0)
235 return (1);
236 input_bytes += input_size;
237 return (0);
240 /* Copy NUM_BYTES of buffer IN_BUF to `out_buff', which may be partly full.
241 When `out_buff' fills up, flush it to file descriptor OUT_DES. */
243 void
244 tape_buffered_write (in_buf, out_des, num_bytes)
245 char *in_buf;
246 int out_des;
247 long num_bytes;
249 register long bytes_left = num_bytes; /* Bytes needing to be copied. */
250 register long space_left; /* Room left in output buffer. */
252 while (bytes_left > 0)
254 space_left = io_block_size - output_size;
255 if (space_left == 0)
256 tape_empty_output_buffer (out_des);
257 else
259 if (bytes_left < space_left)
260 space_left = bytes_left;
261 bcopy (in_buf, out_buff, (unsigned) space_left);
262 out_buff += space_left;
263 output_size += space_left;
264 in_buf += space_left;
265 bytes_left -= space_left;
270 /* Copy NUM_BYTES of buffer IN_BUF to `out_buff', which may be partly full.
271 When `out_buff' fills up, flush it to file descriptor OUT_DES. */
273 void
274 disk_buffered_write (in_buf, out_des, num_bytes)
275 char *in_buf;
276 int out_des;
277 long num_bytes;
279 register long bytes_left = num_bytes; /* Bytes needing to be copied. */
280 register long space_left; /* Room left in output buffer. */
282 while (bytes_left > 0)
284 space_left = DISK_IO_BLOCK_SIZE - output_size;
285 if (space_left == 0)
286 disk_empty_output_buffer (out_des);
287 else
289 if (bytes_left < space_left)
290 space_left = bytes_left;
291 bcopy (in_buf, out_buff, (unsigned) space_left);
292 out_buff += space_left;
293 output_size += space_left;
294 in_buf += space_left;
295 bytes_left -= space_left;
300 /* Copy NUM_BYTES of buffer `in_buff' into IN_BUF.
301 `in_buff' may be partly full.
302 When `in_buff' is exhausted, refill it from file descriptor IN_DES. */
304 void
305 tape_buffered_read (in_buf, in_des, num_bytes)
306 char *in_buf;
307 int in_des;
308 long num_bytes;
310 register long bytes_left = num_bytes; /* Bytes needing to be copied. */
311 register long space_left; /* Bytes to copy from input buffer. */
313 while (bytes_left > 0)
315 if (input_size == 0)
316 tape_fill_input_buffer (in_des, io_block_size);
317 if (bytes_left < input_size)
318 space_left = bytes_left;
319 else
320 space_left = input_size;
321 bcopy (in_buff, in_buf, (unsigned) space_left);
322 in_buff += space_left;
323 in_buf += space_left;
324 input_size -= space_left;
325 bytes_left -= space_left;
329 /* Copy the the next NUM_BYTES bytes of `input_buffer' into PEEK_BUF.
330 If NUM_BYTES bytes are not available, read the next `io_block_size' bytes
331 into the end of `input_buffer' and update `input_size'.
333 Return the number of bytes copied into PEEK_BUF.
334 If the number of bytes returned is less than NUM_BYTES,
335 then EOF has been reached. */
338 tape_buffered_peek (peek_buf, in_des, num_bytes)
339 char *peek_buf;
340 int in_des;
341 int num_bytes;
343 long tmp_input_size;
344 long got_bytes;
345 char *append_buf;
347 #ifdef BROKEN_LONG_TAPE_DRIVER
348 /* Some tape drivers seem to have a signed internal seek pointer and
349 they lose if it overflows and becomes negative (e.g. when writing
350 tapes > 4Gb). Doing an lseek (des, 0, SEEK_SET) seems to reset the
351 seek pointer and prevent it from overflowing. */
352 if (input_is_special
353 && ( (input_bytes_before_lseek += num_bytes) >= 1073741824L) )
355 lseek(in_des, 0L, SEEK_SET);
356 input_bytes_before_lseek = 0;
358 #endif
360 while (input_size < num_bytes)
362 append_buf = in_buff + input_size;
363 if ( (append_buf - input_buffer) >= input_buffer_size)
365 /* We can keep up to 2 "blocks" (either the physical block size
366 or 512 bytes(the size of a tar record), which ever is
367 larger) in the input buffer when we are peeking. We
368 assume that our caller will never be interested in peeking
369 ahead at more than 512 bytes, so we know that by the time
370 we need a 3rd "block" in the buffer we can throw away the
371 first block to make room. */
372 int half;
373 half = input_buffer_size / 2;
374 bcopy (input_buffer + half, input_buffer, half);
375 in_buff = in_buff - half;
376 append_buf = append_buf - half;
378 tmp_input_size = rmtread (in_des, append_buf, io_block_size);
379 if (tmp_input_size == 0)
381 if (input_is_special)
383 get_next_reel (in_des);
384 tmp_input_size = rmtread (in_des, append_buf, io_block_size);
386 else
387 break;
389 if (tmp_input_size < 0)
390 error (1, errno, "read error");
391 input_bytes += tmp_input_size;
392 input_size += tmp_input_size;
394 if (num_bytes <= input_size)
395 got_bytes = num_bytes;
396 else
397 got_bytes = input_size;
398 bcopy (in_buff, peek_buf, (unsigned) got_bytes);
399 return got_bytes;
402 /* Skip the next NUM_BYTES bytes of file descriptor IN_DES. */
404 void
405 tape_toss_input (in_des, num_bytes)
406 int in_des;
407 long num_bytes;
409 register long bytes_left = num_bytes; /* Bytes needing to be copied. */
410 register long space_left; /* Bytes to copy from input buffer. */
412 while (bytes_left > 0)
414 if (input_size == 0)
415 tape_fill_input_buffer (in_des, io_block_size);
416 if (bytes_left < input_size)
417 space_left = bytes_left;
418 else
419 space_left = input_size;
421 if (crc_i_flag && only_verify_crc_flag)
423 int k;
424 for (k = 0; k < space_left; ++k)
425 crc += in_buff[k] & 0xff;
428 in_buff += space_left;
429 input_size -= space_left;
430 bytes_left -= space_left;
434 /* Copy a file using the input and output buffers, which may start out
435 partly full. After the copy, the files are not closed nor the last
436 block flushed to output, and the input buffer may still be partly
437 full. If `crc_i_flag' is set, add each byte to `crc'.
438 IN_DES is the file descriptor for input;
439 OUT_DES is the file descriptor for output;
440 NUM_BYTES is the number of bytes to copy. */
442 void
443 copy_files_tape_to_disk (in_des, out_des, num_bytes)
444 int in_des;
445 int out_des;
446 long num_bytes;
448 long size;
449 long k;
451 while (num_bytes > 0)
453 if (input_size == 0)
454 tape_fill_input_buffer (in_des, io_block_size);
455 size = (input_size < num_bytes) ? input_size : num_bytes;
456 if (crc_i_flag)
458 for (k = 0; k < size; ++k)
459 crc += in_buff[k] & 0xff;
461 disk_buffered_write (in_buff, out_des, size);
462 num_bytes -= size;
463 input_size -= size;
464 in_buff += size;
467 /* Copy a file using the input and output buffers, which may start out
468 partly full. After the copy, the files are not closed nor the last
469 block flushed to output, and the input buffer may still be partly
470 full. If `crc_i_flag' is set, add each byte to `crc'.
471 IN_DES is the file descriptor for input;
472 OUT_DES is the file descriptor for output;
473 NUM_BYTES is the number of bytes to copy. */
475 void
476 copy_files_disk_to_tape (in_des, out_des, num_bytes, filename)
477 int in_des;
478 int out_des;
479 long num_bytes;
480 char *filename;
482 long size;
483 long k;
484 int rc;
485 long original_num_bytes;
487 original_num_bytes = num_bytes;
489 while (num_bytes > 0)
491 if (input_size == 0)
492 if (rc = disk_fill_input_buffer (in_des, DISK_IO_BLOCK_SIZE))
494 if (rc > 0)
495 error (0, 0, "File %s shrunk by %ld bytes, padding with zeros",
496 filename, num_bytes);
497 else
498 error (0, 0, "Read error at byte %ld in file %s, padding with zeros",
499 original_num_bytes - num_bytes, filename);
500 write_nuls_to_file (num_bytes, out_des);
501 break;
503 size = (input_size < num_bytes) ? input_size : num_bytes;
504 if (crc_i_flag)
506 for (k = 0; k < size; ++k)
507 crc += in_buff[k] & 0xff;
509 tape_buffered_write (in_buff, out_des, size);
510 num_bytes -= size;
511 input_size -= size;
512 in_buff += size;
515 /* Copy a file using the input and output buffers, which may start out
516 partly full. After the copy, the files are not closed nor the last
517 block flushed to output, and the input buffer may still be partly
518 full. If `crc_i_flag' is set, add each byte to `crc'.
519 IN_DES is the file descriptor for input;
520 OUT_DES is the file descriptor for output;
521 NUM_BYTES is the number of bytes to copy. */
523 void
524 copy_files_disk_to_disk (in_des, out_des, num_bytes, filename)
525 int in_des;
526 int out_des;
527 long num_bytes;
528 char *filename;
530 long size;
531 long k;
532 long original_num_bytes;
533 int rc;
535 original_num_bytes = num_bytes;
536 while (num_bytes > 0)
538 if (input_size == 0)
539 if (rc = disk_fill_input_buffer (in_des, DISK_IO_BLOCK_SIZE))
541 if (rc > 0)
542 error (0, 0, "File %s shrunk by %ld bytes, padding with zeros",
543 filename, num_bytes);
544 else
545 error (0, 0, "Read error at byte %ld in file %s, padding with zeros",
546 original_num_bytes - num_bytes, filename);
547 write_nuls_to_file (num_bytes, out_des);
548 break;
550 size = (input_size < num_bytes) ? input_size : num_bytes;
551 if (crc_i_flag)
553 for (k = 0; k < size; ++k)
554 crc += in_buff[k] & 0xff;
556 disk_buffered_write (in_buff, out_des, size);
557 num_bytes -= size;
558 input_size -= size;
559 in_buff += size;
563 /* Create all directories up to but not including the last part of NAME.
564 Do not destroy any nondirectories while creating directories. */
566 void
567 create_all_directories (name)
568 char *name;
570 char *dir;
571 int mode;
572 #ifdef HPUX_CDF
573 int cdf;
574 #endif
576 dir = dirname (name);
577 mode = 0700;
578 #ifdef HPUX_CDF
579 cdf = islastparentcdf (name);
580 if (cdf)
582 dir [strlen (dir) - 1] = '\0'; /* remove final + */
583 mode = 04700;
586 #endif
588 if (dir == NULL)
589 error (2, 0, "virtual memory exhausted");
591 if (dir[0] != '.' || dir[1] != '\0')
592 make_path (dir, mode, 0700, -1, -1, (char *) NULL);
594 free (dir);
597 /* Prepare to append to an archive. We have been in
598 process_copy_in, keeping track of the position where
599 the last header started in `last_header_start'. Now we
600 have the starting position of the last header (the TRAILER!!!
601 header, or blank record for tar archives) and we want to start
602 writing (appending) over the last header. The last header may
603 be in the middle of a block, so to keep the buffering in sync
604 we lseek back to the start of the block, read everything up
605 to but not including the last header, lseek back to the start
606 of the block, and then do a copy_buf_out of what we read.
607 Actually, we probably don't have to worry so much about keeping the
608 buffering perfect since you can only append to archives that
609 are disk files. */
611 void
612 prepare_append (out_file_des)
613 int out_file_des;
615 int start_of_header;
616 int start_of_block;
617 int useful_bytes_in_block;
618 char *tmp_buf;
620 start_of_header = last_header_start;
621 /* Figure out how many bytes we will rewrite, and where they start. */
622 useful_bytes_in_block = start_of_header % io_block_size;
623 start_of_block = start_of_header - useful_bytes_in_block;
625 if (lseek (out_file_des, start_of_block, SEEK_SET) < 0)
626 error (1, errno, "cannot seek on output");
627 if (useful_bytes_in_block > 0)
629 tmp_buf = (char *) xmalloc (useful_bytes_in_block);
630 read (out_file_des, tmp_buf, useful_bytes_in_block);
631 if (lseek (out_file_des, start_of_block, SEEK_SET) < 0)
632 error (1, errno, "cannot seek on output");
633 /* fix juo -- is this copy_tape_buf_out? or copy_disk? */
634 tape_buffered_write (tmp_buf, out_file_des, useful_bytes_in_block);
635 free (tmp_buf);
638 /* We are done reading the archive, so clear these since they
639 will now be used for reading in files that we are appending
640 to the archive. */
641 input_size = 0;
642 input_bytes = 0;
643 in_buff = input_buffer;
646 /* Support for remembering inodes with multiple links. Used in the
647 "copy in" and "copy pass" modes for making links instead of copying
648 the file. */
650 struct inode_val
652 unsigned long inode;
653 unsigned long major_num;
654 unsigned long minor_num;
655 char *file_name;
658 /* Inode hash table. Allocated by first call to add_inode. */
659 static struct inode_val **hash_table = NULL;
661 /* Size of current hash table. Initial size is 47. (47 = 2*22 + 3) */
662 static int hash_size = 22;
664 /* Number of elements in current hash table. */
665 static int hash_num;
667 /* Find the file name associated with NODE_NUM. If there is no file
668 associated with NODE_NUM, return NULL. */
670 char *
671 find_inode_file (node_num, major_num, minor_num)
672 unsigned long node_num;
673 unsigned long major_num;
674 unsigned long minor_num;
676 #ifndef __MSDOS__
677 int start; /* Initial hash location. */
678 int temp; /* Rehash search variable. */
680 if (hash_table != NULL)
682 /* Hash function is node number modulo the table size. */
683 start = node_num % hash_size;
685 /* Initial look into the table. */
686 if (hash_table[start] == NULL)
687 return NULL;
688 if (hash_table[start]->inode == node_num
689 && hash_table[start]->major_num == major_num
690 && hash_table[start]->minor_num == minor_num)
691 return hash_table[start]->file_name;
693 /* The home position is full with a different inode record.
694 Do a linear search terminated by a NULL pointer. */
695 for (temp = (start + 1) % hash_size;
696 hash_table[temp] != NULL && temp != start;
697 temp = (temp + 1) % hash_size)
699 if (hash_table[temp]->inode == node_num
700 && hash_table[start]->major_num == major_num
701 && hash_table[start]->minor_num == minor_num)
702 return hash_table[temp]->file_name;
705 #endif
706 return NULL;
709 /* Associate FILE_NAME with the inode NODE_NUM. (Insert into hash table.) */
711 void
712 add_inode (node_num, file_name, major_num, minor_num)
713 unsigned long node_num;
714 char *file_name;
715 unsigned long major_num;
716 unsigned long minor_num;
718 #ifndef __MSDOS__
719 struct inode_val *temp;
721 /* Create new inode record. */
722 temp = (struct inode_val *) xmalloc (sizeof (struct inode_val));
723 temp->inode = node_num;
724 temp->major_num = major_num;
725 temp->minor_num = minor_num;
726 temp->file_name = xstrdup (file_name);
728 /* Do we have to increase the size of (or initially allocate)
729 the hash table? */
730 if (hash_num == hash_size || hash_table == NULL)
732 struct inode_val **old_table; /* Pointer to old table. */
733 int i; /* Index for re-insert loop. */
735 /* Save old table. */
736 old_table = hash_table;
737 if (old_table == NULL)
738 hash_num = 0;
740 /* Calculate new size of table and allocate it.
741 Sequence of table sizes is 47, 97, 197, 397, 797, 1597, 3197, 6397 ...
742 where 3197 and most of the sizes after 6397 are not prime. The other
743 numbers listed are prime. */
744 hash_size = 2 * hash_size + 3;
745 hash_table = (struct inode_val **)
746 xmalloc (hash_size * sizeof (struct inode_val *));
747 bzero (hash_table, hash_size * sizeof (struct inode_val *));
749 /* Insert the values from the old table into the new table. */
750 for (i = 0; i < hash_num; i++)
751 hash_insert (old_table[i]);
753 if (old_table != NULL)
754 free (old_table);
757 /* Insert the new record and increment the count of elements in the
758 hash table. */
759 hash_insert (temp);
760 hash_num++;
761 #endif /* __MSDOS__ */
764 /* Do the hash insert. Used in normal inserts and resizing the hash
765 table. It is guaranteed that there is room to insert the item.
766 NEW_VALUE is the pointer to the previously allocated inode, file
767 name association record. */
769 static void
770 hash_insert (new_value)
771 struct inode_val *new_value;
773 int start; /* Home position for the value. */
774 int temp; /* Used for rehashing. */
776 /* Hash function is node number modulo the table size. */
777 start = new_value->inode % hash_size;
779 /* Do the initial look into the table. */
780 if (hash_table[start] == NULL)
782 hash_table[start] = new_value;
783 return;
786 /* If we get to here, the home position is full with a different inode
787 record. Do a linear search for the first NULL pointer and insert
788 the new item there. */
789 temp = (start + 1) % hash_size;
790 while (hash_table[temp] != NULL)
791 temp = (temp + 1) % hash_size;
793 /* Insert at the NULL. */
794 hash_table[temp] = new_value;
797 /* Open FILE in the mode specified by the command line options
798 and return an open file descriptor for it,
799 or -1 if it can't be opened. */
802 open_archive (file)
803 char *file;
805 int fd;
806 void (*copy_in) (); /* Workaround for pcc bug. */
808 copy_in = process_copy_in;
810 if (copy_function == copy_in)
811 fd = rmtopen (file, O_RDONLY | O_BINARY, 0666);
812 else
814 if (!append_flag)
815 fd = rmtopen (file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
816 else
817 fd = rmtopen (file, O_RDWR | O_BINARY, 0666);
820 return fd;
823 /* Attempt to rewind the tape drive on file descriptor TAPE_DES
824 and take it offline. */
826 void
827 tape_offline (tape_des)
828 int tape_des;
830 #if defined(MTIOCTOP) && defined(MTOFFL)
831 struct mtop control;
833 control.mt_op = MTOFFL;
834 control.mt_count = 1;
835 rmtioctl (tape_des, MTIOCTOP, &control); /* Don't care if it fails. */
836 #endif
839 /* The file on file descriptor TAPE_DES is assumed to be magnetic tape
840 (or floppy disk or other device) and the end of the medium
841 has been reached. Ask the user for to mount a new "tape" to continue
842 the processing. If the user specified the device name on the
843 command line (with the -I, -O, -F or --file options), then we can
844 automatically re-open the same device to use the next medium. If the
845 user did not specify the device name, then we have to ask them which
846 device to use. */
848 void
849 get_next_reel (tape_des)
850 int tape_des;
852 static int reel_number = 1;
853 FILE *tty_in; /* File for interacting with user. */
854 FILE *tty_out; /* File for interacting with user. */
855 int old_tape_des;
856 char *next_archive_name;
857 dynamic_string new_name;
858 char *str_res;
860 ds_init (&new_name, 128);
862 /* Open files for interactive communication. */
863 tty_in = fopen (CONSOLE, "r");
864 if (tty_in == NULL)
865 error (2, errno, CONSOLE);
866 tty_out = fopen (CONSOLE, "w");
867 if (tty_out == NULL)
868 error (2, errno, CONSOLE);
870 old_tape_des = tape_des;
871 tape_offline (tape_des);
872 rmtclose (tape_des);
874 /* Give message and wait for carrage return. User should hit carrage return
875 only after loading the next tape. */
876 ++reel_number;
877 if (new_media_message)
878 fprintf (tty_out, "%s", new_media_message);
879 else if (new_media_message_with_number)
880 fprintf (tty_out, "%s%d%s", new_media_message_with_number, reel_number,
881 new_media_message_after_number);
882 else if (archive_name)
883 fprintf (tty_out, "Found end of volume. Load next volume and press RETURN. ");
884 else
885 fprintf (tty_out, "Found end of volume. To continue, type device/file name when ready.\n");
887 fflush (tty_out);
889 if (archive_name)
891 int c;
894 c = getc (tty_in);
895 while (c != EOF && c != '\n');
897 tape_des = open_archive (archive_name);
898 if (tape_des == -1)
899 error (1, errno, "%s", archive_name);
901 else
905 if (tape_des < 0)
907 fprintf (tty_out,
908 "To continue, type device/file name when ready.\n");
909 fflush (tty_out);
912 str_res = ds_fgets (tty_in, &new_name);
913 if (str_res == NULL || str_res[0] == '\0')
914 exit (1);
915 next_archive_name = str_res;
917 tape_des = open_archive (next_archive_name);
918 if (tape_des == -1)
919 error (0, errno, "%s", next_archive_name);
921 while (tape_des < 0);
924 /* We have to make sure that `tape_des' has not changed its value even
925 though we closed it and reopened it, since there are local
926 copies of it in other routines. This works fine on Unix (even with
927 rmtread and rmtwrite) since open will always return the lowest
928 available file descriptor and we haven't closed any files (e.g.,
929 stdin, stdout or stderr) that were opened before we originally opened
930 the archive. */
932 if (tape_des != old_tape_des)
933 error (1, 0, "internal error: tape descriptor changed from %d to %d",
934 old_tape_des, tape_des);
936 free (new_name.ds_string);
937 fclose (tty_in);
938 fclose (tty_out);
941 /* If MESSAGE does not contain the string "%d", make `new_media_message'
942 a copy of MESSAGE. If MESSAGES does contain the string "%d", make
943 `new_media_message_with_number' a copy of MESSAGE up to, but
944 not including, the string "%d", and make `new_media_message_after_number'
945 a copy of MESSAGE after the string "%d". */
947 void
948 set_new_media_message (message)
949 char *message;
951 char *p;
952 int prev_was_percent;
954 p = message;
955 prev_was_percent = 0;
956 while (*p != '\0')
958 if (*p == 'd' && prev_was_percent)
959 break;
960 prev_was_percent = (*p == '%');
961 ++p;
963 if (*p == '\0')
965 new_media_message = xstrdup (message);
967 else
969 int length = p - message - 1;
971 new_media_message_with_number = xmalloc (length + 1);
972 strncpy (new_media_message_with_number, message, length);
973 new_media_message_with_number[length] = '\0';
974 length = strlen (p + 1);
975 new_media_message_after_number = xmalloc (length + 1);
976 strcpy (new_media_message_after_number, p + 1);
980 #ifdef SYMLINK_USES_UMASK
981 /* Most machines always create symlinks with rwxrwxrwx protection,
982 but some (HP/UX 8.07; maybe DEC's OSF on MIPS, too?) use the
983 umask when creating symlinks, so if your umask is 022 you end
984 up with rwxr-xr-x symlinks (although HP/UX seems to completely
985 ignore the protection). There doesn't seem to be any way to
986 manipulate the modes once the symlinks are created (e.g.
987 a hypothetical "lchmod"), so to create them with the right
988 modes we have to set the umask first. */
991 umasked_symlink (name1, name2, mode)
992 char *name1;
993 char *name2;
994 int mode;
996 int old_umask;
997 int rc;
998 mode = ~(mode & 0777) & 0777;
999 old_umask = umask (mode);
1000 rc = symlink (name1, name2);
1001 umask (old_umask);
1002 return rc;
1004 #endif /* SYMLINK_USES_UMASK */
1006 #if defined(__MSDOS__) && !defined(__GNUC__)
1008 chown (path, owner, group)
1009 char *path;
1010 int owner, group;
1012 return 0;
1014 #endif
1016 #ifdef __TURBOC__
1017 #include <time.h>
1018 #include <fcntl.h>
1019 #include <io.h>
1022 utime (char *filename, struct utimbuf *utb)
1024 extern int errno;
1025 struct tm *tm;
1026 struct ftime filetime;
1027 time_t when;
1028 int fd;
1029 int status;
1031 if (utb == 0)
1032 when = time (0);
1033 else
1034 when = utb->modtime;
1036 fd = _open (filename, O_RDWR);
1037 if (fd == -1)
1038 return -1;
1040 tm = localtime (&when);
1041 if (tm->tm_year < 80)
1042 filetime.ft_year = 0;
1043 else
1044 filetime.ft_year = tm->tm_year - 80;
1045 filetime.ft_month = tm->tm_mon + 1;
1046 filetime.ft_day = tm->tm_mday;
1047 if (tm->tm_hour < 0)
1048 filetime.ft_hour = 0;
1049 else
1050 filetime.ft_hour = tm->tm_hour;
1051 filetime.ft_min = tm->tm_min;
1052 filetime.ft_tsec = tm->tm_sec / 2;
1054 status = setftime (fd, &filetime);
1055 _close (fd);
1056 return status;
1058 #endif
1059 #ifdef HPUX_CDF
1060 /* When we create a cpio archive we mark CDF's by putting an extra `/'
1061 after their component name so we can distinguish the CDF's when we
1062 extract the archive (in case the "hidden" directory's files appear
1063 in the archive before the directory itself). E.g., in the path
1064 "a/b+/c", if b+ is a CDF, we will write this path as "a/b+//c" in
1065 the archive so when we extract the archive we will know that b+
1066 is actually a CDF, and not an ordinary directory whose name happens
1067 to end in `+'. We also do the same thing internally in copypass.c. */
1070 /* Take an input pathname and check it for CDF's. Insert an extra
1071 `/' in the pathname after each "hidden" directory. If we add
1072 any `/'s, return a malloced string (which it will reuse for
1073 later calls so our caller doesn't have to worry about freeing
1074 the string) instead of the original input string. */
1076 char *
1077 add_cdf_double_slashes (input_name)
1078 char *input_name;
1080 static char *ret_name = NULL; /* re-usuable return buffer (malloc'ed) */
1081 static int ret_size = -1; /* size of return buffer. */
1082 char *p;
1083 char *q;
1084 int n;
1085 struct stat dir_stat;
1087 /* Search for a `/' preceeded by a `+'. */
1089 for (p = input_name; *p != '\0'; ++p)
1091 if ( (*p == '+') && (*(p + 1) == '/') )
1092 break;
1095 /* If we didn't find a `/' preceeded by a `+' then there are
1096 no CDF's in this pathname. Return the original pathname. */
1098 if (*p == '\0')
1099 return input_name;
1101 /* There was a `/' preceeded by a `+' in the pathname. If it is a CDF
1102 then we will need to copy the input pathname to our return
1103 buffer so we can insert the extra `/'s. Since we can't tell
1104 yet whether or not it is a CDF we will just always copy the
1105 string to the return buffer. First we have to make sure the
1106 buffer is large enough to hold the string and any number of
1107 extra `/'s we might add. */
1109 n = 2 * (strlen (input_name) + 1);
1110 if (n >= ret_size)
1112 if (ret_size < 0)
1113 ret_name = (char *) malloc (n);
1114 else
1115 ret_name = (char *)realloc (ret_name, n);
1116 ret_size = n;
1119 /* Clear the `/' after this component, so we can stat the pathname
1120 up to and including this component. */
1121 ++p;
1122 *p = '\0';
1123 if ((*xstat) (input_name, &dir_stat) < 0)
1125 error (0, errno, "%s", input_name);
1126 return input_name;
1129 /* Now put back the `/' after this component and copy the pathname up to
1130 and including this component and its trailing `/' to the return
1131 buffer. */
1132 *p++ = '/';
1133 strncpy (ret_name, input_name, p - input_name);
1134 q = ret_name + (p - input_name);
1136 /* If it was a CDF, add another `/'. */
1137 if (S_ISDIR (dir_stat.st_mode) && (dir_stat.st_mode & 04000) )
1138 *q++ = '/';
1140 /* Go through the rest of the input pathname, copying it to the
1141 return buffer, and adding an extra `/' after each CDF. */
1142 while (*p != '\0')
1144 if ( (*p == '+') && (*(p + 1) == '/') )
1146 *q++ = *p++;
1148 *p = '\0';
1149 if ((*xstat) (input_name, &dir_stat) < 0)
1151 error (0, errno, "%s", input_name);
1152 return input_name;
1154 *p = '/';
1156 if (S_ISDIR (dir_stat.st_mode) && (dir_stat.st_mode & 04000) )
1157 *q++ = '/';
1159 *q++ = *p++;
1161 *q = '\0';
1163 return ret_name;
1166 /* Is the last parent directory (e.g., c in a/b/c/d) a CDF? If the
1167 directory name ends in `+' and is followed by 2 `/'s instead of 1
1168 then it is. This is only the case for cpio archives, but we don't
1169 have to worry about tar because tar always has the directory before
1170 its files (or else we lose). */
1172 islastparentcdf(path)
1173 char *path;
1175 char *newpath;
1176 char *slash;
1177 int slash_count;
1178 int length; /* Length of result, not including NUL. */
1180 slash = rindex (path, '/');
1181 if (slash == 0)
1182 return 0;
1183 else
1185 slash_count = 0;
1186 while (slash > path && *slash == '/')
1188 ++slash_count;
1189 --slash;
1193 if ( (*slash == '+') && (slash_count >= 2) )
1194 return 1;
1196 return 0;
1198 #endif
1200 #define DISKBLOCKSIZE (512)
1202 enum sparse_write_states { begin, in_zeros, not_in_zeros };
1205 static int
1206 buf_all_zeros (buf, bufsize)
1207 char *buf;
1208 int bufsize;
1210 int i;
1211 for (i = 0; i < bufsize; ++i)
1213 if (*buf++ != '\0')
1214 return 0;
1216 return 1;
1219 int delayed_seek_count = 0;
1221 /* Write NBYTE bytes from BUF to remote tape connection FILDES.
1222 Return the number of bytes written on success, -1 on error. */
1225 sparse_write (fildes, buf, nbyte)
1226 int fildes;
1227 char *buf;
1228 unsigned int nbyte;
1230 int complete_block_count;
1231 int leftover_bytes_count;
1232 int seek_count;
1233 int write_count;
1234 char *cur_write_start;
1235 int lseek_rc;
1236 int write_rc;
1237 int i;
1238 enum sparse_write_states state;
1240 complete_block_count = nbyte / DISKBLOCKSIZE;
1241 leftover_bytes_count = nbyte % DISKBLOCKSIZE;
1243 if (delayed_seek_count != 0)
1244 state = in_zeros;
1245 else
1246 state = begin;
1248 seek_count = delayed_seek_count;
1250 for (i = 0; i < complete_block_count; ++i)
1252 switch (state)
1254 case begin :
1255 if (buf_all_zeros (buf, DISKBLOCKSIZE))
1257 seek_count = DISKBLOCKSIZE;
1258 state = in_zeros;
1260 else
1262 cur_write_start = buf;
1263 write_count = DISKBLOCKSIZE;
1264 state = not_in_zeros;
1266 buf += DISKBLOCKSIZE;
1267 break;
1268 case in_zeros :
1269 if (buf_all_zeros (buf, DISKBLOCKSIZE))
1271 seek_count += DISKBLOCKSIZE;
1273 else
1275 lseek (fildes, seek_count, SEEK_CUR);
1276 cur_write_start = buf;
1277 write_count = DISKBLOCKSIZE;
1278 state = not_in_zeros;
1280 buf += DISKBLOCKSIZE;
1281 break;
1282 case not_in_zeros :
1283 if (buf_all_zeros (buf, DISKBLOCKSIZE))
1285 write_rc = write (fildes, cur_write_start, write_count);
1286 seek_count = DISKBLOCKSIZE;
1287 state = in_zeros;
1289 else
1291 write_count += DISKBLOCKSIZE;
1293 buf += DISKBLOCKSIZE;
1294 break;
1298 switch (state)
1300 case begin :
1301 case in_zeros :
1302 delayed_seek_count = seek_count;
1303 break;
1304 case not_in_zeros :
1305 write_rc = write (fildes, cur_write_start, write_count);
1306 delayed_seek_count = 0;
1307 break;
1310 if (leftover_bytes_count != 0)
1312 if (delayed_seek_count != 0)
1314 lseek_rc = lseek (fildes, delayed_seek_count, SEEK_CUR);
1315 delayed_seek_count = 0;
1317 write_rc = write (fildes, buf, leftover_bytes_count);
1319 return nbyte;
1322 static void
1323 write_nuls_to_file (num_bytes, out_des)
1324 long num_bytes;
1325 int out_des;
1327 long blocks;
1328 long extra_bytes;
1329 long i;
1331 blocks = num_bytes / 512;
1332 extra_bytes = num_bytes % 512;
1333 for (i = 0; i < extra_bytes; ++i)
1335 if (write (out_des, zeros_512, 512) != 512)
1336 error (1, errno, "error writing NUL's");
1338 if (extra_bytes != 0)
1340 if (write (out_des, zeros_512, extra_bytes) != extra_bytes)
1341 error (1, errno, "error writing NUL's");