From 98f05808fac3b2c439034b9eca55e7e9492e3a9f Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Fri, 21 Sep 2012 16:14:17 +0200 Subject: [PATCH] Fix package.searchpath(). --- src/lib_package.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/lib_package.c b/src/lib_package.c index c04253aa..ca5a5a16 100644 --- a/src/lib_package.c +++ b/src/lib_package.c @@ -279,10 +279,13 @@ static const char *pushnexttemplate(lua_State *L, const char *path) } static const char *searchpath (lua_State *L, const char *name, - const char *path) + const char *path, const char *sep, + const char *dirsep) { - name = luaL_gsub(L, name, ".", LUA_DIRSEP); - lua_pushliteral(L, ""); /* error accumulator */ + luaL_Buffer msg; /* to build error message */ + luaL_buffinit(L, &msg); + if (*sep != '\0') /* non-empty separator? */ + name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ while ((path = pushnexttemplate(L, path)) != NULL) { const char *filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); @@ -291,14 +294,18 @@ static const char *searchpath (lua_State *L, const char *name, return filename; /* return that file name */ lua_pushfstring(L, "\n\tno file " LUA_QS, filename); lua_remove(L, -2); /* remove file name */ - lua_concat(L, 2); /* add entry to possible error message */ + luaL_addvalue(&msg); /* concatenate error msg. entry */ } + luaL_pushresult(&msg); /* create error message */ return NULL; /* not found */ } static int lj_cf_package_searchpath(lua_State *L) { - const char *f = searchpath(L, luaL_checkstring(L, 1), luaL_checkstring(L, 2)); + const char *f = searchpath(L, luaL_checkstring(L, 1), + luaL_checkstring(L, 2), + luaL_optstring(L, 3, "."), + luaL_optstring(L, 4, LUA_DIRSEP)); if (f != NULL) { return 1; } else { /* error message is on top of the stack */ @@ -316,7 +323,7 @@ static const char *findfile(lua_State *L, const char *name, path = lua_tostring(L, -1); if (path == NULL) luaL_error(L, LUA_QL("package.%s") " must be a string", pname); - return searchpath(L, name, path); + return searchpath(L, name, path, ".", LUA_DIRSEP); } static void loaderror(lua_State *L, const char *filename) -- 2.11.4.GIT