Thursday, February 18, 2010

Code WTF

While playing with one opensource project we've found this masterpiece of Hindy C.  I will not even comment it.


int pid, status, ret;
int filedes[2];
rc = pipe(filedes);
pid = fork();
if (pid == 0) {
  ret=0;
  close(filedes[0]);


  rc = functionThatMatters(/* some args */);



  if (!rc) {
    ret = 0;
  } else {
    ret = 1;
  }


  close(filedes[1]);      
  exit(ret);
} else {
  close(filedes[1]);
  close(filedes[0]);
  
  rc = timewait(pid, &status, /* timeout values */);
  rc = WEXITSTATUS(status);
}


if (!rc) {
  (*outStatus)[i] = 1;
  /* LOG WARNING THAT WE HAVE FAILED */
} else {
  (*outStatus)[i] = 0;
}

This reminds me forgotten stories about Miklos...