headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / unistd / link.c
blob74ab9910402548ce94579b9b900da08c53c3600e
1 /*
2 * Copyright 2002-2013, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <errno.h>
8 #include <unistd.h>
10 #include <errno_private.h>
11 #include <syscalls.h>
12 #include <syscall_utils.h>
15 ssize_t
16 readlink(const char *path, char *buffer, size_t bufferSize)
18 return readlinkat(AT_FDCWD, path, buffer, bufferSize);
22 ssize_t
23 readlinkat(int fd, const char *path, char *buffer, size_t bufferSize)
25 size_t linkLen = bufferSize;
26 status_t status = _kern_read_link(fd, path, buffer, &linkLen);
27 if (status < B_OK) {
28 __set_errno(status);
29 return -1;
32 // If the buffer is big enough, null-terminate the string. That's not
33 // required by the standard, but helps non-conforming apps.
34 if (linkLen < bufferSize)
35 buffer[linkLen] = '\0';
37 return linkLen;
41 int
42 symlink(const char *toPath, const char *symlinkPath)
44 int status = _kern_create_symlink(-1, symlinkPath, toPath, 0);
46 RETURN_AND_SET_ERRNO(status);
50 int
51 symlinkat(const char *toPath, int fd, const char *symlinkPath)
53 RETURN_AND_SET_ERRNO(_kern_create_symlink(fd, symlinkPath, toPath, 0));
57 int
58 unlink(const char *path)
60 int status = _kern_unlink(-1, path);
62 RETURN_AND_SET_ERRNO(status);
66 int
67 unlinkat(int fd, const char *path, int flag)
69 if ((flag & AT_REMOVEDIR) != 0)
70 RETURN_AND_SET_ERRNO(_kern_remove_dir(fd, path));
71 else
72 RETURN_AND_SET_ERRNO(_kern_unlink(fd, path));
76 int
77 link(const char *toPath, const char *linkPath)
79 int status = _kern_create_link(-1, linkPath, -1, toPath, true);
80 // Haiku -> POSIX error mapping
81 if (status == B_UNSUPPORTED)
82 status = EPERM;
84 RETURN_AND_SET_ERRNO(status);
88 int
89 linkat(int toFD, const char *toPath, int linkFD, const char *linkPath, int flag)
91 RETURN_AND_SET_ERRNO(_kern_create_link(linkFD, linkPath, toFD, toPath,
92 (flag & AT_SYMLINK_FOLLOW) != 0));