Some classic code from Squirrelmail

    if (file_exists($data_dir . '/dnscache')) {
        $fp = fopen($data_dir . '/dnscache', 'r');
    } else {
        $fp = false;
    }
    if ($fp) {
        flock($fp,LOCK_EX);
    } else {
       $fp = fopen($data_dir . '/dnscache', 'w+');
       fclose($fp);
       $fp = fopen($data_dir . '/dnscache', 'r');
       flock($fp,LOCK_EX);
    }
    $fp1=fopen($data_dir . '/dnscache', 'w+');

Firstly $data_dir is passed round in global scope. (Ouch!) but these are only the start of the code's problems. What is says is "If the cache file exists and I can open it for reading then get an exclusive write lock on the file, otherwise open it for reading and writing and truncate it to zero length, if the file does not exist, attempt to create it. Whether on not I can do that close the file (oops!) now open the file for reading again (remember, if the previous command succeeded it is now empty anyway) and lock it. Now get a new file descriptor to read or write it and truncate it to zero size,attempting to create it if necessary.

Notice all those juicy comment lines that tell you exactly what is going on. What on earth was going through the developer's minds?