Last active
January 22, 2021 16:49
-
-
Save docwhat/03a77a3950a696b2bc243a6df005128f to your computer and use it in GitHub Desktop.
macOS program to get current working directory of any PID.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <libproc.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
// Works on macOS using libproc. | |
int main(int argc, char *argv[]) { | |
int ret; | |
pid_t pid; | |
struct proc_vnodepathinfo vpi; | |
if (argc == 2) { | |
pid = (pid_t)atoi(argv[1]); | |
ret = proc_pidinfo(pid, PROC_PIDVNODEPATHINFO, 0, &vpi, | |
PROC_PIDVNODEPATHINFO_SIZE); | |
if (ret <= 0) { | |
fprintf(stderr, "%s\n", strerror(errno)); | |
return 1; | |
} | |
printf("%s\n", vpi.pvi_cdir.vip_path); | |
} else { | |
printf("Usage: %s <pid>\n\n", argv[0]); | |
printf("Print the current working directory for <pid> to stdout.\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment