=Start=
缘由:
学习需要
正文:
参考解答:
#include <stdlib.h> #include <stdio.h> #ifndef PATH_MAX #define PATH_MAX 128 #endif int main( int argc, char const *argv[]) { if (argc < 2 ) { return 1 ; } char resolved_path[PATH_MAX]; int i; for (i = 1 ; i < argc; i++){ realpath(argv[i], resolved_path); printf( "%s -> %s\n" , argv[i], resolved_path); } return 0 ; } |
&
#ifndef PATH_MAX #define PATH_MAX 1024 #endif int get_path_from_pid ( int pid, char path[PATH_MAX]) { char file[PATH_MAX]; snprintf(file, PATH_MAX, "/proc/%d/exe" , pid); int len = 0 ; if ((len = readlink(file, path, PATH_MAX- 1 )) != - 1 ) { path[len] = '\0' ; return 0 ; } path[ 0 ] = '\0' ; return - 1 ; } |
参考链接:
- https://stackoverflow.com/questions/2341808/how-to-get-the-absolute-path-for-a-given-relative-path-programmatically-in-linux
- https://stackoverflow.com/questions/229012/getting-absolute-path-of-a-file
- http://man7.org/linux/man-pages/man3/realpath.3.html
- http://man7.org/linux/man-pages/man2/readlink.2.html
=END=