Merge from coreutils.
[gnulib.git] / lib / save-cwd.c
1 /* save-cwd.c -- Save and restore current working directory.
2
3    Copyright (C) 1995, 1997, 1998, 2003, 2004, 2005 Free Software
4    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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Written by Jim Meyering.  */
21
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include "save-cwd.h"
27
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #if HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35
36 #if HAVE_FCNTL_H
37 # include <fcntl.h>
38 #else
39 # include <sys/file.h>
40 #endif
41
42 #include <errno.h>
43
44 #include "chdir-long.h"
45 #include "unistd-safer.h"
46 #include "xgetcwd.h"
47
48 /* On systems without the fchdir function (WOE), pretend that open
49    always returns -1 so that save_cwd resorts to using xgetcwd.
50    Since chdir_long requires fchdir, use chdir instead.  */
51 #if !HAVE_FCHDIR
52 # undef open
53 # define open(File, Flags) (-1)
54 # undef fchdir
55 # define fchdir(Fd) (abort (), -1)
56 # undef chdir_long
57 # define chdir_long(Dir) chdir (Dir)
58 #endif
59
60 /* Record the location of the current working directory in CWD so that
61    the program may change to other directories and later use restore_cwd
62    to return to the recorded location.  This function may allocate
63    space using malloc (via xgetcwd) or leave a file descriptor open;
64    use free_cwd to perform the necessary free or close.  Upon failure,
65    no memory is allocated, any locally opened file descriptors are
66    closed;  return non-zero -- in that case, free_cwd need not be
67    called, but doing so is ok.  Otherwise, return zero.
68
69    The `raison d'etre' for this interface is that the working directory
70    is sometimes inaccessible, and getcwd is not robust or as efficient.
71    So, we prefer to use the open/fchdir approach, but fall back on
72    getcwd if necessary.
73
74    Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
75    SCO Xenix.  Also, SunOS 4 and Irix 5.3 provide the function, yet it
76    doesn't work for partitions on which auditing is enabled.  If
77    you're still using an obsolete system with these problems, please
78    send email to the maintainer of this code.  */
79
80 int
81 save_cwd (struct saved_cwd *cwd)
82 {
83   cwd->name = NULL;
84
85   cwd->desc = fd_safer (open (".", O_RDONLY));
86   if (cwd->desc < 0)
87     {
88       cwd->desc = fd_safer (open (".", O_WRONLY));
89       if (cwd->desc < 0)
90         {
91           cwd->name = xgetcwd ();
92           return cwd->name ? 0 : -1;
93         }
94     }
95
96   return 0;
97 }
98
99 /* Change to recorded location, CWD, in directory hierarchy.
100    Upon failure, return -1 (errno is set by chdir or fchdir).
101    Upon success, return zero.  */
102
103 int
104 restore_cwd (const struct saved_cwd *cwd)
105 {
106   if (0 <= cwd->desc)
107     return fchdir (cwd->desc);
108   else
109     return chdir_long (cwd->name);
110 }
111
112 void
113 free_cwd (struct saved_cwd *cwd)
114 {
115   if (cwd->desc >= 0)
116     close (cwd->desc);
117   if (cwd->name)
118     free (cwd->name);
119 }