1 /* getpwd.c - get the working directory */
13 /* Virtually every UN*X system now in common use (except for pre-4.3-tahoe
14 BSD systems) now provides getcwd as called for by POSIX. Allow for
15 the few exceptions to the general rule here. */
17 #if !(defined (POSIX) || defined (USG) || defined (VMS)) || defined (HAVE_GETWD)
18 #include <sys/param.h>
19 extern char *getwd ();
20 #define getcwd(buf,len) getwd(buf)
22 #define GUESSPATHLEN (MAXPATHLEN + 1)
24 #define GUESSPATHLEN 100
26 #else /* (defined (USG) || defined (VMS)) */
27 extern char *getcwd ();
28 /* We actually use this as a starting point, not a limit. */
29 #define GUESSPATHLEN 100
30 #endif /* (defined (USG) || defined (VMS)) */
37 /* Get the working directory. Use the PWD environment variable if it's
38 set correctly, since this is faster and gives more uniform answers
39 to the user. Yield the working directory if successful; otherwise,
40 yield 0 and set errno. */
46 static int failure_errno
;
50 struct stat dotstat
, pwdstat
;
52 if (!p
&& !(errno
= failure_errno
))
54 if (! ((p
= getenv ("PWD")) != 0
56 && stat (p
, &pwdstat
) == 0
57 && stat (".", &dotstat
) == 0
58 && dotstat
.st_ino
== pwdstat
.st_ino
59 && dotstat
.st_dev
== pwdstat
.st_dev
))
61 /* The shortcut didn't work. Try the slow, ``sure'' way. */
62 for (s
= GUESSPATHLEN
; ! getcwd (p
= xmalloc (s
), s
); s
*= 2)
70 errno
= failure_errno
= e
;
76 /* Cache the result. This assumes that the program does
77 not invoke chdir between calls to getpwd. */
86 #define MAXPATHLEN 255
94 if (!pwd
) pwd
= getcwd (xmalloc (MAXPATHLEN
+1), MAXPATHLEN
+1);