summaryrefslogtreecommitdiffstats
path: root/loader/fnmatch-stub.c
blob: 936201a3ab614d08f375d9f0e0d1893c6d5766e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <string.h>
#include <fnmatch.h>

/* A very simplified fnmatch which just supports one
   * in the string and no [, ? or { */
int fnmatch(const char *pattern, const char *string, int flags)
{
  const char *p, *q, *r;

  if (!(flags & ~(FNM_PATHNAME | FNM_PERIOD))
      && strpbrk (pattern, "[?{") == NULL
      && (p = strchr (pattern, '*')) != NULL
      && strchr (p + 1, '*') == NULL)
    {
      if (strncmp (string, pattern, p - pattern))
	return FNM_NOMATCH;
      q = strstr (string + (p - pattern), p + 1);
      r = strchr (string + (p - pattern), '/');
      if (q == NULL || strlen (q) != strlen (p + 1))
	return FNM_NOMATCH;

      if ((flags & FNM_PATHNAME) && (r != NULL && r < q))
	return FNM_NOMATCH;
      return 0;
    }
  fprintf (stderr, "fnmatch stub does not support '%s' patterns\n", pattern);
  abort ();
}