Don't emit diagnostics. Let callers do that.
[gnulib.git] / lib / save-cwd.c
1 /* save-cwd.c -- Save and restore current working directory.
2    Copyright (C) 1995, 1997, 1998, 2003 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
26 #ifdef STDC_HEADERS
27 # include <stdlib.h>
28 #endif
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 #ifndef errno
42 extern int errno;
43 #endif
44
45 #ifndef O_DIRECTORY
46 # define O_DIRECTORY 0
47 #endif
48
49 #include "save-cwd.h"
50 #include "xgetcwd.h"
51
52 /* Record the location of the current working directory in CWD so that
53    the program may change to other directories and later use restore_cwd
54    to return to the recorded location.  This function may allocate
55    space using malloc (via xgetcwd) or leave a file descriptor open;
56    use free_cwd to perform the necessary free or close.  Upon failure,
57    no memory is allocated, any locally opened file descriptors are
58    closed;  return non-zero -- in that case, free_cwd need not be
59    called, but doing so is ok.  Otherwise, return zero.
60
61    The `raison d'etre' for this interface is that some systems lack
62    support for fchdir, and getcwd is not robust or as efficient.
63    So, we prefer to use the open/fchdir approach, but fall back on
64    getcwd if necessary.  Some systems lack fchdir altogether: OS/2,
65    Cygwin (as of March 2003), SCO Xenix.  At least SunOS4 and Irix 5.3
66    provide the function, yet it doesn't work for partitions on which
67    auditing is enabled.  */
68
69 int
70 save_cwd (struct saved_cwd *cwd)
71 {
72   static int have_working_fchdir = 1;
73
74   cwd->desc = -1;
75   cwd->name = NULL;
76
77   if (have_working_fchdir)
78     {
79 #if HAVE_FCHDIR
80       cwd->desc = open (".", O_RDONLY | O_DIRECTORY);
81       if (cwd->desc < 0)
82         return 1;
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 = 0;
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 = 0;
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 nonzero (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) < 0;
129   else
130     return chdir (cwd->name) < 0;
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 }