diff options
author | Leon Bottou <leon@bottou.org> | 2013-08-24 22:18:41 +0400 |
---|---|---|
committer | Leon Bottou <leon@bottou.org> | 2013-08-24 22:18:41 +0400 |
commit | e001b093a47a737dc6c0b0526186918e7e8049c3 (patch) | |
tree | b97c7e5d8164d4fbc0d0362c960189d7dc310e4d | |
parent | 57a14c39b71680f478b6710b418f77c4016d9c35 (diff) |
added paths.uname()
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | dok/index.dok | 29 | ||||
-rw-r--r-- | init.lua.in | 15 | ||||
-rw-r--r-- | paths.c | 72 | ||||
-rw-r--r-- | paths.h.in | 4 |
5 files changed, 110 insertions, 11 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index b28430f..2ef9f2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ IF (UNIX OR NOT WIN32) CHECK_INCLUDE_FILES(time.h HAVE_TIME_H) CHECK_INCLUDE_FILES(sys/time.h HAVE_SYS_TIME_H) CHECK_INCLUDE_FILES(sys/ndir.h HAVE_SYS_NDIR_H) + CHECK_INCLUDE_FILES(sys/utsname.h HAVE_SYS_UTSNAME_H) CHECK_INCLUDE_FILES(sys/dir.h HAVE_SYS_DIR_H) CHECK_INCLUDE_FILES(ndir.h HAVE_NDIR_H) CHECK_FUNCTION_EXISTS(getcwd HAVE_GETCWD) diff --git a/dok/index.dok b/dok/index.dok index ab5b2c9..0b7dbd6 100644 --- a/dok/index.dok +++ b/dok/index.dok @@ -93,7 +93,7 @@ for which the user has no permission to write. {{anchor:paths.dirs.dok}} The following functions can be used -to examine directory contents. +to examine directory contents or manipulate directories. ==== paths.dir(dname) ==== @@ -239,4 +239,29 @@ This directory is used to build variable ''package.cpath''. ==== paths.home ==== {{anchor:paths.home}} -The home directory of the current user.
\ No newline at end of file +The home directory of the current user. + + +===== Operating system info ===== +{{anchor:paths.osinfo.dok}} + + +==== paths.uname() ==== +{{anchor:paths.uname}} + +Returns up to three strings describing the operating system. +The first string is a system name, e.g., "Windows", "Linux", "Darwin", "FreeBSD", etc. +The second string is the network name of this computer. +The third string indicates the processor type. + +==== paths.is_win() ==== +{{anchor:paths.is_win}} + +Returns true if the operating system is Microsoft Windows. + +==== paths.is_mac() ==== +{{anchor:paths.is_mac}} + +Returns true if the operating system is Mac OS X. + + diff --git a/init.lua.in b/init.lua.in index 684dba9..515437f 100644 --- a/init.lua.in +++ b/init.lua.in @@ -44,10 +44,19 @@ install_lua_cpath = concat(install_prefix, install_lua_cpath_subdir) assert(concat(install_bin,install_bin_ridbus) == install_prefix:gsub('/$','')) assert(concat(install_cmake,install_cmake_ridbus) == install_prefix:gsub('/$','')) -if dirp("C:\\") == false then - home = os.getenv('HOME') or '.' +function is_win() + return uname():match('Windows') +end + +function is_mac() + return uname():match('Darwin') +end + +if is_win() then + home = os.getenv('HOMEDRIVE') or 'C:' + home = home .. ( os.getenv('HOMEPATH') or '\\' ) else - home = ( os.getenv('HOMEDRIVE') or 'C:' ) .. ( os.getenv('HOMEPATH') or '\\' ) + home = os.getenv('HOME') or '.' end function files(s) @@ -661,7 +661,8 @@ struct tmpname_s { char tmp[4]; }; -static int gc_tmpname(lua_State *L) +static int +gc_tmpname(lua_State *L) { if (lua_isuserdata(L, -1)) { @@ -678,7 +679,8 @@ static int gc_tmpname(lua_State *L) } -static void add_tmpname(lua_State *L, const char *tmp) +static void +add_tmpname(lua_State *L, const char *tmp) { struct tmpname_s **pp = 0; lua_pushlightuserdata(L, (void*)tmpnames_key); @@ -722,7 +724,8 @@ static void add_tmpname(lua_State *L, const char *tmp) } -static int lua_tmpname(lua_State *L) +static int +lua_tmpname(lua_State *L) { #ifdef LUA_WIN char *tmp = _tempnam("c:/temp", "luatmp"); @@ -743,7 +746,13 @@ static int lua_tmpname(lua_State *L) } } -static int pushresult (lua_State *L, int i, const char *filename) { + + +/* ------------------------------------------------------ */ +/* mkdir, rmdir */ + +static int +pushresult (lua_State *L, int i, const char *filename) { int en = errno; if (i) { lua_pushboolean(L, 1); @@ -757,7 +766,8 @@ static int pushresult (lua_State *L, int i, const char *filename) { } } -static int lua_mkdir(lua_State *L) +static int +lua_mkdir(lua_State *L) { int status = 0; const char *s = luaL_checkstring(L, 1); @@ -775,7 +785,8 @@ static int lua_mkdir(lua_State *L) return pushresult(L, status == 0, s); } -static int lua_rmdir(lua_State *L) +static int +lua_rmdir(lua_State *L) { const char *s = luaL_checkstring(L, 1); #ifdef LUA_WIN @@ -787,6 +798,48 @@ static int lua_rmdir(lua_State *L) } +/* ------------------------------------------------------ */ +/* uname */ + + +static int lua_uname(lua_State *L) +{ +#if defined(LUA_WIN) + const char *name; + SYSTEM_INFO info; + lua_pushliteral(L, "Windows"); + name = getenv("COMPUTERNAME"); + lua_pushstring(L, name ? name : ""); + if (!GetSystemInfo(&info)) + lua_pushliteral(L, ""); + else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) + lua_pushliteral(L, "AMD64"); + else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL) + lua_pushliteral(L, "X86"); + else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM) + lua_pushliteral(L, "ARM"); + else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) + lua_pushliteral(L, "IA64"); + else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) + lua_pushstring(L, ""); + return 3; +#else +# if defined(HAVE_SYS_UTSNAME_H) + struct utsname info; + if (uname(&info) >= 0) + { + lua_pushstring(L, info.sysname); + lua_pushstring(L, info.nodename); + lua_pushstring(L, info.machine); + return 3; + } +# endif + lua_pushstring(L, "Unknown"); + return 1; +#endif +} + + /* ------------------------------------------------------ */ /* require (with global flag) */ @@ -915,6 +968,12 @@ path_require(lua_State *L) #endif + +/* ------------------------------------------------------ */ +/* uname */ + + + /* ------------------------------------------------------ */ /* register */ @@ -931,6 +990,7 @@ static const struct luaL_Reg paths__ [] = { {"tmpname", lua_tmpname}, {"mkdir", lua_mkdir}, {"rmdir", lua_rmdir}, + {"uname", lua_uname}, {"require", path_require}, {NULL, NULL} }; @@ -40,6 +40,7 @@ #cmakedefine HAVE_SYS_TIME_H 1 #cmakedefine HAVE_SYS_NDIR_H 1 #cmakedefine HAVE_SYS_DIR_H 1 +#cmakedefine HAVE_SYS_UTSNAME_H 1 #cmakedefine HAVE_NDIR_H 1 #cmakedefine HAVE_GETCWD 1 @@ -55,6 +56,9 @@ # if HAVE_SYS_TIME_H # include <sys/time.h> # endif +# if HAVE_SYS_UTSNAME_H +# include <sys/utsname.h> +# endif # if HAVE_TIME_H # include <time.h> # endif |