| 1 |
/* save-cwd.c -- Save and restore current working directory. |
| 2 |
|
| 3 |
Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2023 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 <https://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 <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
|
| 30 |
#include "chdir-long.h" |
| 31 |
#include "unistd--.h" |
| 32 |
|
| 33 |
#if GNULIB_FCNTL_SAFER |
| 34 |
# include "fcntl--.h" |
| 35 |
#else |
| 36 |
# define GNULIB_FCNTL_SAFER 0 |
| 37 |
#endif |
| 38 |
|
| 39 |
/* Record the location of the current working directory in CWD so that |
| 40 |
the program may change to other directories and later use restore_cwd |
| 41 |
to return to the recorded location. This function may allocate |
| 42 |
space using malloc (via getcwd) or leave a file descriptor open; |
| 43 |
use free_cwd to perform the necessary free or close. Upon failure, |
| 44 |
no memory is allocated, any locally opened file descriptors are |
| 45 |
closed; return non-zero -- in that case, free_cwd need not be |
| 46 |
called, but doing so is ok. Otherwise, return zero. |
| 47 |
|
| 48 |
The _raison d'etre_ for this interface is that the working directory |
| 49 |
is sometimes inaccessible, and getcwd is not robust or as efficient. |
| 50 |
So, we prefer to use the open/fchdir approach, but fall back on |
| 51 |
getcwd if necessary. This module works for most cases with just |
| 52 |
the getcwd-lgpl module, but to be truly robust, use the getcwd module. |
| 53 |
|
| 54 |
Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin, |
| 55 |
SCO Xenix. Also, SunOS 4 and Irix 5.3 provide the function, yet it |
| 56 |
doesn't work for partitions on which auditing is enabled. If |
| 57 |
you're still using an obsolete system with these problems, please |
| 58 |
send email to the maintainer of this code. */ |
| 59 |
|
| 60 |
int |
| 61 |
save_cwd (struct saved_cwd *cwd) |
| 62 |
{ |
| 63 |
cwd->name = NULL; |
| 64 |
|
| 65 |
cwd->desc = open (".", O_SEARCH | O_CLOEXEC); |
| 66 |
if (!GNULIB_FCNTL_SAFER) |
| 67 |
cwd->desc = fd_safer_flag (cwd->desc, O_CLOEXEC); |
| 68 |
if (cwd->desc < 0) |
| 69 |
{ |
| 70 |
cwd->name = getcwd (NULL, 0); |
| 71 |
return cwd->name ? 0 : -1; |
| 72 |
} |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
/* Change to recorded location, CWD, in directory hierarchy. |
| 78 |
Upon failure, return -1 (errno is set by chdir or fchdir). |
| 79 |
Upon success, return zero. */ |
| 80 |
|
| 81 |
int |
| 82 |
restore_cwd (const struct saved_cwd *cwd) |
| 83 |
{ |
| 84 |
if (0 <= cwd->desc) |
| 85 |
return fchdir (cwd->desc); |
| 86 |
else |
| 87 |
return chdir_long (cwd->name); |
| 88 |
} |
| 89 |
|
| 90 |
void |
| 91 |
free_cwd (struct saved_cwd *cwd) |
| 92 |
{ |
| 93 |
if (cwd->desc >= 0) |
| 94 |
close (cwd->desc); |
| 95 |
free (cwd->name); |
| 96 |
} |