Include stat-time.h, and use its functions instead of the obsolete
[platform/upstream/coreutils.git] / src / cp.c
index 9db7c5f..4e08612 100644 (file)
--- a/src/cp.c
+++ b/src/cp.c
@@ -1,5 +1,5 @@
 /* cp.c  -- file copying (main routines)
-   Copyright (C) 89, 90, 91, 1995-2000 Free Software Foundation.
+   Copyright (C) 89, 90, 91, 1995-2005 Free Software Foundation.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
    Written by Torbjorn Granlund, David MacKenzie, and Jim Meyering. */
 
-#ifdef _AIX
- #pragma alloca
-#endif
-
 #include <config.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include "cp-hash.h"
 #include "error.h"
 #include "dirname.h"
-#include "path-concat.h"
+#include "filenamecat.h"
 #include "quote.h"
+#include "quotearg.h"
+#include "stat-time.h"
+#include "utimens.h"
 
 #define ASSIGN_BASENAME_STRDUPA(Dest, File_name)       \
   do                                                   \
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "cp"
 
-#define AUTHORS "Torbjorn Granlund, David MacKenzie, and Jim Meyering"
-
-#ifndef _POSIX_VERSION
-uid_t geteuid ();
-#endif
+#define AUTHORS "Torbjorn Granlund", "David MacKenzie", "Jim Meyering"
 
-/* Used by do_copy, make_path_private, and re_protect
+/* Used by do_copy, make_dir_parents_private, and re_protect
    to keep a list of leading directories whose protections
    need to be fixed after copying. */
 struct dir_attr
 {
-  int is_new_dir;
-  int slash_offset;
+  bool is_new_dir;
+  size_t slash_offset;
   struct dir_attr *next;
 };
 
@@ -70,15 +65,16 @@ struct dir_attr
    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
 enum
 {
-  TARGET_DIRECTORY_OPTION = CHAR_MAX + 1,
+  COPY_CONTENTS_OPTION = CHAR_MAX + 1,
+  NO_PRESERVE_ATTRIBUTES_OPTION,
+  PARENTS_OPTION,
+  PRESERVE_ATTRIBUTES_OPTION,
+  REPLY_OPTION,
   SPARSE_OPTION,
   STRIP_TRAILING_SLASHES_OPTION,
-  PARENTS_OPTION,
   UNLINK_DEST_BEFORE_OPENING
 };
 
-void strip_trailing_slashes ();
-
 /* Initial number of entries in each hash table entry's table of inodes.  */
 #define INITIAL_HASH_MODULE 100
 
@@ -88,49 +84,62 @@ void strip_trailing_slashes ();
 /* The invocation name of this program.  */
 char *program_name;
 
-/* If nonzero, the command "cp x/e_file e_dir" uses "e_dir/x/e_file"
+/* If true, the command "cp x/e_file e_dir" uses "e_dir/x/e_file"
    as its destination instead of the usual "e_dir/e_file." */
-static int flag_path = 0;
+static bool parents_option = false;
 
 /* Remove any trailing slashes from each SOURCE argument.  */
-static int remove_trailing_slashes;
+static bool remove_trailing_slashes;
 
 static char const *const sparse_type_string[] =
 {
-  "never", "auto", "always", 0
+  "never", "auto", "always", NULL
 };
-
 static enum Sparse_type const sparse_type[] =
 {
   SPARSE_NEVER, SPARSE_AUTO, SPARSE_ALWAYS
 };
+ARGMATCH_VERIFY (sparse_type_string, sparse_type);
 
-/* The error code to return to the system. */
-static int exit_status = 0;
+/* Valid arguments to the `--reply' option. */
+static char const* const reply_args[] =
+{
+  "yes", "no", "query", NULL
+};
+/* The values that correspond to the above strings. */
+static int const reply_vals[] =
+{
+  I_ALWAYS_YES, I_ALWAYS_NO, I_ASK_USER
+};
+ARGMATCH_VERIFY (reply_args, reply_vals);
 
 static struct option const long_opts[] =
 {
   {"archive", no_argument, NULL, 'a'},
   {"backup", optional_argument, NULL, 'b'},
+  {"copy-contents", no_argument, NULL, COPY_CONTENTS_OPTION},
   {"dereference", no_argument, NULL, 'L'},
   {"force", no_argument, NULL, 'f'},
-  {"sparse", required_argument, NULL, SPARSE_OPTION},
   {"interactive", no_argument, NULL, 'i'},
   {"link", no_argument, NULL, 'l'},
-  {"no-dereference", no_argument, NULL, 'd'},
+  {"no-dereference", no_argument, NULL, 'P'},
+  {"no-preserve", required_argument, NULL, NO_PRESERVE_ATTRIBUTES_OPTION},
+  {"no-target-directory", no_argument, NULL, 'T'},
   {"one-file-system", no_argument, NULL, 'x'},
   {"parents", no_argument, NULL, PARENTS_OPTION},
   {"path", no_argument, NULL, PARENTS_OPTION},   /* Deprecated.  */
-  {"preserve", no_argument, NULL, 'p'},
+  {"preserve", optional_argument, NULL, PRESERVE_ATTRIBUTES_OPTION},
   {"recursive", no_argument, NULL, 'R'},
   {"remove-destination", no_argument, NULL, UNLINK_DEST_BEFORE_OPENING},
-  {"strip-trailing-slash", no_argument, NULL, STRIP_TRAILING_SLASHES_OPTION},
+  {"reply", required_argument, NULL, REPLY_OPTION}, /* Deprecated 2005-07-03,
+                                                      remove in 2008. */
+  {"sparse", required_argument, NULL, SPARSE_OPTION},
+  {"strip-trailing-slashes", no_argument, NULL, STRIP_TRAILING_SLASHES_OPTION},
   {"suffix", required_argument, NULL, 'S'},
   {"symbolic-link", no_argument, NULL, 's'},
-  {"target-directory", required_argument, NULL, TARGET_DIRECTORY_OPTION},
+  {"target-directory", required_argument, NULL, 't'},
   {"update", no_argument, NULL, 'u'},
   {"verbose", no_argument, NULL, 'v'},
-  {"version-control", required_argument, NULL, 'V'}, /* Deprecated. FIXME. */
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -139,53 +148,80 @@ static struct option const long_opts[] =
 void
 usage (int status)
 {
-  if (status != 0)
+  if (status != EXIT_SUCCESS)
     fprintf (stderr, _("Try `%s --help' for more information.\n"),
             program_name);
   else
     {
       printf (_("\
-Usage: %s [OPTION]... SOURCE DEST\n\
+Usage: %s [OPTION]... [-T] SOURCE DEST\n\
   or:  %s [OPTION]... SOURCE... DIRECTORY\n\
-  or:  %s [OPTION]... --target-directory=DIRECTORY SOURCE...\n\
+  or:  %s [OPTION]... -DIRECTORY SOURCE...\n\
 "),
              program_name, program_name, program_name);
-      printf (_("\
+      fputs (_("\
 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n\
 \n\
+"), stdout);
+      fputs (_("\
+Mandatory arguments to long options are mandatory for short options too.\n\
+"), stdout);
+      fputs (_("\
   -a, --archive                same as -dpR\n\
       --backup[=CONTROL]       make a backup of each existing destination file\n\
   -b                           like --backup but does not accept an argument\n\
-  -d, --no-dereference         never follow symbolic links\n\
+      --copy-contents          copy contents of special files when recursive\n\
+  -d                           same as --no-dereference --preserve=link\n\
+"), stdout);
+      fputs (_("\
   -f, --force                  if an existing destination file cannot be\n\
-                                   opened, remove it and try again\n\
+                                 opened, remove it and try again\n\
   -i, --interactive            prompt before overwrite\n\
   -H                           follow command-line symbolic links\n\
+"), stdout);
+      fputs (_("\
   -l, --link                   link files instead of copying\n\
   -L, --dereference            always follow symbolic links\n\
-  -p, --preserve               preserve file attributes if possible\n\
-      --parents                append source path to DIRECTORY\n\
-  -P                           same as `--parents' for now; soon to change to\n\
-                                 `--no-dereference' to conform to POSIX\n\
-  -r                           copy recursively, non-directories as files\n\
-                                 WARNING: use -R instead when you might copy\n\
-                                 special files like FIFOs or /dev/zero\n\
+"), stdout);
+      fputs (_("\
+  -P, --no-dereference         never follow symbolic links\n\
+"), stdout);
+      fputs (_("\
+  -p                           same as --preserve=mode,ownership,timestamps\n\
+      --preserve[=ATTR_LIST]   preserve the specified attributes (default:\n\
+                                 mode,ownership,timestamps), if possible\n\
+                                 additional attributes: links, all\n\
+"), stdout);
+      fputs (_("\
+      --no-preserve=ATTR_LIST  don't preserve the specified attributes\n\
+      --parents                use full source file name under DIRECTORY\n\
+"), stdout);
+      fputs (_("\
+  -R, -r, --recursive          copy directories recursively\n\
       --remove-destination     remove each existing destination file before\n\
                                  attempting to open it (contrast with --force)\n\
+"), stdout);
+      fputs (_("\
       --sparse=WHEN            control creation of sparse files\n\
-  -R, --recursive              copy directories recursively\n\
       --strip-trailing-slashes remove any trailing slashes from each SOURCE\n\
                                  argument\n\
+"), stdout);
+      fputs (_("\
   -s, --symbolic-link          make symbolic links instead of copying\n\
   -S, --suffix=SUFFIX          override the usual backup suffix\n\
-      --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY\n\
+  -t, --target-directory=DIRECTORY  copy all SOURCE arguments into DIRECTORY\n\
+  -T, --no-target-directory    treat DEST as a normal file\n\
+"), stdout);
+      fputs (_("\
   -u, --update                 copy only when the SOURCE file is newer\n\
                                  than the destination file or when the\n\
                                  destination file is missing\n\
   -v, --verbose                explain what is being done\n\
   -x, --one-file-system        stay on this file system\n\
-      --help                   display this help and exit\n\
-      --version                output version information and exit\n\
+"), stdout);
+      fputs (HELP_OPTION_DESCRIPTION, stdout);
+      fputs (VERSION_OPTION_DESCRIPTION, stdout);
+      fputs (_("\
 \n\
 By default, sparse SOURCE files are detected by a crude heuristic and the\n\
 corresponding DEST file is made sparse as well.  That is the behavior\n\
@@ -193,72 +229,72 @@ selected by --sparse=auto.  Specify --sparse=always to create a sparse DEST\n\
 file whenever the SOURCE file contains a long enough sequence of zero bytes.\n\
 Use --sparse=never to inhibit creation of sparse files.\n\
 \n\
-"));
-      printf (_("\
+"), stdout);
+      fputs (_("\
 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
 The version control method may be selected via the --backup option or through\n\
 the VERSION_CONTROL environment variable.  Here are the values:\n\
 \n\
+"), stdout);
+      fputs (_("\
   none, off       never make backups (even if --backup is given)\n\
   numbered, t     make numbered backups\n\
   existing, nil   numbered if numbered backups exist, simple otherwise\n\
   simple, never   always make simple backups\n\
-"));
-      printf (_("\
+"), stdout);
+      fputs (_("\
 \n\
 As a special case, cp makes a backup of SOURCE when the force and backup\n\
 options are given and SOURCE and DEST are the same name for an existing,\n\
 regular file.\n\
-"));
-      puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
+"), stdout);
+      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     }
   exit (status);
 }
 
-/* Ensure that the parent directories of CONST_DST_PATH have the
+/* Ensure that the parent directories of CONST_DST_NAME have the
    correct protections, for the --parents option.  This is done
    after all copying has been completed, to allow permissions
    that don't include user write/execute.
 
-   SRC_OFFSET is the index in CONST_DST_PATH of the beginning of the
+   SRC_OFFSET is the index in CONST_DST_NAME of the beginning of the
    source directory name.
 
    ATTR_LIST is a null-terminated linked list of structures that
    indicates the end of the filename of each intermediate directory
-   in CONST_DST_PATH that may need to have its attributes changed.
+   in CONST_DST_NAME that may need to have its attributes changed.
    The command `cp --parents --preserve a/b/c d/e_dir' changes the
    attributes of the directories d/e_dir/a and d/e_dir/a/b to match
    the corresponding source directories regardless of whether they
    existed before the `cp' command was given.
 
-   Return 0 if the parent of CONST_DST_PATH and any intermediate
+   Return true if the parent of CONST_DST_NAME and any intermediate
    directories specified by ATTR_LIST have the proper permissions
-   when done, otherwise 1. */
+   when done */
 
-static int
-re_protect (const char *const_dst_path, int src_offset,
+static bool
+re_protect (char const *const_dst_name, size_t src_offset,
            struct dir_attr *attr_list, const struct cp_options *x)
 {
   struct dir_attr *p;
-  char *dst_path;              /* A copy of CONST_DST_PATH we can change. */
-  char *src_path;              /* The source name in `dst_path'. */
-  uid_t myeuid = geteuid ();
+  char *dst_name;              /* A copy of CONST_DST_NAME we can change. */
+  char *src_name;              /* The source name in `dst_name'. */
 
-  dst_path = (char *) alloca (strlen (const_dst_path) + 1);
-  strcpy (dst_path, const_dst_path);
-  src_path = dst_path + src_offset;
+  ASSIGN_STRDUPA (dst_name, const_dst_name);
+  src_name = dst_name + src_offset;
 
   for (p = attr_list; p; p = p->next)
     {
       struct stat src_sb;
 
-      dst_path[p->slash_offset] = '\0';
+      dst_name[p->slash_offset] = '\0';
 
-      if ((*(x->xstat)) (src_path, &src_sb))
+      if (XSTAT (x, src_name, &src_sb))
        {
-         error (0, errno, _("getting attributes of %s"),
-                quote (src_path));
-         return 1;
+         error (0, errno, _("failed to get attributes of %s"),
+                quote (src_name));
+         return false;
        }
 
       /* Adjust the times (and if possible, ownership) for the copy.
@@ -267,56 +303,50 @@ re_protect (const char *const_dst_path, int src_offset,
 
       if (x->preserve_timestamps)
        {
-         struct utimbuf utb;
-
-         /* There's currently no interface to set file timestamps with
-            better than 1-second resolution, so discard any fractional
-            part of the source timestamp.  */
+         struct timespec timespec[2];
 
-         utb.actime = src_sb.st_atime;
-         utb.modtime = src_sb.st_mtime;
+         timespec[0] = get_stat_atime (&src_sb);
+         timespec[1] = get_stat_mtime (&src_sb);
 
-         if (utime (dst_path, &utb))
+         if (utimens (dst_name, timespec))
            {
-             error (0, errno, _("preserving times for %s"), quote (dst_path));
-             return 1;
+             error (0, errno, _("failed to preserve times for %s"),
+                    quote (dst_name));
+             return false;
            }
        }
 
-      if (x->preserve_owner_and_group)
+      if (x->preserve_ownership)
        {
-         /* If non-root uses -p, it's ok if we can't preserve ownership.
-            But root probably wants to know, e.g. if NFS disallows it,
-            or if the target system doesn't support file ownership.  */
-         if (chown (dst_path, src_sb.st_uid, src_sb.st_gid)
-             && ((errno != EPERM && errno != EINVAL) || myeuid == 0))
+         if (chown (dst_name, src_sb.st_uid, src_sb.st_gid) != 0
+             && ! chown_failure_ok (x))
            {
-             error (0, errno, _("preserving ownership for %s"),
-                    quote (dst_path));
-             return 1;
+             error (0, errno, _("failed to preserve ownership for %s"),
+                    quote (dst_name));
+             return false;
            }
        }
 
-      if (x->preserve_chmod_bits || p->is_new_dir)
+      if (x->preserve_mode | p->is_new_dir)
        {
-         if (chmod (dst_path, src_sb.st_mode & x->umask_kill))
+         if (chmod (dst_name, src_sb.st_mode & x->umask_kill))
            {
-             error (0, errno, _("preserving permissions for %s"),
-                    quote (dst_path));
-             return 1;
+             error (0, errno, _("failed to preserve permissions for %s"),
+                    quote (dst_name));
+             return false;
            }
        }
 
-      dst_path[p->slash_offset] = '/';
+      dst_name[p->slash_offset] = '/';
     }
-  return 0;
+  return true;
 }
 
-/* Ensure that the parent directory of CONST_DIRPATH exists, for
+/* Ensure that the parent directory of CONST_DIR exists, for
    the --parents option.
 
-   SRC_OFFSET is the index in CONST_DIRPATH (which is a destination
-   path) of the beginning of the source directory name.
+   SRC_OFFSET is the index in CONST_DIR (which is a destination
+   directory) of the beginning of the source directory name.
    Create any leading directories that don't already exist,
    giving them permissions MODE.
    If VERBOSE_FMT_STRING is nonzero, use it as a printf format
@@ -325,39 +355,39 @@ re_protect (const char *const_dst_path, int src_offset,
    source and destination directories.
    Creates a linked list of attributes of intermediate directories,
    *ATTR_LIST, for re_protect to use after calling copy.
-   Sets *NEW_DST to 1 if this function creates parent of CONST_DIRPATH.
+   Sets *NEW_DST if this function creates parent of CONST_DIR.
 
-   Return 0 if parent of CONST_DIRPATH exists as a directory with the proper
-   permissions when done, otherwise 1. */
+   Return true if parent of CONST_DIR exists as a directory with the proper
+   permissions when done */
 
-/* FIXME: find a way to synch this function with the one in lib/makepath.c. */
+/* FIXME: Synch this function with the one in ../lib/mkdir-p.c.  */
 
-static int
-make_path_private (const char *const_dirpath, int src_offset, int mode,
-                  const char *verbose_fmt_string, struct dir_attr **attr_list,
-                  int *new_dst, int (*xstat)())
+static bool
+make_dir_parents_private (char const *const_dir, size_t src_offset,
+                         mode_t mode, char const *verbose_fmt_string,
+                         struct dir_attr **attr_list, bool *new_dst,
+                         int (*xstat) ())
 {
   struct stat stats;
-  char *dirpath;               /* A copy of CONST_DIRPATH we can change. */
-  char *src;                   /* Source name in `dirpath'. */
-  char *tmp_dst_dirname;       /* Leading path of `dirpath', malloc. */
-  char *dst_dirname;           /* Leading path of `dirpath', alloca. */
+  char *dir;           /* A copy of CONST_DIR we can change.  */
+  char *src;           /* Source name in DIR.  */
+  char *dst_dir;       /* Leading directory of DIR.  */
+  size_t dirlen;       /* Length of DIR.  */
 
-  dirpath = (char *) alloca (strlen (const_dirpath) + 1);
-  strcpy (dirpath, const_dirpath);
+  ASSIGN_STRDUPA (dir, const_dir);
 
-  src = dirpath + src_offset;
+  src = dir + src_offset;
 
-  tmp_dst_dirname = dir_name (dirpath);
-  dst_dirname = (char *) alloca (strlen (tmp_dst_dirname) + 1);
-  strcpy (dst_dirname, tmp_dst_dirname);
-  free (tmp_dst_dirname);
+  dirlen = dir_len (dir);
+  dst_dir = alloca (dirlen + 1);
+  memcpy (dst_dir, dir, dirlen);
+  dst_dir[dirlen] = '\0';
 
   *attr_list = NULL;
 
-  if ((*xstat) (dst_dirname, &stats))
+  if ((*xstat) (dst_dir, &stats))
     {
-      /* Parent of CONST_DIRNAME does not exist.
+      /* A parent of CONST_DIR does not exist.
         Make all missing intermediate directories. */
       char *slash;
 
@@ -368,152 +398,162 @@ make_path_private (const char *const_dirpath, int src_offset, int mode,
        {
          /* Add this directory to the list of directories whose modes need
             fixing later. */
-         struct dir_attr *new =
-           (struct dir_attr *) xmalloc (sizeof (struct dir_attr));
-         new->slash_offset = slash - dirpath;
+         struct dir_attr *new = xmalloc (sizeof *new);
+         new->slash_offset = slash - dir;
          new->next = *attr_list;
          *attr_list = new;
 
          *slash = '\0';
-         if ((*xstat) (dirpath, &stats))
+         if ((*xstat) (dir, &stats))
            {
-             /* This element of the path does not exist.  We must set
+             /* This component does not exist.  We must set
                 *new_dst and new->is_new_dir inside this loop because,
                 for example, in the command `cp --parents ../a/../b/c e_dir',
-                make_path_private creates only e_dir/../a if ./b already
-                exists. */
-             *new_dst = 1;
-             new->is_new_dir = 1;
-             if (mkdir (dirpath, mode))
+                make_dir_parents_private creates only e_dir/../a if
+                ./b already exists. */
+             *new_dst = true;
+             new->is_new_dir = true;
+             if (mkdir (dir, mode))
                {
                  error (0, errno, _("cannot make directory %s"),
-                        quote (dirpath));
-                 return 1;
+                        quote (dir));
+                 return false;
                }
              else
                {
                  if (verbose_fmt_string != NULL)
-                   printf (verbose_fmt_string, src, dirpath);
+                   printf (verbose_fmt_string, src, dir);
                }
            }
          else if (!S_ISDIR (stats.st_mode))
            {
              error (0, 0, _("%s exists but is not a directory"),
-                    quote (dirpath));
-             return 1;
+                    quote (dir));
+             return false;
            }
          else
            {
-             new->is_new_dir = 0;
-             *new_dst = 0;
+             new->is_new_dir = false;
+             *new_dst = false;
            }
          *slash++ = '/';
 
          /* Avoid unnecessary calls to `stat' when given
-            pathnames containing multiple adjacent slashes.  */
+            file names containing multiple adjacent slashes.  */
          while (*slash == '/')
            slash++;
        }
     }
 
-  /* We get here if the parent of `dirpath' already exists. */
+  /* We get here if the parent of DIR already exists.  */
 
   else if (!S_ISDIR (stats.st_mode))
     {
-      error (0, 0, _("%s exists but is not a directory"), quote (dst_dirname));
-      return 1;
+      error (0, 0, _("%s exists but is not a directory"), quote (dst_dir));
+      return false;
     }
   else
     {
-      *new_dst = 0;
+      *new_dst = false;
     }
-  return 0;
+  return true;
 }
 
-/* Scan the arguments, and copy each by calling copy.
-   Return 0 if successful, 1 if any errors occur. */
+/* FILE is the last operand of this command.
+   Return true if FILE is a directory.
+   But report an error and exit if there is a problem accessing FILE,
+   or if FILE does not exist but would have to refer to an existing
+   directory if it referred to anything at all.
 
-static int
-do_copy (int n_files, char **file, const char *target_directory,
-        const struct cp_options *x)
-{
-  const char *dest;
-  struct stat sb;
-  int new_dst = 0;
-  int ret = 0;
-  int dest_is_dir = 0;
+   If the file exists, store the file's status into *ST.
+   Otherwise, set *NEW_DST.  */
 
-  if (n_files <= 0)
-    {
-      error (0, 0, _("missing file arguments"));
-      usage (1);
-    }
-  if (n_files == 1 && !target_directory)
+static bool
+target_directory_operand (char const *file, struct stat *st, bool *new_dst)
+{
+  char const *b = base_name (file);
+  size_t blen = strlen (b);
+  bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1]));
+  int err = (stat (file, st) == 0 ? 0 : errno);
+  bool is_a_dir = !err && S_ISDIR (st->st_mode);
+  if (err)
     {
-      error (0, 0, _("missing destination file"));
-      usage (1);
+      if (err != ENOENT)
+       error (EXIT_FAILURE, err, _("accessing %s"), quote (file));
+      *new_dst = true;
     }
+  if (is_a_dir < looks_like_a_dir)
+    error (EXIT_FAILURE, err, _("target %s is not a directory"), quote (file));
+  return is_a_dir;
+}
 
-  if (target_directory)
-    dest = target_directory;
-  else
-    {
-      dest = file[n_files - 1];
-      --n_files;
-    }
+/* Scan the arguments, and copy each by calling copy.
+   Return true if successful.  */
 
-  if (lstat (dest, &sb))
-    {
-      if (errno != ENOENT)
-       {
-         error (0, errno, _("accessing %s"), quote (dest));
-         return 1;
-       }
+static bool
+do_copy (int n_files, char **file, const char *target_directory,
+        bool no_target_directory, struct cp_options *x)
+{
+  struct stat sb;
+  bool new_dst = false;
+  bool ok = true;
 
-      new_dst = 1;
-    }
-  else
+  if (n_files <= !target_directory)
     {
-      struct stat sbx;
-
-      /* If `dest' is not a symlink to a nonexistent file, use
-        the results of stat instead of lstat, so we can copy files
-        into symlinks to directories. */
-      if (stat (dest, &sbx) == 0)
-       sb = sbx;
-
-      dest_is_dir = S_ISDIR (sb.st_mode);
+      if (n_files <= 0)
+       error (0, 0, _("missing file operand"));
+      else
+       error (0, 0, _("missing destination file operand after %s"),
+              quote (file[0]));
+      usage (EXIT_FAILURE);
     }
 
-  if (!dest_is_dir)
+  if (no_target_directory)
     {
       if (target_directory)
+       error (EXIT_FAILURE, 0,
+              _("Cannot combine --target-directory (-t) "
+                "and --no-target-directory (-T)"));
+      if (2 < n_files)
        {
-         error (0, 0, _("specified target, %s is not a directory"),
-                quote (dest));
-         usage (1);
-       }
-
-      if (n_files > 1)
-       {
-         error (0, 0,
-        _("copying multiple files, but last argument %s is not a directory"),
-            quote (dest));
-         usage (1);
+         error (0, 0, _("extra operand %s"), quote (file[2]));
+         usage (EXIT_FAILURE);
        }
     }
+  else if (!target_directory)
+    {
+      if (2 <= n_files
+         && target_directory_operand (file[n_files - 1], &sb, &new_dst))
+       target_directory = file[--n_files];
+      else if (2 < n_files)
+       error (EXIT_FAILURE, 0, _("target %s is not a directory"),
+              quote (file[n_files - 1]));
+    }
 
-  if (dest_is_dir)
+  if (target_directory)
     {
       /* cp file1...filen edir
         Copy the files `file1' through `filen'
         to the existing directory `edir'. */
       int i;
+      int (*xstat)() = (x->dereference == DEREF_COMMAND_LINE_ARGUMENTS
+                       || x->dereference == DEREF_ALWAYS
+                       ? stat
+                       : lstat);
+
+      /* Initialize these hash tables only if we'll need them.
+        The problems they're used to detect can arise only if
+        there are two or more files to copy.  */
+      if (2 <= n_files)
+       {
+         dest_info_init (x);
+         src_info_init (x);
+       }
 
       for (i = 0; i < n_files; i++)
        {
-         char *dst_path;
-         int parent_exists = 1; /* True if dir_name (dst_path) exists. */
+         char *dst_name;
+         bool parent_exists = true;  /* True if dir_name (dst_name) exists. */
          struct dir_attr *attr_list;
          char *arg_in_concat = NULL;
          char *arg = file[i];
@@ -523,7 +563,7 @@ do_copy (int n_files, char **file, const char *target_directory,
          if (remove_trailing_slashes)
            strip_trailing_slashes (arg);
 
-         if (flag_path)
+         if (parents_option)
            {
              char *arg_no_trailing_slash;
 
@@ -536,72 +576,65 @@ do_copy (int n_files, char **file, const char *target_directory,
              strip_trailing_slashes (arg_no_trailing_slash);
 
              /* Append all of `arg' (minus any trailing slash) to `dest'.  */
-             dst_path = path_concat (dest, arg_no_trailing_slash,
-                                     &arg_in_concat);
-             if (dst_path == NULL)
-               xalloc_die ();
+             dst_name = file_name_concat (target_directory,
+                                          arg_no_trailing_slash,
+                                          &arg_in_concat);
 
              /* For --parents, we have to make sure that the directory
-                dir_name (dst_path) exists.  We may have to create a few
+                dir_name (dst_name) exists.  We may have to create a few
                 leading directories. */
-             parent_exists = !make_path_private (dst_path,
-                                                 arg_in_concat - dst_path,
-                                                 S_IRWXU,
-                                                 (x->verbose
-                                                  ? "%s -> %s\n" : NULL),
-                                                 &attr_list, &new_dst,
-                                                 x->xstat);
+             parent_exists =
+               (make_dir_parents_private
+                (dst_name, arg_in_concat - dst_name, S_IRWXU,
+                 (x->verbose ? "%s -> %s\n" : NULL),
+                 &attr_list, &new_dst, xstat));
            }
-         else
-           {
+         else
+           {
              char *arg_base;
-             /* Append the last component of `arg' to `dest'.  */
+             /* Append the last component of `arg' to `target_directory'.  */
 
              ASSIGN_BASENAME_STRDUPA (arg_base, arg);
              /* For `cp -R source/.. dest', don't copy into `dest/..'. */
-             dst_path = (STREQ (arg_base, "..")
-                         ? xstrdup (dest)
-                         : path_concat (dest, arg_base, NULL));
+             dst_name = (STREQ (arg_base, "..")
+                         ? xstrdup (target_directory)
+                         : file_name_concat (target_directory, arg_base,
+                                             NULL));
            }
 
          if (!parent_exists)
            {
-             /* make_path_private failed, so don't even attempt the copy. */
-             ret = 1;
-           }
+             /* make_dir_parents_private failed, so don't even
+                attempt the copy.  */
+             ok = false;
+           }
          else
            {
-             int copy_into_self;
-             ret |= copy (arg, dst_path, new_dst, x, &copy_into_self, NULL);
-             forget_all ();
+             bool copy_into_self;
+             ok &= copy (arg, dst_name, new_dst, x, &copy_into_self, NULL);
 
-             if (flag_path)
-               {
-                 ret |= re_protect (dst_path, arg_in_concat - dst_path,
-                                    attr_list, x);
-               }
+             if (parents_option)
+               ok &= re_protect (dst_name, arg_in_concat - dst_name,
+                                 attr_list, x);
            }
 
-         free (dst_path);
+         free (dst_name);
        }
-      return ret;
     }
-  else /* if (n_files == 1) */
+  else /* !target_directory */
     {
-      char *new_dest;
-      char *source;
-      int unused;
-      struct stat source_stats;
+      char const *new_dest;
+      char const *source = file[0];
+      char const *dest = file[1];
+      bool unused;
 
-      if (flag_path)
+      if (parents_option)
        {
          error (0, 0,
-              _("when preserving paths, the destination must be a directory"));
-         usage (1);
+                _("with --parents, the destination must be a directory"));
+         usage (EXIT_FAILURE);
        }
 
-      source = file[0];
-
       /* When the force and backup options have been specified and
         the source and destination are the same name for an existing
         regular file, convert the user's command, e.g.,
@@ -609,101 +642,162 @@ do_copy (int n_files, char **file, const char *target_directory,
         where SUFFIX is determined by any version control options used.  */
 
       if (x->unlink_dest_after_failed_open
-         && x->backup_type != none
+         && x->backup_type != no_backups
          && STREQ (source, dest)
          && !new_dst && S_ISREG (sb.st_mode))
        {
          static struct cp_options x_tmp;
 
          new_dest = find_backup_file_name (dest, x->backup_type);
-         /* Set x->backup_type to `none' so that the normal backup
+         /* Set x->backup_type to `no_backups' so that the normal backup
             mechanism is not used when performing the actual copy.
-            backup_type must be set to `none' only *after* the above
+            backup_type must be set to `no_backups' only *after* the above
             call to find_backup_file_name -- that function uses
             backup_type to determine the suffix it applies.  */
          x_tmp = *x;
-         x_tmp.backup_type = none;
+         x_tmp.backup_type = no_backups;
          x = &x_tmp;
-
-         if (new_dest == NULL)
-           xalloc_die ();
-       }
-
-      /* When the destination is specified with a trailing slash and the
-        source exists but is not a directory, convert the user's command
-        `cp source dest/' to `cp source dest/basename(source)'.  Doing
-        this ensures that the command `cp non-directory file/' will now
-        fail rather than performing the copy.  COPY diagnoses the case of
-        `cp directory non-directory'.  */
-
-      else if (dest[strlen (dest) - 1] == '/'
-         && lstat (source, &source_stats) == 0
-         && !S_ISDIR (source_stats.st_mode))
-       {
-         char *source_base;
-
-         ASSIGN_BASENAME_STRDUPA (source_base, source);
-         new_dest = (char *) alloca (strlen (dest)
-                                     + strlen (source_base) + 1);
-         stpcpy (stpcpy (new_dest, dest), source_base);
        }
       else
        {
-         new_dest = (char *) dest;
+         new_dest = dest;
        }
 
-      return copy (source, new_dest, new_dst, x, &unused, NULL);
+      ok = copy (source, new_dest, 0, x, &unused, NULL);
     }
 
-  /* unreachable */
+  return ok;
 }
 
 static void
 cp_option_init (struct cp_options *x)
 {
-  x->copy_as_regular = 1;
+  x->copy_as_regular = true;
   x->dereference = DEREF_UNDEFINED;
-  x->unlink_dest_before_opening = 0;
-  x->unlink_dest_after_failed_open = 0;
-  x->failed_unlink_is_fatal = 1;
-  x->hard_link = 0;
-  x->interactive = 0;
-  x->myeuid = geteuid ();
-  x->move_mode = 0;
-  x->one_file_system = 0;
-
-  x->preserve_owner_and_group = 0;
-  x->preserve_chmod_bits = 0;
-  x->preserve_timestamps = 0;
-
-  x->require_preserve = 0;
-  x->recursive = 0;
+  x->unlink_dest_before_opening = false;
+  x->unlink_dest_after_failed_open = false;
+  x->hard_link = false;
+  x->interactive = I_UNSPECIFIED;
+  x->chown_privileges = chown_privileges ();
+  x->move_mode = false;
+  x->one_file_system = false;
+
+  x->preserve_ownership = false;
+  x->preserve_links = false;
+  x->preserve_mode = false;
+  x->preserve_timestamps = false;
+
+  x->require_preserve = false;
+  x->recursive = false;
   x->sparse_mode = SPARSE_AUTO;
-  x->symbolic_link = 0;
-  x->set_mode = 0;
+  x->symbolic_link = false;
+  x->set_mode = false;
   x->mode = 0;
 
+  /* Not used.  */
+  x->stdin_tty = false;
+
   /* Find out the current file creation mask, to knock the right bits
      when using chmod.  The creation mask is set to be liberal, so
      that created directories can be written, even if it would not
      have been allowed with the mask this process was started with.  */
   x->umask_kill = ~ umask (0);
 
-  x->update = 0;
-  x->verbose = 0;
+  x->update = false;
+  x->verbose = false;
+  x->dest_info = NULL;
+  x->src_info = NULL;
+}
+
+/* Given a string, ARG, containing a comma-separated list of arguments
+   to the --preserve option, set the appropriate fields of X to ON_OFF.  */
+static void
+decode_preserve_arg (char const *arg, struct cp_options *x, bool on_off)
+{
+  enum File_attribute
+    {
+      PRESERVE_MODE,
+      PRESERVE_TIMESTAMPS,
+      PRESERVE_OWNERSHIP,
+      PRESERVE_LINK,
+      PRESERVE_ALL
+    };
+  static enum File_attribute const preserve_vals[] =
+    {
+      PRESERVE_MODE, PRESERVE_TIMESTAMPS,
+      PRESERVE_OWNERSHIP, PRESERVE_LINK, PRESERVE_ALL
+    };
+  /* Valid arguments to the `--preserve' option. */
+  static char const* const preserve_args[] =
+    {
+      "mode", "timestamps",
+      "ownership", "links", "all", NULL
+    };
+  ARGMATCH_VERIFY (preserve_args, preserve_vals);
+
+  char *arg_writable = xstrdup (arg);
+  char *s = arg_writable;
+  do
+    {
+      /* find next comma */
+      char *comma = strchr (s, ',');
+      enum File_attribute val;
+
+      /* If we found a comma, put a NUL in its place and advance.  */
+      if (comma)
+       *comma++ = 0;
+
+      /* process S.  */
+      val = XARGMATCH ("--preserve", s, preserve_args, preserve_vals);
+      switch (val)
+       {
+       case PRESERVE_MODE:
+         x->preserve_mode = on_off;
+         break;
+
+       case PRESERVE_TIMESTAMPS:
+         x->preserve_timestamps = on_off;
+         break;
+
+       case PRESERVE_OWNERSHIP:
+         x->preserve_ownership = on_off;
+         break;
+
+       case PRESERVE_LINK:
+         x->preserve_links = on_off;
+         break;
+
+       case PRESERVE_ALL:
+         x->preserve_mode = on_off;
+         x->preserve_timestamps = on_off;
+         x->preserve_ownership = on_off;
+         x->preserve_links = on_off;
+         break;
+
+       default:
+         abort ();
+       }
+      s = comma;
+    }
+  while (s);
+
+  free (arg_writable);
 }
 
 int
 main (int argc, char **argv)
 {
   int c;
-  int make_backups = 0;
+  bool ok;
+  bool make_backups = false;
   char *backup_suffix_string;
   char *version_control_string = NULL;
   struct cp_options x;
+  bool copy_contents = false;
   char *target_directory = NULL;
-  int used_P_option = 0;
+  bool no_target_directory = false;
 
+  initialize_main (&argc, &argv);
   program_name = argv[0];
   setlocale (LC_ALL, "");
   bindtextdomain (PACKAGE, LOCALEDIR);
@@ -717,48 +811,44 @@ main (int argc, char **argv)
      we'll actually use backup_suffix_string.  */
   backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
 
-  while ((c = getopt_long (argc, argv, "abdfHilLprsuvxPRS:V:", long_opts, NULL))
+  while ((c = getopt_long (argc, argv, "abdfHilLprst:uvxPRS:T",
+                          long_opts, NULL))
         != -1)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case SPARSE_OPTION:
          x.sparse_mode = XARGMATCH ("--sparse", optarg,
                                     sparse_type_string, sparse_type);
          break;
 
-       case 'a':               /* Like -dpR. */
+       case 'a':               /* Like -dpPR. */
          x.dereference = DEREF_NEVER;
-         x.preserve_owner_and_group = 1;
-         x.preserve_chmod_bits = 1;
-         x.preserve_timestamps = 1;
-         x.require_preserve = 1;
-         x.recursive = 1;
-         x.copy_as_regular = 0;
+         x.preserve_links = true;
+         x.preserve_ownership = true;
+         x.preserve_mode = true;
+         x.preserve_timestamps = true;
+         x.require_preserve = true;
+         x.recursive = true;
          break;
 
-       case 'V':  /* FIXME: this is deprecated.  Remove it in 2001.  */
-         error (0, 0,
-                _("warning: --version-control (-V) is obsolete;  support for\
- it\nwill be removed in some future release.  Use --backup=%s instead."
-                  ), optarg);
-         /* Fall through.  */
-
        case 'b':
-         make_backups = 1;
+         make_backups = true;
          if (optarg)
            version_control_string = optarg;
          break;
 
+       case COPY_CONTENTS_OPTION:
+         copy_contents = true;
+         break;
+
        case 'd':
+         x.preserve_links = true;
          x.dereference = DEREF_NEVER;
          break;
 
        case 'f':
-         x.unlink_dest_after_failed_open = 1;
+         x.unlink_dest_after_failed_open = true;
          break;
 
        case 'H':
@@ -766,75 +856,111 @@ main (int argc, char **argv)
          break;
 
        case 'i':
-         x.interactive = 1;
+         x.interactive = I_ASK_USER;
          break;
 
        case 'l':
-         x.hard_link = 1;
+         x.hard_link = true;
          break;
 
        case 'L':
          x.dereference = DEREF_ALWAYS;
          break;
 
+       case 'P':
+         x.dereference = DEREF_NEVER;
+         break;
+
+       case NO_PRESERVE_ATTRIBUTES_OPTION:
+         decode_preserve_arg (optarg, &x, false);
+         break;
+
+       case PRESERVE_ATTRIBUTES_OPTION:
+         if (optarg == NULL)
+           {
+             /* Fall through to the case for `p' below.  */
+           }
+         else
+           {
+             decode_preserve_arg (optarg, &x, true);
+             x.require_preserve = true;
+             break;
+           }
+
        case 'p':
-         x.preserve_owner_and_group = 1;
-         x.preserve_chmod_bits = 1;
-         x.preserve_timestamps = 1;
-         x.require_preserve = 1;
+         x.preserve_ownership = true;
+         x.preserve_mode = true;
+         x.preserve_timestamps = true;
+         x.require_preserve = true;
          break;
 
-       case 'P':
-         used_P_option = 1;
-         /* fall through */
        case PARENTS_OPTION:
-         flag_path = 1;
+         parents_option = true;
          break;
 
        case 'r':
-         x.recursive = 1;
-         x.copy_as_regular = 1;
+       case 'R':
+         x.recursive = true;
          break;
 
-       case 'R':
-         x.recursive = 1;
-         x.copy_as_regular = 0;
+       case REPLY_OPTION: /* Deprecated */
+         x.interactive = XARGMATCH ("--reply", optarg,
+                                    reply_args, reply_vals);
+         error (0, 0,
+                _("the --reply option is deprecated; use -i or -f instead"));
          break;
 
        case UNLINK_DEST_BEFORE_OPENING:
-         x.unlink_dest_before_opening = 1;
+         x.unlink_dest_before_opening = true;
          break;
 
        case STRIP_TRAILING_SLASHES_OPTION:
-         remove_trailing_slashes = 1;
+         remove_trailing_slashes = true;
          break;
 
        case 's':
 #ifdef S_ISLNK
-         x.symbolic_link = 1;
+         x.symbolic_link = true;
 #else
-         error (1, 0, _("symbolic links are not supported on this system"));
+         error (EXIT_FAILURE, 0,
+                _("symbolic links are not supported on this system"));
 #endif
          break;
 
-       case TARGET_DIRECTORY_OPTION:
+       case 't':
+         if (target_directory)
+           error (EXIT_FAILURE, 0,
+                  _("multiple target directories specified"));
+         else
+           {
+             struct stat st;
+             if (stat (optarg, &st) != 0)
+               error (EXIT_FAILURE, errno, _("accessing %s"), quote (optarg));
+             if (! S_ISDIR (st.st_mode))
+               error (EXIT_FAILURE, 0, _("target %s is not a directory"),
+                      quote (optarg));
+           }
          target_directory = optarg;
          break;
 
+       case 'T':
+         no_target_directory = true;
+         break;
+
        case 'u':
-         x.update = 1;
+         x.update = true;
          break;
 
        case 'v':
-         x.verbose = 1;
+         x.verbose = true;
          break;
 
        case 'x':
-         x.one_file_system = 1;
+         x.one_file_system = true;
          break;
 
        case 'S':
-         make_backups = 1;
+         make_backups = true;
          backup_suffix_string = optarg;
          break;
 
@@ -843,22 +969,14 @@ main (int argc, char **argv)
        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
 
        default:
-         usage (1);
+         usage (EXIT_FAILURE);
        }
     }
 
-  if (x.hard_link && x.symbolic_link)
+  if (x.hard_link & x.symbolic_link)
     {
       error (0, 0, _("cannot make both hard and symbolic links"));
-      usage (1);
-    }
-
-  if (used_P_option)
-    {
-      error (0, 0,
-            _("\
-Warning: the meaning of `-P' will change in the future to conform to POSIX.\n\
-Use `--parents' for the old meaning, and `--no-dereference' for the new one."));
+      usage (EXIT_FAILURE);
     }
 
   if (backup_suffix_string)
@@ -867,9 +985,9 @@ Use `--parents' for the old meaning, and `--no-dereference' for the new one."));
   x.backup_type = (make_backups
                   ? xget_version (_("backup type"),
                                   version_control_string)
-                  : none);
+                  : no_backups);
 
-  if (x.preserve_chmod_bits == 1)
+  if (x.preserve_mode)
     x.umask_kill = ~ (mode_t) 0;
 
   if (x.dereference == DEREF_UNDEFINED)
@@ -884,26 +1002,22 @@ Use `--parents' for the old meaning, and `--no-dereference' for the new one."));
   /* The key difference between -d (--no-dereference) and not is the version
      of `stat' to call.  */
 
-  if (x.dereference == DEREF_NEVER)
-    x.xstat = lstat;
-  else
-    {
-      /* For DEREF_COMMAND_LINE_ARGUMENTS, x.xstat must be stat for
-        each command line argument, but must later be `lstat' for
-        any symlinks that are found via recursive traversal.  */
-      x.xstat = stat;
-    }
+  if (x.recursive)
+    x.copy_as_regular = copy_contents;
 
   /* If --force (-f) was specified and we're in link-creation mode,
      first remove any existing destination file.  */
-  if (x.unlink_dest_after_failed_open && (x.hard_link || x.symbolic_link))
-    x.unlink_dest_before_opening = 1;
+  if (x.unlink_dest_after_failed_open & (x.hard_link | x.symbolic_link))
+    x.unlink_dest_before_opening = true;
 
   /* Allocate space for remembering copied and created files.  */
 
-  hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
+  hash_init ();
+
+  ok = do_copy (argc - optind, argv + optind,
+               target_directory, no_target_directory, &x);
 
-  exit_status |= do_copy (argc - optind, argv + optind, target_directory, &x);
+  forget_all ();
 
-  exit (exit_status);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }