home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / useful / dist / gnu / fileutils / fileutils-3.9-amiga / src / mvdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-13  |  6.5 KB  |  245 lines

  1. /* mvdir -- rename directory on System V r<4
  2.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Helper program for GNU mv on systems that lack the rename system call.
  19.  
  20.    Usage: mvdir from to
  21.  
  22.    FROM must be an existing directory.
  23.    TO must not exist, but its parent must exist.
  24.    This program performs the necessary sanity checking on its arguments.
  25.  
  26.    Must be installed setuid root.
  27.  
  28.    Ian Dall (ian@sibyl.eleceng.ua.oz.au)
  29.    and David MacKenzie (djm@gnu.ai.mit.edu) */
  30.  
  31. #ifdef HAVE_CONFIG_H
  32. #if defined (CONFIG_BROKETS)
  33. /* We use <config.h> instead of "config.h" so that a compilation
  34.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  35.    (which it would do because it found this file in $srcdir).  */
  36. #include <config.h>
  37. #else
  38. #include "config.h"
  39. #endif
  40. #endif
  41.  
  42. #include <stdio.h>
  43. #include <getopt.h>
  44. #include <sys/types.h>
  45. #include <signal.h>
  46. #include "system.h"
  47. #include "version.h"
  48.  
  49. #ifndef HIPRI
  50. #define HIPRI -10
  51. #endif
  52.  
  53. #ifdef DEBUG
  54. #define link(FROM, TO) (printf("Linking %s to %s\n", FROM, TO), 0)
  55. #define unlink(FILE) (printf("Unlinking %s\n", FILE), 0)
  56. #endif
  57.  
  58. /* The name this program was run with. */
  59. char *program_name;
  60.  
  61. char *getcwd ();
  62.  
  63. char *basename ();
  64. char *dirname ();
  65. char *xmalloc ();
  66. void error ();
  67. void strip_trailing_slashes ();
  68.  
  69. static char *fullpath ();
  70.  
  71. /* If non-zero, display usage information and exit.  */
  72. static int show_help;
  73.  
  74. /* If non-zero, print the version on standard output and exit.  */
  75. static int show_version;
  76.  
  77. static struct option const long_options[] =
  78. {
  79.   {"help", no_argument, &show_help, 1},
  80.   {"version", no_argument, &show_version, 1},
  81.   {0, 0, 0, 0}
  82. };
  83.  
  84. static void
  85. usage (status)
  86.      int status;
  87. {
  88.   if (status != 0)
  89.     fprintf (stderr, "Try `%s --help' for more information.\n",
  90.          program_name);
  91.   else
  92.     {
  93.       printf ("Usage: %s [OPTION]... EXISTING_DIR NEW_DIR\n", program_name);
  94.       printf ("\
  95. \n\
  96.    --help      display this help and exit\n\
  97.    --version   output version information and exit\n");
  98.     }
  99.   exit (status);
  100. }
  101.  
  102. main (argc, argv)
  103.      int argc;
  104.      char **argv;
  105. {
  106.   char *from, *from_parent, *from_base;
  107.   char *to, *to_parent, *to_parent_path, *to_base, *to_dotdot;
  108.   struct stat from_stats, to_stats;
  109.   char *slash;
  110.   int c, i;
  111.  
  112.   program_name = argv[0];
  113.  
  114.   while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
  115.     {
  116.       switch (c)
  117.     {
  118.     case 0:
  119.       break;
  120.  
  121.     default:
  122.       usage (2);
  123.     }
  124.     }
  125.  
  126.   if (show_version)
  127.     {
  128.       printf ("%s\n", version_string);
  129.       exit (0);
  130.     }
  131.  
  132.   if (show_help)
  133.     usage (0);
  134.  
  135.   if (argc - optind != 2)
  136.     usage (2);
  137.  
  138.   from = argv[optind];
  139.   to = argv[optind + 1];
  140.   strip_trailing_slashes (from);
  141.   strip_trailing_slashes (to);
  142.   from_parent = dirname (from);
  143.   to_parent = dirname (to);
  144.   if (!from_parent || !to_parent)
  145.     error (2, 0, "virtual memory exhausted");
  146.  
  147.   /* Make sure `from' is not "." or "..". */
  148.   from_base = basename (from);
  149.   if (!strcmp (from_base, ".") || !strcmp (from_base, ".."))
  150.     error (1, 0, "cannot rename `.' or `..'");
  151.  
  152.   /* Even with an effective uid of root, link fails if the target exists.
  153.      That is what we want, so don't unlink `to' first.
  154.      However, we do need to check that the directories that link and unlink
  155.      will modify exist and are writable by the user. */
  156.  
  157.   if (stat (from, &from_stats))
  158.     error (1, errno, "%s", from);
  159.   if (!S_ISDIR (from_stats.st_mode))
  160.     error (1, 0, "`%s' is not a directory", from);
  161.   if (access (from_parent, W_OK))
  162.     error (1, errno, "cannot write to `%s'", from_parent);
  163.   if (access (to_parent, W_OK))
  164.     error (1, errno, "cannot write to `%s'", to_parent);
  165.  
  166.   /* To prevent disconnecting the tree rooted at `from' from its parent,
  167.      quit if any of the directories in `to' are the same (dev and ino)
  168.      as the directory `from'. */
  169.  
  170.   slash = to_parent_path = fullpath (to_parent);
  171.   while (1)
  172.     {
  173.       while (*slash == '/')
  174.     ++slash;
  175.       slash = index (slash, '/');
  176.       if (slash != NULL)
  177.     *slash = '\0';
  178.  
  179.       if (stat (to_parent_path, &to_stats))
  180.     error (1, errno, "%s", to_parent_path);
  181.       if (to_stats.st_dev == from_stats.st_dev
  182.       && to_stats.st_ino == from_stats.st_ino)
  183.     error (1, 0, "`%s' is an ancestor of `%s'", from, to);
  184.       if (slash != NULL)
  185.     *slash++ = '/';
  186.       else
  187.     break;
  188.     }
  189.  
  190.   /* We can't make the renaming atomic, but we do our best. */
  191.   for (i = NSIG; i > 0; i--)
  192.     if (i != SIGKILL)
  193.       signal (i, SIG_IGN);
  194.   setuid (0);            /* Make real uid 0 so it is harder to kill. */
  195.   nice (HIPRI - nice (0));    /* Raise priority. */
  196.  
  197.   if (link (from, to))
  198.     error (1, errno, "cannot link `%s' to `%s'", from, to);
  199.   if (unlink (from))
  200.     error (1, errno, "cannot unlink `%s'", from);
  201.  
  202.   /* Replace the directory's `..' entry.  It used to be a link to
  203.      the parent of `from'; make it a link to the parent of `to' instead.
  204.      To handle the case where `from' is the current directory
  205.      and `to' starts with `../', we go to the full path of `to's parent,
  206.      lest we try to reference the new directory's `..' while creating it.  */
  207.  
  208.   to_base = basename (to);
  209.   i = strlen (to_base);
  210.   to_dotdot = xmalloc (i + 4);
  211.   strcpy (to_dotdot, to_base);
  212.   strcpy (to_dotdot + i, "/..");
  213.  
  214.   if (chdir (to_parent_path))
  215.     error (1, errno, "%s", to_parent_path);
  216.   if (unlink (to_dotdot) && errno != ENOENT)
  217.     error (1, errno, "cannot unlink `%s'", to_dotdot);
  218.   if (link (".", to_dotdot))
  219.     error (1, errno, "cannot link `%s' to `%s'", ".", to_dotdot);
  220.  
  221.   exit (0);
  222. }
  223.  
  224. /* Return the full pathname (from /) of the directory DIR,
  225.    as static data. */
  226.  
  227. static char *
  228. fullpath (dir)
  229.      char *dir;
  230. {
  231.   char wd[PATH_MAX + 2];
  232.   static char path[PATH_MAX + 2];
  233.  
  234.   if (getcwd (wd, PATH_MAX + 2) == NULL)
  235.     error (1, errno, "cannot get current directory");
  236.   if (chdir (dir))
  237.     error (1, errno, "%s", dir);
  238.   if (getcwd (path, PATH_MAX + 2) == NULL)
  239.     error (1, errno, "cannot get current directory");
  240.   if (chdir (wd))
  241.     error (1, errno, "%s", wd);
  242.  
  243.   return path;
  244. }
  245.