File: agetcwd.c

package info (click to toggle)
leafnode 1.11.11-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,468 kB
  • sloc: ansic: 10,914; sh: 1,748; xml: 628; makefile: 291; perl: 84; sed: 4
file content (27 lines) | stat: -rw-r--r-- 622 bytes parent folder | download | duplicates (8)
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 */
    }
}