import of gcc-2.8
[official-gcc.git] / gcc / getpwd.c
blob28eb298ebf9cecf731af134f4c94c2654945d5e1
1 /* getpwd.c - get the working directory */
3 #include "config.h"
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
9 #ifndef errno
10 extern int errno;
11 #endif
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)
21 #ifdef MAXPATHLEN
22 #define GUESSPATHLEN (MAXPATHLEN + 1)
23 #else
24 #define GUESSPATHLEN 100
25 #endif
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)) */
32 char *getenv ();
33 char *xmalloc ();
35 #if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
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. */
42 char *
43 getpwd ()
45 static char *pwd;
46 static int failure_errno;
48 char *p = pwd;
49 size_t s;
50 struct stat dotstat, pwdstat;
52 if (!p && !(errno = failure_errno))
54 if (! ((p = getenv ("PWD")) != 0
55 && *p == '/'
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)
64 int e = errno;
65 free (p);
66 #ifdef ERANGE
67 if (e != ERANGE)
68 #endif
70 errno = failure_errno = e;
71 p = 0;
72 break;
76 /* Cache the result. This assumes that the program does
77 not invoke chdir between calls to getpwd. */
78 pwd = p;
80 return p;
83 #else /* VMS || _WIN32 && !__CYGWIN32__ */
85 #ifndef MAXPATHLEN
86 #define MAXPATHLEN 255
87 #endif
89 char *
90 getpwd ()
92 static char *pwd = 0;
94 if (!pwd)
95 pwd = getcwd (xmalloc (MAXPATHLEN + 1), MAXPATHLEN + 1
96 #ifdef VMS
97 , 0
98 #endif
100 return pwd;
103 #endif /* VMS || _WIN32 && !__CYGWIN32__ */