Skip to content

Instantly share code, notes, and snippets.

@eddix
Created March 22, 2013 07:44
Show Gist options
  • Select an option

  • Save eddix/5219617 to your computer and use it in GitHub Desktop.

Select an option

Save eddix/5219617 to your computer and use it in GitHub Desktop.
get application real path in c. it needs argv[0].
/* 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