Skip to content

Instantly share code, notes, and snippets.

@ik5
Created June 21, 2012 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ik5/2965189 to your computer and use it in GitHub Desktop.
Save ik5/2965189 to your computer and use it in GitHub Desktop.
Using uname with FFI - Based on https://gist.github.com/895585
#!/usr/bin/env ruby
require 'rubygems'
require 'ffi'
module Uname
extend FFI::Library
ffi_lib FFI::CURRENT_PROCESS
class UTSName < FFI::Struct
layout :sysname , [:char, 65],
:nodename , [:char, 65],
:release , [:char, 65],
:version , [:char, 65],
:machine , [:char, 65],
:domainname, [:char, 65]
def to_hash
strings = values.map { |v| v.to_s }
Hash[members.zip strings]
end
end
attach_function :uname, [:pointer], :int
def self.get
uts = UTSName.new
if uname(uts) != 0
{ :sysname => '', :nodename => '', :release => '',
:version => '', :machine => '', :domainname => ''
}
else
uts.to_hash
end
end
puts Uname.get.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment