]> git.pond.sub.org Git - empserver/blob - src/lib/w32/w32io.c
Update copyright notice
[empserver] / src / lib / w32 / w32io.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  w32io.c: POSIX I/O emulation layer for Windows
28  *
29  *  Known contributors to this file:
30  *     Ron Koenderink, 2007
31  *     Markus Armbruster, 2007-2009
32  */
33
34 /*
35  * POSIX sockets are file descriptors.  Windows sockets are something
36  * else, with a separate set of functions to operate on them.  To
37  * present a more POSIX-like interface to our application code, we
38  * provide a compatibility layer that wraps file descriptors around
39  * sockets.
40  */
41
42 #include <config.h>
43
44 #include <errno.h>
45 #include <io.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include "misc.h"
49 #include "sys/uio.h"
50
51 int (*w32_close_function)(int) = _close;
52 int (*w32_read_function)(int, void *, unsigned) = _read;
53 int (*w32_write_function)(int, const void *, unsigned) = _write;
54
55 /*
56  * POSIX equivalent for readv
57  * Modelled after the GNU's libc/sysdeps/posix/readv.c
58  */
59 ssize_t
60 readv(int fd, const struct iovec *iov, int iovcnt)
61 {
62     int i;
63     unsigned char *buffer, *buffer_location;
64     size_t total_bytes = 0;
65     int bytes_read;
66     size_t bytes_left;
67
68     for (i = 0; i < iovcnt; i++) {
69         total_bytes += iov[i].iov_len;
70     }
71
72     buffer = malloc(total_bytes);
73     if (buffer == NULL && total_bytes != 0) {
74         errno = ENOMEM;
75         return -1;
76     }
77
78     bytes_read = read(fd, buffer, total_bytes);
79     if (bytes_read < 0) {
80         free(buffer);
81         return -1;
82     }
83
84     bytes_left = bytes_read;
85     buffer_location = buffer;
86     for (i = 0; i < iovcnt; i++) {
87         size_t copy = MIN(iov[i].iov_len, bytes_left);
88
89         memcpy(iov[i].iov_base, buffer_location, copy);
90
91         buffer_location += copy;
92         bytes_left -= copy;
93         if (bytes_left == 0)
94             break;
95     }
96
97     free(buffer);
98     return bytes_read;
99 }
100
101 /*
102  * POSIX equivalent for writev
103  * Modelled after the GNU's libc/sysdeps/posix/writev.c
104  */
105 ssize_t
106 writev(int fd, const struct iovec *iov, int iovcnt)
107 {
108     int i;
109     unsigned char *buffer, *buffer_location;
110     size_t total_bytes = 0;
111     int bytes_written;
112
113     for (i = 0; i < iovcnt; i++)
114         total_bytes += iov[i].iov_len;
115
116     buffer = malloc(total_bytes);
117     if (buffer == NULL && total_bytes != 0) {
118         errno = ENOMEM;
119         return -1;
120     }
121
122     buffer_location = buffer;
123     for (i = 0; i < iovcnt; i++) {
124         memcpy(buffer_location, iov[i].iov_base, iov[i].iov_len);
125         buffer_location += iov[i].iov_len;
126     }
127
128     bytes_written = write(fd, buffer, total_bytes);
129     free(buffer);
130     return bytes_written;
131 }