1 /* BSD compatible rename and directory rename function for System V.
2 Copyright (C) 1988, 1990 Free Software Foundation, Inc.
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)
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.
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. */
22 #include <sys/types.h>
29 #if STAT_MACROS_BROKEN
33 #if !defined(S_ISDIR) && defined(S_IFDIR)
34 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
37 /* Rename file FROM to file TO.
38 Return 0 if successful, -1 if not. */
41 rename (char *from, char *to)
43 struct stat from_stats, to_stats;
46 if (stat (from, &from_stats))
49 /* Be careful not to unlink `from' if it happens to be equal to `to' or
50 (on filesystems that silently truncate filenames after 14 characters)
51 if `from' and `to' share the significant characters. */
52 if (stat (to, &to_stats))
59 if ((from_stats.st_dev == to_stats.st_dev)
60 && (from_stats.st_ino == to_stats.st_ino))
61 /* `from' and `to' designate the same file on that filesystem. */
64 if (unlink (to) && errno != ENOENT)
70 /* If MVDIR is defined, it should be the full filename of a setuid root
71 program able to link and unlink directories. If MVDIR is not defined,
72 then the capability of renaming directories may be missing. */
74 if (S_ISDIR (from_stats.st_mode))
76 /* Need a setuid root process to link and unlink directories. */
81 error (1, errno, "cannot fork");
84 execl (MVDIR, "mvdir", from, to, (char *) 0);
85 error (255, errno, "cannot run `%s'", MVDIR);
87 default: /* Parent. */
88 while (wait (&status) != pid)
91 errno = 0; /* mvdir printed the system error message. */
103 if (unlink (from) && errno != ENOENT)