os_stat.c 894 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * \file os_stat.c
  3. * \brief Retrieve information about a file.
  4. * \author Copyright (c) 2011 Jason Perkins and the Premake project
  5. */
  6. #include "premake.h"
  7. #include <sys/stat.h>
  8. #include <errno.h>
  9. int os_stat(lua_State* L)
  10. {
  11. struct stat s;
  12. const char* filename = luaL_checkstring(L, 1);
  13. if (stat(filename, &s) != 0)
  14. {
  15. lua_pushnil(L);
  16. switch (errno)
  17. {
  18. case EACCES:
  19. lua_pushfstring(L, "'%s' could not be accessed", filename);
  20. break;
  21. case ENOENT:
  22. lua_pushfstring(L, "'%s' was not found", filename);
  23. break;
  24. default:
  25. lua_pushfstring(L, "An unknown error %d occured while accessing '%s'", errno, filename);
  26. break;
  27. }
  28. return 2;
  29. }
  30. lua_newtable(L);
  31. lua_pushstring(L, "mtime");
  32. lua_pushinteger(L, (lua_Integer)s.st_mtime);
  33. lua_settable(L, -3);
  34. lua_pushstring(L, "size");
  35. lua_pushnumber(L, s.st_size);
  36. lua_settable(L, -3);
  37. return 1;
  38. }