split the contents of fs.isfile and fs.isdirectory into its own function
and add more docs
This commit is contained in:
parent
fdcb8f794a
commit
2a75eab074
1 changed files with 30 additions and 20 deletions
50
lfs.c
50
lfs.c
|
@ -113,9 +113,35 @@ fs_exists(lua_State *L)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
ismode(lua_State *L, mode_t mode)
|
||||||
|
{
|
||||||
|
struct stat sb;
|
||||||
|
const char *path;
|
||||||
|
|
||||||
|
path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
if (stat(path, &sb) != -1) {
|
||||||
|
lua_pushboolean(L, sb.st_mode & mode);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (errno) {
|
||||||
|
case ENOTDIR:
|
||||||
|
case ENOENT:
|
||||||
|
lua_pushboolean(L, 0);
|
||||||
|
return 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return lfail(L);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Returns true if the given path specifies a
|
* Returns true if the given path specifies a
|
||||||
* directory, or false if it does not.
|
* directory, or false if it does not.
|
||||||
|
* Will return false if the given path does not
|
||||||
|
* specify an existing directory entry at all.
|
||||||
*
|
*
|
||||||
* On error returns nil, an error message and a
|
* On error returns nil, an error message and a
|
||||||
* platform-dependent error code.
|
* platform-dependent error code.
|
||||||
|
@ -130,21 +156,14 @@ end
|
||||||
static int
|
static int
|
||||||
fs_isdirectory(lua_State *L)
|
fs_isdirectory(lua_State *L)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
return ismode(L, S_IFDIR);
|
||||||
const char *path;
|
|
||||||
|
|
||||||
path = luaL_checkstring(L, 1);
|
|
||||||
|
|
||||||
if (stat(path, &sb) == -1)
|
|
||||||
return lfail(L);
|
|
||||||
|
|
||||||
lua_pushboolean(L, sb.st_mode & S_IFDIR);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Returns true if the given path specifies a file,
|
* Returns true if the given path specifies a file,
|
||||||
* or false if it does not.
|
* or false if it does not.
|
||||||
|
* Will return false if the given path does not
|
||||||
|
* specify an existing directory entry at all.
|
||||||
*
|
*
|
||||||
* On error returns nil, an error message and a
|
* On error returns nil, an error message and a
|
||||||
* platform-dependent error code.
|
* platform-dependent error code.
|
||||||
|
@ -159,16 +178,7 @@ end
|
||||||
static int
|
static int
|
||||||
fs_isfile(lua_State *L)
|
fs_isfile(lua_State *L)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
return ismode(L, S_IFREG);
|
||||||
const char *path;
|
|
||||||
|
|
||||||
path = luaL_checkstring(L, 1);
|
|
||||||
|
|
||||||
if (stat(path, &sb) == -1)
|
|
||||||
return lfail(L);
|
|
||||||
|
|
||||||
lua_pushboolean(L, sb.st_mode & S_IFREG);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue