3 /* main.c - main program and argument processing for cpio.
4 Copyright (C) 1990, 1991, 1992, 2001, 2003, 2004 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by Phil Nelson <phil@cs.wwu.edu>,
21 David MacKenzie <djm@gnu.ai.mit.edu>,
22 John Oleynick <juo@klinzhai.rutgers.edu>,
23 and Sergey Poznyakoff <gray@mirddin.farlep.net> */
30 #include <sys/types.h>
37 #include "filetypes.h"
42 #include <localedir.h>
45 NO_ABSOLUTE_FILENAMES_OPTION=256,
46 ABSOLUTE_FILENAMES_OPTION,
47 NO_PRESERVE_OWNER_OPTION,
48 ONLY_VERIFY_CRC_OPTION,
49 RENAME_BATCH_FILE_OPTION,
63 const char *argp_program_version = "cpio (" PACKAGE_NAME ") " VERSION;
64 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
65 static char doc[] = N_("GNU `cpio' copies files to and from archives\n\
68 # Copy files named in name-list to the archive\n\
69 cpio -o < name-list [> archive]\n\
70 # Extract files from the archive\n\
71 cpio -i [< archive]\n\
72 # Copy files named in name-list to destination-directory\n\
73 cpio -p destination-directory < name-list\n");
75 /* Print usage error message and exit with error. */
77 #define USAGE_ERROR(args) do { error args; exit(2); } while (0)
78 #define CHECK_USAGE(cond, opt, mode_opt) \
79 if (cond) USAGE_ERROR((0, 0, _("%s is meaningless with %s"), opt, mode_opt));
81 static struct argp_option options[] = {
83 N_("Main operation mode:"), 10},
85 N_("Create the archive (run in copy-out mode)"), 10},
86 {"extract", 'i', 0, 0,
87 N_("Extract files from an archive (run in copy-in mode)")},
88 {"pass-through", 'p', 0, 0,
89 N_("Run in copy-pass mode"), 10},
91 N_("Print a table of contents of the input"), 10},
94 N_("Operation modifiers valid in any mode:"), 100},
96 {"file", 'F', N_("[[USER@]HOST:]FILE-NAME"), 0,
97 N_("Use this FILE-NAME instead of standard input or output. Optional USER and HOST specify the user and host names in case of a remote archive"), 110},
98 {"force-local", FORCE_LOCAL_OPTION, 0, 0,
99 N_("Archive file is local, even if its name contains colons"), 110},
100 {"format", 'H', N_("FORMAT"), 0,
101 N_("Use given archive FORMAT"), 110},
103 N_("Set the I/O block size to 5120 bytes"), 110},
104 {"block-size", BLOCK_SIZE_OPTION, N_("BLOCK-SIZE"), 0,
105 N_("Set the I/O block size to BLOCK-SIZE * 512 bytes"), 110},
107 N_("Use the old portable (ASCII) archive format"), 0},
108 {"dot", 'V', NULL, 0,
109 N_("Print a \".\" for each file processed"), 110},
110 {"io-size", 'C', N_("NUMBER"), 0,
111 N_("Set the I/O block size to the given NUMBER of bytes"), 110},
112 {"message", 'M', N_("STRING"), 0,
113 N_("Print STRING when the end of a volume of the backup media is reached"),
115 {"nonmatching", 'f', 0, 0,
116 N_("Only copy files that do not match any of the given patterns"), 110},
117 {"numeric-uid-gid", 'n', 0, 0,
118 N_("In the verbose table of contents listing, show numeric UID and GID"),
120 {"rsh-command", RSH_COMMAND_OPTION, N_("COMMAND"), 0,
121 N_("Use remote COMMAND instead of rsh"), 110},
122 {"quiet", QUIET_OPTION, NULL, 0,
123 N_("Do not print the number of blocks copied"), 110},
124 {"verbose", 'v', NULL, 0,
125 N_("Verbosely list the files processed"), 110},
127 {"debug", DEBUG_OPTION, NULL, 0,
128 N_("Enable debugging info"), 110},
130 {"warning", 'W', N_("FLAG"), 0,
131 N_("Control warning display. Currently FLAG is one of 'none', 'truncate', 'all'. Multiple options accumulate."), 110 },
135 N_("Operation modifiers valid only in copy-in mode:"), 200},
136 {"pattern-file", 'E', N_("FILE"), 0,
137 N_("In copy-in mode, read additional patterns specifying filenames to extract or list from FILE"), 210},
138 {"no-absolute-filenames", NO_ABSOLUTE_FILENAMES_OPTION, 0, 0,
139 N_("Create all files relative to the current directory"), 210},
140 {"absolute-filenames", ABSOLUTE_FILENAMES_OPTION, 0, 0,
141 N_("do not strip leading file name components that contain \"..\" and leading slashes from file names"), 210},
142 {"only-verify-crc", ONLY_VERIFY_CRC_OPTION, 0, 0,
143 N_("When reading a CRC format archive in copy-in mode, only verify the CRC's of each file in the archive, don't actually extract the files"), 210},
144 {"rename", 'r', 0, 0,
145 N_("Interactively rename files"), 210},
146 {"rename-batch-file", RENAME_BATCH_FILE_OPTION, N_("FILE"), OPTION_HIDDEN,
148 {"swap", 'b', NULL, 0,
149 N_("Swap both halfwords of words and bytes of halfwords in the data. Equivalent to -sS"), 210},
150 {"swap-bytes", 's', NULL, 0,
151 N_("Swap the bytes of each halfword in the files"), 210},
152 {"swap-halfwords", 'S', NULL, 0,
153 N_("Swap the halfwords of each word (4 bytes) in the files"),
155 {"to-stdout", TO_STDOUT_OPTION, NULL, 0,
156 N_("Extract files to standard output"), 210},
160 N_("Operation modifiers valid only in copy-out mode:"), 300},
161 {"append", 'A', 0, 0,
162 N_("Append to an existing archive."), 310 },
163 {NULL, 'O', N_("[[USER@]HOST:]FILE-NAME"), 0,
164 N_("Archive filename to use instead of standard output. Optional USER and HOST specify the user and host names in case of a remote archive"), 310},
168 N_("Operation modifiers valid only in copy-pass mode:"), 400},
170 N_("Link files instead of copying them, when possible"), 410},
174 N_("Operation modifiers valid for copy-out and copy-pass modes:"), 500},
176 N_("A list of filenames is terminated by a null character instead of a newline"), 510 },
177 {NULL, 'I', N_("[[USER@]HOST:]FILE-NAME"), 0,
178 N_("Archive filename to use instead of standard input. Optional USER and HOST specify the user and host names in case of a remote archive"), 510},
179 {"dereference", 'L', 0, 0,
180 N_("Dereference symbolic links (copy the files that they point to instead of copying the links)."), 510},
181 {"owner", 'R', N_("[USER][:.][GROUP]"), 0,
182 N_("Set the ownership of all files created to the specified USER and/or GROUP"), 510},
183 {"sparse", SPARSE_OPTION, NULL, 0,
184 N_("Write files with large blocks of zeros as sparse files"), 510},
185 {"reset-access-time", 'a', NULL, 0,
186 N_("Reset the access times of files after reading them"), 510},
190 N_("Operation modifiers valid for copy-in and copy-pass modes:"), 600},
191 {"preserve-modification-time", 'm', 0, 0,
192 N_("Retain previous file modification times when creating files"), 610},
193 {"make-directories", 'd', 0, 0,
194 N_("Create leading directories where needed"), 610},
195 {"no-preserve-owner", NO_PRESERVE_OWNER_OPTION, 0, 0,
196 N_("Do not change the ownership of the files"), 610},
197 {"unconditional", 'u', NULL, 0,
198 N_("Replace all files unconditionally"), 610},
201 N_("Informative options:"), 700 },
203 {"help", '?', 0, 0, N_("Give this help list"), -1},
204 {"usage", USAGE_OPTION, 0, 0, N_("Give a short usage message"), -1},
205 {"license", LICENSE_OPTION, 0, 0, N_("Print license and exit"), -1},
206 {"version", VERSION_OPTION, 0, 0, N_("Print program version"), -1},
207 /* FIXME -V (--dot) conflicts with the default short option for
213 static char *input_archive_name = 0;
214 static char *output_archive_name = 0;
219 printf ("%s (%s) %s\n%s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION,
220 "Copyright (C) 2004 Free Software Foundation, Inc.\n");
221 printf (_(" GNU cpio is free software; you can redistribute it and/or modify\n"
222 " it under the terms of the GNU General Public License as published by\n"
223 " the Free Software Foundation; either version 2 of the License, or\n"
224 " (at your option) any later version.\n"
226 " GNU cpio is distributed in the hope that it will be useful,\n"
227 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
228 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
229 " GNU General Public License for more details.\n"
231 " You should have received a copy of the GNU General Public License\n"
232 " along with GNU cpio; if not, write to the Free Software\n"
233 " Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n\n"));
238 warn_control (char *arg)
240 static struct warn_tab {
244 { "none", CPIO_WARN_ALL },
245 { "truncate", CPIO_WARN_TRUNCATE },
246 { "all", CPIO_WARN_ALL },
252 if (strcmp (arg, "none") == 0)
258 if (strlen (arg) > 2 && memcmp (arg, "no-", 3) == 0)
261 for (wt = warn_tab; wt->name; wt++)
262 if (strcmp (arg + offset, wt->name) == 0)
265 warn_option &= ~wt->flag;
267 warn_option |= wt->flag;
275 parse_opt (int key, char *arg, struct argp_state *state)
279 case '0': /* Read null-terminated filenames. */
283 case 'a': /* Reset access times. */
284 reset_time_flag = true;
287 case 'A': /* Append to the archive. */
291 case 'b': /* Swap bytes and halfwords. */
292 swap_bytes_flag = true;
293 swap_halfwords_flag = true;
296 case 'B': /* Set block size to 5120. */
297 io_block_size = 5120;
300 case BLOCK_SIZE_OPTION: /* --block-size */
301 io_block_size = atoi (arg);
302 if (io_block_size < 1)
303 error (2, 0, _("invalid block size"));
304 io_block_size *= 512;
307 case 'c': /* Use the old portable ASCII format. */
308 if (archive_format != arf_unknown)
309 USAGE_ERROR ((0, 0, _("Archive format multiply defined")));
311 archive_format = arf_newascii; /* -H newc. */
313 archive_format = arf_oldascii; /* -H odc. */
317 case 'C': /* Block size. */
318 io_block_size = atoi (arg);
319 if (io_block_size < 1)
320 error (2, 0, _("invalid block size"));
323 case 'd': /* Create directories where needed. */
324 create_dir_flag = true;
327 case 'f': /* Only copy files not matching patterns. */
328 copy_matching_files = false;
331 case 'E': /* Pattern file name. */
332 pattern_file_name = arg;
335 case 'F': /* Archive file name. */
339 case 'H': /* Header format name. */
340 if (archive_format != arf_unknown)
341 USAGE_ERROR ((0, 0, _("Archive format multiply defined")));
342 if (!strcasecmp (arg, "crc"))
343 archive_format = arf_crcascii;
344 else if (!strcasecmp (arg, "newc"))
345 archive_format = arf_newascii;
346 else if (!strcasecmp (arg, "odc"))
347 archive_format = arf_oldascii;
348 else if (!strcasecmp (arg, "bin"))
349 archive_format = arf_binary;
350 else if (!strcasecmp (arg, "ustar"))
351 archive_format = arf_ustar;
352 else if (!strcasecmp (arg, "tar"))
353 archive_format = arf_tar;
354 else if (!strcasecmp (arg, "hpodc"))
355 archive_format = arf_hpoldascii;
356 else if (!strcasecmp (arg, "hpbin"))
357 archive_format = arf_hpbinary;
360 invalid archive format `%s'; valid formats are:\n\
361 crc newc odc bin ustar tar (all-caps also recognized)"), arg);
364 case 'i': /* Copy-in mode. */
365 if (copy_function != 0)
366 USAGE_ERROR ((0, 0, _("Mode already defined")));
367 copy_function = process_copy_in;
370 case 'I': /* Input archive file name. */
371 input_archive_name = arg;
374 case 'k': /* Handle corrupted archives. We always handle
375 corrupted archives, but recognize this
376 option for compatability. */
379 case 'l': /* Link files when possible. */
383 case 'L': /* Dereference symbolic links. */
387 case 'm': /* Retain previous file modify times. */
388 retain_time_flag = true;
391 case 'M': /* New media message. */
392 set_new_media_message (arg);
395 case 'n': /* Long list owner and group as numbers. */
399 case NO_ABSOLUTE_FILENAMES_OPTION: /* --no-absolute-filenames */
400 abs_paths_flag = false;
403 case ABSOLUTE_FILENAMES_OPTION: /* --absolute-filenames */
404 abs_paths_flag = true;
407 case NO_PRESERVE_OWNER_OPTION: /* --no-preserve-owner */
408 if (set_owner_flag || set_group_flag)
410 _("--no-preserve-owner cannot be used with --owner")));
411 no_chown_flag = true;
414 case 'o': /* Copy-out mode. */
415 if (copy_function != 0)
416 USAGE_ERROR ((0, 0, _("Mode already defined")));
417 copy_function = process_copy_out;
420 case 'O': /* Output archive file name. */
421 output_archive_name = arg;
424 case ONLY_VERIFY_CRC_OPTION:
425 only_verify_crc_flag = true;
428 case 'p': /* Copy-pass mode. */
429 if (copy_function != 0)
430 USAGE_ERROR ((0, 0, _("Mode already defined")));
431 copy_function = process_copy_pass;
434 case RSH_COMMAND_OPTION:
435 rsh_command_option = arg;
438 case 'r': /* Interactively rename. */
442 case RENAME_BATCH_FILE_OPTION:
443 rename_batch_file = arg;
450 case 'R': /* Set the owner. */
453 _("--owner cannot be used with --no-preserve-owner")));
457 e = parse_user_spec (arg, &set_owner, &set_group, &u, &g);
459 error (2, 0, "%s: %s", arg, e);
463 set_owner_flag = true;
468 set_group_flag = true;
473 case 's': /* Swap bytes. */
474 swap_bytes_flag = true;
477 case 'S': /* Swap halfwords. */
478 swap_halfwords_flag = true;
481 case 't': /* Only print a list. */
485 case 'u': /* Replace all! Unconditionally! */
486 unconditional_flag = true;
489 case 'v': /* Verbose! */
493 case 'V': /* Print `.' for each file. */
498 if (warn_control (arg))
499 argp_error (state, _("Invalid value for --warning option: %s"), arg);
506 case FORCE_LOCAL_OPTION:
507 force_local_option = 1;
516 case TO_STDOUT_OPTION:
517 to_stdout_option = true;
521 argp_state_help (state, state->out_stream, ARGP_HELP_STD_HELP);
525 argp_state_help (state, state->out_stream,
526 ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
530 fprintf (state->out_stream, "%s\n", argp_program_version);
538 return ARGP_ERR_UNKNOWN;
543 static struct argp argp = {
546 N_("[destination-directory]"),
553 /* Process the arguments. Set all options and set up the copy pass
554 directory or the copy in patterns. */
557 process_args (int argc, char *argv[])
559 void (*copy_in) (); /* Work around for pcc bug. */
565 _("You must specify one of -oipt options.\nTry `%s --help' or `%s --usage' for more information.\n"),
566 program_name, program_name));
570 if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER|ARGP_NO_HELP, &index, NULL))
573 /* Do error checking and look at other args. */
575 if (copy_function == 0)
578 copy_function = process_copy_in;
581 _("You must specify one of -oipt options.\nTry `%s --help' or `%s --usage' for more information.\n"),
582 program_name, program_name));
585 /* Work around for pcc bug. */
586 copy_in = process_copy_in;
587 copy_out = process_copy_out;
589 if (copy_function == copy_in)
592 CHECK_USAGE(link_flag, "--link", "--extract");
593 CHECK_USAGE(reset_time_flag, "--reset", "--extract");
594 CHECK_USAGE(xstat != lstat, "--dereference", "--extract");
595 CHECK_USAGE(append_flag, "--append", "--extract");
596 CHECK_USAGE(sparse_flag, "--sparse", "--extract");
597 CHECK_USAGE(output_archive_name, "-O", "--extract");
598 if (to_stdout_option)
600 CHECK_USAGE(create_dir_flag, "--make-directories", "--to-stdout");
601 CHECK_USAGE(rename_flag, "--rename", "--to-stdout");
602 CHECK_USAGE(no_chown_flag, "--no-preserve-owner", "--to-stdout");
603 CHECK_USAGE(set_owner_flag||set_group_flag, "--owner", "--to-stdout");
604 CHECK_USAGE(retain_time_flag, "--preserve-modification-time",
608 if (archive_name && input_archive_name)
609 USAGE_ERROR((0, 0, _("Both -I and -F are used in copy-in mode")));
611 if (archive_format == arf_crcascii)
613 num_patterns = argc - index;
614 save_patterns = &argv[index];
615 if (input_archive_name)
616 archive_name = input_archive_name;
618 else if (copy_function == copy_out)
621 USAGE_ERROR ((0, 0, _("Too many arguments")));
624 CHECK_USAGE(create_dir_flag, "--make-directories", "--create");
625 CHECK_USAGE(rename_flag, "--rename", "--create");
626 CHECK_USAGE(table_flag, "--list", "--create");
627 CHECK_USAGE(unconditional_flag, "--unconditional", "--create");
628 CHECK_USAGE(link_flag, "--link", "--create");
629 CHECK_USAGE(retain_time_flag, "--preserve-modification-time",
631 CHECK_USAGE(no_chown_flag, "--no-preserve-owner", "--create");
632 CHECK_USAGE(set_owner_flag||set_group_flag, "--owner", "--create");
633 CHECK_USAGE(swap_bytes_flag, "--swap-bytes (--swap)", "--create");
634 CHECK_USAGE(swap_halfwords_flag, "--swap-halfwords (--swap)",
636 CHECK_USAGE(to_stdout_option, "--to-stdout", "--create");
638 if (append_flag && !(archive_name || output_archive_name))
640 _("--append is used but no archive file name is given (use -F or -O options")));
642 CHECK_USAGE(rename_batch_file, "--rename-batch-file", "--create");
643 CHECK_USAGE(abs_paths_flag, "--absolute-pathnames", "--create");
644 CHECK_USAGE(input_archive_name, "-I", "--create");
645 if (archive_name && output_archive_name)
646 USAGE_ERROR ((0, 0, _("Both -O and -F are used in copy-out mode")));
648 if (archive_format == arf_unknown)
649 archive_format = arf_binary;
650 if (output_archive_name)
651 archive_name = output_archive_name;
656 if (index != argc - 1)
657 USAGE_ERROR ((0, 0, _("Too many arguments")));
659 if (archive_format != arf_unknown)
661 _("Archive format is not specified in copy-pass mode (use --format option)")));
663 CHECK_USAGE(swap_bytes_flag, "--swap-bytes (--swap)", "--pass-through");
664 CHECK_USAGE(swap_halfwords_flag, "--swap-halfwords (--swap)",
666 CHECK_USAGE(table_flag, "--list", "--pass-through");
667 CHECK_USAGE(rename_flag, "--rename", "--pass-through");
668 CHECK_USAGE(append_flag, "--append", "--pass-through");
669 CHECK_USAGE(rename_batch_file, "--rename-batch-file", "--pass-through");
670 CHECK_USAGE(abs_paths_flag, "--absolute-pathnames",
672 CHECK_USAGE(to_stdout_option, "--to-stdout", "--pass-through");
674 directory_name = argv[index];
679 if (copy_function != copy_in && copy_function != copy_out)
681 _("-F can be used only with --create or --extract")));
682 archive_des = open_archive (archive_name);
684 error (1, errno, "%s", archive_name);
687 /* Prevent SysV non-root users from giving away files inadvertantly.
688 This happens automatically on BSD, where only root can give
690 if (set_owner_flag == false && set_group_flag == false && geteuid ())
691 no_chown_flag = true;
694 /* Initialize the input and output buffers to their proper size and
695 initialize all variables associated with the input and output
699 initialize_buffers ()
701 int in_buf_size, out_buf_size;
703 if (copy_function == process_copy_in)
705 /* Make sure the input buffer can always hold 2 blocks and that it
706 is big enough to hold 1 tar record (512 bytes) even if it
707 is not aligned on a block boundary. The extra buffer space
708 is needed by process_copyin and peek_in_buf to automatically
709 figure out what kind of archive it is reading. */
710 if (io_block_size >= 512)
711 in_buf_size = 2 * io_block_size;
714 out_buf_size = DISK_IO_BLOCK_SIZE;
716 else if (copy_function == process_copy_out)
718 in_buf_size = DISK_IO_BLOCK_SIZE;
719 out_buf_size = io_block_size;
723 in_buf_size = DISK_IO_BLOCK_SIZE;
724 out_buf_size = DISK_IO_BLOCK_SIZE;
727 input_buffer = (char *) xmalloc (in_buf_size);
728 in_buff = input_buffer;
729 input_buffer_size = in_buf_size;
733 output_buffer = (char *) xmalloc (out_buf_size);
734 out_buff = output_buffer;
738 /* Clear the block of zeros. */
739 bzero (zeros_512, 512);
743 main (int argc, char *argv[])
746 setlocale (LC_ALL, "");
748 bindtextdomain (PACKAGE, LOCALEDIR);
749 textdomain (PACKAGE);
751 program_name = argv[0];
754 _fmode = O_BINARY; /* Put stdin and stdout in binary mode. */
756 #ifdef __EMX__ /* gcc on OS/2. */
757 _response (&argc, &argv);
758 _wildcard (&argc, &argv);
761 process_args (argc, argv);
764 initialize_buffers ();
768 if (archive_des >= 0 && rmtclose (archive_des) == -1)
769 error (1, errno, _("error closing archive"));