- Made pciuhci.device and pciehci.device compile again: completed
[AROS.git] / compiler / clib / rename.c
blobf155a7efe22538ad47f22cd145fea30ac12f9add
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function rename().
6 */
8 #include <proto/dos.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include "__upath.h"
14 #define DEBUG 0
15 #include <aros/debug.h>
17 /*****************************************************************************
19 NAME */
20 #include <stdio.h>
22 int rename (
24 /* SYNOPSIS */
25 const char * oldpath,
26 const char * newpath)
28 /* FUNCTION
29 Renames a file or directory.
31 INPUTS
32 oldpath - Complete path to existing file or directory.
33 newpath - Complete path to the new file or directory.
35 RESULT
36 0 on success and -1 on error. In case of an error, errno is set.
38 NOTES
40 EXAMPLE
42 BUGS
44 SEE ALSO
46 INTERNALS
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'))
59 errno = EEXIST;
60 free(aoldpath);
61 return -1;
65 /* try to delete newpath first */
66 Forbid();
68 newlock = Lock(anewpath, SHARED_LOCK);
69 if (newlock)
71 UnLock(newlock);
73 oldlock = Lock(aoldpath, EXCLUSIVE_LOCK);
74 if (oldlock)
76 UnLock(oldlock);
78 /* DeleteFile returns an error if directory is non-empty */
79 if (!DeleteFile(anewpath))
81 LONG ioerr = IoErr();
82 errno = __arosc_ioerr2errno(ioerr);
83 D(bug("rename(%s, %s) delete errno=%d, IoErr=%d\n",
84 aoldpath, anewpath, errno, ioerr));
85 free(aoldpath);
86 Permit();
87 return -1;
92 if (!Rename (aoldpath, anewpath))
94 LONG ioerr = IoErr();
95 errno = __arosc_ioerr2errno(ioerr);
96 D(bug("rename(%s, %s) errno=%d, IoErr=%d\n",
97 aoldpath, anewpath, errno, ioerr));
98 free(aoldpath);
99 Permit();
100 return -1;
103 free(aoldpath);
104 Permit();
105 return 0;
107 } /* rename */