1 /* cp.c -- file copying (main routines)
2 Copyright (C) 89, 90, 91, 1995-2005 Free Software Foundation.
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)
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 Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 Written by Torbjorn Granlund, David MacKenzie, and Jim Meyering. */
22 #include <sys/types.h>
28 #include "backupfile.h"
33 #include "filenamecat.h"
38 #define ASSIGN_BASENAME_STRDUPA(Dest, File_name) \
42 ASSIGN_STRDUPA (tmp_abns_, (File_name)); \
43 strip_trailing_slashes (tmp_abns_); \
44 Dest = base_name (tmp_abns_); \
48 /* The official name of this program (e.g., no `g' prefix). */
49 #define PROGRAM_NAME "cp"
51 #define AUTHORS "Torbjorn Granlund", "David MacKenzie", "Jim Meyering"
53 /* Used by do_copy, make_dir_parents_private, and re_protect
54 to keep a list of leading directories whose protections
55 need to be fixed after copying. */
60 struct dir_attr *next;
63 /* For long options that have no equivalent short option, use a
64 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
67 COPY_CONTENTS_OPTION = CHAR_MAX + 1,
68 NO_PRESERVE_ATTRIBUTES_OPTION,
70 PRESERVE_ATTRIBUTES_OPTION,
73 STRIP_TRAILING_SLASHES_OPTION,
74 UNLINK_DEST_BEFORE_OPENING
77 /* Initial number of entries in each hash table entry's table of inodes. */
78 #define INITIAL_HASH_MODULE 100
80 /* Initial number of entries in the inode hash table. */
81 #define INITIAL_ENTRY_TAB_SIZE 70
83 /* The invocation name of this program. */
86 /* If true, the command "cp x/e_file e_dir" uses "e_dir/x/e_file"
87 as its destination instead of the usual "e_dir/e_file." */
88 static bool parents_option = false;
90 /* Remove any trailing slashes from each SOURCE argument. */
91 static bool remove_trailing_slashes;
93 static char const *const sparse_type_string[] =
95 "never", "auto", "always", NULL
97 static enum Sparse_type const sparse_type[] =
99 SPARSE_NEVER, SPARSE_AUTO, SPARSE_ALWAYS
101 ARGMATCH_VERIFY (sparse_type_string, sparse_type);
103 /* Valid arguments to the `--reply' option. */
104 static char const* const reply_args[] =
106 "yes", "no", "query", NULL
108 /* The values that correspond to the above strings. */
109 static int const reply_vals[] =
111 I_ALWAYS_YES, I_ALWAYS_NO, I_ASK_USER
113 ARGMATCH_VERIFY (reply_args, reply_vals);
115 static struct option const long_opts[] =
117 {"archive", no_argument, NULL, 'a'},
118 {"backup", optional_argument, NULL, 'b'},
119 {"copy-contents", no_argument, NULL, COPY_CONTENTS_OPTION},
120 {"dereference", no_argument, NULL, 'L'},
121 {"force", no_argument, NULL, 'f'},
122 {"interactive", no_argument, NULL, 'i'},
123 {"link", no_argument, NULL, 'l'},
124 {"no-dereference", no_argument, NULL, 'P'},
125 {"no-preserve", required_argument, NULL, NO_PRESERVE_ATTRIBUTES_OPTION},
126 {"no-target-directory", no_argument, NULL, 'T'},
127 {"one-file-system", no_argument, NULL, 'x'},
128 {"parents", no_argument, NULL, PARENTS_OPTION},
129 {"path", no_argument, NULL, PARENTS_OPTION}, /* Deprecated. */
130 {"preserve", optional_argument, NULL, PRESERVE_ATTRIBUTES_OPTION},
131 {"recursive", no_argument, NULL, 'R'},
132 {"remove-destination", no_argument, NULL, UNLINK_DEST_BEFORE_OPENING},
133 {"reply", required_argument, NULL, REPLY_OPTION}, /* Deprecated 2005-07-03,
135 {"sparse", required_argument, NULL, SPARSE_OPTION},
136 {"strip-trailing-slashes", no_argument, NULL, STRIP_TRAILING_SLASHES_OPTION},
137 {"suffix", required_argument, NULL, 'S'},
138 {"symbolic-link", no_argument, NULL, 's'},
139 {"target-directory", required_argument, NULL, 't'},
140 {"update", no_argument, NULL, 'u'},
141 {"verbose", no_argument, NULL, 'v'},
142 {GETOPT_HELP_OPTION_DECL},
143 {GETOPT_VERSION_OPTION_DECL},
150 if (status != EXIT_SUCCESS)
151 fprintf (stderr, _("Try `%s --help' for more information.\n"),
156 Usage: %s [OPTION]... [-T] SOURCE DEST\n\
157 or: %s [OPTION]... SOURCE... DIRECTORY\n\
158 or: %s [OPTION]... -t DIRECTORY SOURCE...\n\
160 program_name, program_name, program_name);
162 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n\
166 Mandatory arguments to long options are mandatory for short options too.\n\
169 -a, --archive same as -dpR\n\
170 --backup[=CONTROL] make a backup of each existing destination file\n\
171 -b like --backup but does not accept an argument\n\
172 --copy-contents copy contents of special files when recursive\n\
173 -d same as --no-dereference --preserve=link\n\
176 -f, --force if an existing destination file cannot be\n\
177 opened, remove it and try again\n\
178 -i, --interactive prompt before overwrite\n\
179 -H follow command-line symbolic links\n\
182 -l, --link link files instead of copying\n\
183 -L, --dereference always follow symbolic links\n\
186 -P, --no-dereference never follow symbolic links\n\
189 -p same as --preserve=mode,ownership,timestamps\n\
190 --preserve[=ATTR_LIST] preserve the specified attributes (default:\n\
191 mode,ownership,timestamps), if possible\n\
192 additional attributes: links, all\n\
195 --no-preserve=ATTR_LIST don't preserve the specified attributes\n\
196 --parents use full source file name under DIRECTORY\n\
199 -R, -r, --recursive copy directories recursively\n\
200 --remove-destination remove each existing destination file before\n\
201 attempting to open it (contrast with --force)\n\
204 --sparse=WHEN control creation of sparse files\n\
205 --strip-trailing-slashes remove any trailing slashes from each SOURCE\n\
209 -s, --symbolic-link make symbolic links instead of copying\n\
210 -S, --suffix=SUFFIX override the usual backup suffix\n\
211 -t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY\n\
212 -T, --no-target-directory treat DEST as a normal file\n\
215 -u, --update copy only when the SOURCE file is newer\n\
216 than the destination file or when the\n\
217 destination file is missing\n\
218 -v, --verbose explain what is being done\n\
219 -x, --one-file-system stay on this file system\n\
221 fputs (HELP_OPTION_DESCRIPTION, stdout);
222 fputs (VERSION_OPTION_DESCRIPTION, stdout);
225 By default, sparse SOURCE files are detected by a crude heuristic and the\n\
226 corresponding DEST file is made sparse as well. That is the behavior\n\
227 selected by --sparse=auto. Specify --sparse=always to create a sparse DEST\n\
228 file whenever the SOURCE file contains a long enough sequence of zero bytes.\n\
229 Use --sparse=never to inhibit creation of sparse files.\n\
233 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
234 The version control method may be selected via the --backup option or through\n\
235 the VERSION_CONTROL environment variable. Here are the values:\n\
239 none, off never make backups (even if --backup is given)\n\
240 numbered, t make numbered backups\n\
241 existing, nil numbered if numbered backups exist, simple otherwise\n\
242 simple, never always make simple backups\n\
246 As a special case, cp makes a backup of SOURCE when the force and backup\n\
247 options are given and SOURCE and DEST are the same name for an existing,\n\
250 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
255 /* Ensure that the parent directories of CONST_DST_NAME have the
256 correct protections, for the --parents option. This is done
257 after all copying has been completed, to allow permissions
258 that don't include user write/execute.
260 SRC_OFFSET is the index in CONST_DST_NAME of the beginning of the
261 source directory name.
263 ATTR_LIST is a null-terminated linked list of structures that
264 indicates the end of the filename of each intermediate directory
265 in CONST_DST_NAME that may need to have its attributes changed.
266 The command `cp --parents --preserve a/b/c d/e_dir' changes the
267 attributes of the directories d/e_dir/a and d/e_dir/a/b to match
268 the corresponding source directories regardless of whether they
269 existed before the `cp' command was given.
271 Return true if the parent of CONST_DST_NAME and any intermediate
272 directories specified by ATTR_LIST have the proper permissions
276 re_protect (char const *const_dst_name, size_t src_offset,
277 struct dir_attr *attr_list, const struct cp_options *x)
280 char *dst_name; /* A copy of CONST_DST_NAME we can change. */
281 char *src_name; /* The source name in `dst_name'. */
283 ASSIGN_STRDUPA (dst_name, const_dst_name);
284 src_name = dst_name + src_offset;
286 for (p = attr_list; p; p = p->next)
290 dst_name[p->slash_offset] = '\0';
292 if (XSTAT (x, src_name, &src_sb))
294 error (0, errno, _("failed to get attributes of %s"),
299 /* Adjust the times (and if possible, ownership) for the copy.
300 chown turns off set[ug]id bits for non-root,
301 so do the chmod last. */
303 if (x->preserve_timestamps)
305 struct timespec timespec[2];
307 timespec[0].tv_sec = src_sb.st_atime;
308 timespec[0].tv_nsec = TIMESPEC_NS (src_sb.st_atim);
309 timespec[1].tv_sec = src_sb.st_mtime;
310 timespec[1].tv_nsec = TIMESPEC_NS (src_sb.st_mtim);
312 if (utimens (dst_name, timespec))
314 error (0, errno, _("failed to preserve times for %s"),
320 if (x->preserve_ownership)
322 if (chown (dst_name, src_sb.st_uid, src_sb.st_gid) != 0
323 && ! chown_failure_ok (x))
325 error (0, errno, _("failed to preserve ownership for %s"),
331 if (x->preserve_mode | p->is_new_dir)
333 if (chmod (dst_name, src_sb.st_mode & x->umask_kill))
335 error (0, errno, _("failed to preserve permissions for %s"),
341 dst_name[p->slash_offset] = '/';
346 /* Ensure that the parent directory of CONST_DIR exists, for
347 the --parents option.
349 SRC_OFFSET is the index in CONST_DIR (which is a destination
350 directory) of the beginning of the source directory name.
351 Create any leading directories that don't already exist,
352 giving them permissions MODE.
353 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
354 string for printing a message after successfully making a directory.
355 The format should take two string arguments: the names of the
356 source and destination directories.
357 Creates a linked list of attributes of intermediate directories,
358 *ATTR_LIST, for re_protect to use after calling copy.
359 Sets *NEW_DST if this function creates parent of CONST_DIR.
361 Return true if parent of CONST_DIR exists as a directory with the proper
362 permissions when done. */
364 /* FIXME: Synch this function with the one in ../lib/mkdir-p.c. */
367 make_dir_parents_private (char const *const_dir, size_t src_offset,
368 mode_t mode, char const *verbose_fmt_string,
369 struct dir_attr **attr_list, bool *new_dst,
373 char *dir; /* A copy of CONST_DIR we can change. */
374 char *src; /* Source name in DIR. */
375 char *dst_dir; /* Leading directory of DIR. */
376 size_t dirlen; /* Length of DIR. */
378 ASSIGN_STRDUPA (dir, const_dir);
380 src = dir + src_offset;
382 dirlen = dir_len (dir);
383 dst_dir = alloca (dirlen + 1);
384 memcpy (dst_dir, dir, dirlen);
385 dst_dir[dirlen] = '\0';
389 if ((*xstat) (dst_dir, &stats))
391 /* A parent of CONST_DIR does not exist.
392 Make all missing intermediate directories. */
396 while (*slash == '/')
398 while ((slash = strchr (slash, '/')))
400 /* Add this directory to the list of directories whose modes need
402 struct dir_attr *new = xmalloc (sizeof *new);
403 new->slash_offset = slash - dir;
404 new->next = *attr_list;
408 if ((*xstat) (dir, &stats))
410 /* This component does not exist. We must set
411 *new_dst and new->is_new_dir inside this loop because,
412 for example, in the command `cp --parents ../a/../b/c e_dir',
413 make_dir_parents_private creates only e_dir/../a if
414 ./b already exists. */
416 new->is_new_dir = true;
417 if (mkdir (dir, mode))
419 error (0, errno, _("cannot make directory %s"),
425 if (verbose_fmt_string != NULL)
426 printf (verbose_fmt_string, src, dir);
429 else if (!S_ISDIR (stats.st_mode))
431 error (0, 0, _("%s exists but is not a directory"),
437 new->is_new_dir = false;
442 /* Avoid unnecessary calls to `stat' when given
443 file names containing multiple adjacent slashes. */
444 while (*slash == '/')
449 /* We get here if the parent of DIR already exists. */
451 else if (!S_ISDIR (stats.st_mode))
453 error (0, 0, _("%s exists but is not a directory"), quote (dst_dir));
463 /* FILE is the last operand of this command.
464 Return true if FILE is a directory.
465 But report an error and exit if there is a problem accessing FILE,
466 or if FILE does not exist but would have to refer to an existing
467 directory if it referred to anything at all.
469 If the file exists, store the file's status into *ST.
470 Otherwise, set *NEW_DST. */
473 target_directory_operand (char const *file, struct stat *st, bool *new_dst)
475 char const *b = base_name (file);
476 size_t blen = strlen (b);
477 bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1]));
478 int err = (stat (file, st) == 0 ? 0 : errno);
479 bool is_a_dir = !err && S_ISDIR (st->st_mode);
483 error (EXIT_FAILURE, err, _("accessing %s"), quote (file));
486 if (is_a_dir < looks_like_a_dir)
487 error (EXIT_FAILURE, err, _("target %s is not a directory"), quote (file));
491 /* Scan the arguments, and copy each by calling copy.
492 Return true if successful. */
495 do_copy (int n_files, char **file, const char *target_directory,
496 bool no_target_directory, struct cp_options *x)
499 bool new_dst = false;
502 if (n_files <= !target_directory)
505 error (0, 0, _("missing file operand"));
507 error (0, 0, _("missing destination file operand after %s"),
509 usage (EXIT_FAILURE);
512 if (no_target_directory)
514 if (target_directory)
515 error (EXIT_FAILURE, 0,
516 _("Cannot combine --target-directory (-t) "
517 "and --no-target-directory (-T)"));
520 error (0, 0, _("extra operand %s"), quote (file[2]));
521 usage (EXIT_FAILURE);
524 else if (!target_directory)
527 && target_directory_operand (file[n_files - 1], &sb, &new_dst))
528 target_directory = file[--n_files];
529 else if (2 < n_files)
530 error (EXIT_FAILURE, 0, _("target %s is not a directory"),
531 quote (file[n_files - 1]));
534 if (target_directory)
536 /* cp file1...filen edir
537 Copy the files `file1' through `filen'
538 to the existing directory `edir'. */
540 int (*xstat)() = (x->dereference == DEREF_COMMAND_LINE_ARGUMENTS
541 || x->dereference == DEREF_ALWAYS
545 /* Initialize these hash tables only if we'll need them.
546 The problems they're used to detect can arise only if
547 there are two or more files to copy. */
554 for (i = 0; i < n_files; i++)
557 bool parent_exists = true; /* True if dir_name (dst_name) exists. */
558 struct dir_attr *attr_list;
559 char *arg_in_concat = NULL;
562 /* Trailing slashes are meaningful (i.e., maybe worth preserving)
563 only in the source file names. */
564 if (remove_trailing_slashes)
565 strip_trailing_slashes (arg);
569 char *arg_no_trailing_slash;
571 /* Use `arg' without trailing slashes in constructing destination
572 file names. Otherwise, we can end up trying to create a
573 directory via `mkdir ("dst/foo/"...', which is not portable.
574 It fails, due to the trailing slash, on at least
575 NetBSD 1.[34] systems. */
576 ASSIGN_STRDUPA (arg_no_trailing_slash, arg);
577 strip_trailing_slashes (arg_no_trailing_slash);
579 /* Append all of `arg' (minus any trailing slash) to `dest'. */
580 dst_name = file_name_concat (target_directory,
581 arg_no_trailing_slash,
584 /* For --parents, we have to make sure that the directory
585 dir_name (dst_name) exists. We may have to create a few
586 leading directories. */
588 (make_dir_parents_private
589 (dst_name, arg_in_concat - dst_name, S_IRWXU,
590 (x->verbose ? "%s -> %s\n" : NULL),
591 &attr_list, &new_dst, xstat));
596 /* Append the last component of `arg' to `target_directory'. */
598 ASSIGN_BASENAME_STRDUPA (arg_base, arg);
599 /* For `cp -R source/.. dest', don't copy into `dest/..'. */
600 dst_name = (STREQ (arg_base, "..")
601 ? xstrdup (target_directory)
602 : file_name_concat (target_directory, arg_base,
608 /* make_dir_parents_private failed, so don't even
615 ok &= copy (arg, dst_name, new_dst, x, ©_into_self, NULL);
618 ok &= re_protect (dst_name, arg_in_concat - dst_name,
625 else /* !target_directory */
627 char const *new_dest;
628 char const *source = file[0];
629 char const *dest = file[1];
635 _("with --parents, the destination must be a directory"));
636 usage (EXIT_FAILURE);
639 /* When the force and backup options have been specified and
640 the source and destination are the same name for an existing
641 regular file, convert the user's command, e.g.,
642 `cp --force --backup foo foo' to `cp --force foo fooSUFFIX'
643 where SUFFIX is determined by any version control options used. */
645 if (x->unlink_dest_after_failed_open
646 && x->backup_type != no_backups
647 && STREQ (source, dest)
648 && !new_dst && S_ISREG (sb.st_mode))
650 static struct cp_options x_tmp;
652 new_dest = find_backup_file_name (dest, x->backup_type);
653 /* Set x->backup_type to `no_backups' so that the normal backup
654 mechanism is not used when performing the actual copy.
655 backup_type must be set to `no_backups' only *after* the above
656 call to find_backup_file_name -- that function uses
657 backup_type to determine the suffix it applies. */
659 x_tmp.backup_type = no_backups;
667 ok = copy (source, new_dest, 0, x, &unused, NULL);
674 cp_option_init (struct cp_options *x)
676 x->copy_as_regular = true;
677 x->dereference = DEREF_UNDEFINED;
678 x->unlink_dest_before_opening = false;
679 x->unlink_dest_after_failed_open = false;
680 x->hard_link = false;
681 x->interactive = I_UNSPECIFIED;
682 x->chown_privileges = chown_privileges ();
683 x->move_mode = false;
684 x->one_file_system = false;
686 x->preserve_ownership = false;
687 x->preserve_links = false;
688 x->preserve_mode = false;
689 x->preserve_timestamps = false;
691 x->require_preserve = false;
692 x->recursive = false;
693 x->sparse_mode = SPARSE_AUTO;
694 x->symbolic_link = false;
699 x->stdin_tty = false;
701 /* Find out the current file creation mask, to knock the right bits
702 when using chmod. The creation mask is set to be liberal, so
703 that created directories can be written, even if it would not
704 have been allowed with the mask this process was started with. */
705 x->umask_kill = ~ umask (0);
713 /* Given a string, ARG, containing a comma-separated list of arguments
714 to the --preserve option, set the appropriate fields of X to ON_OFF. */
716 decode_preserve_arg (char const *arg, struct cp_options *x, bool on_off)
726 static enum File_attribute const preserve_vals[] =
728 PRESERVE_MODE, PRESERVE_TIMESTAMPS,
729 PRESERVE_OWNERSHIP, PRESERVE_LINK, PRESERVE_ALL
731 /* Valid arguments to the `--preserve' option. */
732 static char const* const preserve_args[] =
734 "mode", "timestamps",
735 "ownership", "links", "all", NULL
737 ARGMATCH_VERIFY (preserve_args, preserve_vals);
739 char *arg_writable = xstrdup (arg);
740 char *s = arg_writable;
743 /* find next comma */
744 char *comma = strchr (s, ',');
745 enum File_attribute val;
747 /* If we found a comma, put a NUL in its place and advance. */
752 val = XARGMATCH ("--preserve", s, preserve_args, preserve_vals);
756 x->preserve_mode = on_off;
759 case PRESERVE_TIMESTAMPS:
760 x->preserve_timestamps = on_off;
763 case PRESERVE_OWNERSHIP:
764 x->preserve_ownership = on_off;
768 x->preserve_links = on_off;
772 x->preserve_mode = on_off;
773 x->preserve_timestamps = on_off;
774 x->preserve_ownership = on_off;
775 x->preserve_links = on_off;
789 main (int argc, char **argv)
793 bool make_backups = false;
794 char *backup_suffix_string;
795 char *version_control_string = NULL;
797 bool copy_contents = false;
798 char *target_directory = NULL;
799 bool no_target_directory = false;
801 initialize_main (&argc, &argv);
802 program_name = argv[0];
803 setlocale (LC_ALL, "");
804 bindtextdomain (PACKAGE, LOCALEDIR);
805 textdomain (PACKAGE);
807 atexit (close_stdout);
811 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
812 we'll actually use backup_suffix_string. */
813 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
815 while ((c = getopt_long (argc, argv, "abdfHilLprst:uvxPRS:T",
822 x.sparse_mode = XARGMATCH ("--sparse", optarg,
823 sparse_type_string, sparse_type);
826 case 'a': /* Like -dpPR. */
827 x.dereference = DEREF_NEVER;
828 x.preserve_links = true;
829 x.preserve_ownership = true;
830 x.preserve_mode = true;
831 x.preserve_timestamps = true;
832 x.require_preserve = true;
839 version_control_string = optarg;
842 case COPY_CONTENTS_OPTION:
843 copy_contents = true;
847 x.preserve_links = true;
848 x.dereference = DEREF_NEVER;
852 x.unlink_dest_after_failed_open = true;
856 x.dereference = DEREF_COMMAND_LINE_ARGUMENTS;
860 x.interactive = I_ASK_USER;
868 x.dereference = DEREF_ALWAYS;
872 x.dereference = DEREF_NEVER;
875 case NO_PRESERVE_ATTRIBUTES_OPTION:
876 decode_preserve_arg (optarg, &x, false);
879 case PRESERVE_ATTRIBUTES_OPTION:
882 /* Fall through to the case for `p' below. */
886 decode_preserve_arg (optarg, &x, true);
887 x.require_preserve = true;
892 x.preserve_ownership = true;
893 x.preserve_mode = true;
894 x.preserve_timestamps = true;
895 x.require_preserve = true;
899 parents_option = true;
907 case REPLY_OPTION: /* Deprecated */
908 x.interactive = XARGMATCH ("--reply", optarg,
909 reply_args, reply_vals);
911 _("the --reply option is deprecated; use -i or -f instead"));
914 case UNLINK_DEST_BEFORE_OPENING:
915 x.unlink_dest_before_opening = true;
918 case STRIP_TRAILING_SLASHES_OPTION:
919 remove_trailing_slashes = true;
924 x.symbolic_link = true;
926 error (EXIT_FAILURE, 0,
927 _("symbolic links are not supported on this system"));
932 if (target_directory)
933 error (EXIT_FAILURE, 0,
934 _("multiple target directories specified"));
938 if (stat (optarg, &st) != 0)
939 error (EXIT_FAILURE, errno, _("accessing %s"), quote (optarg));
940 if (! S_ISDIR (st.st_mode))
941 error (EXIT_FAILURE, 0, _("target %s is not a directory"),
944 target_directory = optarg;
948 no_target_directory = true;
960 x.one_file_system = true;
965 backup_suffix_string = optarg;
968 case_GETOPT_HELP_CHAR;
970 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
973 usage (EXIT_FAILURE);
977 if (x.hard_link & x.symbolic_link)
979 error (0, 0, _("cannot make both hard and symbolic links"));
980 usage (EXIT_FAILURE);
983 if (backup_suffix_string)
984 simple_backup_suffix = xstrdup (backup_suffix_string);
986 x.backup_type = (make_backups
987 ? xget_version (_("backup type"),
988 version_control_string)
992 x.umask_kill = ~ (mode_t) 0;
994 if (x.dereference == DEREF_UNDEFINED)
997 /* This is compatible with FreeBSD. */
998 x.dereference = DEREF_NEVER;
1000 x.dereference = DEREF_ALWAYS;
1003 /* The key difference between -d (--no-dereference) and not is the version
1004 of `stat' to call. */
1007 x.copy_as_regular = copy_contents;
1009 /* If --force (-f) was specified and we're in link-creation mode,
1010 first remove any existing destination file. */
1011 if (x.unlink_dest_after_failed_open & (x.hard_link | x.symbolic_link))
1012 x.unlink_dest_before_opening = true;
1014 /* Allocate space for remembering copied and created files. */
1018 ok = do_copy (argc - optind, argv + optind,
1019 target_directory, no_target_directory, &x);
1023 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);