OSDN Git Service

2006-04-24 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / libiberty / getpwd.c
index 2fa3416..fa5c132 100644 (file)
@@ -1,5 +1,17 @@
 /* getpwd.c - get the working directory */
 
+/*
+
+@deftypefn Supplemental char* getpwd (void)
+
+Returns the current working directory.  This implementation caches the
+result on the assumption that the process will not call @code{chdir}
+between calls to @code{getpwd}.
+
+@end deftypefn
+
+*/
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -23,10 +35,9 @@ extern int errno;
 #if HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
-
-/* Prototype these in case the system headers don't provide them. */
-extern char *getpwd ();
-extern char *getwd ();
+#if HAVE_LIMITS_H
+#include <limits.h>
+#endif
 
 #include "libiberty.h"
 
@@ -34,17 +45,17 @@ extern char *getwd ();
    BSD systems) now provides getcwd as called for by POSIX.  Allow for
    the few exceptions to the general rule here.  */
 
-#if !(defined (POSIX) || defined (USG) || defined (VMS)) || defined (HAVE_GETWD)
+#if !defined(HAVE_GETCWD) && defined(HAVE_GETWD)
+/* Prototype in case the system headers doesn't provide it. */
+extern char *getwd ();
 #define getcwd(buf,len) getwd(buf)
+#endif
+
 #ifdef MAXPATHLEN
 #define GUESSPATHLEN (MAXPATHLEN + 1)
 #else
 #define GUESSPATHLEN 100
 #endif
-#else /* (defined (USG) || defined (VMS)) */
-/* We actually use this as a starting point, not a limit.  */
-#define GUESSPATHLEN 100
-#endif /* (defined (USG) || defined (VMS)) */
 
 #if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN__)))
 
@@ -54,7 +65,7 @@ extern char *getwd ();
    yield 0 and set errno.  */
 
 char *
-getpwd ()
+getpwd (void)
 {
   static char *pwd;
   static int failure_errno;
@@ -73,7 +84,7 @@ getpwd ()
             && dotstat.st_dev == pwdstat.st_dev))
 
        /* The shortcut didn't work.  Try the slow, ``sure'' way.  */
-       for (s = GUESSPATHLEN;  ! getcwd (p = xmalloc (s), s);  s *= 2)
+       for (s = GUESSPATHLEN;  !getcwd (p = XNEWVEC (char, s), s);  s *= 2)
          {
            int e = errno;
            free (p);
@@ -101,12 +112,12 @@ getpwd ()
 #endif
 
 char *
-getpwd ()
+getpwd (void)
 {
   static char *pwd = 0;
 
   if (!pwd)
-    pwd = getcwd (xmalloc (MAXPATHLEN + 1), MAXPATHLEN + 1
+    pwd = getcwd (XNEWVEC (char, MAXPATHLEN + 1), MAXPATHLEN + 1
 #ifdef VMS
                  , 0
 #endif