Portability part2

It seems this was resolved by somebody else (within 3 months that this patchset was in the fridge) and I didn't notice while resolving the conflicts, since I use a completely different approach (xasprintf). Decide which one you want to keep and I can revert this commit.