diff options
author | Manuel Pégourié-Gonnard <mpg@elzevir.fr> | 2010-03-28 14:34:44 +0200 |
---|---|---|
committer | Manuel Pégourié-Gonnard <mpg@elzevir.fr> | 2010-03-28 14:34:44 +0200 |
commit | 0bb6d1e8d28f914398d3b3de15cf5317f480d02a (patch) | |
tree | a222ca028b0f3bec9f4057424623323602174c29 /luaextra-os.lua | |
parent | 7047b46b6fe6dead388c8427a7510710f920b617 (diff) | |
parent | 22fa4cc9381179fdd43ca8a251cb49dd2703079d (diff) | |
download | lualibs-0bb6d1e8d28f914398d3b3de15cf5317f480d02a.tar.gz |
Merge remote branch 'github/master'
Diffstat (limited to 'luaextra-os.lua')
-rw-r--r-- | luaextra-os.lua | 217 |
1 files changed, 176 insertions, 41 deletions
diff --git a/luaextra-os.lua b/luaextra-os.lua index b60dfb7..4f0c0c1 100644 --- a/luaextra-os.lua +++ b/luaextra-os.lua @@ -8,7 +8,7 @@ if not modules then modules = { } end modules ['l-os'] = { -- maybe build io.flush in os.execute -local find, format = string.find, string.format +local find, format, gsub = string.find, string.format, string.gsub local random, ceil = math.random, math.ceil local execute, spawn, exec, ioflush = os.execute, os.spawn or os.execute, os.exec or os.execute, io.flush @@ -43,6 +43,12 @@ end os.type = os.type or (io.pathseparator == ";" and "windows") or "unix" os.name = os.name or (os.type == "windows" and "mswin" ) or "linux" +if os.type == "windows" then + os.libsuffix, os.binsuffix = 'dll', 'exe' +else + os.libsuffix, os.binsuffix = 'so', '' +end + function os.launch(str) if os.type == "windows" then os.execute("start " .. str) -- os.spawn ? @@ -51,10 +57,6 @@ function os.launch(str) end end -if not os.setenv then - function os.setenv() return false end -end - if not os.times then -- utime = user time -- stime = system time @@ -86,63 +88,182 @@ end -- no need for function anymore as we have more clever code and helpers now -os.platform = os.name or os.type or "linux" -os.libsuffix = 'so' -os.binsuffix = '' +os.resolvers = os.resolvers or { } -local name = os.name +local resolvers = os.resolvers -if name == "windows" or name == "mswin" or name == "win32" or name == "msdos" or os.type == "windows" then - if os.getenv("PROCESSOR_ARCHITECTURE") == "AMD64" then - os.platform = "mswin-64" - else - os.platform = "mswin" +local osmt = getmetatable(os) or { __index = function(t,k) t[k] = "unset" return "unset" end } +local osix = osmt.__index + +osmt.__index = function(t,k) + return (resolvers[k] or osix)(t,k) +end + +setmetatable(os,osmt) + +if not os.setenv then + + -- we still store them but they won't be seen in + -- child processes although we might pass them some day + -- using command concatination + + local env, getenv = { }, os.getenv + + function os.setenv(k,v) + env[k] = v end - os.libsuffix = 'dll' - os.binsuffix = 'exe' -else - local architecture = os.getenv("HOSTTYPE") or "" - if architecture == "" then - architecture = os.resultof("uname -m") or "" + + function os.getenv(k) + return env[k] or getenv(k) end - if architecture == "" then - local architecture = os.resultof("echo $HOSTTYPE") + +end + +-- we can use HOSTTYPE on some platforms + +local name, platform = os.name or "linux", os.getenv("MTX_PLATFORM") or "" + +local function guess() + local architecture = os.resultof("uname -m") or "" + if architecture ~= "" then + return architecture + end + architecture = os.getenv("HOSTTYPE") or "" + if architecture ~= "" then + return architecture + end + return os.resultof("echo $HOSTTYPE") or "" +end + +if platform ~= "" then + + os.platform = platform + +elseif os.type == "windows" then + + -- we could set the variable directly, no function needed here + + function os.resolvers.platform(t,k) + local platform, architecture = "", os.getenv("PROCESSOR_ARCHITECTURE") or "" + if find(architecture,"AMD64") then + platform = "mswin-64" + else + platform = "mswin" + end + os.setenv("MTX_PLATFORM",platform) + os.platform = platform + return platform end - if name == "linux" then + +elseif name == "linux" then + + function os.resolvers.platform(t,k) + -- we sometims have HOSTTYPE set so let's check that first + local platform, architecture = "", os.getenv("HOSTTYPE") or os.resultof("uname -m") or "" if find(architecture,"x86_64") then - os.platform = "linux-64" + platform = "linux-64" elseif find(architecture,"ppc") then - os.platform = "linux-ppc" + platform = "linux-ppc" else - os.platform = "linux" + platform = "linux" end - elseif name == "macosx" then - if find(architecture,"i386") then - os.platform = "osx-intel" + os.setenv("MTX_PLATFORM",platform) + os.platform = platform + return platform + end + +elseif name == "macosx" then + + --[[ + Identifying the architecture of OSX is quite a mess and this + is the best we can come up with. For some reason $HOSTTYPE is + a kind of pseudo environment variable, not known to the current + environment. And yes, uname cannot be trusted either, so there + is a change that you end up with a 32 bit run on a 64 bit system. + Also, some proper 64 bit intel macs are too cheap (low-end) and + therefore not permitted to run the 64 bit kernel. + ]]-- + + function os.resolvers.platform(t,k) + -- local platform, architecture = "", os.getenv("HOSTTYPE") or "" + -- if architecture == "" then + -- architecture = os.resultof("echo $HOSTTYPE") or "" + -- end + local platform, architecture = "", os.resultof("echo $HOSTTYPE") or "" + if architecture == "" then + -- print("\nI have no clue what kind of OSX you're running so let's assume an 32 bit intel.\n") + platform = "osx-intel" + elseif find(architecture,"i386") then + platform = "osx-intel" elseif find(architecture,"x86_64") then - os.platform = "osx-64" + platform = "osx-64" else - os.platform = "osx-ppc" + platform = "osx-ppc" end - elseif name == "sunos" then + os.setenv("MTX_PLATFORM",platform) + os.platform = platform + return platform + end + +elseif name == "sunos" then + + function os.resolvers.platform(t,k) + local platform, architecture = "", os.resultof("uname -m") or "" if find(architecture,"sparc") then - os.platform = "solaris-sparc" + platform = "solaris-sparc" else -- if architecture == 'i86pc' - os.platform = "solaris-intel" + platform = "solaris-intel" end - elseif name == "freebsd" then + os.setenv("MTX_PLATFORM",platform) + os.platform = platform + return platform + end + +elseif name == "freebsd" then + + function os.resolvers.platform(t,k) + local platform, architecture = "", os.resultof("uname -m") or "" if find(architecture,"amd64") then - os.platform = "freebsd-amd64" + platform = "freebsd-amd64" else - os.platform = "freebsd" + platform = "freebsd" end - else - os.platform = 'linux' + os.setenv("MTX_PLATFORM",platform) + os.platform = platform + return platform end + +elseif name == "kfreebsd" then + + function os.resolvers.platform(t,k) + -- we sometims have HOSTTYPE set so let's check that first + local platform, architecture = "", os.getenv("HOSTTYPE") or os.resultof("uname -m") or "" + if find(architecture,"x86_64") then + platform = "kfreebsd-64" + else + platform = "kfreebsd-i386" + end + os.setenv("MTX_PLATFORM",platform) + os.platform = platform + return platform + end + +else + + -- platform = "linux" + -- os.setenv("MTX_PLATFORM",platform) + -- os.platform = platform + + function os.resolvers.platform(t,k) + local platform = "linux" + os.setenv("MTX_PLATFORM",platform) + os.platform = platform + return platform + end + end -- beware, we set the randomseed --- -- from wikipedia: Version 4 UUIDs use a scheme relying only on random numbers. This algorithm sets the -- version number as well as two reserved bits. All other bits are set using a random or pseudorandom @@ -151,7 +272,6 @@ end -- -- as we don't call this function too often there is not so much risk on repetition - local t = { 8, 9, "a", "b" } function os.uuid() @@ -163,3 +283,18 @@ function os.uuid() random(0xFFFF),random(0xFFFF),random(0xFFFF) ) end + +local d + +function os.timezone(delta) + d = d or tonumber(tonumber(os.date("%H")-os.date("!%H"))) + if delta then + if d > 0 then + return format("+%02i:00",d) + else + return format("-%02i:00",-d) + end + else + return 1 + end +end |