merge with 3.9h
[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 #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 #ifdef STAT_MACROS_BROKEN
30 #undef S_ISDIR
31 #endif /* STAT_MACROS_BROKEN.  */
32
33 #if !defined(S_ISDIR) && defined(S_IFDIR)
34 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
35 #endif
36
37 #include "safe-stat.h"
38
39 /* Rename file FROM to file TO.
40    Return 0 if successful, -1 if not. */
41
42 int
43 rename (from, to)
44      char *from;
45      char *to;
46 {
47   struct stat from_stats, to_stats;
48   int pid, status;
49
50   if (SAFE_STAT (from, &from_stats))
51     return -1;
52
53   /* Be careful not to unlink `from' if it happens to be equal to `to' or
54      (on filesystems that silently truncate filenames after 14 characters)
55      if `from' and `to' share the significant characters. */
56   if (SAFE_STAT (to, &to_stats))
57     {
58       if (errno != ENOENT)
59         return -1;
60     }
61   else
62     {
63       if ((from_stats.st_dev == to_stats.st_dev)
64           && (from_stats.st_ino == to_stats.st_dev))
65         /* `from' and `to' designate the same file on that filesystem. */
66         return 0;
67
68       if (unlink (to) && errno != ENOENT)
69         return -1;
70     }
71
72   if (S_ISDIR (from_stats.st_mode))
73     {
74       /* Need a setuid root process to link and unlink directories. */
75       pid = fork ();
76       switch (pid)
77         {
78         case -1:                /* Error. */
79           error (1, errno, "cannot fork");
80
81         case 0:                 /* Child. */
82           execl (MVDIR, "mvdir", from, to, (char *) 0);
83           error (255, errno, "cannot run `%s'", MVDIR);
84
85         default:                /* Parent. */
86           while (wait (&status) != pid)
87             /* Do nothing. */ ;
88
89           errno = 0;            /* mvdir printed the system error message. */
90           if (status)
91             return -1;
92         }
93     }
94   else
95     {
96       if (link (from, to))
97         return -1;
98       if (unlink (from) && errno != ENOENT)
99         {
100           unlink (to);
101           return -1;
102         }
103     }
104   return 0;
105 }