title: mkdtemp requires _DARWIN_C_SOURCE for unistd.h
body: <p>I am a little puzzled. I have project that I compile with</p>
  
  <pre><code>CFLAGS=-g -O2 -Wall -Wextra -Isrc/main -pthread -rdynamic -DNDEBUG $(OPTFLAGS) -D_FILE_OFFSET_BITS=64 -D_XOPEN_SOURCE=700
  </code></pre>
  
  <p>Now I want to use <code>mkdtemp</code> and therefor include <code>unistd.h</code></p>
  
  <pre><code>char *path = mkdtemp(strdup("/tmp/test-XXXXXX"));
  </code></pre>
  
  <p>On MacOSX the compilation gives some warnings</p>
  
  <pre><code>warning: implicit declaration of function ‘mkdtemp’
  warning: initialization makes pointer from integer without a cast
  </code></pre>
  
  <p>but compiles through. While <code>mkdtemp</code> does return a non-NULL path accessing it results in a EXC_BAD_ACCESS.</p>
  
  <p><strong>Question 1: The template is <code>strdup()</code>ed and the result is non-NULL. How on earth can this result in an EXC_BAD_ACCESS?</strong></p>
  
  <p>Now further down the rabbit hole. Let's get rid of the warnings. Checking <code>unistd.h</code> I find the declaration hidden by the pre processor.</p>
  
  <pre><code>#if     !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
  ...
  char    *mkdtemp(char *);
  ...
  #endif
  </code></pre>
  
  <p>Adding <code>-D_DARWIN_C_SOURCE</code> to the build makes all the problems go away but leaves me with a platform specific build. The 10.6 man page just says</p>
  
  <pre><code> Standard C Library (libc, -lc)
   #include &lt;unistd.h&gt;
  </code></pre>
  
  <p>Removing the <code>_XOPEN_SOURCE</code> from the build makes is work on OSX but then it fails to compile under Linux with</p>
  
  <pre><code>warning: ‘struct FTW’ declared inside parameter list
  warning: its scope is only this definition or declaration, which is probably not what you want
  In function ‘tmp_remove’:
  warning: implicit declaration of function ‘nftw’
  error: ‘FTW_DEPTH’ undeclared (first use in this function)
  error: (Each undeclared identifier is reported only once
  error: for each function it appears in.)
  error: ‘FTW_PHYS’ undeclared (first use in this function)
  </code></pre>
  
  <p><strong>Question 2: So how would you fix this?</strong></p>
  
  <p>The only fix I have found is to <code>#undef</code> _POSIX_C_SOURCE right before the <code>unistd.h</code> include ...but that feels like an ugly hack.</p>