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(HAVE_GETCWD) && defined(HAVE_GETWD)
38 #define getcwd(buf,len) getwd(buf)
42 #define GUESSPATHLEN (MAXPATHLEN + 1)
44 #define GUESSPATHLEN 100
47 #if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN__)))
49 /* Get the working directory. Use the PWD environment variable if it's
50 set correctly, since this is faster and gives more uniform answers
51 to the user. Yield the working directory if successful; otherwise,
52 yield 0 and set errno. */
58 static int failure_errno
;
62 struct stat dotstat
, pwdstat
;
64 if (!p
&& !(errno
= failure_errno
))
66 if (! ((p
= getenv ("PWD")) != 0
68 && stat (p
, &pwdstat
) == 0
69 && stat (".", &dotstat
) == 0
70 && dotstat
.st_ino
== pwdstat
.st_ino
71 && dotstat
.st_dev
== pwdstat
.st_dev
))
73 /* The shortcut didn't work. Try the slow, ``sure'' way. */
74 for (s
= GUESSPATHLEN
; ! getcwd (p
= xmalloc (s
), s
); s
*= 2)
82 errno
= failure_errno
= e
;
88 /* Cache the result. This assumes that the program does
89 not invoke chdir between calls to getpwd. */
95 #else /* VMS || _WIN32 && !__CYGWIN__ */
98 #define MAXPATHLEN 255
104 static char *pwd
= 0;
107 pwd
= getcwd (xmalloc (MAXPATHLEN
+ 1), MAXPATHLEN
+ 1
115 #endif /* VMS || _WIN32 && !__CYGWIN__ */