1 /*
2  * (C) 2001 Uwe Ohse, <uwe@ohse.de>.
3  * Placed in the public domain.
4  */
5 /* @(#) $Id: get_cwd.c 1.6 01/05/03 20:08:26+00:00 uwe@fjoras.ohse.de $ */
6 #include "alloc.h"
7 #include "error.h"
8 #include "stralloc.h"
9 #include "byte.h"
10 #include "str.h"
11 #include "get_cwd.h"
12 #include <unistd.h>
13 
14 char *
get_cwd(void)15 get_cwd(void)
16 {
17 	stralloc sa=STRALLOC_INIT;
18 	unsigned int m=256;
19 	while (m<=8192) { /* ought to be enough */
20 		if (!stralloc_ready(&sa,m+1)) return 0;
21 		if (getcwd(sa.s,m)) {
22 			char *p;
23 			m=str_len(sa.s)+1;
24 			p=alloc(m);
25 			if (!p)
26 				return sa.s;
27 			byte_copy(p,m,sa.s);
28 			stralloc_free(&sa);
29 			return p;
30 		}
31 		if (errno==error_acces)
32 			return 0;
33 		m*=2;
34 	}
35 	return 0;
36 }
37