1 /* 2 * Copyright 2002-2013, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <errno.h> 8 #include <unistd.h> 9 10 #include <errno_private.h> 11 #include <syscalls.h> 12 #include <syscall_utils.h> 13 14 15 ssize_t 16 readlink(const char *path, char *buffer, size_t bufferSize) 17 { 18 return readlinkat(AT_FDCWD, path, buffer, bufferSize); 19 } 20 21 22 ssize_t 23 readlinkat(int fd, const char *path, char *buffer, size_t bufferSize) 24 { 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; 30 } 31 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'; 36 37 return linkLen; 38 } 39 40 41 int 42 symlink(const char *toPath, const char *symlinkPath) 43 { 44 int status = _kern_create_symlink(-1, symlinkPath, toPath, 0); 45 46 RETURN_AND_SET_ERRNO(status); 47 } 48 49 50 int 51 symlinkat(const char *toPath, int fd, const char *symlinkPath) 52 { 53 RETURN_AND_SET_ERRNO(_kern_create_symlink(fd, symlinkPath, toPath, 0)); 54 } 55 56 57 int 58 unlink(const char *path) 59 { 60 int status = _kern_unlink(-1, path); 61 62 RETURN_AND_SET_ERRNO(status); 63 } 64 65 66 int 67 unlinkat(int fd, const char *path, int flag) 68 { 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)); 73 } 74 75 76 int 77 link(const char *toPath, const char *linkPath) 78 { 79 int status = _kern_create_link(-1, linkPath, -1, toPath, true); 80 // Haiku -> POSIX error mapping 81 if (status == B_UNSUPPORTED) 82 status = EPERM; 83 84 RETURN_AND_SET_ERRNO(status); 85 } 86 87 88 int 89 linkat(int toFD, const char *toPath, int linkFD, const char *linkPath, int flag) 90 { 91 RETURN_AND_SET_ERRNO(_kern_create_link(linkFD, linkPath, toFD, toPath, 92 (flag & AT_SYMLINK_FOLLOW) != 0)); 93 } 94 95