Check to see if a string matches a pattern
#include <fnmatch.h>
int fnmatch( const char* pat,
const char* str,
int flags );
libc
The fnmatch() function checks the string specified by the str argument to see if it matches the pattern specified by the pat argument.
The flag argument is a bitwise inclusive OR of the bits described below. It modifies the interpretation of pat and str.
A pattern-matching special character that is quoted is a pattern that matches the special character itself. When not quoted, such special characters have special meaning in the specification of patterns. The pattern-matching special characters and the contexts in which they have their special meaning are as follows:
The ?, * and [ characters aren't special when used inside a bracket expression.
The concatenation of patterns matching a single character is a valid pattern that matches the concatenation of the single characters or collating elements matched by each of the concatenated patterns. For example, the pattern a[bc] matches the strings ab and ac.
The concatenation of one or more patterns matching a single character with one or more asterisks (*) is a valid pattern. In such patterns, each asterisk matches a string of zero or more characters, up to the first character that matches the character following the asterisk in the pattern. For example, the pattern a*d matches the strings ad, abd, and abcd, but not the string abc.
When an asterisk is the first or last character in a pattern, it matches zero or more characters that precede or follow the characters matched by the remainder of the pattern. For example, the pattern a*d* matches the strings ad, abcd, abcdef, aaaad and adddd; the pattern *a*d matches the strings ad, abcd, efabcd, aaaad and adddd.
/*
* The following example accepts a set of patterns
* for filenames as argv[1..argc]. It reads lines
* from standard input, and outputs the lines that
* match any of the patterns.
*/
#include <stdio.h>
#include <fnmatch.h>
#include <stdlib.h>
#include <limits.h>
int main( int argc, char **argv )
{
int i;
char buffer[PATH_MAX+1];
while( gets( buffer ) ) {
for( i = 0; i < argc; i++ ) {
if( fnmatch( argv[i], buffer, 0 ) == 0 ) {
puts( buffer );
break;
}
}
}
exit( EXIT_SUCCESS );
}
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | Yes |
| Signal handler | Yes |
| Thread | Yes |