Use SAFE_STAT instead of stat to avoid unnecessary failure
[gnulib.git] / lib / rename.c
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 #include "safe-stat.h"
47
48 /* Rename file FROM to file TO.
49    Return 0 if successful, -1 if not. */
50
51 int
52 rename (from, to)
53      char *from;
54      char *to;
55 {
56   struct stat from_stats, to_stats;
57   int pid, status;
58
59   if (SAFE_STAT (from, &from_stats))
60     return -1;
61
62   /* Be careful not to unlink `from' if it happens to be equal to `to' or
63      (on filesystems that silently truncate filenames after 14 characters)
64      if `from' and `to' share the significant characters. */
65   if (SAFE_STAT (to, &to_stats))
66     {
67       if (errno != ENOENT)
68         return -1;
69     }
70   else
71     {
72       if ((from_stats.st_dev == to_stats.st_dev)
73           && (from_stats.st_ino == to_stats.st_dev))
74         /* `from' and `to' designate the same file on that filesystem. */
75         return 0;
76
77       if (unlink (to) && errno != ENOENT)
78         return -1;
79     }
80
81   if (S_ISDIR (from_stats.st_mode))
82     {
83       /* Need a setuid root process to link and unlink directories. */
84       pid = fork ();
85       switch (pid)
86         {
87         case -1:                /* Error. */
88           error (1, errno, "cannot fork");
89
90         case 0:                 /* Child. */
91           execl (MVDIR, "mvdir", from, to, (char *) 0);
92           error (255, errno, "cannot run `%s'", MVDIR);
93
94         default:                /* Parent. */
95           while (wait (&status) != pid)
96             /* Do nothing. */ ;
97
98           errno = 0;            /* mvdir printed the system error message. */
99           if (status)
100             return -1;
101         }
102     }
103   else
104     {
105       if (link (from, to))
106         return -1;
107       if (unlink (from) && errno != ENOENT)
108         {
109           unlink (to);
110           return -1;
111         }
112     }
113   return 0;
114 }