package poj;

public class Process {
    public static final int CWD_INITIAL_BUFFER_SIZE = 256;

    static {
        Core.ensureLoaded();
    }

    public static native long getpid();

    public static native long getppid();

    public static native long getpgrp();

    public static native long getpgid(long pid);

    public static native long getuid();

    public static native long getgid();

    public static native long geteuid();

    public static native long getegid();

    /* Returns strlen() */
    public static native int getcwd(byte[] dst) throws Errno;

    public static CString currentWorkingDirectory() throws Errno {
        int buffsize = CWD_INITIAL_BUFFER_SIZE;
        byte[] buff;
        while (true) {
            buff = new byte[buffsize];
            try {
                final int length = getcwd(buff);
                return new CString(buff, length, false);
            } catch (Errno e) {
                if (!e.equals(Errnos.ERANGE))
                    throw e;
            }
            buffsize *= 2;
        }
    }

    public static native void kill(long pid, int sig);
}
