Skip to content

Instantly share code, notes, and snippets.

@pauladam
Created March 5, 2013 22:37
Show Gist options
  • Select an option

  • Save pauladam/5094987 to your computer and use it in GitHub Desktop.

Select an option

Save pauladam/5094987 to your computer and use it in GitHub Desktop.
Example of evaluating c structs from c function calls from Julia. N.B. C struct types are of the OSX variant
type c_passwd
pw_name::Ptr{Uint8}
pw_passwd::Ptr{Uint8}
pw_uid::Int32
pw_gid::Int32
pw_change::Int32
pw_class::Ptr{Uint8}
pw_gecos::Ptr{Uint8}
pw_dir::Ptr{Uint8}
pw_shell::Ptr{Uint8}
pw_expire::Int32
pw_fields::Int32
end
type j_passwd
cpwd::c_passwd
name::ASCIIString
passwd::ASCIIString
uid::Int32
gid::Int32
change::Int32
pw_class::ASCIIString
gecos::ASCIIString
dir::ASCIIString
shell::ASCIIString
expire::Int32
fields::Int32
end
function j_passwd(pt::Ptr{c_passwd})
cpwd = unsafe_ref(pt)
j_passwd(cpwd, bytestring(cpwd.pw_name),
bytestring(cpwd.pw_passwd),
cpwd.pw_uid,
cpwd.pw_gid,
cpwd.pw_change,
bytestring(cpwd.pw_class),
bytestring(cpwd.pw_gecos),
bytestring(cpwd.pw_dir),
bytestring(cpwd.pw_shell),
cpwd.pw_expire,
cpwd.pw_fields)
end
for i=1:300
getpwuid_ret = ccall((:getpwuid,:libc), Ptr{c_passwd}, (Int32,), i)
if getpwuid_ret != C_NULL
pw_entry = j_passwd(getpwuid_ret)
println("$(pw_entry.uid): $(pw_entry.name), $(pw_entry.shell), $(pw_entry.dir)")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment