compiler/clib: Removed unused arosc_init.h file.
[AROS.git] / compiler / clib / symlink.c
blobf70bd274966583db39593544ae0c2179786d3260
1 /*
2 Copyright © 2004-2012, 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 <proto/dos.h>
12 #include <errno.h>
13 #include "__upath.h"
14 #include <aros/debug.h>
16 /*****************************************************************************
18 NAME */
19 #include <unistd.h>
21 int symlink(
23 /* SYNOPSIS */
24 const char *oldpath,
25 const char *newpath)
27 /* FUNCTION
29 INPUTS
31 RESULT
33 NOTES
35 EXAMPLE
37 BUGS
39 SEE ALSO
41 INTERNALS
43 ******************************************************************************/
45 int retval = -1;
46 BPTR lock;
47 LONG ioerr = 0;
48 UBYTE *buffer;
49 int buffersize = 256;
51 if (!oldpath || !newpath) /*safety check */
53 errno = EFAULT;
54 return -1;
57 oldpath = __path_u2a(oldpath);
58 if(!oldpath)
59 return -1;
61 oldpath = strdup(oldpath);
62 if(!oldpath)
64 errno = ENOMEM;
65 return -1;
68 newpath = __path_u2a(newpath);
69 if (!newpath)
71 free((void*) oldpath);
72 return -1;
75 if((lock = Lock((STRPTR)oldpath, SHARED_LOCK)))
79 if(!(buffer = AllocVec(buffersize, MEMF_ANY)))
81 ioerr = ERROR_NO_FREE_STORE;
82 break;
85 /* Get the full path of oldpath */
86 if(NameFromLock(lock, buffer, buffersize))
88 if(MakeLink((STRPTR)newpath, (STRPTR)buffer, TRUE))
89 retval = 0;
90 else
92 ioerr = IoErr();
93 FreeVec(buffer);
94 break;
97 else if(IoErr() != ERROR_LINE_TOO_LONG)
99 ioerr = IoErr();
100 FreeVec(buffer);
101 break;
103 FreeVec(buffer);
104 buffersize *= 2;
106 while(retval != RETURN_OK);
107 UnLock(lock);
109 else
111 /* MakeLink can create symlinks to non-existing files or
112 directories. */
113 if(IoErr() == ERROR_OBJECT_NOT_FOUND)
115 /* In this case it may be difficult to get the full absolute
116 path, so we simply trust the caller here for now */
117 if(MakeLink((STRPTR)newpath, (STRPTR)oldpath, TRUE))
118 retval = 0;
119 else
120 ioerr = IoErr();
122 else
123 ioerr = IoErr();
125 free((void*) oldpath);
127 if(ioerr)
128 errno = __arosc_ioerr2errno(ioerr);
130 return retval;
131 } /* symlink */