Refactor scandir/scandirat to use common tail.