| 1 |
sysadm |
1.1 |
/*
|
| 2 |
|
|
* Copyright (C) 2003 RUSU Mihai (dizzy@roedu.net)
|
| 3 |
|
|
*
|
| 4 |
|
|
* This program is free software; you can redistribute it and/or
|
| 5 |
|
|
* modify it under the terms of the GNU General Public License
|
| 6 |
|
|
* as published by the Free Software Foundation; either version 2
|
| 7 |
|
|
* of the License, or (at your option) any later version.
|
| 8 |
|
|
*
|
| 9 |
|
|
* This program is distributed in the hope that it will be useful,
|
| 10 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
|
|
* GNU General Public License for more details.
|
| 13 |
|
|
*
|
| 14 |
|
|
* You should have received a copy of the GNU General Public License
|
| 15 |
|
|
* along with this program; if not, write to the Free Software
|
| 16 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 17 |
|
|
*/
|
| 18 |
|
|
#include "common/setup_before.h"
|
| 19 |
|
|
#ifndef HAVE_MMAP
|
| 20 |
|
|
#ifdef STDC_HEADERS
|
| 21 |
|
|
# include <stdlib.h>
|
| 22 |
|
|
#else
|
| 23 |
|
|
# ifdef HAVE_MALLOC_H
|
| 24 |
|
|
# include <malloc.h>
|
| 25 |
|
|
# endif
|
| 26 |
|
|
#endif
|
| 27 |
|
|
#ifdef HAVE_SYS_TYPES_H
|
| 28 |
|
|
# include <sys/types.h>
|
| 29 |
|
|
#endif
|
| 30 |
|
|
#ifdef HAVE_UNISTD_H
|
| 31 |
|
|
# include <unistd.h>
|
| 32 |
|
|
#endif
|
| 33 |
|
|
#include "common/xalloc.h"
|
| 34 |
|
|
#include "mmap.h"
|
| 35 |
|
|
#ifdef WIN32
|
| 36 |
|
|
# include <windows.h>
|
| 37 |
|
|
# include <io.h>
|
| 38 |
|
|
#endif
|
| 39 |
|
|
#include "common/setup_after.h"
|
| 40 |
|
|
|
| 41 |
|
|
extern void * pmmap(void *addr, unsigned len, int prot, int flags, int fd, unsigned offset)
|
| 42 |
|
|
{
|
| 43 |
|
|
void *mem;
|
| 44 |
|
|
#ifdef WIN32
|
| 45 |
|
|
HANDLE hFile, hMapping;
|
| 46 |
|
|
|
| 47 |
|
|
/* under win32 we only support readonly mappings, the only ones used in pvpgn now :) */
|
| 48 |
|
|
if (prot != PROT_READ) return NULL;
|
| 49 |
|
|
hFile = (HANDLE) _get_osfhandle(fd);
|
| 50 |
|
|
if (hFile == (HANDLE) - 1) return MAP_FAILED;
|
| 51 |
|
|
hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
|
| 52 |
|
|
if (!hMapping) return MAP_FAILED;
|
| 53 |
|
|
mem = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0 ,0);
|
| 54 |
|
|
#else /* systems without mmap or win32 */
|
| 55 |
|
|
unsigned pos;
|
| 56 |
|
|
int res;
|
| 57 |
|
|
|
| 58 |
|
|
mem = xmalloc(len);
|
| 59 |
|
|
pos = 0;
|
| 60 |
|
|
while(pos < len) {
|
| 61 |
|
|
res = read(fd, (char *)mem + pos, len - pos);
|
| 62 |
|
|
if (res < 0) {
|
| 63 |
|
|
xfree(mem);
|
| 64 |
|
|
return MAP_FAILED;
|
| 65 |
|
|
}
|
| 66 |
|
|
pos += res;
|
| 67 |
|
|
}
|
| 68 |
|
|
#endif
|
| 69 |
|
|
|
| 70 |
|
|
return mem;
|
| 71 |
|
|
}
|
| 72 |
|
|
|
| 73 |
|
|
extern int pmunmap(void *addr, unsigned len)
|
| 74 |
|
|
{
|
| 75 |
|
|
#ifdef WIN32
|
| 76 |
|
|
UnmapViewOfFile(addr);
|
| 77 |
|
|
#else
|
| 78 |
|
|
xfree(addr);
|
| 79 |
|
|
#endif
|
| 80 |
|
|
return 0;
|
| 81 |
|
|
}
|
| 82 |
|
|
#else
|
| 83 |
|
|
typedef int filenotempty; /* make ISO standard happy */
|
| 84 |
|
|
#endif
|