/* getpwd.c - get the working directory */
/* 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))
#define getcwd(buf,len) getwd(buf)
#define GUESSPATHLEN (MAXPATHLEN + 1)
#else /* (defined (USG) || defined (VMS)) */
/* We actually use this as a starting point, not a limit. */
#endif /* (defined (USG) || defined (VMS)) */
/* 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. */
static int failure_errno
;
struct stat dotstat
, pwdstat
;
if (!p
&& !(errno
= failure_errno
))
if (! ((p
= getenv ("PWD")) != 0
&& 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)
errno
= failure_errno
= e
;
/* Cache the result. This assumes that the program does
not invoke chdir between calls to getpwd. */
if (!pwd
) pwd
= getcwd (xmalloc (MAXPATHLEN
+1), MAXPATHLEN
+1);