1/* save-cwd.c -- Save and restore current working directory. 2 3 Copyright (C) 1995, 1997, 1998, 2003, 2004, 2005 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 2, or (at your option) 9 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, write to the Free Software Foundation, 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 19#include <sys/cdefs.h> 20__RCSID("$NetBSD: save-cwd.c,v 1.2 2016/05/17 14:00:09 christos Exp $"); 21 22 23/* Written by Jim Meyering. */ 24 25#ifdef HAVE_CONFIG_H 26# include <config.h> 27#endif 28 29#include "save-cwd.h" 30 31#include <stdbool.h> 32#include <stdio.h> 33#include <stdlib.h> 34 35#if HAVE_UNISTD_H 36# include <unistd.h> 37#endif 38 39#include <fcntl.h> 40 41#include <errno.h> 42 43#include "chdir-long.h" 44#include "unistd-safer.h" 45#include "xgetcwd.h" 46 47/* On systems without the fchdir function (WOE), pretend that open 48 always returns -1 so that save_cwd resorts to using xgetcwd. 49 Since chdir_long requires fchdir, use chdir instead. */ 50#if !HAVE_FCHDIR 51# undef open 52# define open(File, Flags) (-1) 53# undef fchdir 54# define fchdir(Fd) (abort (), -1) 55# undef chdir_long 56# define chdir_long(Dir) chdir (Dir) 57#endif 58 59/* Record the location of the current working directory in CWD so that 60 the program may change to other directories and later use restore_cwd 61 to return to the recorded location. This function may allocate 62 space using malloc (via xgetcwd) or leave a file descriptor open; 63 use free_cwd to perform the necessary free or close. Upon failure, 64 no memory is allocated, any locally opened file descriptors are 65 closed; return non-zero -- in that case, free_cwd need not be 66 called, but doing so is ok. Otherwise, return zero. 67 68 The `raison d'etre' for this interface is that the working directory 69 is sometimes inaccessible, and getcwd is not robust or as efficient. 70 So, we prefer to use the open/fchdir approach, but fall back on 71 getcwd if necessary. 72 73 Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin, 74 SCO Xenix. Also, SunOS 4 and Irix 5.3 provide the function, yet it 75 doesn't work for partitions on which auditing is enabled. If 76 you're still using an obsolete system with these problems, please 77 send email to the maintainer of this code. */ 78 79int 80save_cwd (struct saved_cwd *cwd) 81{ 82 cwd->name = NULL; 83 84 cwd->desc = fd_safer (open (".", O_RDONLY)); 85 if (cwd->desc < 0) 86 { 87 cwd->desc = fd_safer (open (".", O_WRONLY)); 88 if (cwd->desc < 0) 89 { 90 cwd->name = xgetcwd (); 91 return cwd->name ? 0 : -1; 92 } 93 } 94 95 return 0; 96} 97 98/* Change to recorded location, CWD, in directory hierarchy. 99 Upon failure, return -1 (errno is set by chdir or fchdir). 100 Upon success, return zero. */ 101 102int 103restore_cwd (const struct saved_cwd *cwd) 104{ 105 if (0 <= cwd->desc) 106 return fchdir (cwd->desc); 107 else 108 return chdir_long (cwd->name); 109} 110 111void 112free_cwd (struct saved_cwd *cwd) 113{ 114 if (cwd->desc >= 0) 115 close (cwd->desc); 116 if (cwd->name) 117 free (cwd->name); 118} 119