File: agetcwd.c

package info (click to toggle)
leafnode 1.11.10-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,460 kB
  • ctags: 597
  • sloc: ansic: 10,894; sh: 1,736; xml: 628; makefile: 291; perl: 84; sed: 4
file content (27 lines) | stat: -rw-r--r-- 622 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "leafnode.h"
#include <unistd.h>

/** allocates a buffer (stores result into *buf and *size)
 * and fetches the current working directory.
 * *buf must be a malloc()d buffer or NULL on first call.
 * returns success, *buf is free()d when FALSE is returned */
int agetcwd(char **buf, size_t *size) {
    while(1) {
	if (*buf) {
	    if (getcwd(*buf, *size - 1))
		return TRUE;
	    free(*buf);
	    *buf = NULL;
	    if (errno != ERANGE) {
		return FALSE;
	    }
	    *size <<= 1;
	} else {
	    *size = PATH_MAX;
	}
	*buf = critmalloc(*size, "agetcwd");
	(*buf)[*size-1] = 0; /* NUL terminate always */
    }
}