xref: /dflybsd-src/contrib/grep/lib/save-cwd.c (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
1cf28ed85SJohn Marino /* save-cwd.c -- Save and restore current working directory.
2cf28ed85SJohn Marino 
3*09d4459fSDaniel Fojt    Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2020 Free Software
4cf28ed85SJohn Marino    Foundation, Inc.
5cf28ed85SJohn Marino 
6cf28ed85SJohn Marino    This program is free software: you can redistribute it and/or modify
7cf28ed85SJohn Marino    it under the terms of the GNU General Public License as published by
8cf28ed85SJohn Marino    the Free Software Foundation; either version 3 of the License, or
9cf28ed85SJohn Marino    (at your option) any later version.
10cf28ed85SJohn Marino 
11cf28ed85SJohn Marino    This program is distributed in the hope that it will be useful,
12cf28ed85SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
13cf28ed85SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14cf28ed85SJohn Marino    GNU General Public License for more details.
15cf28ed85SJohn Marino 
16cf28ed85SJohn Marino    You should have received a copy of the GNU General Public License
17*09d4459fSDaniel Fojt    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
18cf28ed85SJohn Marino 
19cf28ed85SJohn Marino /* Written by Jim Meyering.  */
20cf28ed85SJohn Marino 
21cf28ed85SJohn Marino #include <config.h>
22cf28ed85SJohn Marino 
23cf28ed85SJohn Marino #include "save-cwd.h"
24cf28ed85SJohn Marino 
25cf28ed85SJohn Marino #include <errno.h>
26cf28ed85SJohn Marino #include <fcntl.h>
27cf28ed85SJohn Marino #include <stdbool.h>
28cf28ed85SJohn Marino #include <stdio.h>
29cf28ed85SJohn Marino #include <stdlib.h>
30cf28ed85SJohn Marino 
31cf28ed85SJohn Marino #include "chdir-long.h"
32cf28ed85SJohn Marino #include "unistd--.h"
33cf28ed85SJohn Marino 
34cf28ed85SJohn Marino #if GNULIB_FCNTL_SAFER
35cf28ed85SJohn Marino # include "fcntl--.h"
36cf28ed85SJohn Marino #else
37cf28ed85SJohn Marino # define GNULIB_FCNTL_SAFER 0
38cf28ed85SJohn Marino #endif
39cf28ed85SJohn Marino 
40cf28ed85SJohn Marino /* Record the location of the current working directory in CWD so that
41cf28ed85SJohn Marino    the program may change to other directories and later use restore_cwd
42cf28ed85SJohn Marino    to return to the recorded location.  This function may allocate
43cf28ed85SJohn Marino    space using malloc (via getcwd) or leave a file descriptor open;
44cf28ed85SJohn Marino    use free_cwd to perform the necessary free or close.  Upon failure,
45cf28ed85SJohn Marino    no memory is allocated, any locally opened file descriptors are
46cf28ed85SJohn Marino    closed;  return non-zero -- in that case, free_cwd need not be
47cf28ed85SJohn Marino    called, but doing so is ok.  Otherwise, return zero.
48cf28ed85SJohn Marino 
49cf28ed85SJohn Marino    The _raison d'etre_ for this interface is that the working directory
50cf28ed85SJohn Marino    is sometimes inaccessible, and getcwd is not robust or as efficient.
51cf28ed85SJohn Marino    So, we prefer to use the open/fchdir approach, but fall back on
52cf28ed85SJohn Marino    getcwd if necessary.  This module works for most cases with just
53cf28ed85SJohn Marino    the getcwd-lgpl module, but to be truly robust, use the getcwd module.
54cf28ed85SJohn Marino 
55cf28ed85SJohn Marino    Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
56cf28ed85SJohn Marino    SCO Xenix.  Also, SunOS 4 and Irix 5.3 provide the function, yet it
57cf28ed85SJohn Marino    doesn't work for partitions on which auditing is enabled.  If
58cf28ed85SJohn Marino    you're still using an obsolete system with these problems, please
59cf28ed85SJohn Marino    send email to the maintainer of this code.  */
60cf28ed85SJohn Marino 
61cf28ed85SJohn Marino int
save_cwd(struct saved_cwd * cwd)62cf28ed85SJohn Marino save_cwd (struct saved_cwd *cwd)
63cf28ed85SJohn Marino {
64cf28ed85SJohn Marino   cwd->name = NULL;
65cf28ed85SJohn Marino 
66*09d4459fSDaniel Fojt   cwd->desc = open (".", O_SEARCH | O_CLOEXEC);
67cf28ed85SJohn Marino   if (!GNULIB_FCNTL_SAFER)
68*09d4459fSDaniel Fojt     cwd->desc = fd_safer_flag (cwd->desc, O_CLOEXEC);
69cf28ed85SJohn Marino   if (cwd->desc < 0)
70cf28ed85SJohn Marino     {
71cf28ed85SJohn Marino       cwd->name = getcwd (NULL, 0);
72cf28ed85SJohn Marino       return cwd->name ? 0 : -1;
73cf28ed85SJohn Marino     }
74cf28ed85SJohn Marino 
75cf28ed85SJohn Marino   return 0;
76cf28ed85SJohn Marino }
77cf28ed85SJohn Marino 
78cf28ed85SJohn Marino /* Change to recorded location, CWD, in directory hierarchy.
79cf28ed85SJohn Marino    Upon failure, return -1 (errno is set by chdir or fchdir).
80cf28ed85SJohn Marino    Upon success, return zero.  */
81cf28ed85SJohn Marino 
82cf28ed85SJohn Marino int
restore_cwd(const struct saved_cwd * cwd)83cf28ed85SJohn Marino restore_cwd (const struct saved_cwd *cwd)
84cf28ed85SJohn Marino {
85cf28ed85SJohn Marino   if (0 <= cwd->desc)
86cf28ed85SJohn Marino     return fchdir (cwd->desc);
87cf28ed85SJohn Marino   else
88cf28ed85SJohn Marino     return chdir_long (cwd->name);
89cf28ed85SJohn Marino }
90cf28ed85SJohn Marino 
91cf28ed85SJohn Marino void
free_cwd(struct saved_cwd * cwd)92cf28ed85SJohn Marino free_cwd (struct saved_cwd *cwd)
93cf28ed85SJohn Marino {
94cf28ed85SJohn Marino   if (cwd->desc >= 0)
95cf28ed85SJohn Marino     close (cwd->desc);
96cf28ed85SJohn Marino   free (cwd->name);
97cf28ed85SJohn Marino }
98