fnmatch() — Match file name or path name

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE
#include <fnmatch.h>

int fnmatch(const char *pattern, const char *string, int flags);

General description

The fnmatch() function matches patterns as described in the XCU specification, Section 2.13.1, Patterns Matching a Single Character, and Section 2.13.2, Patterns Matching Multiple Characters. It checks the string specified by the string argument to see if it matches the pattern specified by the pattern argument.

The flags argument modifies the interpretation of pattern and string. It is the bitwise inclusive-OR of zero or more of the flags defined in the header <fnmatch.h>. If the FNM_PATHNAME flag is set in flags, then a slash character in string will be explicitly matched by a slash in pattern; it will not be matched by either the asterisk or question-mark special characters, nor by a bracket expression. If FNM_PATHNAME is set and either of these characters would match a slash, the function returns FNM_ESLASH. If the FNM_PATHNAME flag is not set, the slash character is treated as an ordinary character.

If FNM_NOESCAPE is not set in flags, a backslash character (\) in pattern followed by any other character will match that second character in string. In particular, \\ will match a backslash in string. If FNM_NOESCAPE is set, a backslash character will be treated as an ordinary character.

If FNM_PERIOD is set in flags, then a leading period in string will match a period in pattern; as described by rule 2 in the XCU specification, Section 2.13.3, Patterns Used for Filename Expansion, where the location of “leading” is indicated by the value of FNM_PATHNAME:
  • If FNM_PATHNAME is set, a period is “leading” if it is the first character in string or if it immediately follows a slash.
  • If FNM_PATHNAME is not set, a period is “leading” only if it is the first character of string.

If FNM_PERIOD is not set, then no special restrictions are placed on matching a period. If FNM_PERIOD is set, and a pattern wildcard would match a leading period as defined by the above rules, then the function returns FNM_EPERIOD.

Returned value

If string matches the pattern specified by pattern, fnmatch() returns 0.

If there is no match, fnmatch() returns FNM_NOMATCH, which is defined in the header <fnmatch.h>.

If an error occurs, fnmatch() returns another nonzero value. See the discussion above for the various possible nonzero returns.

Related information