18 # include <sys/file.h>
29 char *xgetcwd __P((void));
31 /* Record the location of the current working directory in CWD so that
32 the program may change to other directories and later use restore_cwd
33 to return to the recorded location. This function may allocate
34 space using malloc (via xgetcwd) or leave a file descriptor open;
35 use free_cwd to perform the necessary free or close. Upon failure,
36 no memory is allocated, any locally opened file descriptors are
37 closed; return non-zero -- in that case, free_cwd need not be
38 called, but doing so is ok. Otherwise, return zero. */
42 struct saved_cwd *cwd;
44 static int have_working_fchdir = 1;
49 if (have_working_fchdir)
52 cwd->desc = open (".", O_RDONLY);
55 error (0, errno, "cannot open current directory");
60 /* On SunOS 4, fchdir returns EINVAL if accounting is enabled,
61 so we have to fall back to chdir. */
62 if (fchdir (cwd->desc))
68 have_working_fchdir = 0;
72 error (0, errno, "current directory");
78 # endif /* __sun__ || sun */
80 #define fchdir(x) (abort (), 0)
81 have_working_fchdir = 0;
85 if (!have_working_fchdir)
87 cwd->name = xgetcwd ();
88 if (cwd->name == NULL)
90 error (0, errno, "cannot get current directory");
97 /* Change to recorded location, CWD, in directory hierarchy.
98 If "saved working directory", NULL))
102 restore_cwd (cwd, dest, from)
103 const struct saved_cwd *cwd;
110 if (fchdir (cwd->desc))
112 error (0, errno, "cannot return to %s%s%s",
113 (dest ? dest : "saved working directory"),
114 (from ? " from " : ""),
119 else if (chdir (cwd->name) < 0)
121 error (0, errno, "%s", cwd->name);
129 struct saved_cwd *cwd;