2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)rename.c 1.6 04/09/04 Copyright 1998-2003 J. Schilling */
15 * rename() for old systems that don't have it.
17 * Copyright (c) 1998-2003 J. Schilling
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License version 2
22 * as published by the Free Software Foundation.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License along with
30 * this program; see the file COPYING. If not, write to the Free Software
31 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #define rename __nothing__
39 #include <stdio.h> /* XXX not OK but needed for js_xx() in schily.h */
52 #define MAXPATHNAME 1024
55 #if MAXPATHNAME < 1024
57 #define MAXPATHNAME 1024
60 #define MAXNAME MAXPATHNAME
62 #define FILEDESC struct stat
74 char bakname[MAXNAME];
77 BOOL savpresent = FALSE;
78 BOOL newpresent = FALSE;
85 if (lstat(old, &ostat) < 0)
88 if (lstat(new, &xstat) >= 0) {
90 if (ostat.st_dev == xstat.st_dev &&
91 ostat.st_ino == xstat.st_ino)
92 return (0); /* old == new we are done */
95 strplen = snprintf(strpid, sizeof (strpid), ".%lld",
98 if (strlen(new) <= (MAXNAME-strplen) ||
99 strchr(&new[MAXNAME-strplen], '/') == NULL) {
101 * Save old version of file 'new'.
103 strncpy(nname, new, MAXNAME-strplen);
104 nname[MAXNAME-strplen] = '\0';
105 snprintf(bakname, sizeof (bakname), "%s%s", nname, strpid);
107 if (link(new, bakname) >= 0)
112 if (rmdir(new) < 0) {
113 if (geterrno() == ENOTDIR) {
123 * Now add 'new' name to 'old'.
125 if (link(old, new) < 0) {
128 * Make sure not to loose old version of 'new'.
140 unlink(bakname); /* Fails in most cases... */
141 seterrno(serrno); /* ...so restore errno */
144 #endif /* HAVE_RENAME */