File: agetcwd.c

package info (click to toggle)
leafnode 1.12.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,496 kB
  • sloc: ansic: 10,961; sh: 1,709; xml: 627; makefile: 259; perl: 84; sed: 4
file content (29 lines) | stat: -rw-r--r-- 667 bytes parent folder | download | duplicates (3)
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
28
29
#include "leafnode.h"
#include <unistd.h>

/* (C) Copyright 2004 by Matthias Andree */

/** 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 */
    }
}