maint: update all copyright year number ranges
[platform/upstream/coreutils.git] / src / install.c
index bac5c7c..cefee0c 100644 (file)
@@ -1,5 +1,5 @@
 /* install - copy files and set attributes
-   Copyright (C) 1989-1991, 1995-2010 Free Software Foundation, Inc.
+   Copyright (C) 1989-2013 Free Software Foundation, Inc.
 
    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
@@ -24,6 +24,7 @@
 #include <pwd.h>
 #include <grp.h>
 #include <selinux/selinux.h>
+#include <sys/wait.h>
 
 #include "system.h"
 #include "backupfile.h"
 #include "utimens.h"
 #include "xstrtol.h"
 
-/* The official name of this program (e.g., no `g' prefix).  */
+/* The official name of this program (e.g., no 'g' prefix).  */
 #define PROGRAM_NAME "install"
 
 #define AUTHORS proper_name ("David MacKenzie")
 
-#if HAVE_SYS_WAIT_H
-# include <sys/wait.h>
-#endif
-
 static int selinux_enabled = 0;
 static bool use_default_selinux_context = true;
 
@@ -71,35 +68,18 @@ static bool use_default_selinux_context = true;
 # define matchpathcon_init_prefix(a, p) /* empty */
 #endif
 
-static bool change_timestamps (struct stat const *from_sb, char const *to);
-static bool change_attributes (char const *name);
-static bool copy_file (const char *from, const char *to,
-                       const struct cp_options *x);
-static bool install_file_in_file_parents (char const *from, char *to,
-                                          struct cp_options *x);
-static bool install_file_in_dir (const char *from, const char *to_dir,
-                                 const struct cp_options *x);
-static bool install_file_in_file (const char *from, const char *to,
-                                  const struct cp_options *x);
-static void get_ids (void);
-static void strip (char const *name);
-static void announce_mkdir (char const *dir, void *options);
-static int make_ancestor (char const *dir, char const *component,
-                          void *options);
-void usage (int status);
-
 /* The user name that will own the files, or NULL to make the owner
    the current user ID. */
 static char *owner_name;
 
-/* The user ID corresponding to `owner_name'. */
+/* The user ID corresponding to 'owner_name'. */
 static uid_t owner_id;
 
 /* The group name that will own the files, or NULL to make the group
    the current group ID. */
 static char *group_name;
 
-/* The group ID corresponding to `group_name'. */
+/* The group ID corresponding to 'group_name'. */
 static gid_t group_id;
 
 #define DEFAULT_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
@@ -134,7 +114,6 @@ static char const *strip_program = "strip";
 enum
 {
   PRESERVE_CONTEXT_OPTION = CHAR_MAX + 1,
-  PRESERVE_CONTEXT_OPTION_DEPRECATED,
   STRIP_PROGRAM_OPTION
 };
 
@@ -150,9 +129,6 @@ static struct option const long_options[] =
   {"owner", required_argument, NULL, 'o'},
   {"preserve-timestamps", no_argument, NULL, 'p'},
   {"preserve-context", no_argument, NULL, PRESERVE_CONTEXT_OPTION},
-  /* --preserve_context was silently supported until Apr 2009.
-     FIXME: disable altogether in a year or so.  */
-  {"preserve_context", no_argument, NULL, PRESERVE_CONTEXT_OPTION_DEPRECATED},
   {"strip", no_argument, NULL, 's'},
   {"strip-program", required_argument, NULL, STRIP_PROGRAM_OPTION},
   {"suffix", required_argument, NULL, 'S'},
@@ -188,8 +164,8 @@ have_same_content (int a_fd, int b_fd)
 static bool
 extra_mode (mode_t input)
 {
-  const mode_t mask = ~S_IRWXUGO & ~S_IFMT;
-  return !! (input & mask);
+  mode_t mask = S_IRWXUGO | S_IFMT;
+  return !! (input & mask);
 }
 
 /* Return true if copy of file SRC_NAME to file DEST_NAME is necessary. */
