Created
October 29, 2011 17:10
-
-
Save sstephenson/1324788 to your computer and use it in GitHub Desktop.
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 <string.h> | |
#include <stdio.h> | |
#include <sys/param.h> | |
#include <stdlib.h> | |
char *relative_prefix(char *argv0, char *prefix) { | |
char *file_name, *result; | |
size_t size; | |
if (argv0 == NULL) { | |
return realpath(".", prefix); | |
} | |
size = strlen(argv0) + 7; | |
file_name = malloc(size); | |
if (file_name == NULL) { | |
return NULL; | |
} | |
snprintf(file_name, size, "%s/../..", argv0); | |
result = realpath(file_name, prefix); | |
free(file_name); | |
return result; | |
} | |
int main(int argc, char **argv) { | |
char prefix[PATH_MAX]; | |
fprintf(stderr, "libpath = %s\n", relative_prefix(argv[0], prefix)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment