1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #if HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <stdio.h>
- #include <errno.h>
- #ifndef errno
- extern int errno;
- #endif
- #include <sys/types.h>
- #if HAVE_STDLIB_H
- # include <stdlib.h>
- #endif
- #if HAVE_UNISTD_H
- # include <unistd.h>
- #endif
- #if HAVE_GETCWD
- char *getcwd ();
- #else
- char *getwd ();
- # define getcwd(Buf, Max) getwd (Buf)
- #endif
- #include "xalloc.h"
- char *
- xgetcwd ()
- {
- #if defined __GLIBC__ && __GLIBC__ >= 2
- return getcwd (NULL, 0);
- #else
- char *ret;
- size_t path_max;
- char buf[1024];
- errno = 0;
- ret = getcwd (buf, sizeof (buf));
- if (ret != NULL)
- return xstrdup (buf);
- if (errno != ERANGE)
- return NULL;
- path_max = 1 << 10;
- for (;;)
- {
- char *cwd = (char *) xmalloc (path_max);
- int save_errno;
- errno = 0;
- ret = getcwd (cwd, path_max);
- if (ret != NULL)
- return ret;
- save_errno = errno;
- free (cwd);
- if (save_errno != ERANGE)
- {
- errno = save_errno;
- return NULL;
- }
- path_max *= 2;
- if (path_max == 0)
- xalloc_die ();
- }
- #endif
- }
|