Added setting position while duplicating file handles with FMF_WRITE mode. It's neede...
[cake.git] / compiler / clib / symlink.c
blob45f00816ba6ca6714e78bc8bffe43c05f50e3a87
1 /*
2 Copyright © 2004, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX function symlink().
6 */
8 #include <aros/debug.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <proto/dos.h>
13 #include <errno.h>
14 #include "__errno.h"
15 #include "__upath.h"
16 #include <aros/debug.h>
18 int symlink(const char *oldpath, const char *newpath)
20 int retval = -1;
21 BPTR lock;
22 struct FileInfoBlock *fib;
23 int errorset = FALSE;
24 LONG ioerr;
25 UBYTE *buffer;
26 int bufferincrease = 256;
27 int buffersize = bufferincrease;
29 if (!oldpath || !newpath) /*safety check */
31 errno = EFAULT;
32 return -1;
35 if(oldpath = strdup(__path_u2a(oldpath)))
37 newpath = __path_u2a(newpath);
38 if (!newpath)
40 free(oldpath);
41 return -1;
44 if(lock = Lock((STRPTR)oldpath, SHARED_LOCK)) {
47 if(!(buffer = AllocVec(buffersize, MEMF_ANY)))
49 ioerr = ERROR_NO_FREE_STORE;
50 errorset = TRUE;
51 break;
54 /* Get the full path of oldpath */
55 if(NameFromLock(lock, buffer, buffersize))
57 if(MakeLink((STRPTR)newpath,
58 (STRPTR)buffer,
59 TRUE) == DOSTRUE)
60 retval = RETURN_OK;
61 else
63 ioerr = IoErr();
64 errorset = TRUE;
65 break;
68 else if(IoErr() != ERROR_LINE_TOO_LONG)
70 ioerr = IoErr();
71 errorset = TRUE;
72 break;
74 FreeVec(buffer);
75 buffersize += bufferincrease;
77 while(retval != RETURN_OK);
78 UnLock(lock);
80 else
82 ioerr = IoErr();
83 /* I'm not sure if MakeLink is allowed to create symlinks to
84 non-existing files or directories. If yes, then it's fine to
85 enable the following code */
86 #if 0
87 if(ioerr == ERROR_OBJECT_NOT_FOUND)
89 /* On Unices it's perfectly fine to create symlinks to
90 non-existing files or directories, however in this case it
91 may be difficult to get the full absolute path, so we are
92 simply trusting the user here for now */
93 if(MakeLink((STRPTR)newpath,
94 (STRPTR)oldpath,
95 TRUE) == DOSTRUE)
96 retval = RETURN_OK;
97 else
99 ioerr = IoErr();
100 errorset = TRUE;
103 else
104 #endif
105 errorset = TRUE;
107 free(oldpath);
110 if(errorset) errno = IoErr2errno(ioerr);
111 return retval;
112 } /* symlink */