save-cwd.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* save-cwd.c -- Save and restore current working directory.
  2. Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2021 Free Software
  3. Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. /* Written by Jim Meyering. */
  15. #include <config.h>
  16. #include "save-cwd.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "chdir-long.h"
  23. #include "unistd--.h"
  24. #if GNULIB_FCNTL_SAFER
  25. # include "fcntl--.h"
  26. #else
  27. # define GNULIB_FCNTL_SAFER 0
  28. #endif
  29. /* Record the location of the current working directory in CWD so that
  30. the program may change to other directories and later use restore_cwd
  31. to return to the recorded location. This function may allocate
  32. space using malloc (via getcwd) or leave a file descriptor open;
  33. use free_cwd to perform the necessary free or close. Upon failure,
  34. no memory is allocated, any locally opened file descriptors are
  35. closed; return non-zero -- in that case, free_cwd need not be
  36. called, but doing so is ok. Otherwise, return zero.
  37. The _raison d'etre_ for this interface is that the working directory
  38. is sometimes inaccessible, and getcwd is not robust or as efficient.
  39. So, we prefer to use the open/fchdir approach, but fall back on
  40. getcwd if necessary. This module works for most cases with just
  41. the getcwd-lgpl module, but to be truly robust, use the getcwd module.
  42. Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
  43. SCO Xenix. Also, SunOS 4 and Irix 5.3 provide the function, yet it
  44. doesn't work for partitions on which auditing is enabled. If
  45. you're still using an obsolete system with these problems, please
  46. send email to the maintainer of this code. */
  47. int
  48. save_cwd (struct saved_cwd *cwd)
  49. {
  50. cwd->name = NULL;
  51. cwd->desc = open (".", O_SEARCH | O_CLOEXEC);
  52. if (!GNULIB_FCNTL_SAFER)
  53. cwd->desc = fd_safer_flag (cwd->desc, O_CLOEXEC);
  54. if (cwd->desc < 0)
  55. {
  56. cwd->name = getcwd (NULL, 0);
  57. return cwd->name ? 0 : -1;
  58. }
  59. return 0;
  60. }
  61. /* Change to recorded location, CWD, in directory hierarchy.
  62. Upon failure, return -1 (errno is set by chdir or fchdir).
  63. Upon success, return zero. */
  64. int
  65. restore_cwd (const struct saved_cwd *cwd)
  66. {
  67. if (0 <= cwd->desc)
  68. return fchdir (cwd->desc);
  69. else
  70. return chdir_long (cwd->name);
  71. }
  72. void
  73. free_cwd (struct saved_cwd *cwd)
  74. {
  75. if (cwd->desc >= 0)
  76. close (cwd->desc);
  77. free (cwd->name);
  78. }