Skip to content

Instantly share code, notes, and snippets.

@docwhat
Last active January 22, 2021 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save docwhat/03a77a3950a696b2bc243a6df005128f to your computer and use it in GitHub Desktop.
Save docwhat/03a77a3950a696b2bc243a6df005128f to your computer and use it in GitHub Desktop.
macOS program to get current working directory of any PID.
#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