ln: add the --relative option
[platform/upstream/coreutils.git] / lib / xfts.c
1 /* xfts.c -- a wrapper for fts_open
2
3    Copyright (C) 2003-2012 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>.  */
17
18 /* Written by Jim Meyering.  */
19
20 #include <config.h>
21
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <assert.h>
26
27 #include "xalloc.h"
28 #include "xfts.h"
29
30 /* Fail with a proper diagnostic if fts_open fails.  */
31
32 FTS *
33 xfts_open (char * const *argv, int options,
34            int (*compar) (const FTSENT **, const FTSENT **))
35 {
36   FTS *fts = fts_open (argv, options | FTS_CWDFD, compar);
37   if (fts == NULL)
38     {
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 ();
43     }
44
45   return fts;
46 }
47
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)
57 {
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));
65 }