Fix directory layout.
Revert "Replace strndup with malloc() + strncpy, which seems to be more portable."

This reverts commit c7976d8b112ee93c7e1b60fd0965ed67a68834e5.
Added a strndup implementation for the systems that don't support it.