1 | /* getpwd.c - get the working directory */ |
---|
2 | |
---|
3 | #include "config.h" |
---|
4 | |
---|
5 | #include <errno.h> |
---|
6 | #include <sys/types.h> |
---|
7 | #include <sys/stat.h> |
---|
8 | |
---|
9 | #ifndef errno |
---|
10 | extern int errno; |
---|
11 | #endif |
---|
12 | |
---|
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. */ |
---|
16 | |
---|
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)) */ |
---|
31 | |
---|
32 | char *getenv (); |
---|
33 | char *xmalloc (); |
---|
34 | |
---|
35 | #if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN32__))) |
---|
36 | |
---|
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. */ |
---|
41 | |
---|
42 | char * |
---|
43 | getpwd () |
---|
44 | { |
---|
45 | static char *pwd; |
---|
46 | static int failure_errno; |
---|
47 | |
---|
48 | char *p = pwd; |
---|
49 | size_t s; |
---|
50 | struct stat dotstat, pwdstat; |
---|
51 | |
---|
52 | if (!p && !(errno = failure_errno)) |
---|
53 | { |
---|
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)) |
---|
60 | |
---|
61 | /* The shortcut didn't work. Try the slow, ``sure'' way. */ |
---|
62 | for (s = GUESSPATHLEN; ! getcwd (p = xmalloc (s), s); s *= 2) |
---|
63 | { |
---|
64 | int e = errno; |
---|
65 | free (p); |
---|
66 | #ifdef ERANGE |
---|
67 | if (e != ERANGE) |
---|
68 | #endif |
---|
69 | { |
---|
70 | errno = failure_errno = e; |
---|
71 | p = 0; |
---|
72 | break; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /* Cache the result. This assumes that the program does |
---|
77 | not invoke chdir between calls to getpwd. */ |
---|
78 | pwd = p; |
---|
79 | } |
---|
80 | return p; |
---|
81 | } |
---|
82 | |
---|
83 | #else /* VMS || _WIN32 && !__CYGWIN32__ */ |
---|
84 | |
---|
85 | #ifndef MAXPATHLEN |
---|
86 | #define MAXPATHLEN 255 |
---|
87 | #endif |
---|
88 | |
---|
89 | char * |
---|
90 | getpwd () |
---|
91 | { |
---|
92 | static char *pwd = 0; |
---|
93 | |
---|
94 | if (!pwd) |
---|
95 | pwd = getcwd (xmalloc (MAXPATHLEN + 1), MAXPATHLEN + 1 |
---|
96 | #ifdef VMS |
---|
97 | , 0 |
---|
98 | #endif |
---|
99 | ); |
---|
100 | return pwd; |
---|
101 | } |
---|
102 | |
---|
103 | #endif /* VMS || _WIN32 && !__CYGWIN32__ */ |
---|