Skip to content

Instantly share code, notes, and snippets.

@rene-d
Created October 10, 2018 02:34
Show Gist options
  • Save rene-d/079cd530d21de8c7e8e0614b53689a9a to your computer and use it in GitHub Desktop.
Save rene-d/079cd530d21de8c7e8e0614b53689a9a to your computer and use it in GitHub Desktop.
get the process path by PID on macOS. example: make pathfind && ./pathfind 86504
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libproc.h>
int main(int argc, char* argv[])
{
pid_t pid;
int ret;
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
for (int i = 1; i < argc; ++i)
{
pid = (pid_t) atoi(argv[i]);
ret = proc_pidpath(pid, pathbuf, sizeof(pathbuf));
if (ret <= 0)
{
fprintf(stderr, "pid %d: proc_pidpath error: %s\n", pid, strerror(errno));
}
else
{
printf("pid %d: %s\n", pid, pathbuf);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment