1992-08-20 14:49:22 -04:00
|
|
|
/* getpwd.c - get the working directory */
|
|
|
|
|
|
|
|
#include "config.h"
|
Major cutover to using system.h:
* Makefile.in (alias.o, bitmap.o, c-aux-info.o, c-common.o,
c-decl.o, c-iterate.o, c-lang.o, c-lex.o, c-pragma.o, c-typeck.o,
caller-save.o, calls.o, collect2.o, combine.o, cse.o, dbxout.o,
dwarf2out.o, dwarfout.o, emit-rtl.o, except.o, explow.o, expmed.o,
expr.o, final.o, flow.o, function.o, getpwd.o, global.o,
integrate.o, jump.o, local-alloc.o, loop.o, optabs.o, pexecute.o,
prefix.o, print-rtl.o, print-tree.o, profile.o, real.o, recog.o,
reg-stack.o, regclass.o, regmove.o, reload.o, reload1.o, reorg.o,
rtl.o, rtlanal.o, sdbout.o, stmt.o, stor-layout.o, stupid.o,
tlink.o, toplev.o, tree.o, unroll.o, varasm.o, xcoffout.o): Depend
on system.h.
* alias.c, bitmap.c, c-aux-info.c, c-common.c, c-decl.c,
c-iterate.c, c-lang.c, c-lex.c, c-pragma.c, c-typeck.c,
caller-save.c, calls.c, collect2.c, combine.c, cse.c, dbxout.c,
dwarf2out.c, dwarfout.c, emit-rtl.c, except.c, explow.c, expmed.c,
expr.c, final.c, flow.c, function.c, gcc.c, getpwd.c, global.c,
integrate.c, jump.c, local-alloc.c, loop.c, optabs.c, pexecute.c,
prefix.c, print-rtl.c, print-tree.c, profile.c, real.c, recog.c,
reg-stack.c, regclass.c, regmove.c, reload.c, reload1.c, reorg.c,
rtl.c, rtlanal.c, sched.c, sdbout.c, stmt.c, stor-layout.c,
stupid.c, tlink.c, toplev.c, tree.c, unroll.c, varasm.c,
xcoffout.c: Include system.h. Organize include ordering so
that stdarg/varargs comes before other system headers. Remove
spurious casts of functions assured of a prototype in system.h.
From-SVN: r18726
1998-03-20 09:58:42 -05:00
|
|
|
#include "system.h"
|
1992-08-20 14:49:22 -04:00
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
1994-10-14 10:29:15 -04:00
|
|
|
#if !(defined (POSIX) || defined (USG) || defined (VMS)) || defined (HAVE_GETWD)
|
1992-08-20 14:49:22 -04:00
|
|
|
#define getcwd(buf,len) getwd(buf)
|
1994-09-14 10:11:15 -04:00
|
|
|
#ifdef MAXPATHLEN
|
1992-08-20 14:49:22 -04:00
|
|
|
#define GUESSPATHLEN (MAXPATHLEN + 1)
|
1994-09-14 10:11:15 -04:00
|
|
|
#else
|
|
|
|
#define GUESSPATHLEN 100
|
|
|
|
#endif
|
1992-08-20 14:49:22 -04:00
|
|
|
#else /* (defined (USG) || defined (VMS)) */
|
|
|
|
/* We actually use this as a starting point, not a limit. */
|
|
|
|
#define GUESSPATHLEN 100
|
|
|
|
#endif /* (defined (USG) || defined (VMS)) */
|
|
|
|
|
1998-11-12 14:37:47 -05:00
|
|
|
#if !(defined (VMS) || (defined(_WIN32) && !defined(__CYGWIN__)))
|
1992-08-20 14:49:22 -04:00
|
|
|
|
|
|
|
/* 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 *
|
|
|
|
getpwd ()
|
|
|
|
{
|
|
|
|
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. */
|
|
|
|
for (s = GUESSPATHLEN; ! getcwd (p = xmalloc (s), s); s *= 2)
|
|
|
|
{
|
|
|
|
int e = errno;
|
|
|
|
free (p);
|
1992-12-19 01:32:08 -05:00
|
|
|
#ifdef ERANGE
|
1992-08-20 14:49:22 -04:00
|
|
|
if (e != ERANGE)
|
1992-12-19 01:32:08 -05:00
|
|
|
#endif
|
1992-08-20 14:49:22 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1998-11-12 14:37:47 -05:00
|
|
|
#else /* VMS || _WIN32 && !__CYGWIN__ */
|
1992-08-20 14:49:22 -04:00
|
|
|
|
|
|
|
#ifndef MAXPATHLEN
|
|
|
|
#define MAXPATHLEN 255
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *
|
|
|
|
getpwd ()
|
|
|
|
{
|
|
|
|
static char *pwd = 0;
|
|
|
|
|
Major cutover to using system.h:
* Makefile.in (alias.o, bitmap.o, c-aux-info.o, c-common.o,
c-decl.o, c-iterate.o, c-lang.o, c-lex.o, c-pragma.o, c-typeck.o,
caller-save.o, calls.o, collect2.o, combine.o, cse.o, dbxout.o,
dwarf2out.o, dwarfout.o, emit-rtl.o, except.o, explow.o, expmed.o,
expr.o, final.o, flow.o, function.o, getpwd.o, global.o,
integrate.o, jump.o, local-alloc.o, loop.o, optabs.o, pexecute.o,
prefix.o, print-rtl.o, print-tree.o, profile.o, real.o, recog.o,
reg-stack.o, regclass.o, regmove.o, reload.o, reload1.o, reorg.o,
rtl.o, rtlanal.o, sdbout.o, stmt.o, stor-layout.o, stupid.o,
tlink.o, toplev.o, tree.o, unroll.o, varasm.o, xcoffout.o): Depend
on system.h.
* alias.c, bitmap.c, c-aux-info.c, c-common.c, c-decl.c,
c-iterate.c, c-lang.c, c-lex.c, c-pragma.c, c-typeck.c,
caller-save.c, calls.c, collect2.c, combine.c, cse.c, dbxout.c,
dwarf2out.c, dwarfout.c, emit-rtl.c, except.c, explow.c, expmed.c,
expr.c, final.c, flow.c, function.c, gcc.c, getpwd.c, global.c,
integrate.c, jump.c, local-alloc.c, loop.c, optabs.c, pexecute.c,
prefix.c, print-rtl.c, print-tree.c, profile.c, real.c, recog.c,
reg-stack.c, regclass.c, regmove.c, reload.c, reload1.c, reorg.c,
rtl.c, rtlanal.c, sched.c, sdbout.c, stmt.c, stor-layout.c,
stupid.c, tlink.c, toplev.c, tree.c, unroll.c, varasm.c,
xcoffout.c: Include system.h. Organize include ordering so
that stdarg/varargs comes before other system headers. Remove
spurious casts of functions assured of a prototype in system.h.
From-SVN: r18726
1998-03-20 09:58:42 -05:00
|
|
|
if (!pwd)
|
|
|
|
pwd = getcwd (xmalloc (MAXPATHLEN + 1), MAXPATHLEN + 1
|
|
|
|
#ifdef VMS
|
|
|
|
, 0
|
|
|
|
#endif
|
|
|
|
);
|
1992-08-20 14:49:22 -04:00
|
|
|
return pwd;
|
|
|
|
}
|
|
|
|
|
1998-11-12 14:37:47 -05:00
|
|
|
#endif /* VMS || _WIN32 && !__CYGWIN__ */
|