home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / EDITOR / NVI179B / NVI179B.ZIP / clib / mmap.c < prev    next >
C/C++ Source or Header  |  1996-06-14  |  946b  |  51 lines

  1. #include "config.h"
  2.  
  3. #include <sys/types.h>
  4.  
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7.  
  8. /*
  9.  * This function fakes mmap() by reading `len' bytes from the file descriptor
  10.  * `fd' and returning a pointer to that memory.  The "mapped" region can later
  11.  * be deallocated with munmap().
  12.  *
  13.  * Note: ONLY reading is supported and only reading of the exact size of the
  14.  * file will work.
  15.  *
  16.  * PUBLIC: #ifndef HAVE_MMAP
  17.  * PUBLIC: char *mmap __P((char *, size_t, int, int, int, off_t));
  18.  * PUBLIC: #endif
  19.  */
  20. char *
  21. mmap(addr, len, prot, flags, fd, off)
  22.     char *addr;
  23.     size_t len;
  24.     int prot, flags, fd;
  25.     off_t off;
  26. {
  27.     char *ptr;
  28.  
  29.     if ((ptr = (char *)malloc(len)) == 0)
  30.         return ((char *)-1);
  31.     if (read(fd, ptr, len) < 0) {
  32.         free(ptr);
  33.         return ((char *)-1);
  34.     }
  35.     return (ptr);
  36. }
  37.  
  38. /*
  39.  * PUBLIC: #ifndef HAVE_MMAP
  40.  * PUBLIC: int munmap __P((char *, size_t));
  41.  * PUBLIC: #endif
  42.  */
  43. int
  44. munmap(addr, len)
  45.     char *addr;
  46.     size_t len;
  47. {
  48.     free(addr);
  49.     return (0);
  50. }
  51.