1 /* getpwd.c - get the working directory */
2 
3 /*
4 
5 @deftypefn Supplemental char* getpwd (void)
6 
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}.
10 
11 @end deftypefn
12 
13 */
14 
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18 
19 #include <sys/types.h>
20 
21 #include <errno.h>
22 #ifndef errno
23 extern int errno;
24 #endif
25 
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef HAVE_SYS_PARAM_H
33 #include <sys/param.h>
34 #endif
35 #if HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
38 #if HAVE_LIMITS_H
39 #include <limits.h>
40 #endif
41 
42 #include "libiberty.h"
43 
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.  */
47 
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)
52 #endif
53 
54 #ifdef MAXPATHLEN
55 #define GUESSPATHLEN (MAXPATHLEN + 1)
56 #else
57 #define GUESSPATHLEN 100
58 #endif
59 
60 #if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN__)))
61 
62 #ifdef _WIN32
63 #include <direct.h>
64 #define getcwd	_getcwd
65 #endif
66 
67 /* Get the working directory.  Use the PWD environment variable if it's
68    set correctly, since this is faster and gives more uniform answers
69    to the user.  Yield the working directory if successful; otherwise,
70    yield 0 and set errno.  */
71 
72 char *
getpwd(void)73 getpwd (void)
74 {
75   static char *pwd;
76   static int failure_errno;
77 
78   char *p = pwd;
79   size_t s;
80   struct stat dotstat, pwdstat;
81 
82   if (!p && !(errno = failure_errno))
83     {
84       if (! ((p = getenv ("PWD")) != 0
85 	     && *p == '/'
86 	     && stat (p, &pwdstat) == 0
87 	     && stat (".", &dotstat) == 0
88 	     && dotstat.st_ino == pwdstat.st_ino
89 	     && dotstat.st_dev == pwdstat.st_dev))
90 
91 	/* The shortcut didn't work.  Try the slow, ``sure'' way.  */
92 	for (s = GUESSPATHLEN;  !getcwd (p = XNEWVEC (char, s), s);  s *= 2)
93 	  {
94 	    int e = errno;
95 	    free (p);
96 #ifdef ERANGE
97 	    if (e != ERANGE)
98 #endif
99 	      {
100 		errno = failure_errno = e;
101 		p = 0;
102 		break;
103 	      }
104 	  }
105 
106       /* Cache the result.  This assumes that the program does
107 	 not invoke chdir between calls to getpwd.  */
108       pwd = p;
109     }
110   return p;
111 }
112 
113 #else	/* VMS || _WIN32 && !__CYGWIN__ */
114 
115 #ifndef MAXPATHLEN
116 #define MAXPATHLEN 255
117 #endif
118 
119 char *
getpwd(void)120 getpwd (void)
121 {
122   static char *pwd = 0;
123 
124   if (!pwd)
125     pwd = getcwd (XNEWVEC (char, MAXPATHLEN + 1), MAXPATHLEN + 1
126 #ifdef VMS
127 		  , 0
128 #endif
129 		  );
130   return pwd;
131 }
132 
133 #endif	/* VMS || _WIN32 && !__CYGWIN__ */
134