jnr.ffi.Runtime runtime = Runtime.getSystemRuntime();
Pointer NULL = Pointer.wrap(runtime, 0L);
SmallC sc = Library.loadLibrary("c", SmallC.class);
final IntByReference oldlenp = new IntByReference(4);
rs = sc.sysctl(new int[]{CTL_KERN, KERN_PROCARGS2, sc.getpid()}, 3,
NULL, oldlenp,
NULL, 0);
if (rs != 0) {
throw new IllegalStateException(sc.strerror(rs));
}
final Pointer oldp = Memory.allocateDirect(runtime, oldlenp.getValue());
rs = sc.sysctl(new int[]{CTL_KERN, KERN_PROCARGS2, sc.getpid()}, 3,
oldp, oldlenp,
NULL, 0);
if (rs != 0) {
throw new IllegalStateException(sc.strerror(rs));
}
class ProcessStuffReader
{
private int offset = 0;
private final int argc;
private final int size;
private final List<String> argv;
ProcessStuffReader()
{
this.size = oldlenp.getValue();
argc = readInt();
readString(); // exec path?
skipNulls();
List<String> argv = new ArrayList<String>(argc);
for (int i = 0; i < argc; i++) {