details
This commit is contained in:
25
lauxlib.c
25
lauxlib.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lauxlib.c,v 1.125 2004/09/21 16:54:32 roberto Exp roberto $
|
** $Id: lauxlib.c,v 1.126 2004/09/29 21:03:14 roberto Exp roberto $
|
||||||
** Auxiliary functions for building Lua libraries
|
** Auxiliary functions for building Lua libraries
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -375,9 +375,19 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int readable (const char *fname) {
|
||||||
|
int err;
|
||||||
|
FILE *f = fopen(fname, "r"); /* try to open file */
|
||||||
|
if (f == NULL) return 0; /* open failed */
|
||||||
|
getc(f); /* try to read it */
|
||||||
|
err = ferror(f);
|
||||||
|
fclose(f);
|
||||||
|
return (err == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
|
LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
|
||||||
const char *path) {
|
const char *path) {
|
||||||
FILE *f;
|
|
||||||
const char *p = path;
|
const char *p = path;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
const char *fname;
|
const char *fname;
|
||||||
@@ -387,15 +397,8 @@ LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
|
|||||||
}
|
}
|
||||||
fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
|
fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
|
||||||
lua_remove(L, -2); /* remove path template */
|
lua_remove(L, -2); /* remove path template */
|
||||||
f = fopen(fname, "r"); /* try to open it */
|
if (readable(fname)) /* does file exist and is readable? */
|
||||||
if (f) {
|
return fname; /* return that file name */
|
||||||
int err;
|
|
||||||
getc(f); /* try to read file */
|
|
||||||
err = ferror(f);
|
|
||||||
fclose(f);
|
|
||||||
if (err == 0) /* open and read sucessful? */
|
|
||||||
return fname; /* return that file name */
|
|
||||||
}
|
|
||||||
lua_pop(L, 1); /* remove file name */
|
lua_pop(L, 1); /* remove file name */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user