| 1 | /* Copyright (C) 2011-2012 Free Software Foundation, Inc.
|
|---|
| 2 | This file is part of gnulib.
|
|---|
| 3 |
|
|---|
| 4 | This program is free software: you can redistribute it and/or modify
|
|---|
| 5 | it under the terms of the GNU General Public License as published by
|
|---|
| 6 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 7 | (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | This program is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License
|
|---|
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|---|
| 16 |
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 |
|
|---|
| 19 | /* Specification */
|
|---|
| 20 | #include <unistd.h>
|
|---|
| 21 |
|
|---|
| 22 | #include <errno.h>
|
|---|
| 23 | #include <string.h>
|
|---|
| 24 |
|
|---|
| 25 | #if GNULIB_GETCWD
|
|---|
| 26 | /* Favor GPL getcwd.c if both getcwd and getcwd-lgpl modules are in use. */
|
|---|
| 27 | typedef int dummy;
|
|---|
| 28 | #else
|
|---|
| 29 |
|
|---|
| 30 | /* Get the name of the current working directory, and put it in SIZE
|
|---|
| 31 | bytes of BUF. Returns NULL if the directory couldn't be determined
|
|---|
| 32 | (perhaps because the absolute name was longer than PATH_MAX, or
|
|---|
| 33 | because of missing read/search permissions on parent directories)
|
|---|
| 34 | or SIZE was too small. If successful, returns BUF. If BUF is
|
|---|
| 35 | NULL, an array is allocated with 'malloc'; the array is SIZE bytes
|
|---|
| 36 | long, unless SIZE == 0, in which case it is as big as
|
|---|
| 37 | necessary. */
|
|---|
| 38 |
|
|---|
| 39 | # undef getcwd
|
|---|
| 40 | char *
|
|---|
| 41 | rpl_getcwd (char *buf, size_t size)
|
|---|
| 42 | {
|
|---|
| 43 | char *ptr;
|
|---|
| 44 | char *result;
|
|---|
| 45 |
|
|---|
| 46 | /* Handle single size operations. */
|
|---|
| 47 | if (buf)
|
|---|
| 48 | {
|
|---|
| 49 | if (!size)
|
|---|
| 50 | {
|
|---|
| 51 | errno = EINVAL;
|
|---|
| 52 | return NULL;
|
|---|
| 53 | }
|
|---|
| 54 | return getcwd (buf, size);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if (size)
|
|---|
| 58 | {
|
|---|
| 59 | buf = malloc (size);
|
|---|
| 60 | if (!buf)
|
|---|
| 61 | {
|
|---|
| 62 | errno = ENOMEM;
|
|---|
| 63 | return NULL;
|
|---|
| 64 | }
|
|---|
| 65 | result = getcwd (buf, size);
|
|---|
| 66 | if (!result)
|
|---|
| 67 | {
|
|---|
| 68 | int saved_errno = errno;
|
|---|
| 69 | free (buf);
|
|---|
| 70 | errno = saved_errno;
|
|---|
| 71 | }
|
|---|
| 72 | return result;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /* Flexible sizing requested. Avoid over-allocation for the common
|
|---|
| 76 | case of a name that fits within a 4k page, minus some space for
|
|---|
| 77 | local variables, to be sure we don't skip over a guard page. */
|
|---|
| 78 | {
|
|---|
| 79 | char tmp[4032];
|
|---|
| 80 | size = sizeof tmp;
|
|---|
| 81 | ptr = getcwd (tmp, size);
|
|---|
| 82 | if (ptr)
|
|---|
| 83 | {
|
|---|
| 84 | result = strdup (ptr);
|
|---|
| 85 | if (!result)
|
|---|
| 86 | errno = ENOMEM;
|
|---|
| 87 | return result;
|
|---|
| 88 | }
|
|---|
| 89 | if (errno != ERANGE)
|
|---|
| 90 | return NULL;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /* My what a large directory name we have. */
|
|---|
| 94 | do
|
|---|
| 95 | {
|
|---|
| 96 | size <<= 1;
|
|---|
| 97 | ptr = realloc (buf, size);
|
|---|
| 98 | if (ptr == NULL)
|
|---|
| 99 | {
|
|---|
| 100 | free (buf);
|
|---|
| 101 | errno = ENOMEM;
|
|---|
| 102 | return NULL;
|
|---|
| 103 | }
|
|---|
| 104 | buf = ptr;
|
|---|
| 105 | result = getcwd (buf, size);
|
|---|
| 106 | }
|
|---|
| 107 | while (!result && errno == ERANGE);
|
|---|
| 108 |
|
|---|
| 109 | if (!result)
|
|---|
| 110 | {
|
|---|
| 111 | int saved_errno = errno;
|
|---|
| 112 | free (buf);
|
|---|
| 113 | errno = saved_errno;
|
|---|
| 114 | }
|
|---|
| 115 | else
|
|---|
| 116 | {
|
|---|
| 117 | /* Trim to fit, if possible. */
|
|---|
| 118 | result = realloc (buf, strlen (buf) + 1);
|
|---|
| 119 | if (!result)
|
|---|
| 120 | result = buf;
|
|---|
| 121 | }
|
|---|
| 122 | return result;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | #endif
|
|---|