1995-12-08 12:50:48 +08:00
|
|
|
/* save-cwd.c -- Save and restore current working directory.
|
2004-05-12 00:42:17 +08:00
|
|
|
Copyright (C) 1995, 1997, 1998, 2003, 2004 Free Software Foundation, Inc.
|
1995-12-08 12:50:48 +08:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
1996-07-15 11:36:16 +08:00
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
|
|
|
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
1995-12-08 12:50:48 +08:00
|
|
|
|
2003-03-05 15:24:22 +08:00
|
|
|
/* Written by Jim Meyering. */
|
1995-12-08 12:50:48 +08:00
|
|
|
|
1998-03-15 19:09:10 +08:00
|
|
|
#if HAVE_CONFIG_H
|
1995-08-04 23:23:25 +08:00
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2003-09-11 17:07:35 +08:00
|
|
|
#include <stdlib.h>
|
1995-08-04 23:23:25 +08:00
|
|
|
|
1998-03-15 19:09:10 +08:00
|
|
|
#if HAVE_UNISTD_H
|
1995-08-04 23:23:25 +08:00
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1998-03-15 19:09:10 +08:00
|
|
|
#if HAVE_FCNTL_H
|
1995-08-04 23:23:25 +08:00
|
|
|
# include <fcntl.h>
|
|
|
|
#else
|
|
|
|
# include <sys/file.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
1997-06-12 20:58:47 +08:00
|
|
|
#ifndef errno
|
1995-08-04 23:23:25 +08:00
|
|
|
extern int errno;
|
|
|
|
#endif
|
|
|
|
|
2000-10-22 14:57:42 +08:00
|
|
|
#ifndef O_DIRECTORY
|
|
|
|
# define O_DIRECTORY 0
|
|
|
|
#endif
|
|
|
|
|
1995-08-04 23:23:25 +08:00
|
|
|
#include "save-cwd.h"
|
2003-01-11 06:48:02 +08:00
|
|
|
#include "xgetcwd.h"
|
1995-08-04 23:23:25 +08:00
|
|
|
|
|
|
|
/* Record the location of the current working directory in CWD so that
|
|
|
|
the program may change to other directories and later use restore_cwd
|
|
|
|
to return to the recorded location. This function may allocate
|
|
|
|
space using malloc (via xgetcwd) or leave a file descriptor open;
|
|
|
|
use free_cwd to perform the necessary free or close. Upon failure,
|
|
|
|
no memory is allocated, any locally opened file descriptors are
|
|
|
|
closed; return non-zero -- in that case, free_cwd need not be
|
2003-03-05 15:24:22 +08:00
|
|
|
called, but doing so is ok. Otherwise, return zero.
|
|
|
|
|
|
|
|
The `raison d'etre' for this interface is that some systems lack
|
|
|
|
support for fchdir, and getcwd is not robust or as efficient.
|
|
|
|
So, we prefer to use the open/fchdir approach, but fall back on
|
|
|
|
getcwd if necessary. Some systems lack fchdir altogether: OS/2,
|
2003-07-23 14:24:12 +08:00
|
|
|
Cygwin (as of March 2003), SCO Xenix. At least SunOS 4 and Irix 5.3
|
2003-03-05 15:24:22 +08:00
|
|
|
provide the function, yet it doesn't work for partitions on which
|
|
|
|
auditing is enabled. */
|
1995-08-04 23:23:25 +08:00
|
|
|
|
|
|
|
int
|
1998-12-07 11:12:10 +08:00
|
|
|
save_cwd (struct saved_cwd *cwd)
|
1995-08-04 23:23:25 +08:00
|
|
|
{
|
|
|
|
static int have_working_fchdir = 1;
|
|
|
|
|
|
|
|
cwd->desc = -1;
|
|
|
|
cwd->name = NULL;
|
|
|
|
|
|
|
|
if (have_working_fchdir)
|
|
|
|
{
|
1998-03-15 19:09:10 +08:00
|
|
|
#if HAVE_FCHDIR
|
2000-10-22 14:57:42 +08:00
|
|
|
cwd->desc = open (".", O_RDONLY | O_DIRECTORY);
|
2004-05-12 02:34:38 +08:00
|
|
|
if (cwd->desc < 0)
|
|
|
|
cwd->desc = open (".", O_WRONLY | O_DIRECTORY);
|
1995-08-04 23:23:25 +08:00
|
|
|
if (cwd->desc < 0)
|
2004-05-12 00:42:17 +08:00
|
|
|
{
|
|
|
|
cwd->name = xgetcwd ();
|
|
|
|
return cwd->name == NULL;
|
|
|
|
}
|
1995-08-04 23:23:25 +08:00
|
|
|
|
|
|
|
# if __sun__ || sun
|
2003-03-05 05:27:34 +08:00
|
|
|
/* On SunOS 4 and IRIX 5.3, fchdir returns EINVAL when auditing
|
|
|
|
is enabled, so we have to fall back to chdir. */
|
1995-08-04 23:23:25 +08:00
|
|
|
if (fchdir (cwd->desc))
|
|
|
|
{
|
|
|
|
if (errno == EINVAL)
|
|
|
|
{
|
|
|
|
close (cwd->desc);
|
|
|
|
cwd->desc = -1;
|
|
|
|
have_working_fchdir = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-07-13 00:07:30 +08:00
|
|
|
int saved_errno = errno;
|
1995-08-04 23:23:25 +08:00
|
|
|
close (cwd->desc);
|
|
|
|
cwd->desc = -1;
|
2003-07-13 00:07:30 +08:00
|
|
|
errno = saved_errno;
|
1995-08-04 23:23:25 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif /* __sun__ || sun */
|
|
|
|
#else
|
1997-06-12 20:58:47 +08:00
|
|
|
# define fchdir(x) (abort (), 0)
|
1995-08-04 23:23:25 +08:00
|
|
|
have_working_fchdir = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_working_fchdir)
|
|
|
|
{
|
|
|
|
cwd->name = xgetcwd ();
|
|
|
|
if (cwd->name == NULL)
|
2003-07-13 00:07:30 +08:00
|
|
|
return 1;
|
1995-08-04 23:23:25 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change to recorded location, CWD, in directory hierarchy.
|
2003-03-05 05:27:34 +08:00
|
|
|
Upon failure, return nonzero (errno is set by chdir or fchdir).
|
|
|
|
Upon success, return zero. */
|
1995-08-04 23:23:25 +08:00
|
|
|
|
|
|
|
int
|
2003-03-05 05:27:34 +08:00
|
|
|
restore_cwd (const struct saved_cwd *cwd)
|
1995-08-04 23:23:25 +08:00
|
|
|
{
|
2003-03-05 05:27:34 +08:00
|
|
|
if (0 <= cwd->desc)
|
|
|
|
return fchdir (cwd->desc) < 0;
|
|
|
|
else
|
|
|
|
return chdir (cwd->name) < 0;
|
1995-08-04 23:23:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-07 11:12:10 +08:00
|
|
|
free_cwd (struct saved_cwd *cwd)
|
1995-08-04 23:23:25 +08:00
|
|
|
{
|
|
|
|
if (cwd->desc >= 0)
|
|
|
|
close (cwd->desc);
|
|
|
|
if (cwd->name)
|
|
|
|
free (cwd->name);
|
|
|
|
}
|