Rev | Line | |
---|
[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[821] | 2 | /*
|
---|
| 3 | * xgetcwd.c -- return current directory with unlimited length
|
---|
| 4 | * Copyright (C) 1992, 1996 Free Software Foundation, Inc.
|
---|
| 5 | * Written by David MacKenzie <djm@gnu.ai.mit.edu>.
|
---|
| 6 | *
|
---|
| 7 | * Special function for busybox written by Vladimir Oleynik <dzo@simtreas.ru>
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include "libbb.h"
|
---|
| 11 |
|
---|
| 12 | /* Amount to increase buffer size by in each try. */
|
---|
| 13 | #define PATH_INCR 32
|
---|
| 14 |
|
---|
| 15 | /* Return the current directory, newly allocated, arbitrarily long.
|
---|
| 16 | Return NULL and set errno on error.
|
---|
| 17 | If argument is not NULL (previous usage allocate memory), call free()
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | char *
|
---|
[1765] | 21 | xrealloc_getcwd_or_warn(char *cwd)
|
---|
[821] | 22 | {
|
---|
[1765] | 23 | char *ret;
|
---|
| 24 | unsigned path_max;
|
---|
[821] | 25 |
|
---|
[1765] | 26 | path_max = (unsigned) PATH_MAX;
|
---|
| 27 | path_max += 2; /* The getcwd docs say to do this. */
|
---|
[821] | 28 |
|
---|
[1765] | 29 | if (cwd == NULL)
|
---|
| 30 | cwd = xmalloc(path_max);
|
---|
[821] | 31 |
|
---|
[1765] | 32 | while ((ret = getcwd(cwd, path_max)) == NULL && errno == ERANGE) {
|
---|
| 33 | path_max += PATH_INCR;
|
---|
| 34 | cwd = xrealloc(cwd, path_max);
|
---|
| 35 | }
|
---|
[821] | 36 |
|
---|
[1765] | 37 | if (ret == NULL) {
|
---|
| 38 | free(cwd);
|
---|
| 39 | bb_perror_msg("getcwd");
|
---|
| 40 | return NULL;
|
---|
| 41 | }
|
---|
[821] | 42 |
|
---|
[1765] | 43 | return cwd;
|
---|
[821] | 44 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.