Freebsd comptibility patch

I also created the FreeBSD port with most of these changes: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=192367 Port builds and works fine.