#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "myname.h"
#include "token.h"
#include "callcheck.h"


/*
 * if result is false, prints the string representation of errno and terminates
 * the process
 */
void callcheck (int result) {
  if (result) {
    return;
  }

  fprintf(stderr, "%s: %s\n", myname, strerror(errno));
  exit(EXIT_FAILURE);
}


/*
 * if errno is set, prints the string representation of errno and terminates
 * the process
 */
void ecallcheck () {
  if (errno == 0) {
    return;
  }

  fprintf(stderr, "%s: %s\n", myname, strerror(errno));
  exit(EXIT_FAILURE);
}


/*
 * if result is false, prints the given message and the string representation
 * of errno, then terminates process
 */
void scallcheck (int result, char *message) {
  if (result) {
    return;
  }

  fprintf(stderr, "%s: %s (%s)\n", myname, message, strerror(errno));
  exit(EXIT_FAILURE);
}


/*
 * if result is false, prints the message then terminates the process
 */
void mcallcheck (int result, char *message) {
  if (result) {
    return;
  }

  fprintf(stderr, "%s: %s\n", myname, message);
  exit(EXIT_FAILURE);
}


/*
 * if errno is set, prints the message and the string representation of errno,
 * then terminates the process
 */
void escallcheck (char *message) {
  if (errno == 0) {
    return;
  }

  fprintf(stderr, "%s: %s (%s)\n", myname, message, strerror(errno));
  exit(EXIT_FAILURE);
}


