Remove K&R cruft.
[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 #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         return 1;
80
81 # if __sun__ || sun
82       /* On SunOS 4 and IRIX 5.3, fchdir returns EINVAL when auditing
83          is enabled, so we have to fall back to chdir.  */
84       if (fchdir (cwd->desc))
85         {
86           if (errno == EINVAL)
87             {
88               close (cwd->desc);
89               cwd->desc = -1;
90               have_working_fchdir = 0;
91             }
92           else
93             {
94               int saved_errno = errno;
95               close (cwd->desc);
96               cwd->desc = -1;
97               errno = saved_errno;
98               return 1;
99             }
100         }
101 # endif /* __sun__ || sun */
102 #else
103 # define fchdir(x) (abort (), 0)
104       have_working_fchdir = 0;
105 #endif
106     }
107
108   if (!have_working_fchdir)
109     {
110       cwd->name = xgetcwd ();
111       if (cwd->name == NULL)
112         return 1;
113     }
114   return 0;
115 }
116
117 /* Change to recorded location, CWD, in directory hierarchy.
118    Upon failure, return nonzero (errno is set by chdir or fchdir).
119    Upon success, return zero.  */
120
121 int
122 restore_cwd (const struct saved_cwd *cwd)
123 {
124   if (0 <= cwd->desc)
125     return fchdir (cwd->desc) < 0;
126   else
127     return chdir (cwd->name) < 0;
128 }
129
130 void
131 free_cwd (struct saved_cwd *cwd)
132 {
133   if (cwd->desc >= 0)
134     close (cwd->desc);
135   if (cwd->name)
136     free (cwd->name);
137 }