1 /* getpwd.c - get the working directory */
5 @deftypefn Supplemental char* getpwd (void)
7 Returns the current working directory. This implementation caches the
8 result on the assumption that the process will not call @code{chdir}
9 between calls to @code{getpwd}.
19 #include <sys/types.h>
32 #ifdef HAVE_SYS_PARAM_H
33 #include <sys/param.h>
42 #include "libiberty.h"
44 /* Virtually every UN*X system now in common use (except for pre-4.3-tahoe
45 BSD systems) now provides getcwd as called for by POSIX. Allow for
46 the few exceptions to the general rule here. */
48 #if !defined(HAVE_GETCWD) && defined(HAVE_GETWD)
49 /* Prototype in case the system headers doesn't provide it. */
50 extern char *getwd ();
51 #define getcwd(buf,len) getwd(buf)
55 #define GUESSPATHLEN (MAXPATHLEN + 1)
57 #define GUESSPATHLEN 100
60 #if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN__)))
62 /* Get the working directory. Use the PWD environment variable if it's
63 set correctly, since this is faster and gives more uniform answers
64 to the user. Yield the working directory if successful; otherwise,
65 yield 0 and set errno. */
71 static int failure_errno
;
75 struct stat dotstat
, pwdstat
;
77 if (!p
&& !(errno
= failure_errno
))
79 if (! ((p
= getenv ("PWD")) != 0
81 && stat (p
, &pwdstat
) == 0
82 && stat (".", &dotstat
) == 0
83 && dotstat
.st_ino
== pwdstat
.st_ino
84 && dotstat
.st_dev
== pwdstat
.st_dev
))
86 /* The shortcut didn't work. Try the slow, ``sure'' way. */
87 for (s
= GUESSPATHLEN
; !getcwd (p
= XNEWVEC (char, s
), s
); s
*= 2)
95 errno
= failure_errno
= e
;
101 /* Cache the result. This assumes that the program does
102 not invoke chdir between calls to getpwd. */
108 #else /* VMS || _WIN32 && !__CYGWIN__ */
111 #define MAXPATHLEN 255
117 static char *pwd
= 0;
120 pwd
= getcwd (XNEWVEC (char, MAXPATHLEN
+ 1), MAXPATHLEN
+ 1
128 #endif /* VMS || _WIN32 && !__CYGWIN__ */
This page took 0.040251 seconds and 4 git commands to generate.