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