@@ -216,9 +192,27 @@ need_copy (const char *src_name, const char *dest_name,
     return true;
 
   if (src_sb.st_size != dest_sb.st_size
-      || (dest_sb.st_mode & CHMOD_MODE_BITS) != mode
-      || dest_sb.st_uid != (owner_id == (uid_t) -1 ? getuid () : owner_id)
-      || dest_sb.st_gid != (group_id == (gid_t) -1 ? getgid () : group_id))
+      || (dest_sb.st_mode & CHMOD_MODE_BITS) != mode)
+    return true;
+
+  if (owner_id == (uid_t) -1)
+    {
+      errno = 0;
+      uid_t ruid = getuid ();
+      if ((ruid == (uid_t) -1 && errno) || dest_sb.st_uid != ruid)
+        return true;
+    }
+  else if (dest_sb.st_uid != owner_id)
+    return true;
+
+  if (group_id == (uid_t) -1)
+    {
+      errno = 0;
+      gid_t rgid = getgid ();
+      if ((rgid == (uid_t) -1 && errno) || dest_sb.st_gid != rgid)
+        return true;
+    }
+  else if (dest_sb.st_gid != group_id)
     return true;
 
   /* compare SELinux context if preserving */
@@ -281,7 +275,9 @@ cp_option_init (struct cp_options *x)
   x->preserve_links = false;
   x->preserve_mode = false;
   x->preserve_timestamps = false;
+  x->explicit_no_preserve_mode = false;
   x->reduce_diagnostics=false;
+  x->data_copy_required = true;
   x->require_preserve = false;
   x->require_preserve_context = false;
   x->require_preserve_xattr = false;
@@ -364,8 +360,8 @@ setdefaultfilecon (char const *file)
 
   /* If there's an error determining the context, or it has none,
      return to allow default context */
-  if ((matchpathcon (file, st.st_mode, &scontext) != 0) ||
-      STREQ (scontext, "<<none>>"))
+  if ((matchpathcon (file, st.st_mode, &scontext) != 0)
+      || STREQ (scontext, "<<none>>"))
     {
       if (scontext != NULL)
         freecon (scontext);
@@ -409,6 +405,27 @@ target_directory_operand (char const *file)
   return is_a_dir;
 }
 
+/* Report that directory DIR was made, if OPTIONS requests this.  */
+static void
+announce_mkdir (char const *dir, void *options)
+{
+  struct cp_options const *x = options;
+  if (x->verbose)
+    prog_fprintf (stdout, _("creating directory %s"), quote (dir));
+}
+
+/* Make ancestor directory DIR, whose last file name component is
+   COMPONENT, with options OPTIONS.  Assume the working directory is
+   COMPONENT's parent.  */
+static int
+make_ancestor (char const *dir, char const *component, void *options)
+{
+  int r = mkdir (component, DEFAULT_MODE);
+  if (r == 0)
+    announce_mkdir (dir, options);
+  return r;
+}
+
 /* Process a command-line file name, for the -d option.  */
 static int
 process_dir (char *dir, struct savewd *wd, void *options)
@@ -421,417 +438,76 @@ process_dir (char *dir, struct savewd *wd, void *options)
           : EXIT_FAILURE);
 }
 
