Skip to content

Instantly share code, notes, and snippets.

@sstephenson
Created October 29, 2011 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sstephenson/1324788 to your computer and use it in GitHub Desktop.
Save sstephenson/1324788 to your computer and use it in GitHub Desktop.
#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