maint: improve static and dynamic checking
[coreutils.git] / gl / lib / xfts.c
blob3cb77610ab2e8e85ef6eca83cb102fd73fde1795
1 /* xfts.c -- a wrapper for fts_open
3 Copyright (C) 2003-2023 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 <https://www.gnu.org/licenses/>. */
18 /* Written by Jim Meyering. */
20 #include <config.h>
22 #include <stdlib.h>
23 #include <errno.h>
25 #include "assure.h"
26 #include "xalloc.h"
27 #include "xfts.h"
29 /* Fail with a proper diagnostic if fts_open fails. */
31 FTS *
32 xfts_open (char * const *argv, int options,
33 int (*compar) (const FTSENT **, const FTSENT **))
35 FTS *fts = fts_open (argv, options | FTS_CWDFD, compar);
36 if (fts == nullptr)
38 /* This can fail in two ways: out of memory or with errno==EINVAL,
39 which indicates it was called with invalid bit_flags. */
40 affirm (errno != EINVAL);
41 xalloc_die ();
44 return fts;
47 /* When fts_read returns FTS_DC to indicate a directory cycle,
48 it may or may not indicate a real problem. When a program like
49 chgrp performs a recursive traversal that requires traversing
50 symbolic links, it is *not* a problem. However, when invoked
51 with "-P -R", it deserves a warning. The fts_options member
52 records the options that control this aspect of fts's behavior,
53 so test that. */
54 bool
55 cycle_warning_required (FTS const *fts, FTSENT const *ent)
57 #define ISSET(Fts,Opt) ((Fts)->fts_options & (Opt))
58 /* When dereferencing no symlinks, or when dereferencing only
59 those listed on the command line and we're not processing
60 a command-line argument, then a cycle is a serious problem. */
61 return ((ISSET (fts, FTS_PHYSICAL) && !ISSET (fts, FTS_COMFOLLOW))
62 || (ISSET (fts, FTS_PHYSICAL) && ISSET (fts, FTS_COMFOLLOW)
63 && ent->fts_level != FTS_ROOTLEVEL));