"Fossies" - the Fresh Open Source Software Archive

Member "leafnode-1.12.0/agetcwd.c" (28 Dec 2021, 667 Bytes) of package /linux/misc/leafnode-1.12.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "agetcwd.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.11.12_vs_1.12.0.

    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