title: Overriding C library functions, calling original
body: <p>I am a bit puzzled on how and why this code works as it does. I have not actually encountered this in any project I've worked on, and I have not even thought of doing it myself.</p>
  
  <p>override_getline.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define OVERRIDE_GETLINE
  
  #ifdef OVERRIDE_GETLINE
  ssize_t getline(char **lineptr, size_t *n, FILE *stream)
  {
      printf("getline &amp;lineptr=%p &amp;n=%p &amp;stream=%p\n", lineptr, n, stream);
      return -1; // note: errno has undefined value
  }
  #endif
  </code></pre>
  
  <p>main.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main()
  {
      char *buf = NULL;
      size_t len = 0;
      printf("Hello World! %zd\n", getline(&amp;buf, &amp;len, stdin));
      return 0;
  }
  </code></pre>
  
  <p>And finally, example compile and run command:</p>
  
  <pre><code>gcc main.c override_getline.c &amp;&amp; ./a.out
  </code></pre>
  
  <p>With the <code>OVERRIDE_GETLINE</code> define, the custom function gets called, and if it is commented out, normal library function gets called, and both work as expected.</p>
  
  <h2>Questions</h2>
  
  <ol>
  <li><p>What is the correct term for this? "Overriding", "shadowing", something else?</p></li>
  <li><p>Is this gcc-specific, or POSIX, or ANSI C, or even undefined in all?</p></li>
  <li><p>Does it make any difference if function is ANSI C function or (like here) a POSIX function?</p></li>
  <li><p>Where does the overriding function get called? By other <code>.o</code> files in the same linking, at least, and I presume <code>.a</code> files added to link command too. How about static or dynamic libs added with <code>-l</code> command line option of linker?</p></li>
  <li><p><strong>If it is possible, how do I call the library version of getline from the overriden getline?</strong></p></li>
  </ol>