autoconf and FreeBSD fixes.

This pull request includes a few autoconf changes to make some header
checks more robust and portable.

They should be harmless on Linux and make things work more smoothly on
the BSDs.