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.

Revisions

  1. eddix created this gist Mar 22, 2013.
    25 changes: 25 additions & 0 deletions app_path.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    /* 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;
    }