| 349 | |
| 350 | |
| 351 | static const char *findfile (lua_State *L, const char *name, |
| 352 | const char *pname) { |
| 353 | const char *path; |
| 354 | name = luaL_gsub(L, name, ".", LUA_DIRSEP); |
| 355 | lua_getfield(L, LUA_ENVIRONINDEX, pname); |
| 356 | path = lua_tostring(L, -1); |
| 357 | if (path == NULL) |
| 358 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname); |
| 359 | lua_pushliteral(L, ""); /* error accumulator */ |
| 360 | while ((path = pushnexttemplate(L, path)) != NULL) { |
| 361 | const char *filename; |
| 362 | filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); |
| 363 | lua_remove(L, -2); /* remove path template */ |
| 364 | if (readable(filename)) /* does file exist and is readable? */ |
| 365 | return filename; /* return that file name */ |
| 366 | lua_pushfstring(L, "\n\tno file " LUA_QS, filename); |
| 367 | lua_remove(L, -2); /* remove file name */ |
| 368 | lua_concat(L, 2); /* add entry to possible error message */ |
| 369 | } |
| 370 | return NULL; /* not found */ |
| 371 | } |
| 372 | |
| 373 | |
| 374 | static void loaderror (lua_State *L, const char *filename) { |
no test coverage detected