X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fsave-cwd.c;h=5f8eb7ca53e12b09a50a3f4885d4a6816c2a6f2d;hb=64ee293cdac319d0f38cd0d31e758a823b7163ad;hp=e158e8b6b2c1dd021f367a803952ba9667d513fb;hpb=7dd49d2811431dceac40473f3d65d13f6552cce4;p=gnulib.git diff --git a/lib/save-cwd.c b/lib/save-cwd.c index e158e8b6b..5f8eb7ca5 100644 --- a/lib/save-cwd.c +++ b/lib/save-cwd.c @@ -1,7 +1,7 @@ /* save-cwd.c -- Save and restore current working directory. - Copyright (C) 1995, 1997, 1998, 2003, 2004, 2005, 2006 Free - Software Foundation, Inc. + Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2011 Free Software + Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,31 +23,25 @@ #include "save-cwd.h" #include +#include #include #include #include -#include #include "chdir-long.h" -#include "fcntl--.h" -#include "xgetcwd.h" - -/* On systems without the fchdir function (WOE), pretend that open - always returns -1 so that save_cwd resorts to using xgetcwd. - Since chdir_long requires fchdir, use chdir instead. */ -#if !HAVE_FCHDIR -# undef open -# define open(File, Flags) (-1) -# undef fchdir -# define fchdir(Fd) (abort (), -1) -# undef chdir_long -# define chdir_long(Dir) chdir (Dir) +#include "unistd--.h" +#include "cloexec.h" + +#if GNULIB_FCNTL_SAFER +# include "fcntl--.h" +#else +# define GNULIB_FCNTL_SAFER 0 #endif /* Record the location of the current working directory in CWD so that the program may change to other directories and later use restore_cwd to return to the recorded location. This function may allocate - space using malloc (via xgetcwd) or leave a file descriptor open; + space using malloc (via getcwd) or leave a file descriptor open; use free_cwd to perform the necessary free or close. Upon failure, no memory is allocated, any locally opened file descriptors are closed; return non-zero -- in that case, free_cwd need not be @@ -56,7 +50,8 @@ The `raison d'etre' for this interface is that the working directory is sometimes inaccessible, and getcwd is not robust or as efficient. So, we prefer to use the open/fchdir approach, but fall back on - getcwd if necessary. + getcwd if necessary. This module works for most cases with just + the getcwd-lgpl module, but to be truly robust, use the getcwd module. Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin, SCO Xenix. Also, SunOS 4 and Irix 5.3 provide the function, yet it @@ -69,13 +64,16 @@ save_cwd (struct saved_cwd *cwd) { cwd->name = NULL; - cwd->desc = open (".", O_RDONLY); + cwd->desc = open (".", O_SEARCH); + if (!GNULIB_FCNTL_SAFER) + cwd->desc = fd_safer (cwd->desc); if (cwd->desc < 0) { - cwd->name = xgetcwd (); + cwd->name = getcwd (NULL, 0); return cwd->name ? 0 : -1; } + set_cloexec_flag (cwd->desc, true); return 0; }