/* Given a process ID, this program will print its parent using procfs */
#include <stdio.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/procfs.h>

main(int argc, char *argv[]) {
struct prstatus pstat;
char procfile[30]="/proc/";
int fd;

if (argc < 2) {
fprintf(stderr, "You must supply an argument\n");
exit(1);
}

strcat(procfile, argv[1]);

if ((fd = open(procfile, O_RDONLY)) < 0) {
fprintf(stderr, "Error opening %s", procfile);
perror(" ");
exit(1);
}

/* Grab process information/status */
if (ioctl(fd, PIOCSTATUS, (void *) &pstat) < 0) {
perror("getting process status");
exit(1);
}

printf("%d\n", pstat.pr_ppid);
exit(0);
}
