diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-12-20 11:47:29 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-12-20 11:47:29 -0200 |
commit | 4194de70e72cd7048872463308f5a9a34bf994dc (patch) | |
tree | 54e9fd90dd534a0d57869e44ad7997debdbb871d /lauxlib.c | |
parent | c6254dceffca200e4111f28af4230b6c892ec0d6 (diff) | |
download | lua-4194de70e72cd7048872463308f5a9a34bf994dc.tar.gz lua-4194de70e72cd7048872463308f5a9a34bf994dc.tar.bz2 lua-4194de70e72cd7048872463308f5a9a34bf994dc.zip |
details
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 25 |
1 files changed, 14 insertions, 11 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.125 2004/09/21 16:54:32 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.126 2004/09/29 21:03:14 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -375,9 +375,19 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, | |||
375 | } | 375 | } |
376 | 376 | ||
377 | 377 | ||
378 | static int readable (const char *fname) { | ||
379 | int err; | ||
380 | FILE *f = fopen(fname, "r"); /* try to open file */ | ||
381 | if (f == NULL) return 0; /* open failed */ | ||
382 | getc(f); /* try to read it */ | ||
383 | err = ferror(f); | ||
384 | fclose(f); | ||
385 | return (err == 0); | ||
386 | } | ||
387 | |||
388 | |||
378 | LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, | 389 | LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, |
379 | const char *path) { | 390 | const char *path) { |
380 | FILE *f; | ||
381 | const char *p = path; | 391 | const char *p = path; |
382 | for (;;) { | 392 | for (;;) { |
383 | const char *fname; | 393 | const char *fname; |
@@ -387,15 +397,8 @@ LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, | |||
387 | } | 397 | } |
388 | fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); | 398 | fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); |
389 | lua_remove(L, -2); /* remove path template */ | 399 | lua_remove(L, -2); /* remove path template */ |
390 | f = fopen(fname, "r"); /* try to open it */ | 400 | if (readable(fname)) /* does file exist and is readable? */ |
391 | if (f) { | 401 | return fname; /* return that file name */ |
392 | int err; | ||
393 | getc(f); /* try to read file */ | ||
394 | err = ferror(f); | ||
395 | fclose(f); | ||
396 | if (err == 0) /* open and read sucessful? */ | ||
397 | return fname; /* return that file name */ | ||
398 | } | ||
399 | lua_pop(L, 1); /* remove file name */ | 402 | lua_pop(L, 1); /* remove file name */ |
400 | } | 403 | } |
401 | } | 404 | } |