1 /* 2 * Copyright 2002-2009, 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 <stdlib.h> 9 #include <unistd.h> 10 11 #include <syscalls.h> 12 #include <syscall_utils.h> 13 14 15 int 16 chdir(const char *path) 17 { 18 RETURN_AND_SET_ERRNO(_kern_setcwd(-1, path)); 19 } 20 21 22 int 23 fchdir(int fd) 24 { 25 RETURN_AND_SET_ERRNO(_kern_setcwd(fd, NULL)); 26 } 27 28 29 char * 30 getcwd(char *buffer, size_t size) 31 { 32 bool allocated = false; 33 status_t status; 34 35 if (buffer == NULL) { 36 buffer = malloc(size = PATH_MAX); 37 if (buffer == NULL) { 38 errno = B_NO_MEMORY; 39 return NULL; 40 } 41 42 allocated = true; 43 } 44 45 status = _kern_getcwd(buffer, size); 46 if (status < B_OK) { 47 if (allocated) 48 free(buffer); 49 50 errno = status; 51 return NULL; 52 } 53 return buffer; 54 } 55 56 57 int 58 rmdir(const char *path) 59 { 60 RETURN_AND_SET_ERRNO(_kern_remove_dir(-1, path)); 61 } 62 63