-int
-main (int argc, char **argv)
-{
-  int optc;
-  int exit_status = EXIT_SUCCESS;
-  const char *specified_mode = NULL;
-  bool make_backups = false;
-  char *backup_suffix_string;
-  char *version_control_string = NULL;
-  bool mkdir_and_install = false;
-  struct cp_options x;
-  char const *target_directory = NULL;
-  bool no_target_directory = false;
-  int n_files;
-  char **file;
-  bool strip_program_specified = false;
-  security_context_t scontext = NULL;
-  /* set iff kernel has extra selinux system calls */
-  selinux_enabled = (0 < is_selinux_enabled ());
-
-  initialize_main (&argc, &argv);
-  set_program_name (argv[0]);
-  setlocale (LC_ALL, "");
-  bindtextdomain (PACKAGE, LOCALEDIR);
-  textdomain (PACKAGE);
+/* Copy file FROM onto file TO, creating TO if necessary.
+   Return true if successful.  */
 
-  atexit (close_stdin);
+static bool
+copy_file (const char *from, const char *to, const struct cp_options *x)
+{
+  bool copy_into_self;
 
-  cp_option_init (&x);
+  if (copy_only_if_needed && !need_copy (from, to, x))
+    return true;
 
-  owner_name = NULL;
-  group_name = NULL;
-  strip_files = false;
-  dir_arg = false;
-  umask (0);
+  /* Allow installing from non-regular files like /dev/null.
+     Charles Karney reported that some Sun version of install allows that
+     and that sendmail's installation process relies on the behavior.
+     However, since !x->recursive, the call to "copy" will fail if FROM
+     is a directory.  */
 
-  /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
-     we'll actually use backup_suffix_string.  */
-  backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
+  return copy (from, to, false, x, &copy_into_self, NULL);
+}
 
-  while ((optc = getopt_long (argc, argv, "bcCsDdg:m:o:pt:TvS:Z:", long_options,
-                              NULL)) != -1)
-    {
-      switch (optc)
-        {
-        case 'b':
-          make_backups = true;
-          if (optarg)
-            version_control_string = optarg;
-          break;
-        case 'c':
-          break;
-        case 'C':
-          copy_only_if_needed = true;
-          break;
-        case 's':
-          strip_files = true;
-#ifdef SIGCHLD
-          /* System V fork+wait does not work if SIGCHLD is ignored.  */
-          signal (SIGCHLD, SIG_DFL);
-#endif
-          break;
-        case STRIP_PROGRAM_OPTION:
-          strip_program = xstrdup (optarg);
-          strip_program_specified = true;
-          break;
-        case 'd':
-          dir_arg = true;
-          break;
-        case 'D':
-          mkdir_and_install = true;
-          break;
-        case 'v':
-          x.verbose = true;
-          break;
-        case 'g':
-          group_name = optarg;
-          break;
-        case 'm':
-          specified_mode = optarg;
-          break;
-        case 'o':
-          owner_name = optarg;
-          break;
-        case 'p':
-          x.preserve_timestamps = true;
-          break;
-        case 'S':
-          make_backups = true;
-          backup_suffix_string = optarg;
-          break;
-        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;
+/* Set the attributes of file or directory NAME.
+   Return true if successful.  */
 
-        case PRESERVE_CONTEXT_OPTION_DEPRECATED:
-          error (0, 0, _("WARNING: --preserve_context is deprecated; "
-                         "use --preserve-context instead"));
-          /* fall through */
-        case PRESERVE_CONTEXT_OPTION:
-          if ( ! selinux_enabled)
-            {
-              error (0, 0, _("WARNING: ignoring --preserve-context; "
-                             "this kernel is not SELinux-enabled"));
-              break;
-            }
-          x.preserve_security_context = true;
-          use_default_selinux_context = false;
-          break;
-        case 'Z':
-          if ( ! selinux_enabled)
-            {
-              error (0, 0, _("WARNING: ignoring --context (-Z); "
-                             "this kernel is not SELinux-enabled"));
-              break;
-            }
-          scontext = optarg;
-          use_default_selinux_context = false;
-          break;
-        case_GETOPT_HELP_CHAR;
-        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
-        default:
-          usage (EXIT_FAILURE);
-        }
-    }
+static bool
+change_attributes (char const *name)
+{
+  bool ok = false;
+  /* chown must precede chmod because on some systems,
+     chown clears the set[ug]id bits for non-superusers,
+     resulting in incorrect permissions.
+     On System V, users can give away files with chown and then not
+     be able to chmod them.  So don't give files away.
 
-  /* Check for invalid combinations of arguments. */
-  if (dir_arg && strip_files)
-    error (EXIT_FAILURE, 0,
-           _("the strip option may not be used when installing a directory"));
-  if (dir_arg && target_directory)
-    error (EXIT_FAILURE, 0,
-           _("target directory not allowed when installing a directory"));
+     We don't normally ignore errors from chown because the idea of
+     the install command is that the file is supposed to end up with
+     precisely the attributes that the user specified (or defaulted).
+     If the file doesn't end up with the group they asked for, they'll
+     want to know.  */
 
-  if (x.preserve_security_context && scontext != NULL)
-    error (EXIT_FAILURE, 0,
-           _("cannot force target context to %s and preserve it"),
-           quote (scontext));
+  if (! (owner_id == (uid_t) -1 && group_id == (gid_t) -1)
+      && lchown (name, owner_id, group_id) != 0)
+    error (0, errno, _("cannot change ownership of %s"), quote (name));
+  else if (chmod (name, mode) != 0)
+    error (0, errno, _("cannot change permissions of %s"), quote (name));
+  else
+    ok = true;
 
-  if (backup_suffix_string)
-    simple_backup_suffix = xstrdup (backup_suffix_string);
+  if (use_default_selinux_context)
+    setdefaultfilecon (name);
 
-  x.backup_type = (make_backups
-                   ? xget_version (_("backup type"),
-                                   version_control_string)
-                   : no_backups);
+  return ok;
+}
 
-  if (scontext && setfscreatecon (scontext) < 0)
-    error (EXIT_FAILURE, errno,
-           _("failed to set default file creation context to %s"),
-           quote (scontext));
+/* Set the timestamps of file DEST to match those of SRC_SB.
+   Return true if successful.  */
 
-  n_files = argc - optind;
-  file = argv + optind;
+static bool
+change_timestamps (struct stat const *src_sb, char const *dest)
+{
+  struct timespec timespec[2];
+  timespec[0] = get_stat_atime (src_sb);
+  timespec[1] = get_stat_mtime (src_sb);
 
-  if (n_files <= ! (dir_arg || target_directory))
+  if (utimens (dest, timespec))
     {
-      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);
+      error (0, errno, _("cannot set time stamps for %s"), quote (dest));
+      return false;
     }
-
-  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, _("extra operand %s"), quote (file[2]));
-          usage (EXIT_FAILURE);
-        }
-    }
-  else if (! (dir_arg || target_directory))
-    {
-      if (2 <= n_files && target_directory_operand (file[n_files - 1]))
-        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 (specified_mode)
-    {
-      struct mode_change *change = mode_compile (specified_mode);
-      if (!change)
-        error (EXIT_FAILURE, 0, _("invalid mode %s"), quote (specified_mode));
-      mode = mode_adjust (0, false, 0, change, NULL);
-      dir_mode = mode_adjust (0, true, 0, change, &dir_mode_bits);
-      free (change);
-    }
-
-  if (strip_program_specified && !strip_files)
-    error (0, 0, _("WARNING: ignoring --strip-program option as -s option was "
-                   "not specified"));
-
-  if (copy_only_if_needed && x.preserve_timestamps)
-    {
-      error (0, 0, _("options --compare (-C) and --preserve-timestamps are "
-                     "mutually exclusive"));
-      usage (EXIT_FAILURE);
-    }
-
-  if (copy_only_if_needed && strip_files)
-    {
-      error (0, 0, _("options --compare (-C) and --strip are mutually "
-                     "exclusive"));
-      usage (EXIT_FAILURE);
-    }
-
-  if (copy_only_if_needed && extra_mode (mode))
-    error (0, 0, _("the --compare (-C) option is ignored when you"
-                   " specify a mode with non-permission bits"));
-
-  get_ids ();
-
-  if (dir_arg)
-    exit_status = savewd_process_files (n_files, file, process_dir, &x);
-  else
-    {
-      /* FIXME: it's a little gross that this initialization is
-         required by copy.c::copy. */
-      hash_init ();
-
-      if (!target_directory)
-        {
-          if (! (mkdir_and_install
-                 ? install_file_in_file_parents (file[0], file[1], &x)
-                 : install_file_in_file (file[0], file[1], &x)))
-            exit_status = EXIT_FAILURE;
-        }
-      else
-        {
-          int i;
-          dest_info_init (&x);
-          for (i = 0; i < n_files; i++)
-            if (! install_file_in_dir (file[i], target_directory, &x))
-              exit_status = EXIT_FAILURE;
-        }
-    }
-
-  exit (exit_status);
-}
-
-/* Copy file FROM onto file TO, creating any missing parent directories of TO.
-   Return true if successful.  */
-
-static bool
-install_file_in_file_parents (char const *from, char *to,
-                              struct cp_options *x)
-{
-  bool save_working_directory =
-    ! (IS_ABSOLUTE_FILE_NAME (from) && IS_ABSOLUTE_FILE_NAME (to));
-  int status = EXIT_SUCCESS;
-
-  struct savewd wd;
-  savewd_init (&wd);
-  if (! save_working_directory)
-    savewd_finish (&wd);
-
-  if (mkancesdirs (to, &wd, make_ancestor, x) == -1)
-    {
-      error (0, errno, _("cannot create directory %s"), to);
-      status = EXIT_FAILURE;
-    }
-
-  if (save_working_directory)
-    {
-      int restore_result = savewd_restore (&wd, status);
-      int restore_errno = errno;
-      savewd_finish (&wd);
-      if (EXIT_SUCCESS < restore_result)
-        return false;
-      if (restore_result < 0 && status == EXIT_SUCCESS)
-        {
-          error (0, restore_errno, _("cannot create directory %s"), to);
-          return false;
-        }
-    }
-
-  return (status == EXIT_SUCCESS && install_file_in_file (from, to, x));
-}
-
-/* Copy file FROM onto file TO and give TO the appropriate
-   attributes.
-   Return true if successful.  */
-
-static bool
-install_file_in_file (const char *from, const char *to,
-                      const struct cp_options *x)
-{
-  struct stat from_sb;
-  if (x->preserve_timestamps && stat (from, &from_sb) != 0)
-    {
-      error (0, errno, _("cannot stat %s"), quote (from));
-      return false;
-    }
-  if (! copy_file (from, to, x))
-    return false;
-  if (strip_files)
-    strip (to);
-  if (x->preserve_timestamps && (strip_files || ! S_ISREG (from_sb.st_mode))
-      && ! change_timestamps (&from_sb, to))
-    return false;
-  return change_attributes (to);
-}
-
-/* Copy file FROM into directory TO_DIR, keeping its same name,
-   and give the copy the appropriate attributes.
-   Return true if successful.  */
-
-static bool
-install_file_in_dir (const char *from, const char *to_dir,
-                     const struct cp_options *x)
-{
-  const char *from_base = last_component (from);
-  char *to = file_name_concat (to_dir, from_base, NULL);
-  bool ret = install_file_in_file (from, to, x);
-  free (to);
-  return ret;
-}
-
-/* Copy file FROM onto file TO, creating TO if necessary.
-   Return true if successful.  */
-
-static bool
-copy_file (const char *from, const char *to, const struct cp_options *x)
-{
-  bool copy_into_self;
-
-  if (copy_only_if_needed && !need_copy (from, to, x))
-    return true;
-
-  /* Allow installing from non-regular files like /dev/null.
-     Charles Karney reported that some Sun version of install allows that
-     and that sendmail's installation process relies on the behavior.
-     However, since !x->recursive, the call to "copy" will fail if FROM
-     is a directory.  */
-
-  return copy (from, to, false, x, &copy_into_self, NULL);
-}
-
-/* Set the attributes of file or directory NAME.
-   Return true if successful.  */
-
-static bool
-change_attributes (char const *name)
-{
-  bool ok = false;
-  /* chown must precede chmod because on some systems,
-     chown clears the set[ug]id bits for non-superusers,
-     resulting in incorrect permissions.
-     On System V, users can give away files with chown and then not
-     be able to chmod them.  So don't give files away.
-
-     We don't normally ignore errors from chown because the idea of
-     the install command is that the file is supposed to end up with
-     precisely the attributes that the user specified (or defaulted).
-     If the file doesn't end up with the group they asked for, they'll
-     want to know.  */
-
-  if (! (owner_id == (uid_t) -1 && group_id == (gid_t) -1)
-      && lchown (name, owner_id, group_id) != 0)
-    error (0, errno, _("cannot change ownership of %s"), quote (name));
-  else if (chmod (name, mode) != 0)
-    error (0, errno, _("cannot change permissions of %s"), quote (name));
-  else
-    ok = true;
-
-  if (use_default_selinux_context)
-    setdefaultfilecon (name);
-
-  return ok;
-}
-
-/* Set the timestamps of file TO to match those of file FROM.
-   Return true if successful.  */
-
-static bool
-change_timestamps (struct stat const *from_sb, char const *to)
-{
-  struct timespec timespec[2];
-  timespec[0] = get_stat_atime (from_sb);
-  timespec[1] = get_stat_mtime (from_sb);
-
-  if (utimens (to, timespec))
-    {
-      error (0, errno, _("cannot set time stamps for %s"), quote (to));
-      return false;
-    }
-  return true;
-}
+  return true;
+}
 
 /* Strip the symbol table from the file NAME.
    We could dig the magic number out of the file first to
@@ -908,33 +584,11 @@ get_ids (void)
     group_id = (gid_t) -1;
 }
 
-/* Report that directory DIR was made, if OPTIONS requests this.  */
-static void
-announce_mkdir (char const *dir, void *options)
-{
-  struct cp_options const *x = options;
-  if (x->verbose)
-    prog_fprintf (stdout, _("creating directory %s"), quote (dir));
-}
-
-/* Make ancestor directory DIR, whose last file name component is
-   COMPONENT, with options OPTIONS.  Assume the working directory is
-   COMPONENT's parent.  */
-static int
-make_ancestor (char const *dir, char const *component, void *options)
-{
-  int r = mkdir (component, DEFAULT_MODE);
-  if (r == 0)
-    announce_mkdir (dir, options);
-  return r;
-}
-
 void
 usage (int status)
 {
   if (status != EXIT_SUCCESS)
-    fprintf (stderr, _("Try `%s --help' for more information.\n"),
-             program_name);
+    emit_try_help ();
   else
     {
       printf (_("\
@@ -987,14 +641,15 @@ Mandatory arguments to long options are mandatory for short options too.\n\
 "), stdout);
       fputs (_("\
       --preserve-context  preserve SELinux security context\n\
-  -Z, --context=CONTEXT  set SELinux security context of files and directories\n\
+  -Z, --context=CONTEXT  set SELinux security context of files and directories\
+\n\
 "), stdout);
 
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
       fputs (_("\
 \n\
-The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
+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\
@@ -1009,3 +664,340 @@ the VERSION_CONTROL environment variable.  Here are the values:\n\
     }
   exit (status);
 }
+
+/* Copy file FROM onto file TO and give TO the appropriate
+   attributes.
+   Return true if successful.  */
+
+static bool
+install_file_in_file (const char *from, const char *to,
+                      const struct cp_options *x)
+{
+  struct stat from_sb;
+  if (x->preserve_timestamps && stat (from, &from_sb) != 0)
+    {
+      error (0, errno, _("cannot stat %s"), quote (from));
+      return false;
+    }
+  if (! copy_file (from, to, x))
+    return false;
+  if (strip_files)
+    strip (to);
+  if (x->preserve_timestamps && (strip_files || ! S_ISREG (from_sb.st_mode))
+      && ! change_timestamps (&from_sb, to))
+    return false;
+  return change_attributes (to);
+}
+
+/* Copy file FROM onto file TO, creating any missing parent directories of TO.
+   Return true if successful.  */
+
+static bool
+install_file_in_file_parents (char const *from, char *to,
+                              struct cp_options *x)
+{
+  bool save_working_directory =
+    ! (IS_ABSOLUTE_FILE_NAME (from) && IS_ABSOLUTE_FILE_NAME (to));
+  int status = EXIT_SUCCESS;
+
+  struct savewd wd;
+  savewd_init (&wd);
+  if (! save_working_directory)
+    savewd_finish (&wd);
+
+  if (mkancesdirs (to, &wd, make_ancestor, x) == -1)
+    {
+      error (0, errno, _("cannot create directory %s"), to);
+      status = EXIT_FAILURE;
+    }
+
+  if (save_working_directory)
+    {
+      int restore_result = savewd_restore (&wd, status);
+      int restore_errno = errno;
+      savewd_finish (&wd);
+      if (EXIT_SUCCESS < restore_result)
+        return false;
+      if (restore_result < 0 && status == EXIT_SUCCESS)
+        {
+          error (0, restore_errno, _("cannot create directory %s"), to);
+          return false;
+        }
+    }
+
+  return (status == EXIT_SUCCESS && install_file_in_file (from, to, x));
+}
+
+/* Copy file FROM into directory TO_DIR, keeping its same name,
+   and give the copy the appropriate attributes.
+   Return true if successful.  */
+
+static bool
+install_file_in_dir (const char *from, const char *to_dir,
+                     const struct cp_options *x)
+{
+  const char *from_base = last_component (from);
+  char *to = file_name_concat (to_dir, from_base, NULL);
+  bool ret = install_file_in_file (from, to, x);
+  free (to);
+  return ret;
+}
+
+int
+main (int argc, char **argv)
+{
+  int optc;
+  int exit_status = EXIT_SUCCESS;
+  const char *specified_mode = NULL;
+  bool make_backups = false;
+  char *backup_suffix_string;
+  char *version_control_string = NULL;
+  bool mkdir_and_install = false;
+  struct cp_options x;
+  char const *target_directory = NULL;
+  bool no_target_directory = false;
+  int n_files;
+  char **file;
+  bool strip_program_specified = false;
+  security_context_t scontext = NULL;
+  /* set iff kernel has extra selinux system calls */
+  selinux_enabled = (0 < is_selinux_enabled ());
+
+  initialize_main (&argc, &argv);
+  set_program_name (argv[0]);
+  setlocale (LC_ALL, "");
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
+
+  atexit (close_stdin);
+
+  cp_option_init (&x);
+
+  owner_name = NULL;
+  group_name = NULL;
+  strip_files = false;
+  dir_arg = false;
+  umask (0);
+
+  /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
+     we'll actually use backup_suffix_string.  */
+  backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
+
+  while ((optc = getopt_long (argc, argv, "bcCsDdg:m:o:pt:TvS:Z:", long_options,
+                              NULL)) != -1)
+    {
+      switch (optc)
+        {
+        case 'b':
+          make_backups = true;
+          if (optarg)
+            version_control_string = optarg;
+          break;
+        case 'c':
+          break;
+        case 'C':
+          copy_only_if_needed = true;
+          break;
+        case 's':
+          strip_files = true;
+#ifdef SIGCHLD
+          /* System V fork+wait does not work if SIGCHLD is ignored.  */
+          signal (SIGCHLD, SIG_DFL);
+#endif
+          break;
+        case STRIP_PROGRAM_OPTION:
+          strip_program = xstrdup (optarg);
+          strip_program_specified = true;
+          break;
+        case 'd':
+          dir_arg = true;
+          break;
+        case 'D':
+          mkdir_and_install = true;
+          break;
+        case 'v':
+          x.verbose = true;
+          break;
+        case 'g':
+          group_name = optarg;
+          break;
+        case 'm':
+          specified_mode = optarg;
+          break;
+        case 'o':
+          owner_name = optarg;
+          break;
+        case 'p':
+          x.preserve_timestamps = true;
+          break;
+        case 'S':
+          make_backups = true;
+          backup_suffix_string = optarg;
+          break;
+        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 PRESERVE_CONTEXT_OPTION:
+          if ( ! selinux_enabled)
+            {
+              error (0, 0, _("WARNING: ignoring --preserve-context; "
+                             "this kernel is not SELinux-enabled"));
+              break;
+            }
+          x.preserve_security_context = true;
+          use_default_selinux_context = false;
+          break;
+        case 'Z':
+          if ( ! selinux_enabled)
+            {
+              error (0, 0, _("WARNING: ignoring --context (-Z); "
+                             "this kernel is not SELinux-enabled"));
+              break;
+            }
+          scontext = optarg;
+          use_default_selinux_context = false;
+          break;
+        case_GETOPT_HELP_CHAR;
+        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+        default:
+          usage (EXIT_FAILURE);
+        }
+    }
+
+  /* Check for invalid combinations of arguments. */
+  if (dir_arg && strip_files)
+    error (EXIT_FAILURE, 0,
+           _("the strip option may not be used when installing a directory"));
+  if (dir_arg && target_directory)
+    error (EXIT_FAILURE, 0,
+           _("target directory not allowed when installing a directory"));
+
+  if (x.preserve_security_context && scontext != NULL)
+    error (EXIT_FAILURE, 0,
+           _("cannot force target context to %s and preserve it"),
+           quote (scontext));
+
+  if (backup_suffix_string)
+    simple_backup_suffix = xstrdup (backup_suffix_string);
+
+  x.backup_type = (make_backups
+                   ? xget_version (_("backup type"),
+                                   version_control_string)
+                   : no_backups);
+
+  if (scontext && setfscreatecon (scontext) < 0)
+    error (EXIT_FAILURE, errno,
+           _("failed to set default file creation context to %s"),
+           quote (scontext));
+
+  n_files = argc - optind;
+  file = argv + optind;
+
+  if (n_files <= ! (dir_arg || target_directory))
+    {
+      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 (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, _("extra operand %s"), quote (file[2]));
+          usage (EXIT_FAILURE);
+        }
+    }
+  else if (! (dir_arg || target_directory))
+    {
+      if (2 <= n_files && target_directory_operand (file[n_files - 1]))
+        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 (specified_mode)
+    {
+      struct mode_change *change = mode_compile (specified_mode);
+      if (!change)
+        error (EXIT_FAILURE, 0, _("invalid mode %s"), quote (specified_mode));
+      mode = mode_adjust (0, false, 0, change, NULL);
+      dir_mode = mode_adjust (0, true, 0, change, &dir_mode_bits);
+      free (change);
+    }
+
+  if (strip_program_specified && !strip_files)
+    error (0, 0, _("WARNING: ignoring --strip-program option as -s option was "
+                   "not specified"));
+
+  if (copy_only_if_needed && x.preserve_timestamps)
+    {
+      error (0, 0, _("options --compare (-C) and --preserve-timestamps are "
+                     "mutually exclusive"));
+      usage (EXIT_FAILURE);
+    }
+
+  if (copy_only_if_needed && strip_files)
+    {
+      error (0, 0, _("options --compare (-C) and --strip are mutually "
+                     "exclusive"));
+      usage (EXIT_FAILURE);
+    }
+
+  if (copy_only_if_needed && extra_mode (mode))
+    error (0, 0, _("the --compare (-C) option is ignored when you"
+                   " specify a mode with non-permission bits"));
+
+  get_ids ();
+
+  if (dir_arg)
+    exit_status = savewd_process_files (n_files, file, process_dir, &x);
+  else
+    {
+      /* FIXME: it's a little gross that this initialization is
+         required by copy.c::copy. */
+      hash_init ();
+
+      if (!target_directory)
+        {
+          if (! (mkdir_and_install
+                 ? install_file_in_file_parents (file[0], file[1], &x)
+                 : install_file_in_file (file[0], file[1], &x)))
+            exit_status = EXIT_FAILURE;
+        }
+      else
+        {
+          int i;
+          dest_info_init (&x);
+          for (i = 0; i < n_files; i++)
+            if (! install_file_in_dir (file[i], target_directory, &x))
+              exit_status = EXIT_FAILURE;
+        }
+    }
+
+  exit (exit_status);
+}