Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. /**
  2.  * returns an absolute path from a relative file name
  3.  */
  4.  
  5. #ifdef __cplusplus
  6.   #include "lua.hpp"
  7. #else
  8.   #include "lua.h"
  9.   #include "lualib.h"
  10.   #include "lauxlib.h"
  11. #endif
  12. #include <math.h>
  13.  
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <errno.h>
  17.  
  18. #include "libluaph1.h"
  19.  
  20. #ifdef __cplusplus
  21. extern "C"{
  22. #endif
  23.  
  24. static int os_setenv(lua_State* L) {
  25.     const char* name = luaL_checkstring(L, 1);
  26.     const char* value = luaL_checkstring(L, 2);
  27.    
  28.     int len = strlen(name) + strlen(value) + 2; //adding one char for '=' and another for '\0'
  29.     char buffer[len];
  30.     strcpy(buffer, name);
  31.     strcat(buffer, "=");
  32.     strcat(buffer, value);
  33.     buffer[len] = 0;
  34.    
  35.     int res = putenv(buffer);
  36.     printf("env (%d): %s, %s\n", res, buffer, getenv(name));
  37.     if (res != 0) {
  38.         lua_pushnil(L);
  39.         lua_pushfstring(L, "unable to set environment variable '%s', errno %d : %s", name, errno, strerror(errno));
  40.         return 2;
  41.     }
  42.    
  43.     lua_pushnumber(L, res);
  44.     return 1;
  45. }
  46.  
  47. static int os_getenv(lua_State* L) {
  48.     const char* name = luaL_checkstring(L, 1);
  49.    
  50.     char *buffer;
  51.     printf("name: %s\n", name);
  52.     buffer = getenv(name);
  53.     printf("env: %s\n", buffer);
  54.     if (buffer == NULL) {
  55.         lua_pushnil(L);
  56.         lua_pushfstring(L, "unable to get environment variable '%s'", name);
  57.         return 2;
  58.     }
  59.    
  60.     lua_pushstring(L, buffer);
  61.     return 1;
  62. }
  63.  
  64. static int os_realpath(lua_State* L) {
  65.     char result[PH_PATH_MAX];
  66.     int ok;
  67.  
  68.     const char* path = luaL_checkstring(L, 1);
  69.  
  70. #ifdef __MINGW32__
  71.     ok = (_fullpath(result, path, PH_PATH_MAX) != NULL);
  72. #else
  73.     ok = (realpath(path, result) != NULL);
  74. #endif
  75.  
  76.     if (!ok) {
  77.         lua_pushnil(L);
  78.         lua_pushfstring(L, "unable to fetch real path of '%s', errno %d : %s", path, errno, strerror(errno));
  79.         return 2;
  80.     }
  81.  
  82.     lua_pushstring(L, result);
  83.     return 1;
  84. }
  85.  
  86. //library to be registered
  87. static const struct luaL_Reg libluaph1 [] = {
  88.       {"realpath", os_realpath},
  89.       {"setenv", os_setenv},
  90.       {"getenv", os_getenv},
  91.       {NULL, NULL}  /* sentinel */
  92.     };
  93.  
  94. //name of this function is not flexible
  95. int luaopen_libluaph1 (lua_State *L){
  96.     luaL_newlib(L, libluaph1);
  97.     return 1;
  98. }
  99.  
  100. #ifdef __cplusplus
  101. }
  102. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement