Listing 1 Error handling using multiple returns

main()
   {
   char *error_ptr;

   if ((error_ptr = initialize()) != NULL)
      {
      fprintf(stderr, "error during %s", error_ptr);
      exit(1)
      }
    // processing ...
   (void) cleanup(2, NULL);
    exit(0);
   }

int fd;
char *buf;

char *initialized(void)
   {
    if((buf = malloc(BUFSIZ)) == NULL)
        return cleanup(0, "malloc");
       // uses cleanup for consistency
    if ((fd = open("filename", 0_RDONLY)) < 0)
        return cleanup(1, "open");
    if (read(fd, buf, sizeof(buf) < 0)
        return cleanup(2, "read");
    return NULL;
   }

char *cleanup(int start_from, char *where_failed)
   {
    switch (start_from)
      {
   case 2:
      close(fd);
   case 1:
      free(buf);
      }
   // could instead print error message here
   // if where_failed ! = Null
    return where_failed;
   }

/* End of File */