diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-04-09 22:48:12 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-04-09 22:48:12 +0000 |
commit | e5dfced23a904d08afa5dcee190c3c3d845d9f50 (patch) | |
tree | ef367ee8a9096884fb40debdc9e10af8583f9d5f /libbb/xgetcwd.c | |
parent | a75e2867435faa68ea03735fe09ad298fa3e4e72 (diff) | |
download | busybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.tar.gz busybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.tar.bz2 busybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.zip |
Apply Vladimir's latest cleanup patch.
-Erik
Diffstat (limited to 'libbb/xgetcwd.c')
-rw-r--r-- | libbb/xgetcwd.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/libbb/xgetcwd.c b/libbb/xgetcwd.c new file mode 100644 index 000000000..274668166 --- /dev/null +++ b/libbb/xgetcwd.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * xgetcwd.c -- return current directory with unlimited length | ||
3 | * Copyright (C) 1992, 1996 Free Software Foundation, Inc. | ||
4 | * Written by David MacKenzie <djm@gnu.ai.mit.edu>. | ||
5 | * | ||
6 | * Special function for busybox written by Vladimir Oleynik <vodz@usa.net> | ||
7 | */ | ||
8 | |||
9 | #include <stdlib.h> | ||
10 | #include <errno.h> | ||
11 | #include <unistd.h> | ||
12 | #include <limits.h> | ||
13 | #include "libbb.h" | ||
14 | |||
15 | /* Amount to increase buffer size by in each try. */ | ||
16 | #define PATH_INCR 32 | ||
17 | |||
18 | /* Return the current directory, newly allocated, arbitrarily long. | ||
19 | Return NULL and set errno on error. | ||
20 | If argument is not NULL (previous usage allocate memory), call free() | ||
21 | */ | ||
22 | |||
23 | char * | ||
24 | xgetcwd (char *cwd) | ||
25 | { | ||
26 | char *ret; | ||
27 | unsigned path_max; | ||
28 | |||
29 | errno = 0; | ||
30 | path_max = (unsigned) PATH_MAX; | ||
31 | path_max += 2; /* The getcwd docs say to do this. */ | ||
32 | |||
33 | if(cwd==0) | ||
34 | cwd = xmalloc (path_max); | ||
35 | |||
36 | errno = 0; | ||
37 | while ((ret = getcwd (cwd, path_max)) == NULL && errno == ERANGE) { | ||
38 | path_max += PATH_INCR; | ||
39 | cwd = xrealloc (cwd, path_max); | ||
40 | errno = 0; | ||
41 | } | ||
42 | |||
43 | if (ret == NULL) { | ||
44 | int save_errno = errno; | ||
45 | free (cwd); | ||
46 | errno = save_errno; | ||
47 | perror_msg("getcwd()"); | ||
48 | return NULL; | ||
49 | } | ||
50 | |||
51 | return cwd; | ||
52 | } | ||