Fnmatch: int fnmatch (const char * pattern, const char * string, int flags );
The fnmatch () function checks whether the string argument matches the pattern argument, which is a shell wildcard pattern. It is used to determine whether the string conforms to the pattern structure.
-
Fnm_noescape
-
If this flag is set, the backslash is a common character instead of an escape character.
-
Fnm_pathname
-
If this flag is set,
StringOnly match the slash in
PatternIt cannot match the asterisk (*) or question mark (?) The metacharacters cannot match the brackets ([]) that contain the slash.
-
Fnm_period
-
If this flag is set,
StringThe start point number in must match
Pattern. A vertex is considered as the starting point. If it is
StringThe first character, or if both
Fnm_pathname, Followed by the dot next to the slash.
-
Fnm_file_name
-
This is
Fnm_pathname.
-
Fnm_leading_dir
-
If this flag (GNU extension) is set, the pattern must match
String. This flag is mainly used internally for glibc and is only implemented under certain conditions.
-
Fnm_casefold
-
If this flag (GNU extension) is set, case-insensitive for pattern matching.
Return Value: 0,
StringMatch
Pattern; Fnm_nomatch, no match; or other non-zero values, if an error occurs.
(The program comes from the Network)
# Include <fnmatch. h> # include <stdio. h> # include <sys/types. h> # include <dirent. h> int main (INT argc, char * argv []) {char * pattern; dir * dir; struct dirent * entry; int ret; dir = opendir (argv [2]); // open the specified path pattern = argv [1]; // If (Dir! = NULL) {// obtain the files in the folder one by one while (Entry = readdir (DIR ))! = NULL) {ret = fnmatch (pattern, entry-> d_name, fnm_pathname | fnm_period); If (ret = 0) // pattern-compliant Structure {printf ("% s \ n", entry-> d_name);} else if (ret = fnm_nomatch) {continue ;} else {printf ("error file = % s \ n", entry-> d_name) ;}} closedir (DIR );}}