This is mapfile.c in view mode; [Download] [Up]
/* Portable way of mapping files into memory. Copyright 1993 by Michael L.H. Brouwer (michael@urc.tue.nl) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. $Modified: Fri Apr 2 03:41:42 1993 by michael $ $Id: mapfile.c,v 1.7 1993/04/04 22:46:12 michael Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #ifdef HAVE_LIBC_H #include <libc.h> #endif #ifdef HAVE_UNISTD_H #include <unistd.h> #endif #include <sys/types.h> #include <sys/stat.h> #ifdef HAVE_FCNTL_H #include <fcntl.h> #endif #if defined (HAVE_MAP_FD) && defined (HAVE_VM_DEALLOCATE) #include <mach/mach.h> #elif defined (HAVE_MMAP) && defined (HAVE_MUNMAP) #include <sys/mman.h> #else /* no HAVE_MAP_FD nor HAVE_MMAP */ #include <stdlib.h> #endif #include "global.h" #include "mapfile.h" /* Map a file read-only into mh->mem. Return 0 on success, return -1 if something went wrong after having reported an error through errorf. */ int map_file (map_handle *mh) { struct stat st; mh->fd = open (mh->name, O_RDONLY); if (mh->fd == -1) { errorf ("%s: open %s", "map_file", mh->name); return (-1); } if (fstat (mh->fd, &st)) { errorf ("%s: stat %s", "map_file", mh->name); close (mh->fd); return (-1); } mh->size = st.st_size; if (!mh->size) return (0); #if defined (HAVE_MAP_FD) && defined (HAVE_VM_DEALLOCATE) mh->mem = 0; if (map_fd (mh->fd, 0, (vm_offset_t *) &mh->mem, TRUE, (vm_size_t) mh->size) != KERN_SUCCESS) { errorf ("%s: map_fd %s", "map_file", mh->name); close (mh->fd); return (-1); } #elif defined (HAVE_MMAP) && defined (HAVE_MUNMAP) mh->mem = 0; mh->mem = (char *) mmap ((caddr_t) mh->mem, mh->size, PROT_READ, MAP_SHARED, mh->fd, 0); if (mh->mem == (char *) -1) { errorf ("%s: mmap %s", "map_file", mh->name); return (-1); } #else /* no HAVE_MAP_FD nor HAVE_MMAP */ /* Well, if we can't map the file, just read the whole thing into a buffer. */ mh->mem = malloc (mh->size); if (!mh->mem) { errorf ("%s: malloc %ld for file %s", "map_file", (unsigned long) mh->size, mh->name); close (mh->fd); return (-1); } if (read (mh->fd, mh->mem, mh->size) != mh->size) { errorf ("%s: read %s", "map_file", mh->name); free (mh->mem); close (mh->fd); return (-1); } #endif return (0); } /* map_file */ /* Unmap a file mapped with map_file. Return 0 on success, return -1 if something went wrong after having reported an error through errorf. */ int unmap_file (map_handle *mh) { if (mh->size) { #if defined (HAVE_MAP_FD) if (vm_deallocate (task_self (), (vm_address_t) mh->mem, (vm_size_t) mh->size) != KERN_SUCCESS) { errorf ("%s: vm_deallocate %ld %ld for file %s", "unmap_file", (unsigned long) mh->mem, (unsigned long) mh->size, mh->name); close (mh->fd); return (-1); } #elif defined (HAVE_MMAP) if (munmap (mh->mem, mh->size) == -1) { errorf ("%s: munmap %ld %ld for file %s", "unmap_file", (unsigned long) mh->mem, (unsigned long) mh->size, mh->name); close (mh->fd); return (-1); } #else /* no HAVE_MAP_FD nor HAVE_MMAP */ free (mh->mem); #endif } close (mh->fd); return (0); } /* unmap_file */
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.