FNMATCH(3POSIX)

πŸ’– FNMATCH(3POSIX) β€” the POSIX Programmer's Manual girlies πŸ’–

okay bestie, so this whole page lives in the POSIX Programmer's Manual ✨. heads up tho: the Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for details of Linux behavior, fr), or β€” plot twist β€” the interface might not even be implemented on Linux at all. πŸ™ˆ


πŸŽ€ NAME πŸŽ€

fnmatch β€” match a filename string or a pathname. that's it, that's the whole job. πŸ’…


✨ SYNOPSIS ✨

#include <fnmatch.h>

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

πŸ¦‹ DESCRIPTION πŸ¦‹

so what fnmatch() does is match patterns exactly the way it's described in the Shell and Utilities volume of POSIX.1‐2017, Section 2.13.1, Patterns Matching a Single Character, and Section 2.13.2, Patterns Matching Multiple Characters. it takes the string in the string argument and checks whether it matches the pattern in the pattern argument. vibe check passed or not, no in-between. πŸ’–

the flags argument modifies how pattern and string get interpreted, and it's the bitwise-inclusive OR of zero or more of the flags defined in <fnmatch.h>. got it? iconic. now the deets:

  • if the FNM_PATHNAME flag is set in flags, then a <slash> character ('/') in string shall be explicitly matched by a <slash> in pattern β€” it shall not be matched by either the <asterisk> or <question-mark> special characters, and not by a bracket expression either. no shortcuts, no sneaky loopholes. 🚫
  • if FNM_PATHNAME is not set, the <slash> character is just treated as an ordinary character. no main character energy for the slash. ✨
  • if FNM_NOESCAPE is not set in flags, then a <backslash> character in pattern followed by any other character shall match that second character in string. in particular, "\\" shall match a <backslash> in string. but β€” and this is the messy part 😭 β€” if pattern ends with an unescaped <backslash>, then fnmatch() shall return a non-zero value (which means either no match or an error, and yeah it does not tell you which, sorry bestie).
  • if FNM_NOESCAPE is set, a <backslash> character is treated as an ordinary character. plain. simple. at peace. 🧘
  • if FNM_PERIOD is set in flags, then a leading <period> ('.') in string shall match a <period> in pattern β€” just like rule 2 in the Shell and Utilities volume of POSIX.1‐2017, Section 2.13.3, Patterns Used for Filename Expansion describes, where the location of "leading" is indicated by the value of FNM_PATHNAME:
    • if FNM_PATHNAME is set, a <period> counts as "leading" if it's the first character in string, or if it immediately follows a <slash>.
    • if FNM_PATHNAME is not set, a <period> counts as "leading" only if it's the first character of string.
  • if FNM_PERIOD is not set, then no special restrictions are placed on matching a period. free the period. πŸ•ŠοΈ

🌈 RETURN VALUE 🌈

  • if string matches the pattern in pattern β†’ fnmatch() shall return 0. slay. πŸ’…
  • if there's no match β†’ fnmatch() shall return FNM_NOMATCH, which is defined in <fnmatch.h>. 😭
  • if an error occurs β†’ fnmatch() shall return another non-zero value. yes, a different non-zero value. keep your eyes peeled. πŸ‘€

πŸ’€ ERRORS πŸ’€

No errors are defined. (not me having nothing to report 😭)

the following sections are informative. 🫢


πŸ“ EXAMPLES πŸ“

None. that's right, zero examples. we're on our own out here bestie. πŸ’€


πŸ’œ APPLICATION USAGE πŸ’œ

the fnmatch() function has two major uses, and they're both lowkey iconic:

  1. it could be used by an application or utility that needs to read a directory and apply a pattern against each entry. the find utility is an example of this. πŸ”₯
  2. it can also be used by the pax utility to process its pattern operands, or by applications that need to match strings in a similar manner. πŸ’–

the name fnmatch() is intended to imply filename match, rather than pathname match. the default action of this function is to match filename strings, not pathnames, since it gives no special significance to the <slash> character. but with the FNM_PATHNAME flag, fnmatch() does match pathnames β€” just without tilde expansion, without parameter expansion, and without any special treatment for a <period> at the beginning of a filename. so like, pathname mode but make it minimal. ✨


πŸ‘‘ RATIONALE πŸ‘‘

this function replaced the REG_FILENAME flag of regcomp() in early proposals of this volume of POSIX.1‐2017. it provides virtually the same functionality as the regcomp() and regexec() functions using the REG_FILENAME and REG_FSLASH flags β€” and btw, the REG_FSLASH flag was proposed for regcomp(), and would have had the opposite effect from FNM_PATHNAME β€” but with a simpler function and less system overhead. ate and left no crumbs. πŸŽ€


🧚 FUTURE DIRECTIONS 🧚

None. the future is now, and it's the same as today. πŸ¦‹


⭐ SEE ALSO ⭐

  • glob()
  • Section 2.6, Word Expansions

the Base Definitions volume of POSIX.1‐2017, <fnmatch.h> πŸ’–


portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . πŸ™ˆ

fr fr, if something looks weird, it's probably a conversion oopsie, not a vibe. report it bestie. 🫢

okay that's the whole thing, no cap. this page is from IEEE/The Open Group, 2017 (yeah, the FNMATCH(3POSIX) era ✨), and you just read the entire thing like a good little scholar. obsessed with you. mwah. πŸ’–πŸŽ€πŸ’