#define TRUE  1
#define FALSE 0

void makesure (int condition, char *errormsg, int retcode) {
/* Make sure a condition is true, else exit after a brief message */
  if (!condition) {
    fprintf(stderr,errormsg);
    exit(retcode);
    }
  return;
  }

FILE *find (char *filename, char *mode) {
/* Try to open the file in the current directory, "SYS:Config", and "s:".
   Return as fopen does. */
  FILE *result;
  char buffer[256];
  if ((result=fopen(filename,mode))==NULL) {
      sprintf(buffer,"SYS:Config/%s",filename);
      if ((result=fopen(buffer,mode))==NULL) {
          sprintf(buffer,"s:%s",filename);
          if ((result=fopen(buffer,mode))==NULL)
              return NULL;
          }
      }
  return result;
  }

FILE *fopen_or_die(char *filename, char *mode, char *errormsg, int retcode) {
/* attempt to open a file, or if you can't do it, print error and exit.  The
   error message should have a %s for the filename in it. */
  FILE *result;
  if ((result=fopen(filename,mode))==NULL) {
      fprintf(stderr,errormsg,filename);
      exit(retcode);
      }
  return result;
  }

FILE *find_or_die(char *filename, char *mode, char *errormsg, int retcode) {
/* as fopen_or_die but uses find instead of fopen. */
  FILE *result;
  if ((result=find(filename,mode))==NULL) {
      fprintf(stderr,errormsg,filename);
      exit(retcode);
      }
  return result;
  }

void checkusage(int minparms, char *usage, int argc, char *argv[]) {
/* checks to see if the user gave insufficient parms or the first parm begins
   with a ?.  If either one, shows usage and quits with return code 0. */
  if ((argc<=minparms) || ((argv[1][0])=='?')) {
      fprintf(stderr,usage);
      exit(0);
      }
  return;
  }

void add_extension (char *filename, char *extension) {
/* if the filename given doesn't already end in the extension, add it on.
   it is assumed space exists in the filename buffer for the extension. */
  int i,j;
  i=strlen(filename)-strlen(extension);
  j=0;
  for (;filename[i]!='\0' && toupper(filename[i])==toupper(extension[j]);i++)
    j++;
  /* one of the above conditions is now false.  Which one??? */
  if (filename[i]=='\0') /* we got all the way with a match */
      return;
  i=strlen(filename);
  for (j=0;extension[j]!='\0';j++)
    filename[i++]=extension[j];
  filename[i]='\0';
  return;
  }

void stripnewline (char *buffer) {
  if (buffer[strlen(buffer)-1]=='\n')
      buffer[strlen(buffer)-1]='\0';
  }

void pause()
/* Say "Press any key to continue..." and wait, then clear that line */
{
  char buf[2];
  printf("\x1b[1;7mPress RETURN to continue...\x1b[0m");
  fgets(buf,2,stdin);
  }

