* sysdeps/posix/getcwd.c: Correct last patch.  Handle getcwd
	(NULL, != 0) correctly.
This commit is contained in:
Ulrich Drepper 1999-07-07 14:15:25 +00:00
parent 6c790888b4
commit 1823e76b95
2 changed files with 18 additions and 11 deletions

View File

@ -1,5 +1,8 @@
1999-07-07 Ulrich Drepper <drepper@cygnus.com> 1999-07-07 Ulrich Drepper <drepper@cygnus.com>
* sysdeps/posix/getcwd.c: Correct last patch. Handle getcwd
(NULL, != 0) correctly.
* elf/dl-load.c: Use a few more __builtin_expect. * elf/dl-load.c: Use a few more __builtin_expect.
* sysdeps/posix/getcwd.c: When resizing buffer make sure new size * sysdeps/posix/getcwd.c: When resizing buffer make sure new size

View File

@ -224,6 +224,7 @@ __getcwd (buf, size)
register char *pathp; register char *pathp;
struct stat st; struct stat st;
int prev_errno = errno; int prev_errno = errno;
size_t allocated = size;
if (size == 0) if (size == 0)
{ {
@ -233,19 +234,19 @@ __getcwd (buf, size)
return NULL; return NULL;
} }
size = PATH_MAX + 1; allocated = PATH_MAX + 1;
} }
if (buf != NULL) if (buf != NULL)
path = buf; path = buf;
else else
{ {
path = malloc (size); path = malloc (allocated);
if (path == NULL) if (path == NULL)
return NULL; return NULL;
} }
pathp = path + size; pathp = path + allocated;
*--pathp = '\0'; *--pathp = '\0';
if (__lstat (".", &st) < 0) if (__lstat (".", &st) < 0)
@ -362,7 +363,7 @@ __getcwd (buf, size)
if ((size_t) (pathp - path) <= namlen) if ((size_t) (pathp - path) <= namlen)
{ {
if (buf != NULL) if (size != 0)
{ {
(void) __closedir (dirstream); (void) __closedir (dirstream);
__set_errno (ERANGE); __set_errno (ERANGE);
@ -371,20 +372,23 @@ __getcwd (buf, size)
else else
{ {
char *tmp; char *tmp;
size_t oldsize = allocated;
size = 2 * MAX (size, namlen); allocated = 2 * MAX (allocated, namlen);
tmp = realloc (path, size); tmp = realloc (path, allocated);
if (tmp == NULL) if (tmp == NULL)
{ {
(void) __closedir (dirstream); (void) __closedir (dirstream);
__set_errno (ENOMEM);/* closedir might have changed it.*/ __set_errno (ENOMEM);/* closedir might have changed it.*/
goto lose; goto lose;
} }
pathp = &tmp[pathp - path + size / 2];
path = tmp;
/* Move current contents up to the end of the buffer. /* Move current contents up to the end of the buffer.
This is guaranteed to be non-overlapping. */ This is guaranteed to be non-overlapping. */
memcpy (pathp, pathp - size / 2, path + size - pathp); pathp = memcpy (tmp + allocated - (path + oldsize - pathp),
tmp + (pathp - path),
path + oldsize - pathp);
path = tmp;
} }
} }
pathp -= namlen; pathp -= namlen;
@ -397,13 +401,13 @@ __getcwd (buf, size)
thisino = dotino; thisino = dotino;
} }
if (pathp == &path[size - 1]) if (pathp == &path[allocated - 1])
*--pathp = '/'; *--pathp = '/';
if (dotlist != dots) if (dotlist != dots)
free ((__ptr_t) dotlist); free ((__ptr_t) dotlist);
memmove (path, pathp, path + size - pathp); memmove (path, pathp, path + allocated - pathp);
/* Restore errno on successful return. */ /* Restore errno on successful return. */
__set_errno (prev_errno); __set_errno (prev_errno);