diff options
author | ghazi <ghazi@138bc75d-0d04-0410-961f-82ee72b054a4> | 1999-08-31 00:50:36 +0000 |
---|---|---|
committer | ghazi <ghazi@138bc75d-0d04-0410-961f-82ee72b054a4> | 1999-08-31 00:50:36 +0000 |
commit | 71774460a34cfac36daed52a4fc33eb1a5323d79 (patch) | |
tree | a1e4c9bfa7ee3e601f8a9bb78fe7b268f30e707b /libiberty/getpwd.c | |
parent | 9cb2f3388d1f5b5277304ab5530461dba5c7806d (diff) | |
download | gcc-71774460a34cfac36daed52a4fc33eb1a5323d79.tar.gz |
* Makefile.in (CFILES): Add calloc.c and getpwd.c.
(REQUIRED_OFILES): Add getpwd.o.
(getpwd.o): Add target.
* configure.in (AC_PREREQ): Bump to 2.13.
(AC_CHECK_HEADERS): Add check for <sys/stat.h>.
* getpwd.c: New file, moved here from gcc.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@28996 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libiberty/getpwd.c')
-rw-r--r-- | libiberty/getpwd.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/libiberty/getpwd.c b/libiberty/getpwd.c new file mode 100644 index 00000000000..2fa341681e3 --- /dev/null +++ b/libiberty/getpwd.c @@ -0,0 +1,117 @@ +/* getpwd.c - get the working directory */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <sys/types.h> + +#include <errno.h> +#ifndef errno +extern int errno; +#endif + +#ifdef HAVE_STDLIB_H +#include <stdlib.h> +#endif +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_SYS_PARAM_H +#include <sys/param.h> +#endif +#if HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif + +/* Prototype these in case the system headers don't provide them. */ +extern char *getpwd (); +extern char *getwd (); + +#include "libiberty.h" + +/* Virtually every UN*X system now in common use (except for pre-4.3-tahoe + BSD systems) now provides getcwd as called for by POSIX. Allow for + the few exceptions to the general rule here. */ + +#if !(defined (POSIX) || defined (USG) || defined (VMS)) || defined (HAVE_GETWD) +#define getcwd(buf,len) getwd(buf) +#ifdef MAXPATHLEN +#define GUESSPATHLEN (MAXPATHLEN + 1) +#else +#define GUESSPATHLEN 100 +#endif +#else /* (defined (USG) || defined (VMS)) */ +/* We actually use this as a starting point, not a limit. */ +#define GUESSPATHLEN 100 +#endif /* (defined (USG) || defined (VMS)) */ + +#if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN__))) + +/* Get the working directory. Use the PWD environment variable if it's + set correctly, since this is faster and gives more uniform answers + to the user. Yield the working directory if successful; otherwise, + yield 0 and set errno. */ + +char * +getpwd () +{ + static char *pwd; + static int failure_errno; + + char *p = pwd; + size_t s; + struct stat dotstat, pwdstat; + + if (!p && !(errno = failure_errno)) + { + if (! ((p = getenv ("PWD")) != 0 + && *p == '/' + && stat (p, &pwdstat) == 0 + && stat (".", &dotstat) == 0 + && dotstat.st_ino == pwdstat.st_ino + && dotstat.st_dev == pwdstat.st_dev)) + + /* The shortcut didn't work. Try the slow, ``sure'' way. */ + for (s = GUESSPATHLEN; ! getcwd (p = xmalloc (s), s); s *= 2) + { + int e = errno; + free (p); +#ifdef ERANGE + if (e != ERANGE) +#endif + { + errno = failure_errno = e; + p = 0; + break; + } + } + + /* Cache the result. This assumes that the program does + not invoke chdir between calls to getpwd. */ + pwd = p; + } + return p; +} + +#else /* VMS || _WIN32 && !__CYGWIN__ */ + +#ifndef MAXPATHLEN +#define MAXPATHLEN 255 +#endif + +char * +getpwd () +{ + static char *pwd = 0; + + if (!pwd) + pwd = getcwd (xmalloc (MAXPATHLEN + 1), MAXPATHLEN + 1 +#ifdef VMS + , 0 +#endif + ); + return pwd; +} + +#endif /* VMS || _WIN32 && !__CYGWIN__ */ |