empserver/src/lib/w32/w32io.c
Markus Armbruster c9de868d55 Change naming convention of POSIX emulation layer for Windows
Use prefix w32 instead of posix: Rename posixfile.c, posixio.c to
w32file.c, w32io.c.  Rename posix_accept(), posix_bind(),
posix_close(), posix_fd2socket(), posix_listen(), posix_mkdir(),
posix_setsockopt(), posix_shutdown(), posix_socket() to w32_accept(),
w32_bind(), w32_fd2socket(), w32_list(), w32_mkdir(),
w32_setsockopt(), w32_shutdown(), w32_socket().
2009-11-30 19:45:28 +01:00

137 lines
3.5 KiB
C

/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* 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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See files README, COPYING and CREDITS in the root of the source
* tree for related information and legal notices. It is expected
* that future projects/authors will amend these files as needed.
*
* ---
*
* w32io.c: POSIX I/O emulation layer for Windows
*
* Known contributors to this file:
* Ron Koenderink, 2007
* Markus Armbruster, 2007-2009
*/
/*
* POSIX sockets are file descriptors. Windows sockets are something
* else, with a separate set of functions to operate on them. To
* present a more POSIX-like interface to our application code, we
* provide a compatibility layer that wraps file descriptors around
* sockets.
*/
#include <config.h>
#include <errno.h>
#include <io.h>
#include <stdlib.h>
#include <string.h>
#include "misc.h"
#include "sys/uio.h"
int (*w32_close_function)(int) = _close;
int (*w32_read_function)(int, void *, unsigned) = _read;
int (*w32_write_function)(int, const void *, unsigned) = _write;
/*
* POSIX equivalent for readv
* Modelled after the GNU's libc/sysdeps/posix/readv.c
*/
ssize_t
readv(int fd, const struct iovec *iov, int iovcnt)
{
int i;
unsigned char *buffer, *buffer_location;
size_t total_bytes = 0;
int bytes_read;
size_t bytes_left;
for (i = 0; i < iovcnt; i++) {
total_bytes += iov[i].iov_len;
}
buffer = malloc(total_bytes);
if (buffer == NULL && total_bytes != 0) {
errno = ENOMEM;
return -1;
}
bytes_read = read(fd, buffer, total_bytes);
if (bytes_read <= 0) {
free(buffer);
return -1;
}
bytes_left = bytes_read;
buffer_location = buffer;
for (i = 0; i < iovcnt; i++) {
size_t copy = MIN(iov[i].iov_len, bytes_left);
memcpy(iov[i].iov_base, buffer_location, copy);
buffer_location += copy;
bytes_left -= copy;
if (bytes_left == 0)
break;
}
free(buffer);
return bytes_read;
}
/*
* POSIX equivalent for writev
* Modelled after the GNU's libc/sysdeps/posix/writev.c
*/
ssize_t
writev(int fd, const struct iovec *iov, int iovcnt)
{
int i;
unsigned char *buffer, *buffer_location;
size_t total_bytes = 0;
int bytes_written;
for (i = 0; i < iovcnt; i++)
total_bytes += iov[i].iov_len;
buffer = malloc(total_bytes);
if (buffer == NULL && total_bytes != 0) {
errno = ENOMEM;
return -1;
}
buffer_location = buffer;
for (i = 0; i < iovcnt; i++) {
memcpy(buffer_location, iov[i].iov_base, iov[i].iov_len);
buffer_location += iov[i].iov_len;
}
bytes_written = write(fd, buffer, total_bytes);
free(buffer);
if (bytes_written <= 0)
return -1;
return bytes_written;
}