Fix portability issues on *BSD and MacOS