2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
9 #include <aros/debug.h>
10 #include <proto/exec.h>
11 #include "dos_intern.h"
14 /*****************************************************************************
17 #include <exec/types.h>
18 #include <proto/dos.h>
20 AROS_LH2(LONG
, Rename
,
23 AROS_LHA(CONST_STRPTR
, oldName
, D1
),
24 AROS_LHA(CONST_STRPTR
, newName
, D2
),
27 struct DosLibrary
*, DOSBase
, 13, Dos
)
30 Renames a given file. The old name and the new name must point to the
34 oldName - Name of the file to rename
35 newName - New name of the file to rename
38 boolean - DOSTRUE or DOSFALSE. IoErr() provides additional information
51 *****************************************************************************/
55 struct DevProc
*olddvp
, *newdvp
;
57 struct Process
*me
= (struct Process
*)FindTask(NULL
);
58 char buf1
[256], vol
[32];
61 BSTR bstrNewName
, bstrOldName
;
63 ASSERT_VALID_PROCESS(me
);
65 len
= SplitName(oldName
, ':', vol
, 0, sizeof(vol
) - 1);
70 BPTR lock
= Lock(oldName
, SHARED_LOCK
);
73 if (NameFromLock(lock
, buf1
, sizeof(buf1
) - 1))
80 /* convert to absolute path */
81 if (NameFromLock(me
->pr_CurrentDir
, buf1
, sizeof(buf1
) - 1))
83 int namelen
= strlen(oldName
);
85 if (len
+ namelen
< sizeof(buf1
) - 1)
87 if (buf1
[len
- 1] != ':')
89 CopyMem(oldName
, buf1
+ len
, namelen
+ 1);
95 len
= SplitName(newName
, ':', vol
, 0, sizeof(vol
) - 1);
99 /* get real name of destination path */
104 strcpy(buf2
, newName
);
105 pos
= strrchr(buf2
, '/');
112 *(++pos
) = '\0'; /* Keep slashes in name, to support a/b//c => a/c case */
114 lock
= Lock(buf2
, SHARED_LOCK
);
117 if (NameFromLock(lock
, buf2
, sizeof(buf2
) - 1))
121 pos2
= newName
+ (int)(pos
- buf2
);
122 namelen
= strlen(pos2
);
123 if (len
+ namelen
< sizeof(buf2
) - 1)
125 if (buf2
[len
- 1] != ':')
127 CopyMem(pos2
, buf2
+ len
, namelen
+ 1);
136 /* convert to absolute path */
137 if (NameFromLock(me
->pr_CurrentDir
, buf2
, sizeof(buf2
) - 1))
139 int namelen
= strlen(newName
);
141 if (len
+ namelen
< sizeof(buf2
) - 1)
143 if (buf2
[len
- 1] != ':')
145 CopyMem(newName
, buf2
+ len
, namelen
+ 1);
151 D(bug("[Dos] rename %s %s\n", oldName
, newName
));
153 /* get device pointers */
154 if ((olddvp
= GetDeviceProc(oldName
, NULL
)) == NULL
||
155 (newdvp
= GetDeviceProc(newName
, NULL
)) == NULL
) {
156 FreeDeviceProc(olddvp
);
160 /* make sure they're on the same device
161 * XXX this is insufficient, see comments in samedevice.c */
162 if (olddvp
->dvp_Port
!= newdvp
->dvp_Port
) {
163 FreeDeviceProc(olddvp
);
164 FreeDeviceProc(newdvp
);
165 SetIoErr(ERROR_RENAME_ACROSS_DEVICES
);
169 bstrNewName
= C2BSTR(newName
);
170 bstrOldName
= C2BSTR(oldName
);
171 status
= dopacket4(DOSBase
, NULL
, olddvp
->dvp_Port
, ACTION_RENAME_OBJECT
, olddvp
->dvp_Lock
, bstrOldName
, newdvp
->dvp_Lock
, bstrNewName
);
172 FREEC2BSTR(bstrOldName
);
173 FREEC2BSTR(bstrNewName
);
175 FreeDeviceProc(olddvp
);
176 FreeDeviceProc(newdvp
);