/* Author: Ram Samudrala (me@ram.org)
 * Version: O1.0
 * Detail: <http://www.ram.org/computing/cgi_common/cgi_common.html>
 * November 22, 1995.
 *
 * See the URL above for terms of use and general help.
 *
 * This is a set of general purpose error functions I use in my CGI
 * programs.
 */

#include "cgi_common.h"
#include "cgi_display.h"
#include "cgi_defines.h"

/******************************************************************/

void display_error(char error_string[])
{
  display_content_type(error_string);
  printf("<h3> %s </h3>\n", error_string);
  printf("<hr>\n");
  printf("<p> If this is not the expected result, please notify\n");
  printf("<var><a href=\"mailto:%s\">%s</a></var> of this error. Thank you. </p>\n", CARETAKER, CARETAKER);
  display_signature();
  exit(FALSE);
}

/******************************************************************/

void check_method(char method[], char routine_name[])
{
  if(strcmp(getenv("REQUEST_METHOD"), method) != 0) 
    {
      char buf[200];
      
      sprintf(buf, "%s(): access method error!", routine_name);
      display_error(buf);
    }
  return;
}

/******************************************************************/

void check_content_type(char routine_name[])
{
  if(strcmp(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded")) 
    {
      char buf[200];

      sprintf(buf, "%s(): content type error!", routine_name);
      display_error(buf);      
    }
  return;
}

/******************************************************************/

void check_eof(int status, char routine_name[])
{
  if (status == EOF)
    {
      char buf[200];
      
      sprintf(buf, "check_eof(): EOF returned in %s()!", routine_name);
      display_error(buf);
    }
  return;
}

/******************************************************************/

void check_max_count(int count, int max_count, char routine_name[])
{
  if (count >= max_count)
    {
      char buf[200];
      
      sprintf(buf, "check_max_count(): max_count (%d) exceeded in %s()!", max_count, routine_name);
      display_error(buf);
    }
  return;
}

/******************************************************************/

void open_file(FILE **fp, char filename[], char status[],  char routine_name[])
{
  char buf[20];
  
  switch (status[0])
    {
    case 'r':
      strcpy(buf, "reading");
      break;
    case 'a':
      strcpy(buf, "appending");
      break;
    case 'w':
      strcpy(buf, "writing");
      break;
    default:
      strcpy(buf, "error!");
      break;
    }

  if ((*fp = fopen(filename, status)) == NULL)
    {
      char buf2[200];
      
      sprintf(buf2, "%s(): couldn't open %s for %s!\n", routine_name, filename, buf);
      display_error(buf2);
    }
  
  /* Lock the file */
  if ((flock(fileno(*fp), LOCK_EX)) == -1)
    display_error("open_file(): error locking file!");
  return;
}

/******************************************************************/

void close_file(FILE **fp, char filename[], char routine_name[])
{
  if ((flock(fileno(*fp), LOCK_UN)) == -1)
    display_error("open_file(): error unlocking file!");

  if (fclose(*fp) == EOF)
    {
      char buf[200];
  
      sprintf(buf, "%s(): couldn't close %s!", routine_name, filename);
      display_error(buf);
    }
  
  return;
}

/******************************************************************/
