aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorGlenn Morris2014-08-10 17:59:34 -0700
committerGlenn Morris2014-08-10 17:59:34 -0700
commitc7367d2de3343e56171c4fe6d439a3ed5f40d06c (patch)
tree54fe9166f6b4320d9518bbac11e2d575a9fda7c2 /lib
parent6b7d077506304f440d311fa7b29d210b7a3e121c (diff)
parentf314e84fce8b394da20aa1d69121c74fb34f9a1e (diff)
downloademacs-c7367d2de3343e56171c4fe6d439a3ed5f40d06c.tar.gz
emacs-c7367d2de3343e56171c4fe6d439a3ed5f40d06c.zip
Merge from emacs-24; up to 2014-06-28T23:35:17Z!rgm@gnu.org
Diffstat (limited to 'lib')
-rw-r--r--lib/save-cwd.c88
-rw-r--r--lib/save-cwd.h40
2 files changed, 101 insertions, 27 deletions
diff --git a/lib/save-cwd.c b/lib/save-cwd.c
index b8dae34ca02..833783cbab0 100644
--- a/lib/save-cwd.c
+++ b/lib/save-cwd.c
@@ -1,3 +1,89 @@
1/* save-cwd.c -- Save and restore current working directory.
2
3 Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2014 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/* Gnulib needs to save and restore the current working directory to
20 fully emulate functions like fstatat. But Emacs doesn't care what
21 the current working directory is; it always uses absolute file
22 names. This module replaces the Gnulib module by omitting the code
23 that Emacs does not need. */
24
1#include <config.h> 25#include <config.h>
2#define SAVE_CWD_INLINE _GL_EXTERN_INLINE 26
3#include "save-cwd.h" 27#include "save-cwd.h"
28
29#include <fcntl.h>
30#include <unistd.h>
31
32/* Record the location of the current working directory in CWD so that
33 the program may change to other directories and later use restore_cwd
34 to return to the recorded location. This function may allocate
35 space using malloc (via getcwd) or leave a file descriptor open;
36 use free_cwd to perform the necessary free or close. Upon failure,
37 no memory is allocated, any locally opened file descriptors are
38 closed; return non-zero -- in that case, free_cwd need not be
39 called, but doing so is ok. Otherwise, return zero.
40
41 The _raison d'etre_ for this interface is that the working directory
42 is sometimes inaccessible, and getcwd is not robust or as efficient.
43 So, we prefer to use the open/fchdir approach, but fall back on
44 getcwd if necessary. This module works for most cases with just
45 the getcwd-lgpl module, but to be truly robust, use the getcwd module.
46
47 Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
48 SCO Xenix. Also, SunOS 4 and Irix 5.3 provide the function, yet it
49 doesn't work for partitions on which auditing is enabled. If
50 you're still using an obsolete system with these problems, please
51 send email to the maintainer of this code. */
52
53#if !defined HAVE_FCHDIR && !defined fchdir
54# define fchdir(fd) (-1)
55#endif
56
57int
58save_cwd (struct saved_cwd *cwd)
59{
60 cwd->desc = open (".", O_SEARCH | O_CLOEXEC);
61 /* The 'name' member is present only to minimize differences from
62 gnulib. Initialize it to zero, if only to simplify debugging. */
63 cwd->name = 0;
64 return 0;
65}
66
67/* Change to recorded location, CWD, in directory hierarchy.
68 Upon failure, return -1 (errno is set by chdir or fchdir).
69 Upon success, return zero. */
70
71int
72restore_cwd (const struct saved_cwd *cwd)
73{
74 /* Restore the previous directory if possible, to avoid tying down
75 the file system of the new directory (Bug#18232).
76 Don't worry if fchdir fails, as Emacs doesn't care what the
77 working directory is. The fchdir call is inside an 'if' merely to
78 pacify compilers that complain if fchdir's return value is ignored. */
79 if (fchdir (cwd->desc) == 0)
80 return 0;
81
82 return 0;
83}
84
85void
86free_cwd (struct saved_cwd *cwd)
87{
88 close (cwd->desc);
89}
diff --git a/lib/save-cwd.h b/lib/save-cwd.h
index 9a1eb3519c8..6b84e4601d3 100644
--- a/lib/save-cwd.h
+++ b/lib/save-cwd.h
@@ -1,6 +1,7 @@
1/* Do not save and restore the current working directory. 1/* Save and restore current working directory.
2 2
3 Copyright 2013-2014 Free Software Foundation, Inc. 3 Copyright (C) 1995, 1997-1998, 2003, 2009-2014 Free Software Foundation,
4 Inc.
4 5
5 This program is free software: you can redistribute it and/or modify 6 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 7 it under the terms of the GNU General Public License as published by
@@ -15,32 +16,19 @@
15 You should have received a copy of the GNU General Public License 16 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 18
18/* Gnulib needs to save and restore the current working directory to 19/* Written by Jim Meyering. */
19 fully emulate functions like fstatat. But Emacs doesn't care what
20 the current working directory is; it always uses absolute file
21 names. This module replaces the Gnulib module by omitting the code
22 that Emacs does not need. */
23 20
24#ifndef SAVE_CWD_H 21#ifndef SAVE_CWD_H
25#define SAVE_CWD_H 1 22# define SAVE_CWD_H 1
26 23
27_GL_INLINE_HEADER_BEGIN 24struct saved_cwd
28#ifndef SAVE_CWD_INLINE 25 {
29# define SAVE_CWD_INLINE _GL_INLINE 26 int desc;
30#endif 27 char *name;
28 };
31 29
32struct saved_cwd { int desc; }; 30int save_cwd (struct saved_cwd *cwd);
31int restore_cwd (const struct saved_cwd *cwd);
32void free_cwd (struct saved_cwd *cwd);
33 33
34SAVE_CWD_INLINE int 34#endif /* SAVE_CWD_H */
35save_cwd (struct saved_cwd *cwd)
36{
37 cwd->desc = -1;
38 return 0;
39}
40
41SAVE_CWD_INLINE int restore_cwd (struct saved_cwd const *cwd) { return 0; }
42SAVE_CWD_INLINE void free_cwd (struct saved_cwd *cwd) { }
43
44_GL_INLINE_HEADER_END
45
46#endif