17d077db890d0b766d9d045f74c53d045c94b36d
[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 <stdio.h>
25 #include <stdlib.h>
26
27 #if HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30
31 #if HAVE_FCNTL_H
32 # include <fcntl.h>
33 #else
34 # include <sys/file.h>
35 #endif
36
37 #include <errno.h>
38 #ifndef errno
39 extern int errno;
40 #endif
41
42 #ifndef O_DIRECTORY
43 # define O_DIRECTORY 0
44 #endif
45
46 #include "save-cwd.h"
47 #include "xgetcwd.h"
48
49 /* Record the location of the current working directory in CWD so that
50    the program may change to other directories and later use restore_cwd
51    to return to the recorded location.  This function may allocate
52    space using malloc (via xgetcwd) or leave a file descriptor open;
53    use free_cwd to perform the necessary free or close.  Upon failure,
54    no memory is allocated, any locally opened file descriptors are
55    closed;  return non-zero -- in that case, free_cwd need not be
56    called, but doing so is ok.  Otherwise, return zero.
57
58    The `raison d'etre' for this interface is that some systems lack
59    support for fchdir, and getcwd is not robust or as efficient.
60    So, we prefer to use the open/fchdir approach, but fall back on
61    getcwd if necessary.  Some systems lack fchdir altogether: OS/2,
62    Cygwin (as of March 2003), SCO Xenix.  At least SunOS 4 and Irix 5.3
63    provide the function, yet it doesn't work for partitions on which
64    auditing is enabled.  */
65
66 int
67 save_cwd (struct saved_cwd *cwd)
68 {
69   static int have_working_fchdir = 1;
70
71   cwd->desc = -1;
72   cwd->name = NULL;
73
74   if (have_working_fchdir)
75     {
76 #if HAVE_FCHDIR
77       cwd->desc = open (".", O_RDONLY | O_DIRECTORY);
78       if (cwd->desc < 0)
79         cwd->desc = open (".", O_WRONLY | O_DIRECTORY);
80       if (cwd->desc < 0)
81         {
82           cwd->name = xgetcwd ();
83           return cwd->name == NULL;
84         }
85
86 # if __sun__ || sun
87       /* On SunOS 4 and IRIX 5.3, fchdir returns EINVAL when auditing
88          is enabled, so we have to fall back to chdir.  */
89       if (fchdir (cwd->desc))
90         {
91           if (errno == EINVAL)
92             {
93               close (cwd->desc);
94               cwd->desc = -1;
95               have_working_fchdir = 0;
96             }
97           else
98             {
99               int saved_errno = errno;
100               close (cwd->desc);
101               cwd->desc = -1;
102               errno = saved_errno;
103               return 1;
104             }
105         }
106 # endif /* __sun__ || sun */
107 #else
108 # define fchdir(x) (abort (), 0)
109       have_working_fchdir = 0;
110 #endif
111     }
112
113   if (!have_working_fchdir)
114     {
115       cwd->name = xgetcwd ();
116       if (cwd->name == NULL)
117         return 1;
118     }
119   return 0;
120 }
121
122 /* Change to recorded location, CWD, in directory hierarchy.
123    Upon failure, return nonzero (errno is set by chdir or fchdir).
124    Upon success, return zero.  */
125
126 int
127 restore_cwd (const struct saved_cwd *cwd)
128 {
129   if (0 <= cwd->desc)
130     return fchdir (cwd->desc) < 0;
131   else
132     return chdir (cwd->name) < 0;
133 }
134
135 void
136 free_cwd (struct saved_cwd *cwd)
137 {
138   if (cwd->desc >= 0)
139     close (cwd->desc);
140   if (cwd->name)
141     free (cwd->name);
142 }