Rename module 'memxfrm' to 'amemxfrm'.
[gnulib.git] / lib / save-cwd.c
1 /* save-cwd.c -- Save and restore current working directory.
2
3    Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2010 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 3 of the License, or
9    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
18
19 /* Written by Jim Meyering.  */
20
21 #include <config.h>
22
23 #include "save-cwd.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "chdir-long.h"
32 #include "unistd--.h"
33 #include "xgetcwd.h"
34 #include "cloexec.h"
35
36 #if GNULIB_FCNTL_SAFER
37 # include "fcntl--.h"
38 #else
39 # define GNULIB_FCNTL_SAFER 0
40 #endif
41
42 /* On systems without the fchdir function (WOE), pretend that open
43    always returns -1 so that save_cwd resorts to using xgetcwd.
44    Since chdir_long requires fchdir, use chdir instead.  */
45 #if !HAVE_FCHDIR
46 # undef open
47 # define open(File, Flags) (-1)
48 # undef fchdir
49 # define fchdir(Fd) (abort (), -1)
50 # undef chdir_long
51 # define chdir_long(Dir) chdir (Dir)
52 #endif
53
54 /* Record the location of the current working directory in CWD so that
55    the program may change to other directories and later use restore_cwd
56    to return to the recorded location.  This function may allocate
57    space using malloc (via xgetcwd) or leave a file descriptor open;
58    use free_cwd to perform the necessary free or close.  Upon failure,
59    no memory is allocated, any locally opened file descriptors are
60    closed;  return non-zero -- in that case, free_cwd need not be
61    called, but doing so is ok.  Otherwise, return zero.
62
63    The `raison d'etre' for this interface is that the working directory
64    is sometimes inaccessible, and getcwd is not robust or as efficient.
65    So, we prefer to use the open/fchdir approach, but fall back on
66    getcwd if necessary.
67
68    Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
69    SCO Xenix.  Also, SunOS 4 and Irix 5.3 provide the function, yet it
70    doesn't work for partitions on which auditing is enabled.  If
71    you're still using an obsolete system with these problems, please
72    send email to the maintainer of this code.  */
73
74 int
75 save_cwd (struct saved_cwd *cwd)
76 {
77   cwd->name = NULL;
78
79   cwd->desc = open (".", O_RDONLY);
80   if (!GNULIB_FCNTL_SAFER)
81     cwd->desc = fd_safer (cwd->desc);
82   if (cwd->desc < 0)
83     {
84       cwd->name = xgetcwd ();
85       return cwd->name ? 0 : -1;
86     }
87
88   set_cloexec_flag (cwd->desc, true);
89   return 0;
90 }
91
92 /* Change to recorded location, CWD, in directory hierarchy.
93    Upon failure, return -1 (errno is set by chdir or fchdir).
94    Upon success, return zero.  */
95
96 int
97 restore_cwd (const struct saved_cwd *cwd)
98 {
99   if (0 <= cwd->desc)
100     return fchdir (cwd->desc);
101   else
102     return chdir_long (cwd->name);
103 }
104
105 void
106 free_cwd (struct saved_cwd *cwd)
107 {
108   if (cwd->desc >= 0)
109     close (cwd->desc);
110   free (cwd->name);
111 }