*** empty log message ***
[platform/upstream/coreutils.git] / src / ln.c
index 72d50fd..5dd3c51 100644 (file)
--- a/src/ln.c
+++ b/src/ln.c
@@ -1,5 +1,5 @@
 /* `ln' program to create links between files.
-   Copyright (C) 86, 89, 90, 91, 1995-2004 Free Software Foundation, Inc.
+   Copyright (C) 1986, 1989-1991, 1995-2006 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
@@ -13,7 +13,7 @@
 
    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.  */
 \f
 /* Written by Mike Parker and David MacKenzie. */
 
@@ -27,6 +27,7 @@
 #include "backupfile.h"
 #include "dirname.h"
 #include "error.h"
+#include "filenamecat.h"
 #include "quote.h"
 #include "yesno.h"
 
 # define ENABLE_HARD_LINK_TO_SYMLINK_WARNING 0
 #endif
 
-int link ();                   /* Some systems don't declare this anywhere. */
-
-#ifdef S_ISLNK
-int symlink ();
-#endif
-
 /* In being careful not even to try to make hard links to directories,
    we have to know whether link(2) follows symlinks.  If it does, then
    we have to *stat* the `source' to see if the resulting link would be
@@ -59,60 +54,35 @@ int symlink ();
   lstat (File, Stat_buf)
 #endif
 
-/* Construct a string NEW_DEST by concatenating DEST, a slash, and
-   basename(SOURCE) in alloca'd memory.  Don't modify DEST or SOURCE.  */
-
-#define PATH_BASENAME_CONCAT(new_dest, dest, source)                   \
-    do                                                                 \
-      {                                                                        \
-       const char *source_base;                                        \
-       char *tmp_source;                                               \
-       size_t buf_len = strlen (source) + 1;                           \
-                                                                       \
-       tmp_source = alloca (buf_len);                                  \
-       memcpy (tmp_source, (source), buf_len);                         \
-       strip_trailing_slashes (tmp_source);                            \
-       source_base = base_name (tmp_source);                           \
-                                                                       \
-       (new_dest) = alloca (strlen ((dest)) + 1                        \
-                                     + strlen (source_base) + 1);      \
-       stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
-      }                                                                        \
-    while (0)
-
 /* The name by which the program was run, for error messages.  */
 char *program_name;
 
 /* FIXME: document */
 static enum backup_type backup_type;
 
-/* A pointer to the function used to make links.  This will point to either
-   `link' or `symlink'. */
-static int (*linkfunc) ();
+/* If true, make symbolic links; otherwise, make hard links.  */
+static bool symbolic_link;
 
-/* If nonzero, make symbolic links; otherwise, make hard links.  */
-static int symbolic_link;
+/* If true, ask the user before removing existing files.  */
+static bool interactive;
 
-/* If nonzero, ask the user before removing existing files.  */
-static int interactive;
+/* If true, remove existing files unconditionally.  */
+static bool remove_existing_files;
 
-/* If nonzero, remove existing files unconditionally.  */
-static int remove_existing_files;
+/* If true, list each file as it is moved. */
+static bool verbose;
 
-/* If nonzero, list each file as it is moved. */
-static int verbose;
-
-/* If nonzero, allow the superuser to *attempt* to make hard links
+/* If true, allow the superuser to *attempt* to make hard links
    to directories.  However, it appears that this option is not useful
    in practice, since even the superuser is prohibited from hard-linking
    directories on most (all?) existing systems.  */
-static int hard_dir_link;
+static bool hard_dir_link;
 
 /* If nonzero, and the specified destination is a symbolic link to a
    directory, treat it just as if it were a directory.  Otherwise, the
    command `ln --force --no-dereference file symlink-to-dir' deletes
    symlink-to-dir before creating the new link.  */
-static int dereference_dest_dir_symlinks = 1;
+static bool dereference_dest_dir_symlinks = true;
 
 static struct option const long_options[] =
 {
@@ -126,7 +96,6 @@ static struct option const long_options[] =
   {"target-directory", required_argument, NULL, 't'},
   {"symbolic", no_argument, NULL, 's'},
   {"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}
@@ -140,7 +109,7 @@ static struct option const long_options[] =
 static bool
 target_directory_operand (char const *file)
 {
-  char const *b = base_name (file);
+  char const *b = last_component (file);
   size_t blen = strlen (b);
   bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1]));
   struct stat st;
