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