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