1 #include "leafnode.h" 2 #include <unistd.h> 3 4 /* (C) Copyright 2004 by Matthias Andree */ 5 6 /** allocates a buffer (stores result into *buf and *size) 7 * and fetches the current working directory. 8 * *buf must be a malloc()d buffer or NULL on first call. 9 * returns success, *buf is free()d when FALSE is returned */ 10 int agetcwd(char **buf, size_t *size) { 11 while(1) { 12 if (*buf) { 13 if (getcwd(*buf, *size - 1)) 14 return TRUE; 15 free(*buf); 16 *buf = NULL; 17 if (errno != ERANGE) { 18 return FALSE; 19 } 20 *size <<= 1; 21 } else { 22 *size = PATH_MAX; 23 } 24 *buf = critmalloc(*size, "agetcwd"); 25 (*buf)[*size-1] = 0; /* NUL terminate always */ 26 } 27 } 28 29