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