xref: /haiku/src/system/libroot/posix/unistd/link.c (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
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 	size_t linkLen = bufferSize;
24 	status_t status = _kern_read_link(-1, path, buffer, &linkLen);
25 	if (status < B_OK) {
26 		errno = status;
27 		return -1;
28 	}
29 
30 	// If the buffer is big enough, null-terminate the string. That's not
31 	// required by the standard, but helps non-conforming apps.
32 	if (linkLen < bufferSize)
33 		buffer[linkLen] = '\0';
34 
35 	return linkLen;
36 }
37 
38 
39 int
40 symlink(const char *toPath, const char *symlinkPath)
41 {
42 	int status = _kern_create_symlink(-1, symlinkPath, toPath, 0);
43 
44 	RETURN_AND_SET_ERRNO(status);
45 }
46 
47 
48 int
49 unlink(const char *path)
50 {
51 	int status = _kern_unlink(-1, path);
52 
53 	RETURN_AND_SET_ERRNO(status);
54 }
55 
56 
57 int
58 link(const char *toPath, const char *linkPath)
59 {
60 	int status = _kern_create_link(linkPath, toPath);
61 
62 	RETURN_AND_SET_ERRNO(status);
63 }
64 
65