use same location as .configured, etc, to store .files-touched
[AROS.git] / compiler / clib / readlink.c
blob11dc086386f29343d9ac97988171ef8fea451fea
1 /*
2 Copyright © 1995-2010, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C function readlink().
6 */
8 #include <aros/debug.h>
10 #include <dos/filesystem.h>
11 #include <proto/dos.h>
13 #include <errno.h>
15 #include "__arosc_privdata.h"
16 #include "__errno.h"
17 #include "__filesystem_support.h"
18 #include "__upath.h"
20 /*****************************************************************************
22 NAME */
23 #include <unistd.h>
25 ssize_t readlink(
27 /* SYNOPSIS */
28 const char *path,
29 char *buf,
30 size_t bufsize)
32 /* FUNCTION
33 Places the contents of a symbolic link in a buffer of given size. No NUL
34 char is appended to the buffer.
36 INPUTS
37 path - the path to the symbolic link
38 buf - pointer to the buffer where to store the symbolic link content
39 bufsize - the size of the buffer in bytes
41 RESULT
42 The call returns the count of characters placed in the buffer if it
43 succeeds, or a -1 if an error occurs, placing the error code in the
44 global variable errno.
47 ssize_t res = -1;
48 struct IOFileSys iofs;
49 struct DevProc *dvp = NULL;
50 LONG error;
51 struct Process *me = (struct Process *)FindTask(NULL);
53 /* check for empty path before potential conversion from "." to "" */
54 if (__doupath && path && *path == '\0')
56 errno = ENOENT;
57 return res;
60 path = __path_u2a(path);
61 if (path == NULL)
62 return res;
64 /* we need to know if it is really a soft link */
65 InitIOFS(&iofs, FSA_OPEN, DOSBase);
66 iofs.io_Union.io_OPEN.io_FileMode = FMF_READ;
67 iofs.io_Union.io_OPEN.io_Filename = StripVolume(path);
71 if ((dvp = GetDeviceProc(path, dvp)) == NULL)
73 error = IoErr();
74 break;
77 error = DoIOFS(&iofs, dvp, NULL, DOSBase);
79 while (error == ERROR_OBJECT_NOT_FOUND);
81 if (error == ERROR_NO_MORE_ENTRIES)
82 error = me->pr_Result2 = ERROR_OBJECT_NOT_FOUND;
83 else if (error == 0)
85 /* open I/O request successful, but this is not*/
86 /* what we want, don't forget to close again */
87 InitIOFS(&iofs, FSA_CLOSE, DOSBase);
88 DoIO((struct IORequest *) &iofs);
89 /* set an error that translates to EINVAL */
90 error = me->pr_Result2 = ERROR_OBJECT_WRONG_TYPE;
92 else if (error == ERROR_IS_SOFT_LINK)
94 /* it is a soft link, try to read it */
95 res = ReadLink(dvp->dvp_Port, dvp->dvp_Lock, path, buf, bufsize);
96 if (res == -1)
97 error = IoErr();
98 else
100 if (res == -2)
101 res = bufsize;
103 /* clear the soft link error */
104 error = me->pr_Result2 = 0;
108 FreeDeviceProc(dvp);
110 if (error)
111 errno = IoErr2errno(error);
113 return res;