aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Pall <mike>2012-09-21 16:14:17 +0200
committerMike Pall <mike>2012-09-21 16:14:17 +0200
commit98f05808fac3b2c439034b9eca55e7e9492e3a9f (patch)
treef70c95f0c308d22933c26ff2c0750868c5f3a82c /src
parent40afe71c3f4cc54a4f929f1b57d6ab5331d770f8 (diff)
downloadluajit-98f05808fac3b2c439034b9eca55e7e9492e3a9f.tar.gz
luajit-98f05808fac3b2c439034b9eca55e7e9492e3a9f.tar.bz2
luajit-98f05808fac3b2c439034b9eca55e7e9492e3a9f.zip
Fix package.searchpath().
Diffstat (limited to 'src')
-rw-r--r--src/lib_package.c19
1 files 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)
279} 279}
280 280
281static const char *searchpath (lua_State *L, const char *name, 281static const char *searchpath (lua_State *L, const char *name,
282 const char *path) 282 const char *path, const char *sep,
283 const char *dirsep)
283{ 284{
284 name = luaL_gsub(L, name, ".", LUA_DIRSEP); 285 luaL_Buffer msg; /* to build error message */
285 lua_pushliteral(L, ""); /* error accumulator */ 286 luaL_buffinit(L, &msg);
287 if (*sep != '\0') /* non-empty separator? */
288 name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
286 while ((path = pushnexttemplate(L, path)) != NULL) { 289 while ((path = pushnexttemplate(L, path)) != NULL) {
287 const char *filename = luaL_gsub(L, lua_tostring(L, -1), 290 const char *filename = luaL_gsub(L, lua_tostring(L, -1),
288 LUA_PATH_MARK, name); 291 LUA_PATH_MARK, name);
@@ -291,14 +294,18 @@ static const char *searchpath (lua_State *L, const char *name,
291 return filename; /* return that file name */ 294 return filename; /* return that file name */
292 lua_pushfstring(L, "\n\tno file " LUA_QS, filename); 295 lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
293 lua_remove(L, -2); /* remove file name */ 296 lua_remove(L, -2); /* remove file name */
294 lua_concat(L, 2); /* add entry to possible error message */ 297 luaL_addvalue(&msg); /* concatenate error msg. entry */
295 } 298 }
299 luaL_pushresult(&msg); /* create error message */
296 return NULL; /* not found */ 300 return NULL; /* not found */
297} 301}
298 302
299static int lj_cf_package_searchpath(lua_State *L) 303static int lj_cf_package_searchpath(lua_State *L)
300{ 304{
301 const char *f = searchpath(L, luaL_checkstring(L, 1), luaL_checkstring(L, 2)); 305 const char *f = searchpath(L, luaL_checkstring(L, 1),
306 luaL_checkstring(L, 2),
307 luaL_optstring(L, 3, "."),
308 luaL_optstring(L, 4, LUA_DIRSEP));
302 if (f != NULL) { 309 if (f != NULL) {
303 return 1; 310 return 1;
304 } else { /* error message is on top of the stack */ 311 } else { /* error message is on top of the stack */
@@ -316,7 +323,7 @@ static const char *findfile(lua_State *L, const char *name,
316 path = lua_tostring(L, -1); 323 path = lua_tostring(L, -1);
317 if (path == NULL) 324 if (path == NULL)
318 luaL_error(L, LUA_QL("package.%s") " must be a string", pname); 325 luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
319 return searchpath(L, name, path); 326 return searchpath(L, name, path, ".", LUA_DIRSEP);
320} 327}
321 328
322static void loaderror(lua_State *L, const char *filename) 329static void loaderror(lua_State *L, const char *filename)