
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "support.h"

int
ixmmap_open (const char *filename, char **buf, int *len)
{
   int fd, e;
   void *map;
   struct stat sbuf;

   if ((fd = open (filename, O_RDONLY)) == -1)
      return (-errno);

   if (fstat (fd, &sbuf) == -1)
   {
      e = errno;
      close (fd);
      return (-e);
   }

   if ((map = mmap (0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
   {
      e = errno;
      close (fd);
      return (-e);
   }

   close (fd);

   *buf = map;
   *len = sbuf.st_size;

   return (0);
}

int
ixmmap_close (char *buf, int len)
{
   if (munmap (buf, len) == -1)
      return (-errno);

   return (0);
}

