home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / lib / rename.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  137 lines

  1. /* rename.c -- BSD compatible directory function for System V
  2.    Copyright (C) 1988, 1990 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. #ifdef HAVE_CONFIG_H
  19. #if defined (CONFIG_BROKETS)
  20. /* We use <config.h> instead of "config.h" so that a compilation
  21.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  22.    (which it would do because it found this file in $srcdir).  */
  23. #include <config.h>
  24. #else
  25. #include "config.h"
  26. #endif
  27. #endif
  28.  
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31.  
  32. #include <errno.h>
  33. #ifndef errno
  34. extern int errno;
  35. #endif
  36.  
  37. #ifdef HAVE_VFORK
  38. #define fork vfork
  39. #endif
  40.  
  41. /* Rename file FROM to file TO.
  42.    Return 0 if successful, -1 if not. */
  43.  
  44. int
  45. rename (from, to)
  46.      char *from;
  47.      char *to;
  48. {
  49.   struct stat from_stats, to_stats;
  50.   int pid, status;
  51.  
  52.   if (stat (from, &from_stats))
  53.     return -1;
  54.  
  55.   /* Be careful not to unlink `from' if it happens to be equal to `to' or
  56.      (on filesystems that silently truncate filenames after 14 characters)
  57.      if `from' and `to' share the significant characters. */
  58.   if (stat (to, &to_stats))
  59.     {
  60.       if (errno != ENOENT)
  61.         return -1;
  62.     }
  63.   else
  64.     {
  65.       if ((from_stats.st_dev == to_stats.st_dev)
  66.           && (from_stats.st_ino == to_stats.st_dev))
  67.         /* `from' and `to' designate the same file on that filesystem. */
  68.         return 0;
  69.  
  70.       if (unlink (to) && errno != ENOENT)
  71.         return -1;
  72.     }
  73.  
  74. #ifdef MVDIR
  75.  
  76. /* If MVDIR is defined, it should be the full filename of a setuid root
  77.    program able to link and unlink directories.  If MVDIR is not defined,
  78.    then the capability of renaming directories may be missing.  */
  79.  
  80. #ifdef    STAT_MACROS_BROKEN
  81. #ifdef S_ISDIR
  82. #undef S_ISDIR
  83. #endif
  84. #endif    /* STAT_MACROS_BROKEN.  */
  85.  
  86. #if !defined(S_ISDIR) && defined(S_IFDIR)
  87. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  88. #endif
  89.  
  90.   if (S_ISDIR (from_stats.st_mode))
  91.     {
  92.       /* Need a setuid root process to link and unlink directories. */
  93.       pid = fork ();
  94.       switch (pid)
  95.     {
  96.     case -1:        /* Error. */
  97.       error (1, errno, "cannot fork");
  98.  
  99.     case 0:            /* Child. */
  100.       execl (MVDIR, "mvdir", from, to, (char *) 0);
  101.       error (255, errno, "cannot run `%s'", MVDIR);
  102.  
  103.     default:        /* Parent. */
  104.       while (wait (&status) != pid)
  105.         /* Do nothing. */ ;
  106.  
  107.       errno = 0;        /* mvdir printed the system error message. */
  108.       if (status)
  109.         return -1;
  110.     }
  111.     }
  112.   else
  113.     {
  114.       if (link (from, to))
  115.     return -1;
  116.       if (unlink (from) && errno != ENOENT)
  117.     {
  118.       unlink (to);
  119.       return -1;
  120.     }
  121.     }
  122.  
  123. #else /* not MVDIR */
  124.  
  125.   if (link (from, to))
  126.     return -1;
  127.   if (unlink (from) && errno != ENOENT)
  128.     {
  129.       unlink (to);
  130.       return -1;
  131.     }
  132.  
  133. #endif /* not MVDIR */
  134.  
  135.   return 0;
  136. }
  137.