add qsort_r wrapper to fix differences between BSD and GNU qsort_r

The order of parameters of both qsort_r and its compar function differ,
the wrapper matches the GNU version and swaps things around on FreeBSD.
replaced a strdupa with a variable length array
Merge PR#385 - Freebsd fixes