(rename): Use pid_t instead of int; check status
[gnulib.git] / lib / rename.c
1 /* BSD compatible rename and directory rename 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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #if 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 errno
26 extern int errno;
27 #endif
28
29 #if STAT_MACROS_BROKEN
30 # undef S_ISDIR
31 #endif
32
33 #if !defined(S_ISDIR) && defined(S_IFDIR)
34 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
35 #endif
36
37 /* Rename file FROM to file TO.
38    Return 0 if successful, -1 if not. */
39
40 int
41 rename (char *from, char *to)
42 {
43   struct stat from_stats, to_stats;
44
45   if (stat (from, &from_stats))
46     return -1;
47
48   /* Be careful not to unlink `from' if it happens to be equal to `to' or
49      (on filesystems that silently truncate filenames after 14 characters)
50      if `from' and `to' share the significant characters. */
51   if (stat (to, &to_stats))
52     {
53       if (errno != ENOENT)
54         return -1;
55     }
56   else
57     {
58       if ((from_stats.st_dev == to_stats.st_dev)
59           && (from_stats.st_ino == to_stats.st_ino))
60         /* `from' and `to' designate the same file on that filesystem. */
61         return 0;
62
63       if (unlink (to) && errno != ENOENT)
64         return -1;
65     }
66
67 #ifdef MVDIR
68
69 /* If MVDIR is defined, it should be the full filename of a setuid root
70    program able to link and unlink directories.  If MVDIR is not defined,
71    then the capability of renaming directories may be missing.  */
72
73   if (S_ISDIR (from_stats.st_mode))
74     {
75       /* Need a setuid root process to link and unlink directories. */
76       int status;
77       pid_t pid = fork ();
78       switch (pid)
79         {
80         case -1:                /* Error. */
81           return -1;            /* errno already set */
82
83         case 0:                 /* Child. */
84           execl (MVDIR, "mvdir", from, to, (char *) 0);
85           _exit (1);
86
87         default:                /* Parent. */
88           while (wait (&status) != pid)
89             /* Do nothing. */ ;
90
91           if (status)
92             {
93               /* MVDIR failed.  */
94               errno = EIO;
95               return -1;
96             }
97         }
98     }
99   else
100
101 #endif /* MVDIR */
102
103     {
104       if (link (from, to))
105         return -1;
106       if (unlink (from) && errno != ENOENT)
107         {
108           unlink (to);
109           return -1;
110         }
111     }
112   return 0;
113 }