Skip to content

Instantly share code, notes, and snippets.

@uplime
Created June 12, 2026 17:18
Show Gist options
  • Select an option

  • Save uplime/18bf75019aabf3f853a127dae824728e to your computer and use it in GitHub Desktop.

Select an option

Save uplime/18bf75019aabf3f853a127dae824728e to your computer and use it in GitHub Desktop.
A simple and effective glob star matcher.
int match(const char *needle, const char *haystack) {
while(*haystack || *needle) {
if(*needle == '*') {
if(*(needle + 1) == '\0') {
return 1;
} else if(*(needle + 1) == '*') {
needle += 1;
} else if(*(needle + 1) == *haystack) {
needle += 1;
} else if(*haystack == '\0') {
needle += 1;
} else {
haystack += 1;
}
} else if(*needle == *haystack) {
needle += 1;
haystack += 1;
} else {
return 0;
}
}
return 1;
}
int main(int argc, char **argv) {
if(argc != 3) {
return 1;
}
return !match(argv[1], argv[2]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment