1 /* save-cwd.c -- Save and restore current working directory.
2 Copyright (C) 1995, 1997, 1998, 2003, 2004 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Written by Jim Meyering. */
37 # include <sys/file.h>
42 #include "chdir-long.h"
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.
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. */
63 save_cwd (struct saved_cwd
*cwd
)
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;
74 bool have_working_fchdir
= true;
75 bool fchdir_needs_testing
= false;
81 if (have_working_fchdir
)
83 cwd
->desc
= open (".", O_RDONLY
);
86 cwd
->desc
= open (".", O_WRONLY
);
89 cwd
->name
= xgetcwd ();
90 return cwd
->name
? 0 : -1;
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)
98 int saved_errno
= errno
;
101 if (saved_errno
!= EINVAL
)
106 have_working_fchdir
= false;
110 if (!have_working_fchdir
)
112 cwd
->name
= xgetcwd ();
113 if (cwd
->name
== NULL
)
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. */
124 restore_cwd (const struct saved_cwd
*cwd
)
127 return fchdir (cwd
->desc
);
129 return chdir_long (cwd
->name
);
133 free_cwd (struct saved_cwd
*cwd
)