build: merge in bootstrap changes from gnulib
[coreutils.git] / lib / xfts.c
blobda3183ee4a3b576baa9684fd126b1877a1284245
1 /* xfts.c -- a wrapper for fts_open
3 Copyright (C) 2003, 2005-2007, 2009-2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Jim Meyering. */
20 #include <config.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <assert.h>
27 #include "xalloc.h"
28 #include "xfts.h"
30 /* Fail with a proper diagnostic if fts_open fails. */
32 FTS *
33 xfts_open (char * const *argv, int options,
34 int (*compar) (const FTSENT **, const FTSENT **))
36 FTS *fts = fts_open (argv, options | FTS_CWDFD, compar);
37 if (fts == NULL)
39 /* This can fail in two ways: out of memory or with errno==EINVAL,
40 which indicates it was called with invalid bit_flags. */
41 assert (errno != EINVAL);
42 xalloc_die ();
45 return fts;
48 /* When fts_read returns FTS_DC to indicate a directory cycle,
49 it may or may not indicate a real problem. When a program like
50 chgrp performs a recursive traversal that requires traversing
51 symbolic links, it is *not* a problem. However, when invoked
52 with "-P -R", it deserves a warning. The fts_options member
53 records the options that control this aspect of fts's behavior,
54 so test that. */
55 bool
56 cycle_warning_required (FTS const *fts, FTSENT const *ent)
58 #define ISSET(Fts,Opt) ((Fts)->fts_options & (Opt))
59 /* When dereferencing no symlinks, or when dereferencing only
60 those listed on the command line and we're not processing
61 a command-line argument, then a cycle is a serious problem. */
62 return ((ISSET (fts, FTS_PHYSICAL) && !ISSET (fts, FTS_COMFOLLOW))
63 || (ISSET (fts, FTS_PHYSICAL) && ISSET (fts, FTS_COMFOLLOW)
64 && ent->fts_level != FTS_ROOTLEVEL));