vcmbox -> mbox
[AROS.git] / compiler / stdc / rename.c
blob894a46958e9ed07b68ad179c957be2d1a66c531a
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function rename().
6 */
7 #include <proto/dos.h>
8 #include <errno.h>
10 #define DEBUG 0
11 #include <aros/debug.h>
13 /*****************************************************************************
15 NAME */
16 #include <stdio.h>
18 int rename (
20 /* SYNOPSIS */
21 const char * oldpath,
22 const char * newpath)
24 /* FUNCTION
25 Renames a file or directory.
27 INPUTS
28 oldpath - Complete path to existing file or directory.
29 newpath - Complete path to the new file or directory.
31 RESULT
32 0 on success and -1 on error. In case of an error, errno is set.
34 NOTES
36 EXAMPLE
38 BUGS
40 SEE ALSO
42 INTERNALS
44 ******************************************************************************/
46 BPTR oldlock, newlock;
48 /* try to delete newpath first */
49 Forbid();
51 newlock = Lock(newpath, SHARED_LOCK);
52 if (newlock)
54 UnLock(newlock);
56 oldlock = Lock(oldpath, EXCLUSIVE_LOCK);
57 if (oldlock)
59 UnLock(oldlock);
61 /* DeleteFile returns an error if directory is non-empty */
62 if (!DeleteFile(newpath))
64 LONG ioerr = IoErr();
65 errno = __stdc_ioerr2errno(ioerr);
66 D(bug("rename(%s, %s) delete errno=%d, IoErr=%d\n",
67 oldpath, newpath, errno, ioerr));
68 Permit();
69 return -1;
74 if (!Rename (oldpath, newpath))
76 LONG ioerr = IoErr();
77 errno = __stdc_ioerr2errno(ioerr);
78 D(bug("rename(%s, %s) errno=%d, IoErr=%d\n",
79 oldpath, newpath, errno, ioerr));
80 Permit();
81 return -1;
84 Permit();
85 return 0;
87 } /* rename */