copy-acl: ignore ENOTSUP on HP-UX
[gnulib.git] / tests / test-exclude.c
1 /* Test suite for exclude.
2    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3    This file is part of the GNUlib Library.
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 #include <config.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdbool.h>
24 #include <fnmatch.h>
25
26 #include "exclude.h"
27 #include "progname.h"
28 #include "error.h"
29 #include "argmatch.h"
30
31 #ifndef FNM_CASEFOLD
32 # define FNM_CASEFOLD 0
33 #endif
34 #ifndef FNM_LEADING_DIR
35 # define FNM_LEADING_DIR 0
36 #endif
37
38 char const * const exclude_keywords[] = {
39   "noescape",
40   "pathname",
41   "period",
42   "leading_dir",
43   "casefold",
44   "anchored",
45   "include",
46   "wildcards",
47   NULL
48 };
49
50 int exclude_flags[] = {
51   FNM_NOESCAPE,
52   FNM_PATHNAME,
53   FNM_PERIOD,
54   FNM_LEADING_DIR,
55   FNM_CASEFOLD,
56   EXCLUDE_ANCHORED,
57   EXCLUDE_INCLUDE,
58   EXCLUDE_WILDCARDS
59 };
60
61 ARGMATCH_VERIFY (exclude_keywords, exclude_flags);
62
63 /* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
64    thus must link with a definition of that function.  Provide it here.  */
65 #ifdef ARGMATCH_DIE_DECL
66 #ifndef __attribute__
67 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
68 #  define __attribute__(x) /* empty */
69 # endif
70 #endif
71
72 #ifndef ATTRIBUTE_NORETURN
73 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
74 #endif
75
76 ARGMATCH_DIE_DECL ATTRIBUTE_NORETURN;
77 ARGMATCH_DIE_DECL { exit (1); }
78 #endif
79
80 int
81 main (int argc, char **argv)
82 {
83   int exclude_options = 0;
84   struct exclude *exclude = new_exclude ();
85
86   set_program_name (argv[0]);
87
88   if (argc == 1)
89     error (1, 0, "usage: %s file -- words...", argv[0]);
90
91   while (--argc)
92     {
93       char *opt = *++argv;
94       if (opt[0] == '-')
95         {
96           int neg = 0;
97           int flag;
98           char *s = opt + 1;
99
100           if (opt[1] == '-' && opt[2] == 0)
101             {
102               argc--;
103               break;
104             }
105           if (strlen (s) > 3 && memcmp (s, "no-", 3) == 0)
106             {
107               neg = 1;
108               s += 3;
109             }
110           flag = XARGMATCH (opt, s, exclude_keywords, exclude_flags);
111           if (neg)
112             exclude_options &= ~flag;
113           else
114             exclude_options |= flag;
115         }
116       else if (add_exclude_file (add_exclude, exclude, opt,
117                                  exclude_options, '\n') != 0)
118         error (1, errno, "error loading %s", opt);
119     }
120
121   for (; argc; --argc)
122     {
123       char *word = *++argv;
124
125       printf ("%s: %d\n", word, excluded_file_name (exclude, word));
126     }
127   return 0;
128 }