Created
October 10, 2018 02:34
-
-
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
This file contains hidden or 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 <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