I\'m looking for an example of how to use the realpath function in a C program. I can\'t seem to find one on the web or in any of my C programming books.
Minimalist but it does the job!
Build
gcc -o realpath -x c - <<< $'#include\n#include\nint main(int c,char**v){puts(realpath(v[1],0));}'
Test
$> ./realpath ~/../../../usr/./bin/./awk
/bin/gawk
$> readlink -f ~/../../../usr/./bin/./awk
/bin/gawk
Requirements
<<<
and $' ... \n ... '
Crash
My minimalist one-line command line builds an executable realpath
that produces a Segmentation fault
when the path does not exist. Instead of writing if
/else
blocs to handle that issue within my answer, I have added below some links to let you have a look on the Busybox implementation of realpath
and readlink
.
For a more complete source code, have a look on this simple implementation.
Official Git repository
GitHub mirror repository