November 24th, 2014

Random API of the Day

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
[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[1], "r") ?  Then logged it?  Perhaps your programs would produce better output if you called realpath(3) first.

Leave a Response

Imhotep theme designed by Chris Lin. Proudly powered by Wordpress.
XHTML | CSS | RSS | Comments RSS