OSDN Git Service

* Makefile.in (CFILES): Add calloc.c and getpwd.c.
[pf3gnuchains/gcc-fork.git] / libiberty / getpwd.c
1 /* getpwd.c - get the working directory */
2
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6
7 #include <sys/types.h>
8
9 #include <errno.h>
10 #ifndef errno
11 extern int errno;
12 #endif
13
14 #ifdef HAVE_STDLIB_H
15 #include <stdlib.h>
16 #endif
17 #ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 #ifdef HAVE_SYS_PARAM_H
21 #include <sys/param.h>
22 #endif
23 #if HAVE_SYS_STAT_H
24 #include <sys/stat.h>
25 #endif
26
27 /* Prototype these in case the system headers don't provide them. */
28 extern char *getpwd ();
29 extern char *getwd ();
30
31 #include "libiberty.h"
32
33 /* Virtually every UN*X system now in common use (except for pre-4.3-tahoe
34    BSD systems) now provides getcwd as called for by POSIX.  Allow for
35    the few exceptions to the general rule here.  */
36
37 #if !(defined (POSIX) || defined (USG) || defined (VMS)) || defined (HAVE_GETWD)
38 #define getcwd(buf,len) getwd(buf)
39 #ifdef MAXPATHLEN
40 #define GUESSPATHLEN (MAXPATHLEN + 1)
41 #else
42 #define GUESSPATHLEN 100
43 #endif
44 #else /* (defined (USG) || defined (VMS)) */
45 /* We actually use this as a starting point, not a limit.  */
46 #define GUESSPATHLEN 100
47 #endif /* (defined (USG) || defined (VMS)) */
48
49 #if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN__)))
50
51 /* Get the working directory.  Use the PWD environment variable if it's
52    set correctly, since this is faster and gives more uniform answers
53    to the user.  Yield the working directory if successful; otherwise,
54    yield 0 and set errno.  */
55
56 char *
57 getpwd ()
58 {
59   static char *pwd;
60   static int failure_errno;
61
62   char *p = pwd;
63   size_t s;
64   struct stat dotstat, pwdstat;
65
66   if (!p && !(errno = failure_errno))
67     {
68       if (! ((p = getenv ("PWD")) != 0
69              && *p == '/'
70              && stat (p, &pwdstat) == 0
71              && stat (".", &dotstat) == 0
72              && dotstat.st_ino == pwdstat.st_ino
73              && dotstat.st_dev == pwdstat.st_dev))
74
75         /* The shortcut didn't work.  Try the slow, ``sure'' way.  */
76         for (s = GUESSPATHLEN;  ! getcwd (p = xmalloc (s), s);  s *= 2)
77           {
78             int e = errno;
79             free (p);
80 #ifdef ERANGE
81             if (e != ERANGE)
82 #endif
83               {
84                 errno = failure_errno = e;
85                 p = 0;
86                 break;
87               }
88           }
89
90       /* Cache the result.  This assumes that the program does
91          not invoke chdir between calls to getpwd.  */
92       pwd = p;
93     }
94   return p;
95 }
96
97 #else   /* VMS || _WIN32 && !__CYGWIN__ */
98
99 #ifndef MAXPATHLEN
100 #define MAXPATHLEN 255
101 #endif
102
103 char *
104 getpwd ()
105 {
106   static char *pwd = 0;
107
108   if (!pwd)
109     pwd = getcwd (xmalloc (MAXPATHLEN + 1), MAXPATHLEN + 1
110 #ifdef VMS
111                   , 0
112 #endif
113                   );
114   return pwd;
115 }
116
117 #endif  /* VMS || _WIN32 && !__CYGWIN__ */