strdup the buffers before calling dirname/basename