1 /* 2 * Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <syscalls.h> 8 9 #include <unistd.h> 10 #include <stdlib.h> 11 #include <errno.h> 12 13 14 #define RETURN_AND_SET_ERRNO(err) \ 15 if (err < 0) { \ 16 errno = err; \ 17 return -1; \ 18 } \ 19 return err; 20 21 22 int 23 chdir(const char *path) 24 { 25 int status = _kern_setcwd(-1, path); 26 27 RETURN_AND_SET_ERRNO(status); 28 } 29 30 31 int 32 fchdir(int fd) 33 { 34 int status = _kern_setcwd(fd, NULL); 35 36 RETURN_AND_SET_ERRNO(status); 37 } 38 39 40 char * 41 getcwd(char *buffer, size_t size) 42 { 43 bool allocated = false; 44 status_t status; 45 46 if (buffer == NULL) { 47 buffer = malloc(size = PATH_MAX); 48 if (buffer == NULL) { 49 errno = B_NO_MEMORY; 50 return NULL; 51 } 52 53 allocated = true; 54 } 55 56 status = _kern_getcwd(buffer, size); 57 if (status < B_OK) { 58 if (allocated) 59 free(buffer); 60 61 errno = status; 62 return NULL; 63 } 64 return buffer; 65 } 66 67 68 int 69 rmdir(const char *path) 70 { 71 int status = _kern_remove_dir(path); 72 73 RETURN_AND_SET_ERRNO(status); 74 } 75 76