Random API of the Day
Jason posted in Uncategorized on May 15th, 2006
Your RAD editor apologizes for having been out lately, due to a malpractice lawsuit involving the use of BIG5 instead of GB encoding in a Chinese heart-transplant robot (“yes, that’s precisely where the robot told me to stab the patient, your honor”).
Today’s random API is realpath(3), which you can find on any POSIX compliant system. Given a pathname, realpath(3) will “clean it up” by resolving symbolic links, and translating shorthand directories like `.’ and `..’. On some systems, there’s even a realpath(1) command which just calls realpath(3):
[thalakan@shaitan /usr/share/man/man3]> realpath /usr/share/../share/../../bin/../usr/share/man/man3 /usr/share/man/man3 [thalakan@shaitan /usr/share/man/man3]>
This is very handy for canonicalizing user input prior to opening a file.Â How many times have you done things like
fopen(argv, "r") ?Â Then logged it?Â Perhaps your programs would produce better output if you called realpath(3) first.