Created
March 31, 2011 00:28
-
-
Save jarib/895585 to your computer and use it in GitHub Desktop.
uname() - ruby ffi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ffi' | |
module Uname | |
extend FFI::Library | |
ffi_lib FFI::CURRENT_PROCESS | |
class UtsName < FFI::Struct | |
layout :sysname , [:char, 256], | |
:nodename, [:char, 256], | |
:release , [:char, 256], | |
:version , [:char, 256], | |
:machine , [:char, 256] | |
def to_hash | |
strings = values.map { |v| v.to_s } | |
Hash[members.zip strings] | |
end | |
end | |
attach_function :uname, [:pointer], :int | |
def self.get | |
un = UtsName.new | |
if uname(un) != 0 | |
raise "failed" # check errno | |
end | |
un.to_hash | |
end | |
end | |
if __FILE__ == $0 | |
p Uname.get | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment