Ignore the usually-ignored files in git
[findutils.git] / lib / dircallback.c
blobc788890431091669908ef521c0548c9be719dbff
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)
7 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 USA.
20 /* This file was written by James Youngman, based on gnulib'c at-func.c.
24 #include <config.h>
27 #include "openat.h"
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <errno.h>
32 #include "fcntl--.h"
33 #include "lstat.h"
34 #include "save-cwd.h"
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() */
42 #ifdef HAVE_LOCALE_H
43 #include <locale.h>
44 #endif
46 #if ENABLE_NLS
47 # include <libintl.h>
48 # define _(Text) gettext (Text)
49 #else
50 # define _(Text) Text
51 #define textdomain(Domain)
52 #define bindtextdomain(Package, Directory)
53 #endif
54 #ifdef gettext_noop
55 # define N_(String) gettext_noop (String)
56 #else
57 /* See locate.c for explanation as to why not use (String) */
58 # define N_(String) String
59 #endif
63 int
64 run_in_dir (int dirfd, int (*callback)(void*), void *usercontext)
66 if (dirfd == AT_FDCWD)
68 return (*callback)(usercontext);
70 else
72 struct saved_cwd saved_cwd;
73 int saved_errno;
74 int err;
76 if (save_cwd (&saved_cwd) != 0)
77 openat_save_fail (errno);
79 if (fchdir (dirfd) != 0)
81 saved_errno = errno;
82 free_cwd (&saved_cwd);
83 errno = saved_errno;
84 return -1;
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);
95 if (saved_errno)
96 errno = saved_errno;
97 return err;