Improve portability for FreeBSD

These changes help FreeBSD to build user land applications.