libefivar: Remove unneeded WARNS setting (lib/Makefile.inc has it).
[dragonfly.git] / contrib / cvs-1.12 / lib / save-cwd.c
blob8cfa419b9c977950027bfdcc6938dab3185ff3d2
1 /* save-cwd.c -- Save and restore current working directory.
3 Copyright (C) 1995, 1997, 1998, 2003, 2004, 2005 Free Software
4 Foundation, Inc.
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 2, or (at your option)
9 any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* Written by Jim Meyering. */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include "save-cwd.h"
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 #if HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #include <fcntl.h>
38 #include <errno.h>
40 #include "unistd-safer.h"
41 #include "xgetcwd.h"
43 /* On systems without the fchdir function (WOE), pretend that open
44 always returns -1 so that save_cwd resorts to using xgetcwd.
45 Since chdir_long requires fchdir, use chdir instead. */
46 #if !HAVE_FCHDIR
47 # undef open
48 # define open(File, Flags) (-1)
49 # undef fchdir
50 # define fchdir(Fd) (abort (), -1)
51 # undef chdir_long
52 # define chdir_long(Dir) chdir (Dir)
53 #endif
55 /* Record the location of the current working directory in CWD so that
56 the program may change to other directories and later use restore_cwd
57 to return to the recorded location. This function may allocate
58 space using malloc (via xgetcwd) or leave a file descriptor open;
59 use free_cwd to perform the necessary free or close. Upon failure,
60 no memory is allocated, any locally opened file descriptors are
61 closed; return non-zero -- in that case, free_cwd need not be
62 called, but doing so is ok. Otherwise, return zero.
64 The `raison d'etre' for this interface is that the working directory
65 is sometimes inaccessible, and getcwd is not robust or as efficient.
66 So, we prefer to use the open/fchdir approach, but fall back on
67 getcwd if necessary.
69 Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
70 SCO Xenix. Also, SunOS 4 and Irix 5.3 provide the function, yet it
71 doesn't work for partitions on which auditing is enabled. If
72 you're still using an obsolete system with these problems, please
73 send email to the maintainer of this code. */
75 int
76 save_cwd (struct saved_cwd *cwd)
78 cwd->name = NULL;
80 cwd->desc = fd_safer (open (".", O_RDONLY));
81 if (cwd->desc < 0)
83 cwd->desc = fd_safer (open (".", O_WRONLY));
84 if (cwd->desc < 0)
86 cwd->name = xgetcwd ();
87 return cwd->name ? 0 : -1;
91 return 0;
94 /* Change to recorded location, CWD, in directory hierarchy.
95 Upon failure, return -1 (errno is set by chdir or fchdir).
96 Upon success, return zero. */
98 int
99 restore_cwd (const struct saved_cwd *cwd)
101 if (0 <= cwd->desc)
102 return fchdir (cwd->desc);
103 else
104 return -1;
107 void
108 free_cwd (struct saved_cwd *cwd)
110 if (cwd->desc >= 0)
111 close (cwd->desc);
112 if (cwd->name)
113 free (cwd->name);