@@ -156,16 +125,17 @@ target_directory_operand (char const *file)
 
 /* Make a link DEST to the (usually) existing file SOURCE.
    Symbolic links to nonexistent files are allowed.
-   If DEST_IS_DIR, put the link to SOURCE in the DEST directory.
-   Return 1 if there is an error, otherwise 0.  */
+   Return true if successful.  */
 
-static int
-do_link (const char *source, const char *dest, bool dest_is_dir)
+static bool
+do_link (const char *source, const char *dest)
 {
   struct stat source_stats;
   struct stat dest_stats;
   char *dest_backup = NULL;
-  int lstat_status = -1;
+  bool lstat_ok = false;
+  bool source_is_dir = false;
+  bool ok;
 
   /* Use stat here instead of lstat.
      On SVR4, link does not follow symlinks, so this check disallows
@@ -176,7 +146,7 @@ do_link (const char *source, const char *dest, bool dest_is_dir)
       if (STAT_LIKE_LINK (source, &source_stats) != 0)
        {
          error (0, errno, _("accessing %s"), quote (source));
-         return 1;
+         return false;
        }
 
       if (ENABLE_HARD_LINK_TO_SYMLINK_WARNING
@@ -187,29 +157,25 @@ do_link (const char *source, const char *dest, bool dest_is_dir)
                 quote (source));
        }
 
-      if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
+      if (S_ISDIR (source_stats.st_mode))
        {
-         error (0, 0, _("%s: hard link not allowed for directory"),
-                quote (source));
-         return 1;
+         source_is_dir = true;
+         if (! hard_dir_link)
+           {
+             error (0, 0, _("%s: hard link not allowed for directory"),
+                    quote (source));
+             return false;
+           }
        }
     }
 
-  if (dest_is_dir)
+  if (remove_existing_files || interactive || backup_type != no_backups)
     {
-      /* Treat DEST as a directory; build the full filename.  */
-      char *new_dest;
-      PATH_BASENAME_CONCAT (new_dest, dest, source);
-      dest = new_dest;
-    }
-
-  if (remove_existing_files || interactive || backup_type != none)
-    {
-      lstat_status = lstat (dest, &dest_stats);
-      if (lstat_status != 0 && errno != ENOENT)
+      lstat_ok = (lstat (dest, &dest_stats) == 0);
+      if (!lstat_ok && errno != ENOENT)
        {
          error (0, errno, _("accessing %s"), quote (dest));
-         return 1;
+         return false;
        }
     }
 
