Path: blob/main/lib/libc/tests/gen/test-fnmatch.c
105174 views
/*-1* Copyright (c) 2010 Jilles Tjoelker2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/param.h>27#include <stdio.h>28#include <stdlib.h>29#include <string.h>30#include <unistd.h>3132#include "fnmatch_testcases.h"3334static int35write_sh_tests(const char *progname, int num)36{37size_t i;38struct testcase *t;3940printf("# Generated by %s -s %d, do not edit.\n", progname, num);41printf("# $" "FreeBSD$\n");42printf("failures=\n");43printf("failed() { printf '%%s\\n' \"Failed: $1 '$2' '$3'\"; failures=x$failures; }\n");44if (num == 1) {45printf("testmatch() { eval \"case \\$2 in ''$1) ;; *) failed testmatch \\\"\\$@\\\";; esac\"; }\n");46printf("testnomatch() { eval \"case \\$2 in ''$1) failed testnomatch \\\"\\$@\\\";; esac\"; }\n");47} else if (num == 2) {48printf("# We do not treat a backslash specially in this case,\n");49printf("# but this is not the case in all shells.\n");50printf("netestmatch() { case $2 in $1) ;; *) failed netestmatch \"$@\";; esac; }\n");51printf("netestnomatch() { case $2 in $1) failed netestnomatch \"$@\";; esac; }\n");52}5354for (i = 0; i < nitems(testcases); i++) {55t = &testcases[i];56if (strchr(t->pattern, '\'') != NULL ||57strchr(t->string, '\'') != NULL)58continue;59if (t->flags == 0 && strcmp(t->pattern, "\\") == 0)60continue;61if (num == 1 && t->flags == 0)62printf("test%smatch '%s' '%s'\n",63t->result == FNM_NOMATCH ? "no" : "",64t->pattern, t->string);65if (num == 2 && (t->flags == FNM_NOESCAPE ||66(t->flags == 0 && strchr(t->pattern, '\\') == NULL)))67printf("netest%smatch '%s' '%s'\n",68t->result == FNM_NOMATCH ? "no" : "",69t->pattern, t->string);70}71printf("[ -z \"$failures\" ]\n");72return 0;73}7475static void76usage(char *progname)77{78fprintf(stderr, "usage: %s [-s num]\n", progname);79fprintf(stderr, "-s option writes tests for sh(1), num is 1 or 2\n");80}8182int83main(int argc, char *argv[])84{85int opt;8687while ((opt = getopt(argc, argv, "s:")) != -1) {88switch (opt) {89case 's':90return (write_sh_tests(argv[0], atoi(optarg)));91default:92usage(argv[0]);93exit(1);94}95}96usage(argv[0]);97exit(1);98}99100101