old-cross-binutils/libiberty/getpwd.c

129 lines
2.5 KiB
C
Raw Normal View History

2000-02-22 15:59:20 +00:00
/* getpwd.c - get the working directory */
2001-09-26 18:45:50 +00:00
/*
2001-10-07 22:42:23 +00:00
@deftypefn Supplemental char* getpwd (void)
2001-09-26 18:45:50 +00:00
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
*/
2000-02-22 15:59:20 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <errno.h>
#ifndef errno
extern int errno;
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
2004-01-22 19:34:11 +00:00
#if HAVE_LIMITS_H
#include <limits.h>
#endif
2000-02-22 15:59:20 +00:00
#include "libiberty.h"
/* Virtually every UN*X system now in common use (except for pre-4.3-tahoe
BSD systems) now provides getcwd as called for by POSIX. Allow for
the few exceptions to the general rule here. */
#if !defined(HAVE_GETCWD) && defined(HAVE_GETWD)
2005-05-16 18:02:47 +00:00
/* Prototype in case the system headers doesn't provide it. */
extern char *getwd ();
2000-02-22 15:59:20 +00:00
#define getcwd(buf,len) getwd(buf)
#endif
#ifdef MAXPATHLEN
#define GUESSPATHLEN (MAXPATHLEN + 1)
#else
#define GUESSPATHLEN 100
#endif
#if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN__)))
/* Get the working directory. Use the PWD environment variable if it's
set correctly, since this is faster and gives more uniform answers
to the user. Yield the working directory if successful; otherwise,
yield 0 and set errno. */
char *
2005-03-28 02:09:01 +00:00
getpwd (void)
2000-02-22 15:59:20 +00:00
{
static char *pwd;
static int failure_errno;
char *p = pwd;
size_t s;
struct stat dotstat, pwdstat;
if (!p && !(errno = failure_errno))
{
if (! ((p = getenv ("PWD")) != 0
&& *p == '/'
&& stat (p, &pwdstat) == 0
&& stat (".", &dotstat) == 0
&& dotstat.st_ino == pwdstat.st_ino
&& dotstat.st_dev == pwdstat.st_dev))
/* The shortcut didn't work. Try the slow, ``sure'' way. */
2005-05-24 21:01:33 +00:00
for (s = GUESSPATHLEN; !getcwd (p = XNEWVEC (char, s), s); s *= 2)
2000-02-22 15:59:20 +00:00
{
int e = errno;
free (p);
#ifdef ERANGE
if (e != ERANGE)
#endif
{
errno = failure_errno = e;
p = 0;
break;
}
}
/* Cache the result. This assumes that the program does
not invoke chdir between calls to getpwd. */
pwd = p;
}
return p;
}
#else /* VMS || _WIN32 && !__CYGWIN__ */
#ifndef MAXPATHLEN
#define MAXPATHLEN 255
#endif
char *
2005-03-28 02:09:01 +00:00
getpwd (void)
2000-02-22 15:59:20 +00:00
{
static char *pwd = 0;
if (!pwd)
2005-05-24 21:01:33 +00:00
pwd = getcwd (XNEWVEC (char, MAXPATHLEN + 1), MAXPATHLEN + 1
2000-02-22 15:59:20 +00:00
#ifdef VMS
, 0
#endif
);
return pwd;
}
#endif /* VMS || _WIN32 && !__CYGWIN__ */