1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #if HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <stdio.h>
- #include <errno.h>
- #ifndef errno
- extern int errno;
- #endif
- #include <sys/types.h>
- #include "pathmax.h"
- #if HAVE_GETCWD
- char *getcwd ();
- #else
- char *getwd ();
- # define getcwd(Buf, Max) getwd (Buf)
- #endif
- extern void *xmalloc ();
- extern char *xstrdup ();
- extern void free ();
- char *
- xgetcwd ()
- {
- #if defined __GLIBC__ && __GLIBC__ >= 2
- return getcwd (NULL, 0);
- #else
- char *ret;
- unsigned 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 = 1300;
- path_max += 2;
- for (;;)
- {
- char *cwd = (char *) xmalloc (path_max);
- errno = 0;
- ret = getcwd (cwd, path_max);
- if (ret != NULL)
- return ret;
- if (errno != ERANGE)
- {
- int save_errno = errno;
- free (cwd);
- errno = save_errno;
- return NULL;
- }
- free (cwd);
- path_max += path_max / 16;
- path_max += 32;
- }
- #endif
- }
|