Added new function os.stat to fetch information about a file
This commit is contained in:
parent
764400861b
commit
3e8ee40315
@ -27,6 +27,7 @@
|
||||
* Feature 3100194: English alais' for Optimize flags
|
||||
* Bug 3308203: Incorrect relative paths for gmake sibling static libraries (Adam)
|
||||
* Bug 3277343: SM_SERVERR2 is not always defined by default (Martin Ridgers)
|
||||
* Added os.stat
|
||||
|
||||
-------
|
||||
4.3
|
||||
|
46
src/host/os_stat.c
Executable file
46
src/host/os_stat.c
Executable file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* \file os_stat.c
|
||||
* \brief Retrieve information about a file.
|
||||
* \author Copyright (c) 2011 Jason Perkins and the Premake project
|
||||
*/
|
||||
|
||||
#include "premake.h"
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
|
||||
int os_stat(lua_State* L)
|
||||
{
|
||||
struct stat s;
|
||||
|
||||
const char* filename = luaL_checkstring(L, 1);
|
||||
if (stat(filename, &s) != 0)
|
||||
{
|
||||
lua_pushnil(L);
|
||||
switch (errno)
|
||||
{
|
||||
case EACCES:
|
||||
lua_pushfstring(L, "'%s' could not be accessed", filename);
|
||||
break;
|
||||
case ENOENT:
|
||||
lua_pushfstring(L, "'%s' was not found", filename);
|
||||
break;
|
||||
default:
|
||||
lua_pushfstring(L, "An unknown error %d occured while accessing '%s'", errno, filename);
|
||||
break;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
lua_newtable(L);
|
||||
|
||||
lua_pushstring(L, "mtime");
|
||||
lua_pushinteger(L, (lua_Integer)s.st_mtime);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushstring(L, "size");
|
||||
lua_pushnumber(L, s.st_size);
|
||||
lua_settable(L, -3);
|
||||
|
||||
return 1;
|
||||
}
|
@ -49,6 +49,7 @@ static const luaL_Reg os_functions[] = {
|
||||
{ "mkdir", os_mkdir },
|
||||
{ "pathsearch", os_pathsearch },
|
||||
{ "rmdir", os_rmdir },
|
||||
{ "stat", os_stat },
|
||||
{ "uuid", os_uuid },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
@ -67,6 +67,7 @@ int os_matchstart(lua_State* L);
|
||||
int os_mkdir(lua_State* L);
|
||||
int os_pathsearch(lua_State* L);
|
||||
int os_rmdir(lua_State* L);
|
||||
int os_stat(lua_State* L);
|
||||
int os_uuid(lua_State* L);
|
||||
int string_endswith(lua_State* L);
|
||||
|
||||
|
Reference in New Issue
Block a user