for the Mac OS. This isn't anywhere near complete, and is wrong in a few important regards, but I think it's heading in the right direction. [originally from svn r2953]