title: Can I intercept normal stdio calls in a C program, do some work and then call the original ones?
body: <p>I have a bunch of C files that try to read and write CSV and other random data to and from disk using stdio functions like <code>fread()</code>, <code>fwrite()</code>, <code>fseek()</code>. (If it matters, it's for a university assignment where we are to experiment with IO performance using different block sizes, and various structures to track data on disk files and so on.)</p>
  
  <p>What I wanted to do was compile these source files (there are dozens of them) 
  without the definitions for <code>fopen()</code>, <code>fread()</code>, <code>fwrite()</code> that come from <code>&lt;stdio.h&gt;</code>. I want to supply my own <code>fopen()</code>, <code>fread()</code>, <code>fwrite()</code> where I track some information, like which process tried to read which file, and how many blocks/pages where read and things like that, and then call the normal stdio functions.</p>
  
  <p>I don't want to have to go through every line of every file and change <code>fopen()</code> to <code>my_fopen()</code> .... is there better way to do this at compile time? </p>
  
  <p>I am half way working on a Python program that scans the source files and changes these calls with my functions but it's getting a bit messy and I am kind of lost. I thought maybe there is a better way to do this; if you could point me in the right direction, like what to search for that would be great.</p>
  
  <p>Also I don't want to use some Linux profiling stuff that reports which syscalls where made and what not; I just want to execute some code before calling these functions.</p>