home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / cvs-1.8.7-src.tgz / tar.out / fsf / cvs / lib / rename.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  2KB  |  85 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. #include "config.h"
  20. #endif
  21.  
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #ifndef STDC_HEADERS
  26. extern int errno;
  27. #endif
  28.  
  29. /* Rename file FROM to file TO.
  30.    Return 0 if successful, -1 if not. */
  31.  
  32. int
  33. rename (from, to)
  34.      char *from;
  35.      char *to;
  36. {
  37.   struct stat from_stats;
  38.   int pid, status;
  39.  
  40.   if (stat (from, &from_stats) == 0)
  41.     {
  42.       /* We don't check existence_error because the systems which need it
  43.      have rename().  */
  44.       if (unlink (to) && errno != ENOENT)
  45.     return -1;
  46.       if ((from_stats.st_mode & S_IFMT) == S_IFDIR)
  47.     {
  48. #ifdef MVDIR
  49.       /* I don't think MVDIR ever gets defined, but I don't think
  50.          it matters, because I don't think CVS ever calls rename()
  51.          on directories.  */
  52.  
  53.       /* Need a setuid root process to link and unlink directories. */
  54.       pid = fork ();
  55.       switch (pid)
  56.         {
  57.         case -1:        /* Error. */
  58.           error (1, errno, "cannot fork");
  59.  
  60.         case 0:        /* Child. */
  61.           execl (MVDIR, "mvdir", from, to, (char *) 0);
  62.           error (255, errno, "cannot run `%s'", MVDIR);
  63.  
  64.         default:        /* Parent. */
  65.           while (wait (&status) != pid)
  66.         /* Do nothing. */ ;
  67.  
  68.           errno = 0;    /* mvdir printed the system error message. */
  69.           return status != 0 ? -1 : 0;
  70.         }
  71. #else /* no MVDIR */
  72.       error (1, 0, "internal error: cannot move directories");
  73. #endif /* no MVDIR */
  74.     }
  75.       else
  76.     {
  77.       /* We don't check existence_error because the systems which need it
  78.          have rename().  */
  79.       if (link (from, to) == 0 && (unlink (from) == 0 || errno == ENOENT))
  80.         return 0;
  81.     }
  82.     }
  83.   return -1;
  84. }
  85.