@@ -219,13 +185,13 @@ do_link (const char *source, const char *dest, bool dest_is_dir)
      But if the source and destination are the same, don't remove
      anything and fail right here.  */
   if (remove_existing_files
-      && lstat_status == 0
+      && lstat_ok
       /* Allow `ln -sf --backup k k' to succeed in creating the
         self-referential symlink, but don't allow the hard-linking
         equivalent: `ln -f k k' (with or without --backup) to get
         beyond this point, because the error message you'd get is
         misleading.  */
-      && (backup_type == none || !symbolic_link)
+      && (backup_type == no_backups || !symbolic_link)
       && (!symbolic_link || stat (source, &source_stats) == 0)
       && SAME_INODE (source_stats, dest_stats)
       /* The following detects whether removing DEST will also remove
@@ -236,58 +202,43 @@ do_link (const char *source, const char *dest, bool dest_is_dir)
     {
       error (0, 0, _("%s and %s are the same file"),
             quote_n (0, source), quote_n (1, dest));
-      return 1;
+      return false;
     }
 
-  if (lstat_status == 0)
+  if (lstat_ok)
     {
       if (S_ISDIR (dest_stats.st_mode))
        {
          error (0, 0, _("%s: cannot overwrite directory"), quote (dest));
-         return 1;
+         return false;
        }
       if (interactive)
        {
          fprintf (stderr, _("%s: replace %s? "), program_name, quote (dest));
          if (!yesno ())
-           return 0;
+           return true;
+         remove_existing_files = true;
        }
 
-      if (backup_type != none)
+      if (backup_type != no_backups)
        {
-         char *tmp_backup = find_backup_file_name (dest, backup_type);
-         size_t buf_len = strlen (tmp_backup) + 1;
-         dest_backup = alloca (buf_len);
-         memcpy (dest_backup, tmp_backup, buf_len);
-         free (tmp_backup);
-         if (rename (dest, dest_backup))
+         dest_backup = find_backup_file_name (dest, backup_type);
+         if (rename (dest, dest_backup) != 0)
            {
-             if (errno != ENOENT)
+             int rename_errno = errno;
+             free (dest_backup);
+             dest_backup = NULL;
+             if (rename_errno != ENOENT)
                {
-                 error (0, errno, _("cannot backup %s"), quote (dest));
-                 return 1;
+                 error (0, rename_errno, _("cannot backup %s"), quote (dest));
+                 return false;
                }
-             else
-               dest_backup = NULL;
            }
        }
     }
 
-  if (verbose)
-    {
-      printf ((symbolic_link
-              ? _("create symbolic link %s to %s")
-              : _("create hard link %s to %s")),
-             quote_n (0, dest), quote_n (1, source));
-      if (dest_backup)
-       printf (_(" (backup: %s)"), quote (dest_backup));
-      putchar ('\n');
-    }
-
-  if ((*linkfunc) (source, dest) == 0)
-    {
-      return 0;
-    }
+  ok = ((symbolic_link ? symlink (source, dest) : link (source, dest))
+       == 0);
 
   /* If the attempt to create a link failed and we are removing or
      backing up destinations, unlink the destination and try again.
@@ -295,7 +246,7 @@ do_link (const char *source, const char *dest, bool dest_is_dir)
      POSIX 1003.1-2004 requires that ln -f A B must unlink B even on
      failure (e.g., when A does not exist).  This is counterintuitive,
      and we submitted a defect report
-     <http://www.opengroup.org/sophocles/show_mail.tpl?source=L&listname=austin-review-l&id=1795>
+     <http://www.opengroup.org/austin/mailarchives/ag-review/msg01794.html>
      (2004-06-24).  If the committee does not fix the standard we'll
      have to change the behavior of ln -f, at least if POSIXLY_CORRECT
      is set.  In the meantime ln -f A B will not unlink B unless the
@@ -307,30 +258,53 @@ do_link (const char *source, const char *dest, bool dest_is_dir)
      If we didn't remove DEST in that case, the subsequent LINKFUNC
      call would fail.  */
 
-  if (errno == EEXIST && (remove_existing_files || dest_backup))
+  if (!ok && errno == EEXIST && (remove_existing_files || dest_backup))
     {
       if (unlink (dest) != 0)
        {
          error (0, errno, _("cannot remove %s"), quote (dest));
-         return 1;
+         free (dest_backup);
+         return false;
        }
 
-      if (linkfunc (source, dest) == 0)
-       return 0;
+      ok = ((symbolic_link ? symlink (source, dest) : link (source, dest))
+           == 0);
     }
 
-  error (0, errno,
-        (symbolic_link
-         ? _("creating symbolic link %s to %s")
-         : _("creating hard link %s to %s")),
-        quote_n (0, dest), quote_n (1, source));
-
-  if (dest_backup)
+  if (ok)
     {
-      if (rename (dest_backup, dest))
-       error (0, errno, _("cannot un-backup %s"), quote (dest));
+      if (verbose)
+       {
+         if (dest_backup)
+           printf ("%s ~ ", quote (dest_backup));
+         printf ("%s %c> %s\n", quote_n (0, dest), (symbolic_link ? '-' : '='),
+                 quote_n (1, source));
+       }
+    }
+  else
+    {
+      error (0, errno,
+            (symbolic_link
+             ? (errno != ENAMETOOLONG && *source
+                ? _("creating symbolic link %s")
+                : _("creating symbolic link %s -> %s"))
+             : (errno == EMLINK && !source_is_dir
+                ? _("creating hard link to %.0s%s")
+                : (errno == EDQUOT || errno == EEXIST || errno == ENOSPC
+                   || errno == EROFS)
+                ? _("creating hard link %s")
+                : _("creating hard link %s => %s"))),
+            quote_n (0, dest), quote_n (1, source));
+
+      if (dest_backup)
+       {
+         if (rename (dest_backup, dest) != 0)
+           error (0, errno, _("cannot un-backup %s"), quote (dest));
+       }
     }
-  return 1;
+
+  free (dest_backup);
+  return ok;
 }
 
 void
