Skip to content

Instantly share code, notes, and snippets.

@tftio
Created February 8, 2015 17:00
Show Gist options
  • Save tftio/2f1dbefdf9b771de7997 to your computer and use it in GitHub Desktop.
Save tftio/2f1dbefdf9b771de7997 to your computer and use it in GitHub Desktop.
realpath
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
/* cc -Wall -Wextra -pedantic -Werror -o realpath realpath.c */
/* or */
/* gcc -std=gnu99 -Wall -Wextra -pedantic -Werror -o realpath realpath.c */
int usage (char * path, int rv) {
printf("%s <path> returns the canonical pathname for <path>, which must exist\n", basename(path));
return(rv);
}
int main(int argc, char **argv) {
if (argc < 2) {
exit(usage(argv[0], 1));
}
char *p = realpath(argv[1], NULL);
if (p == NULL) {
exit(usage(argv[0], 1));
}
printf("%s\n", p);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment