2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
5 ANSI C function rename().
15 #include <aros/debug.h>
17 /*****************************************************************************
29 Renames a file or directory.
32 oldpath - Complete path to existing file or directory.
33 newpath - Complete path to the new file or directory.
36 0 on success and -1 on error. In case of an error, errno is set.
48 ******************************************************************************/
50 STRPTR aoldpath
= (STRPTR
)strdup((const char*)__path_u2a(oldpath
));
51 CONST_STRPTR anewpath
= __path_u2a(newpath
);
52 BPTR oldlock
, newlock
;
54 /* __path_u2a has resolved paths like /toto/../a */
55 if (anewpath
[0] == '.')
57 if (anewpath
[1] == '\0' || (anewpath
[1] == '.' && anewpath
[2] == '\0'))
65 /* try to delete newpath first */
68 newlock
= Lock(anewpath
, SHARED_LOCK
);
73 oldlock
= Lock(aoldpath
, EXCLUSIVE_LOCK
);
78 /* DeleteFile returns an error if directory is non-empty */
79 if (!DeleteFile(anewpath
))
82 errno
= IoErr2errno(ioerr
);
83 D(bug("rename(%s, %s) delete errno=%d, IoErr=%d\n",
84 aoldpath
, anewpath
, errno
, ioerr
));
92 if (!Rename (aoldpath
, anewpath
))
95 errno
= IoErr2errno(ioerr
);
96 D(bug("rename(%s, %s) errno=%d, IoErr=%d\n",
97 aoldpath
, anewpath
, errno
, ioerr
));