Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 29 Aug 2025 15:05:07 GMT
From:      Ed Maste <emaste@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: d52dc0b4bf6c - stable/14 - flua: lposix: add fnmatch function
Message-ID:  <202508291505.57TF57Fh080561@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/14 has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=d52dc0b4bf6c2fb34b4a9ab614247b7c5524ff32

commit d52dc0b4bf6c2fb34b4a9ab614247b7c5524ff32
Author:     Stefan Eßer <se@FreeBSD.org>
AuthorDate: 2024-10-28 15:22:08 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2025-08-29 14:57:36 +0000

    flua: lposix: add fnmatch function
    
    The fnmatch function matches a string against a shell-style filename
    pattern. It is a complex function and cannot easily be implenented
    using regular expressions. Adding fnmatch to flua increases the amd64
    binary by less than 1 KB.
    
    Approved by:    markj
    MFC after:      3 days
    Differential Revision:  https://reviews.freebsd.org/D46849
    
    (cherry picked from commit f35ccf46c7c6cb7d26994ec5dc5926ea53be0116)
---
 libexec/flua/linit_flua.c     |  1 +
 libexec/flua/modules/lposix.c | 41 +++++++++++++++++++++++++++++++++++++++++
 libexec/flua/modules/lposix.h |  1 +
 3 files changed, 43 insertions(+)

diff --git a/libexec/flua/linit_flua.c b/libexec/flua/linit_flua.c
index 13c2c7fcc52f..9d712066485a 100644
--- a/libexec/flua/linit_flua.c
+++ b/libexec/flua/linit_flua.c
@@ -56,6 +56,7 @@ static const luaL_Reg loadedlibs[] = {
 #endif
   /* FreeBSD Extensions */
   {"lfs", luaopen_lfs},
+  {"posix.fnmatch", luaopen_posix_fnmatch},
   {"posix.libgen", luaopen_posix_libgen},
   {"posix.stdlib", luaopen_posix_stdlib},
   {"posix.sys.stat", luaopen_posix_sys_stat},
diff --git a/libexec/flua/modules/lposix.c b/libexec/flua/modules/lposix.c
index 4422d54917e1..2e8e9bfc389e 100644
--- a/libexec/flua/modules/lposix.c
+++ b/libexec/flua/modules/lposix.c
@@ -9,6 +9,7 @@
 #include <sys/wait.h>
 
 #include <errno.h>
+#include <fnmatch.h>
 #include <grp.h>
 #include <libgen.h>
 #include <pwd.h>
@@ -173,6 +174,23 @@ err:
 
 }
 
+static int
+lua_fnmatch(lua_State *L)
+{
+	const char *pattern, *string;
+	int flags, n;
+
+	n = lua_gettop(L);
+	luaL_argcheck(L, n == 2 || n == 3, 4, "need 2 or 3 arguments");
+
+	pattern = luaL_checkstring(L, 1);
+	string = luaL_checkstring(L, 2);
+	flags = luaL_optinteger(L, 3, 0);
+	lua_pushinteger(L, fnmatch(pattern, string, flags));
+
+	return (1);
+}
+
 static int
 lua_uname(lua_State *L)
 {
@@ -471,6 +489,11 @@ static const struct luaL_Reg stdliblib[] = {
 	{ NULL, NULL },
 };
 
+static const struct luaL_Reg fnmatchlib[] = {
+	REG_SIMPLE(fnmatch),
+	{ NULL, NULL },
+};
+
 static const struct luaL_Reg sys_statlib[] = {
 	REG_SIMPLE(chmod),
 	{ NULL, NULL },
@@ -515,6 +538,24 @@ luaopen_posix_stdlib(lua_State *L)
 	return (1);
 }
 
+int
+luaopen_posix_fnmatch(lua_State *L)
+{
+	luaL_newlib(L, fnmatchlib);
+
+#define	setkv(f) do {			\
+	lua_pushinteger(L, f);		\
+	lua_setfield(L, -2, #f);	\
+} while (0)
+	setkv(FNM_PATHNAME);
+	setkv(FNM_NOESCAPE);
+	setkv(FNM_NOMATCH);
+	setkv(FNM_PERIOD);
+#undef setkv
+
+	return 1;
+}
+
 int
 luaopen_posix_sys_stat(lua_State *L)
 {
diff --git a/libexec/flua/modules/lposix.h b/libexec/flua/modules/lposix.h
index 9d2c97b0d2dc..da7079056826 100644
--- a/libexec/flua/modules/lposix.h
+++ b/libexec/flua/modules/lposix.h
@@ -7,6 +7,7 @@
 
 #include <lua.h>
 
+int luaopen_posix_fnmatch(lua_State *L);
 int luaopen_posix_libgen(lua_State *L);
 int luaopen_posix_stdlib(lua_State *L);
 int luaopen_posix_sys_stat(lua_State *L);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202508291505.57TF57Fh080561>