Kagera uHTTP Daemon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @PROJECT Kagera uHTTP Daemon
  3. * @COPYRIGHT See COPYING in the top level directory
  4. * @FILE match.c
  5. * @PURPOSE Simple shell-style filename matcher
  6. * @DEVELOPERS Rafal Kupiec <belliash@asiotec.eu.org>
  7. * Jef Poskanzer <jef@acme.com>
  8. */
  9. #include <string.h>
  10. #include "match.h"
  11. int match(const char* pattern, const char* string) {
  12. const char* or;
  13. for(;;) {
  14. or = strchr( pattern, '|' );
  15. if(or == (char*) 0) {
  16. return match_one(pattern, strlen(pattern), string);
  17. }
  18. if(match_one( pattern, or - pattern, string)) {
  19. return 1;
  20. }
  21. pattern = or + 1;
  22. }
  23. }
  24. static int match_one(const char* pattern, int patternlen, const char* string) {
  25. const char* p;
  26. for(p = pattern; p - pattern < patternlen; ++p, ++string) {
  27. if(*p == '?' && *string != '\0') {
  28. continue;
  29. }
  30. if(*p == '*') {
  31. int i, pl;
  32. ++p;
  33. if(*p == '*') {
  34. ++p;
  35. i = strlen(string);
  36. } else {
  37. i = strcspn(string, "/");
  38. }
  39. pl = patternlen - (p - pattern);
  40. for(; i >= 0; --i) {
  41. if(match_one(p, pl, &(string[i]))) {
  42. return 1;
  43. }
  44. }
  45. return 0;
  46. }
  47. if(*p != *string) {
  48. return 0;
  49. }
  50. }
  51. if(*string == '\0') {
  52. return 1;
  53. }
  54. return 0;
  55. }