@@ -378,7 +352,7 @@ Mandatory arguments to long options are mandatory for short options too.\n\
   -t, --target-directory=DIRECTORY  specify the DIRECTORY in which to create\n\
                                 the links\n\
   -T, --no-target-directory   treat LINK_NAME as a normal file\n\
-  -v, --verbose               print name of each file before linking\n\
+  -v, --verbose               print name of each linked file\n\
 "), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
@@ -404,11 +378,11 @@ int
 main (int argc, char **argv)
 {
   int c;
-  int errors;
-  int make_backups = 0;
+  bool ok;
+  bool make_backups = false;
   char *backup_suffix_string;
   char *version_control_string = NULL;
-  char *target_directory = NULL;
+  char const *target_directory = NULL;
   bool no_target_directory = false;
   int n_files;
   char **file;
@@ -426,47 +400,36 @@ main (int argc, char **argv)
   backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
 
   symbolic_link = remove_existing_files = interactive = verbose
-    = hard_dir_link = 0;
-  errors = 0;
+    = hard_dir_link = false;
 
-  while ((c = getopt_long (argc, argv, "bdfinst:vFS:TV:", long_options, NULL))
+  while ((c = getopt_long (argc, argv, "bdfinst:vFS:T", long_options, NULL))
         != -1)
     {
       switch (c)
        {
-       case 0:                 /* Long-named option. */
-         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 'd':
        case 'F':
-         hard_dir_link = 1;
+         hard_dir_link = true;
          break;
        case 'f':
-         remove_existing_files = 1;
-         interactive = 0;
+         remove_existing_files = true;
+         interactive = false;
          break;
        case 'i':
-         remove_existing_files = 0;
-         interactive = 1;
+         remove_existing_files = false;
+         interactive = true;
          break;
        case 'n':
-         dereference_dest_dir_symlinks = 0;
+         dereference_dest_dir_symlinks = false;
          break;
        case 's':
 #ifdef S_ISLNK
-         symbolic_link = 1;
+         symbolic_link = true;
 #else
          error (EXIT_FAILURE, 0,
                 _("symbolic links are not supported on this system"));
@@ -490,10 +453,10 @@ main (int argc, char **argv)
          no_target_directory = true;
          break;
        case 'v':
-         verbose = 1;
+         verbose = true;
          break;
        case 'S':
-         make_backups = 1;
+         make_backups = true;
          backup_suffix_string = optarg;
          break;
        case_GETOPT_HELP_CHAR;
@@ -541,26 +504,30 @@ main (int argc, char **argv)
               quote (file[n_files - 1]));
     }
 
-  if (symbolic_link)
-    linkfunc = symlink;
-  else
-    linkfunc = link;
-
   if (backup_suffix_string)
     simple_backup_suffix = xstrdup (backup_suffix_string);
 
   backup_type = (make_backups
                 ? xget_version (_("backup type"), version_control_string)
-                : none);
+                : no_backups);
 
   if (target_directory)
     {
       int i;
+      ok = true;
       for (i = 0; i < n_files; ++i)
-       errors |= do_link (file[i], target_directory, true);
+       {
+         char *dest_base;
+         char *dest = file_name_concat (target_directory,
+                                        last_component (file[i]),
+                                        &dest_base);
+         strip_trailing_slashes (dest_base);
+         ok &= do_link (file[i], dest);
+         free (dest);
+       }
     }
   else
-    errors = do_link (file[0], file[1], false);
+    ok = do_link (file[0], file[1]);
 
-  exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }