Blame gnulib-tests/test-exclude.c

Packit Service fdd496
/* Test suite for exclude.
Packit Service fdd496
   Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit Service fdd496
   This file is part of the GNUlib Library.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
#include <stdio.h>
Packit Service fdd496
#include <stdlib.h>
Packit Service fdd496
#include <errno.h>
Packit Service fdd496
#include <string.h>
Packit Service fdd496
#include <stdbool.h>
Packit Service fdd496
#include <fnmatch.h>
Packit Service fdd496
Packit Service fdd496
#include "exclude.h"
Packit Service fdd496
#include "error.h"
Packit Service fdd496
#include "argmatch.h"
Packit Service fdd496
Packit Service fdd496
#ifndef FNM_CASEFOLD
Packit Service fdd496
# define FNM_CASEFOLD 0
Packit Service fdd496
#endif
Packit Service fdd496
#ifndef FNM_LEADING_DIR
Packit Service fdd496
# define FNM_LEADING_DIR 0
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
char const * const exclude_keywords[] = {
Packit Service fdd496
  "noescape",
Packit Service fdd496
  "pathname",
Packit Service fdd496
  "period",
Packit Service fdd496
  "leading_dir",
Packit Service fdd496
  "casefold",
Packit Service fdd496
  "anchored",
Packit Service fdd496
  "include",
Packit Service fdd496
  "wildcards",
Packit Service fdd496
  NULL
Packit Service fdd496
};
Packit Service fdd496
Packit Service fdd496
int exclude_flags[] = {
Packit Service fdd496
  FNM_NOESCAPE,
Packit Service fdd496
  FNM_PATHNAME,
Packit Service fdd496
  FNM_PERIOD,
Packit Service fdd496
  FNM_LEADING_DIR,
Packit Service fdd496
  FNM_CASEFOLD,
Packit Service fdd496
  EXCLUDE_ANCHORED,
Packit Service fdd496
  EXCLUDE_INCLUDE,
Packit Service fdd496
  EXCLUDE_WILDCARDS
Packit Service fdd496
};
Packit Service fdd496
Packit Service fdd496
ARGMATCH_VERIFY (exclude_keywords, exclude_flags);
Packit Service fdd496
Packit Service fdd496
/* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
Packit Service fdd496
   thus must link with a definition of that function.  Provide it here.  */
Packit Service fdd496
#ifdef ARGMATCH_DIE_DECL
Packit Service fdd496
Packit Service fdd496
_Noreturn ARGMATCH_DIE_DECL;
Packit Service fdd496
ARGMATCH_DIE_DECL { exit (1); }
Packit Service fdd496
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
int
Packit Service fdd496
main (int argc, char **argv)
Packit Service fdd496
{
Packit Service fdd496
  int exclude_options = 0;
Packit Service fdd496
  struct exclude *exclude = new_exclude ();
Packit Service fdd496
Packit Service fdd496
  if (argc == 1)
Packit Service fdd496
    error (1, 0, "usage: %s file -- words...", argv[0]);
Packit Service fdd496
Packit Service fdd496
  while (--argc)
Packit Service fdd496
    {
Packit Service fdd496
      char *opt = *++argv;
Packit Service fdd496
      if (opt[0] == '-')
Packit Service fdd496
        {
Packit Service fdd496
          int neg = 0;
Packit Service fdd496
          int flag;
Packit Service fdd496
          char *s = opt + 1;
Packit Service fdd496
Packit Service fdd496
          if (opt[1] == '-' && opt[2] == 0)
Packit Service fdd496
            {
Packit Service fdd496
              argc--;
Packit Service fdd496
              break;
Packit Service fdd496
            }
Packit Service fdd496
          if (strlen (s) > 3 && memcmp (s, "no-", 3) == 0)
Packit Service fdd496
            {
Packit Service fdd496
              neg = 1;
Packit Service fdd496
              s += 3;
Packit Service fdd496
            }
Packit Service fdd496
          flag = XARGMATCH (opt, s, exclude_keywords, exclude_flags);
Packit Service fdd496
          if (neg)
Packit Service fdd496
            exclude_options &= ~flag;
Packit Service fdd496
          else
Packit Service fdd496
            exclude_options |= flag;
Packit Service fdd496
Packit Service fdd496
          /* Skip this test if invoked with -leading-dir on a system that
Packit Service fdd496
             lacks support for FNM_LEADING_DIR. */
Packit Service fdd496
          if (strcmp (s, "leading_dir") == 0 && FNM_LEADING_DIR == 0)
Packit Service fdd496
            exit (77);
Packit Service fdd496
Packit Service fdd496
          /* Likewise for -casefold and FNM_CASEFOLD.  */
Packit Service fdd496
          if (strcmp (s, "casefold") == 0 && FNM_CASEFOLD == 0)
Packit Service fdd496
            exit (77);
Packit Service fdd496
        }
Packit Service fdd496
      else if (add_exclude_file (add_exclude, exclude, opt,
Packit Service fdd496
                                 exclude_options, '\n') != 0)
Packit Service fdd496
        error (1, errno, "error loading %s", opt);
Packit Service fdd496
    }
Packit Service fdd496
Packit Service fdd496
  for (; argc; --argc)
Packit Service fdd496
    {
Packit Service fdd496
      char *word = *++argv;
Packit Service fdd496
Packit Service fdd496
      printf ("%s: %d\n", word, excluded_file_name (exclude, word));
Packit Service fdd496
    }
Packit Service fdd496
Packit Service fdd496
  free_exclude (exclude);
Packit Service fdd496
  return 0;
Packit Service fdd496
}