xref: /haiku/src/system/libroot/posix/unistd/link.c (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1 /*
2  * Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <unistd.h>
8 #include <errno.h>
9 #include <syscalls.h>
10 
11 
12 #define RETURN_AND_SET_ERRNO(err) \
13 	if (err < 0) { \
14 		errno = err; \
15 		return -1; \
16 	} \
17 	return err;
18 
19 
20 ssize_t
21 readlink(const char *path, char *buffer, size_t bufferSize)
22 {
23 	status_t status = _kern_read_link(-1, path, buffer, &bufferSize);
24 	if (status < B_OK && status != B_BUFFER_OVERFLOW) {
25 		errno = status;
26 		return -1;
27 	}
28 
29 	return bufferSize;
30 }
31 
32 
33 int
34 symlink(const char *toPath, const char *symlinkPath)
35 {
36 	int status = _kern_create_symlink(-1, symlinkPath, toPath, 0);
37 
38 	RETURN_AND_SET_ERRNO(status);
39 }
40 
41 
42 int
43 unlink(const char *path)
44 {
45 	int status = _kern_unlink(-1, path);
46 
47 	RETURN_AND_SET_ERRNO(status);
48 }
49 
50 
51 int
52 link(const char *toPath, const char *linkPath)
53 {
54 	int status = _kern_create_link(linkPath, toPath);
55 
56 	RETURN_AND_SET_ERRNO(status);
57 }
58 
59