1 /* listfile.c -- run a function in a specific directory
2 Copyright (C) 2007, 2008, 2009, 2010 Free Software 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.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /* This file was written by James Youngman, based on gnulib'c at-func.c.
37 run_in_dir (const struct saved_cwd *there,
38 int (*callback)(void*), void *usercontext)
42 struct saved_cwd here;
43 if (0 == save_cwd (&here))
45 if (0 == restore_cwd (there))
47 err = callback(usercontext);
48 saved_errno = (err < 0 ? errno : 0);
52 openat_restore_fail (errno);
55 if (restore_cwd (&here) != 0)
56 openat_restore_fail (errno);
62 openat_save_fail (errno);
71 run_in_dirfd (int dir_fd, int (*callback)(void*), void *usercontext)
73 if (dir_fd == AT_FDCWD)
75 return (*callback)(usercontext);
79 struct saved_cwd saved_cwd;
83 if (save_cwd (&saved_cwd) != 0)
84 openat_save_fail (errno);
86 if (fchdir (dir_fd) != 0)
89 free_cwd (&saved_cwd);
94 err = (*callback)(usercontext);
95 saved_errno = (err < 0 ? errno : 0);
97 if (restore_cwd (&saved_cwd) != 0)
98 openat_restore_fail (errno);
100 free_cwd (&saved_cwd);