Skip to content

Instantly share code, notes, and snippets.

@headius
Created December 6, 2011 19:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/1439542 to your computer and use it in GitHub Desktop.
Save headius/1439542 to your computer and use it in GitHub Desktop.
require 'ffi'
module ChdirExec
extend FFI::Library
ffi_lib 'c'
attach_function :execlp, [:string, :varargs], :int
attach_function :chdir, [:string], :int
def self.chdir_exec(dir, cmd, *argv)
chdir(dir)
argv.unshift(cmd)
argv = ([:string] * argv.size).zip(argv).flatten
argv <<:int
argv << 0
execlp(cmd, *argv)
raise SystemCallError.new(FFI.errno)
end
end
ChdirExec.chdir_exec('/bin', '/bin/sh')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment