Merge from coreutils.
[gnulib.git] / lib / save-cwd.c
1 /* save-cwd.c -- Save and restore current working directory.
2    Copyright (C) 1995, 1997, 1998, 2003, 2004 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Jim Meyering.  */
19
20 #if HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #if HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31
32 #if HAVE_FCNTL_H
33 # include <fcntl.h>
34 #else
35 # include <sys/file.h>
36 #endif
37
38 #include <errno.h>
39
40 #ifndef O_DIRECTORY
41 # define O_DIRECTORY 0
42 #endif
43
44 #include "save-cwd.h"
45 #include "xgetcwd.h"
46
47 /* Record the location of the current working directory in CWD so that
48    the program may change to other directories and later use restore_cwd
49    to return to the recorded location.  This function may allocate
50    space using malloc (via xgetcwd) or leave a file descriptor open;
51    use free_cwd to perform the necessary free or close.  Upon failure,
52    no memory is allocated, any locally opened file descriptors are
53    closed;  return non-zero -- in that case, free_cwd need not be
54    called, but doing so is ok.  Otherwise, return zero.
55
56    The `raison d'etre' for this interface is that some systems lack
57    support for fchdir, and getcwd is not robust or as efficient.
58    So, we prefer to use the open/fchdir approach, but fall back on
59    getcwd if necessary.  Some systems lack fchdir altogether: OS/2,
60    Cygwin (as of March 2003), SCO Xenix.  At least SunOS 4 and Irix 5.3
61    provide the function, yet it doesn't work for partitions on which
62    auditing is enabled.  */
63
64 int
65 save_cwd (struct saved_cwd *cwd)
66 {
67   static bool have_working_fchdir = true;
68
69   cwd->desc = -1;
70   cwd->name = NULL;
71
72   if (have_working_fchdir)
73     {
74 #if HAVE_FCHDIR
75       cwd->desc = open (".", O_RDONLY | O_DIRECTORY);
76       if (cwd->desc < 0)
77         cwd->desc = open (".", O_WRONLY | O_DIRECTORY);
78       if (cwd->desc < 0)
79         {
80           cwd->name = xgetcwd ();
81           return cwd->name ? 0 : -1;
82         }
83
84 # if __sun__ || sun
85       /* On SunOS 4 and IRIX 5.3, fchdir returns EINVAL when auditing
86          is enabled, so we have to fall back to chdir.  */
87       if (fchdir (cwd->desc))
88         {
89           if (errno == EINVAL)
90             {
91               close (cwd->desc);
92               cwd->desc = -1;
93               have_working_fchdir = false;
94             }
95           else
96             {
97               int saved_errno = errno;
98               close (cwd->desc);
99               cwd->desc = -1;
100               errno = saved_errno;
101               return -1;
102             }
103         }
104 # endif /* __sun__ || sun */
105 #else
106 # define fchdir(x) (abort (), 0)
107       have_working_fchdir = false;
108 #endif
109     }
110
111   if (!have_working_fchdir)
112     {
113       cwd->name = xgetcwd ();
114       if (cwd->name == NULL)
115         return -1;
116     }
117   return 0;
118 }
119
120 /* Change to recorded location, CWD, in directory hierarchy.
121    Upon failure, return -1 (errno is set by chdir or fchdir).
122    Upon success, return zero.  */
123
124 int
125 restore_cwd (const struct saved_cwd *cwd)
126 {
127   if (0 <= cwd->desc)
128     return fchdir (cwd->desc);
129   else
130     return chdir (cwd->name);
131 }
132
133 void
134 free_cwd (struct saved_cwd *cwd)
135 {
136   if (cwd->desc >= 0)
137     close (cwd->desc);
138   if (cwd->name)
139     free (cwd->name);
140 }