1 /* getpwd.c - get the working directory */
20 #ifdef HAVE_SYS_PARAM_H
21 #include <sys/param.h>
27 /* Prototype these in case the system headers don't provide them. */
28 extern char *getpwd ();
29 extern char *getwd ();
31 #include "libiberty.h"
33 /* Virtually every UN*X system now in common use (except for pre-4.3-tahoe
34 BSD systems) now provides getcwd as called for by POSIX. Allow for
35 the few exceptions to the general rule here. */
37 #if !(defined (POSIX) || defined (USG) || defined (VMS)) || defined (HAVE_GETWD)
38 #define getcwd(buf,len) getwd(buf)
40 #define GUESSPATHLEN (MAXPATHLEN + 1)
42 #define GUESSPATHLEN 100
44 #else /* (defined (USG) || defined (VMS)) */
45 /* We actually use this as a starting point, not a limit. */
46 #define GUESSPATHLEN 100
47 #endif /* (defined (USG) || defined (VMS)) */
49 #if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN__)))
51 /* Get the working directory. Use the PWD environment variable if it's
52 set correctly, since this is faster and gives more uniform answers
53 to the user. Yield the working directory if successful; otherwise,
54 yield 0 and set errno. */
60 static int failure_errno;
64 struct stat dotstat, pwdstat;
66 if (!p && !(errno = failure_errno))
68 if (! ((p = getenv ("PWD")) != 0
70 && stat (p, &pwdstat) == 0
71 && stat (".", &dotstat) == 0
72 && dotstat.st_ino == pwdstat.st_ino
73 && dotstat.st_dev == pwdstat.st_dev))
75 /* The shortcut didn't work. Try the slow, ``sure'' way. */
76 for (s = GUESSPATHLEN; ! getcwd (p = xmalloc (s), s); s *= 2)
84 errno = failure_errno = e;
90 /* Cache the result. This assumes that the program does
91 not invoke chdir between calls to getpwd. */
97 #else /* VMS || _WIN32 && !__CYGWIN__ */
100 #define MAXPATHLEN 255
106 static char *pwd = 0;
109 pwd = getcwd (xmalloc (MAXPATHLEN + 1), MAXPATHLEN + 1
117 #endif /* VMS || _WIN32 && !__CYGWIN__ */