Skip to content

Instantly share code, notes, and snippets.

@soulik
Created May 12, 2015 18:23
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save soulik/82e9d02a818ce12498d1 to your computer and use it in GitHub Desktop.
Save soulik/82e9d02a818ce12498d1 to your computer and use it in GitHub Desktop.
OS type and CPU architecture detection in Lua language
local function getOS()
local raw_os_name, raw_arch_name = '', ''
-- LuaJIT shortcut
if jit and jit.os and jit.arch then
raw_os_name = jit.os
raw_arch_name = jit.arch
else
-- is popen supported?
local popen_status, popen_result = pcall(io.popen, "")
if popen_status then
popen_result:close()
-- Unix-based OS
raw_os_name = io.popen('uname -s','r'):read('*l')
raw_arch_name = io.popen('uname -m','r'):read('*l')
else
-- Windows
local env_OS = os.getenv('OS')
local env_ARCH = os.getenv('PROCESSOR_ARCHITECTURE')
if env_OS and env_ARCH then
raw_os_name, raw_arch_name = env_OS, env_ARCH
end
end
end
raw_os_name = (raw_os_name):lower()
raw_arch_name = (raw_arch_name):lower()
local os_patterns = {
['windows'] = 'Windows',
['linux'] = 'Linux',
['mac'] = 'Mac',
['darwin'] = 'Mac',
['^mingw'] = 'Windows',
['^cygwin'] = 'Windows',
['bsd$'] = 'BSD',
['SunOS'] = 'Solaris',
}
local arch_patterns = {
['^x86$'] = 'x86',
['i[%d]86'] = 'x86',
['amd64'] = 'x86_64',
['x86_64'] = 'x86_64',
['Power Macintosh'] = 'powerpc',
['^arm'] = 'arm',
['^mips'] = 'mips',
}
local os_name, arch_name = 'unknown', 'unknown'
for pattern, name in pairs(os_patterns) do
if raw_os_name:match(pattern) then
os_name = name
break
end
end
for pattern, name in pairs(arch_patterns) do
if raw_arch_name:match(pattern) then
arch_name = name
break
end
end
return os_name, arch_name
end
if select(1, ...) ~= 'os_name' then
print(("%q %q"):format(getOS()))
else
return {
getOS = getOS,
}
end
@mccrafter1212
Copy link

I realize this is 3 years old, although os.getenv('OS') doesn't exist as far as I can tell (tested multiple times all on a Windows OS). A nil value is always returned.

@Aiq0
Copy link

Aiq0 commented Jan 1, 2021

@soulik Shouldn't there be ['sunos'] instead of ['SunOs'] in os_patterns and ['power macintosh'] instead of ['Power Macintosh'] in arch_patterns as raw_os_name and raw_arch_name are lowered (else it won't be matched and so detected incorrectly in those cases)?
Edit: Also, there is missing for example x64 architecture pattern (which I get from jit.arch). There is list of what can be in jit.os and what can be in jit.arch

@bluebird75
Copy link

I created a minimal project out of this : https://github.com/bluebird75/lua_get_os_name

@soulik
Copy link
Author

soulik commented May 24, 2022

That's cool @bluebird75 😃 . I remember this code started as a quick way to determine OS I'm running since there was simply no other way without additional dependencies.

@Ismoh
Copy link

Ismoh commented Dec 3, 2022

Thanks for sharing this! 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment