I just received an amazing response to a question that I had posed to Apple’s unix-porting mailing list.  I was having a problem compiling bspatch (there were some missing type declarations), and had suggested one potential solution.  Terry’s response:

No, it’s also missing an off_t definition.
You definitely need:

======
*** bspatch.c.org       Mon Aug  4 14:58:06 2008
— bspatch.c   Mon Aug  4 14:58:09 2008
***************
*** 28,33 ****
— 28,34 —-
__FBSDID(”$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:06 cperciva Exp $”);
#endif

+ #include <sys/types.h>
#include <bzlib.h>
#include <stdlib.h>
#include <stdio.h>
======

The code was written incorrectly assuming promiscuous headers. The FreeBSD headers pollute the POSIX namespace with symbols they are technically not permitted to define in scope, as a side effect of how the headers are organized. They would fail The Open Group VSX/VSU header tests for UNIX conformance. You basically need to include a standard header for the different types you end up using in the code.

See also:

<http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html>

PS: The u_char is a side effect (BSD) type, and is pulled in only because of historical use, and the fact that _DARWIN_C_SOURCE is a superset of _POSIX_C_SOURCE/_XOPEN_SOURCE; this may end up changing in some future release. The correct type is most likely either simply “unsigned char”, or if you wanted to be more correct and used a sized type so the structure packing doesn’t change on you, “uint8_t” after “#include <stdint.h>”.

Wow.

He could have said, “You need to #include <sys/types.h>” and been done with it.  Instead, he explained the why of the situation, and thus I learned:

  • if you have any errors about missing type declarations, make sure types.h is included
  • FreeBSD has a messy implementation of the POSIX types system, so porting from FreeBSD to other OSes will often give type errors
  • Apple really cares about being a platform company, so they take care of their developers

This illustrates what good can happen when a company’s vision lives and breathes.  When your vision lives and breathes your company exudes it from every orifice.  This, it seems, is but one of the many aspects of company balance that comprise success.

Thanks Apple, you taught me a lesson today - in more ways than one.