Sync from coreutils.
[gnulib.git] / lib / save-cwd.c
1 /* save-cwd.c -- Save and restore current working directory.
2
3    Copyright (C) 1995, 1997, 1998, 2003, 2004, 2005 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by Jim Meyering.  */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "save-cwd.h"
27
28 #include <errno.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include "chdir-long.h"
35 #include "fcntl--.h"
36 #include "xgetcwd.h"
37
38 /* On systems without the fchdir function (WOE), pretend that open
39    always returns -1 so that save_cwd resorts to using xgetcwd.
40    Since chdir_long requires fchdir, use chdir instead.  */
41 #if !HAVE_FCHDIR
42 # undef open
43 # define open(File, Flags) (-1)
44 # undef fchdir
45 # define fchdir(Fd) (abort (), -1)
46 # undef chdir_long
47 # define chdir_long(Dir) chdir (Dir)
48 #endif
49
50 /* Record the location of the current working directory in CWD so that
51    the program may change to other directories and later use restore_cwd
52    to return to the recorded location.  This function may allocate
53    space using malloc (via xgetcwd) or leave a file descriptor open;
54    use free_cwd to perform the necessary free or close.  Upon failure,
55    no memory is allocated, any locally opened file descriptors are
56    closed;  return non-zero -- in that case, free_cwd need not be
57    called, but doing so is ok.  Otherwise, return zero.
58
59    The `raison d'etre' for this interface is that the working directory
60    is sometimes inaccessible, and getcwd is not robust or as efficient.
61    So, we prefer to use the open/fchdir approach, but fall back on
62    getcwd if necessary.
63
64    Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
65    SCO Xenix.  Also, SunOS 4 and Irix 5.3 provide the function, yet it
66    doesn't work for partitions on which auditing is enabled.  If
67    you're still using an obsolete system with these problems, please
68    send email to the maintainer of this code.  */
69
70 int
71 save_cwd (struct saved_cwd *cwd)
72 {
73   cwd->name = NULL;
74
75   cwd->desc = open (".", O_RDONLY);
76   if (cwd->desc < 0)
77     {
78       cwd->desc = open (".", O_WRONLY);
79       if (cwd->desc < 0)
80         {
81           cwd->name = xgetcwd ();
82           return cwd->name ? 0 : -1;
83         }
84     }
85
86   return 0;
87 }
88
89 /* Change to recorded location, CWD, in directory hierarchy.
90    Upon failure, return -1 (errno is set by chdir or fchdir).
91    Upon success, return zero.  */
92
93 int
94 restore_cwd (const struct saved_cwd *cwd)
95 {
96   if (0 <= cwd->desc)
97     return fchdir (cwd->desc);
98   else
99     return chdir_long (cwd->name);
100 }
101
102 void
103 free_cwd (struct saved_cwd *cwd)
104 {
105   if (cwd->desc >= 0)
106     close (cwd->desc);
107   if (cwd->name)
108     free (cwd->name);
109 }