1 /* listfile.c -- run a function in a specific directory
2 Copyright (C) 2007 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 2, or (at your option)
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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 /* This file was written by James Youngman, based on gnulib'c at-func.c.
37 /* The presence of unistd.h is assumed by gnulib these days, so we
38 * might as well assume it too.
40 #include <unistd.h> /* for readlink() */
48 # define _(Text) gettext (Text)
51 #define textdomain(Domain)
52 #define bindtextdomain(Package, Directory)
55 # define N_(String) gettext_noop (String)
57 /* See locate.c for explanation as to why not use (String) */
58 # define N_(String) String
64 run_in_dir (int dirfd
, int (*callback
)(void*), void *usercontext
)
66 if (dirfd
== AT_FDCWD
)
68 return (*callback
)(usercontext
);
72 struct saved_cwd saved_cwd
;
76 if (save_cwd (&saved_cwd
) != 0)
77 openat_save_fail (errno
);
79 if (fchdir (dirfd
) != 0)
82 free_cwd (&saved_cwd
);
87 err
= (*callback
)(usercontext
);
88 saved_errno
= (err
< 0 ? errno
: 0);
90 if (restore_cwd (&saved_cwd
) != 0)
91 openat_restore_fail (errno
);
93 free_cwd (&saved_cwd
);