Created
March 22, 2013 07:44
-
-
Save eddix/5219617 to your computer and use it in GitHub Desktop.
get application real path in c. it needs argv[0].
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
| /* app_path | |
| * | |
| * get application path, it need argv[0], and store the result to path. | |
| * | |
| * */ | |
| char * app_path(char * path, const char * argv0) | |
| { | |
| char buf[PATH_MAX]; | |
| char * pos; | |
| if (argv0[0] == '/') { // run with absolute path | |
| strcpy(buf, argv0); | |
| } else { // run with relative path | |
| if(NULL == getcwd(buf, PATH_MAX)) { | |
| perror("getcwd error"); | |
| return NULL; | |
| } | |
| strcat(buf, "/"); | |
| strcat(buf, argv0); | |
| } | |
| if (NULL == realpath(buf, path)) { | |
| perror("realpath error"); | |
| return NULL; | |
| } | |
| return path; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment