home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / fileutils-3.9-src.lha / src / amiga / fileutils-3.9 / lib / rename.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-12  |  2.8 KB  |  113 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. #include <errno.h>
  32. #ifndef STDC_HEADERS
  33. extern int errno;
  34. #endif
  35.  
  36. #ifdef    STAT_MACROS_BROKEN
  37. #ifdef S_ISDIR
  38. #undef S_ISDIR
  39. #endif
  40. #endif    /* STAT_MACROS_BROKEN.  */
  41.  
  42. #if !defined(S_ISDIR) && defined(S_IFDIR)
  43. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  44. #endif
  45.  
  46. /* Rename file FROM to file TO.
  47.    Return 0 if successful, -1 if not. */
  48.  
  49. int
  50. rename (from, to)
  51.      char *from;
  52.      char *to;
  53. {
  54.   struct stat from_stats, to_stats;
  55.   int pid, status;
  56.  
  57.   if (stat (from, &from_stats))
  58.     return -1;
  59.  
  60.   /* Be careful not to unlink `from' if it happens to be equal to `to' or
  61.      (on filesystems that silently truncate filenames after 14 characters)
  62.      if `from' and `to' share the significant characters. */
  63.   if (stat (to, &to_stats))
  64.     {
  65.       if (errno != ENOENT)
  66.         return -1;
  67.     }
  68.   else
  69.     {
  70.       if ((from_stats.st_dev == to_stats.st_dev)
  71.           && (from_stats.st_ino == to_stats.st_dev))
  72.         /* `from' and `to' designate the same file on that filesystem. */
  73.         return 0;
  74.  
  75.       if (unlink (to) && errno != ENOENT)
  76.         return -1;
  77.     }
  78.  
  79.   if (S_ISDIR (from_stats.st_mode))
  80.     {
  81.       /* Need a setuid root process to link and unlink directories. */
  82.       pid = fork ();
  83.       switch (pid)
  84.     {
  85.     case -1:        /* Error. */
  86.       error (1, errno, "cannot fork");
  87.  
  88.     case 0:            /* Child. */
  89.       execl (MVDIR, "mvdir", from, to, (char *) 0);
  90.       error (255, errno, "cannot run `%s'", MVDIR);
  91.  
  92.     default:        /* Parent. */
  93.       while (wait (&status) != pid)
  94.         /* Do nothing. */ ;
  95.  
  96.       errno = 0;        /* mvdir printed the system error message. */
  97.       if (status)
  98.         return -1;
  99.     }
  100.     }
  101.   else
  102.     {
  103.       if (link (from, to))
  104.     return -1;
  105.       if (unlink (from) && errno != ENOENT)
  106.     {
  107.       unlink (to);
  108.       return -1;
  109.     }
  110.     }
  111.   return 0;
  112. }
  113.