OS X compatibility fixes

Code fixes for compatibility with OS X, see https://github.com/minetest/minetest/issues/1242. I have tested this on OS X and Linux. Some code is borrowed from github.com/constcast/vermont, which is also GPL'ed.
I think it would be better like this:
```cmake
set(PLATFORM_LIBS -lpthread -lrt ${CMAKE_DL_LIBS})
if(APPLE)
    set(PLATFORM_LIBS "-framework CoreFoundation" ${PLATFORM_LIBS})
endif(APPLE)
```