1/* save-cwd.c -- Save and restore current working directory.
2
3   Copyright (C) 1995, 1997, 1998, 2003, 2004, 2005, 2006 Free
4   Software Foundation, Inc.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20/* Written by Jim Meyering.  */
21
22#include <config.h>
23
24#include "save-cwd.h"
25
26#include <errno.h>
27#include <stdbool.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31
32#include "chdir-long.h"
33#include "fcntl--.h"
34#include "xgetcwd.h"
35
36/* On systems without the fchdir function (WOE), pretend that open
37   always returns -1 so that save_cwd resorts to using xgetcwd.
38   Since chdir_long requires fchdir, use chdir instead.  */
39#if !HAVE_FCHDIR
40# undef open
41# define open(File, Flags) (-1)
42# undef fchdir
43# define fchdir(Fd) (abort (), -1)
44# undef chdir_long
45# define chdir_long(Dir) chdir (Dir)
46#endif
47
48/* Record the location of the current working directory in CWD so that
49   the program may change to other directories and later use restore_cwd
50   to return to the recorded location.  This function may allocate
51   space using malloc (via xgetcwd) or leave a file descriptor open;
52   use free_cwd to perform the necessary free or close.  Upon failure,
53   no memory is allocated, any locally opened file descriptors are
54   closed;  return non-zero -- in that case, free_cwd need not be
55   called, but doing so is ok.  Otherwise, return zero.
56
57   The `raison d'etre' for this interface is that the working directory
58   is sometimes inaccessible, and getcwd is not robust or as efficient.
59   So, we prefer to use the open/fchdir approach, but fall back on
60   getcwd if necessary.
61
62   Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
63   SCO Xenix.  Also, SunOS 4 and Irix 5.3 provide the function, yet it
64   doesn't work for partitions on which auditing is enabled.  If
65   you're still using an obsolete system with these problems, please
66   send email to the maintainer of this code.  */
67
68int
69save_cwd (struct saved_cwd *cwd)
70{
71  cwd->name = NULL;
72
73  cwd->desc = open (".", O_RDONLY);
74  if (cwd->desc < 0)
75    {
76      cwd->name = xgetcwd ();
77      return cwd->name ? 0 : -1;
78    }
79
80  return 0;
81}
82
83/* Change to recorded location, CWD, in directory hierarchy.
84   Upon failure, return -1 (errno is set by chdir or fchdir).
85   Upon success, return zero.  */
86
87int
88restore_cwd (const struct saved_cwd *cwd)
89{
90  if (0 <= cwd->desc)
91    return fchdir (cwd->desc);
92  else
93    return chdir_long (cwd->name);
94}
95
96void
97free_cwd (struct saved_cwd *cwd)
98{
99  if (cwd->desc >= 0)
100    close (cwd->desc);
101  if (cwd->name)
102    free (cwd->name);
103}
104