| 472 | |
| 473 | |
| 474 | static const char *searchpath (lua_State *L, const char *name, |
| 475 | const char *path, |
| 476 | const char *sep, |
| 477 | const char *dirsep) { |
| 478 | luaL_Buffer buff; |
| 479 | char *pathname; /* path with name inserted */ |
| 480 | char *endpathname; /* its end */ |
| 481 | const char *filename; |
| 482 | /* separator is non-empty and appears in 'name'? */ |
| 483 | if (*sep != '\0' && strchr(name, *sep) != NULL) |
| 484 | name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ |
| 485 | luaL_buffinit(L, &buff); |
| 486 | /* add path to the buffer, replacing marks ('?') with the file name */ |
| 487 | luaL_addgsub(&buff, path, LUA_PATH_MARK, name); |
| 488 | luaL_addchar(&buff, '\0'); |
| 489 | pathname = luaL_buffaddr(&buff); /* writable list of file names */ |
| 490 | endpathname = pathname + luaL_bufflen(&buff) - 1; |
| 491 | while ((filename = getnextfilename(&pathname, endpathname)) != NULL) { |
| 492 | if (readable(filename)) /* does file exist and is readable? */ |
| 493 | return lua_pushstring(L, filename); /* save and return name */ |
| 494 | } |
| 495 | luaL_pushresult(&buff); /* push path to create error message */ |
| 496 | pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ |
| 497 | return NULL; /* not found */ |
| 498 | } |
| 499 | |
| 500 | |
| 501 | static int ll_searchpath (lua_State *L) { |
no test coverage detected