This commit is contained in:
Manoj Ampalam 2016-01-13 12:52:58 -08:00
parent 36c5c9e89f
commit a3e7bf4c4c
8 changed files with 219 additions and 38 deletions

View File

@ -0,0 +1,46 @@
#pragma once
#include <memory.h>
#define fd_set w32_fd_set
#undef FD_ZERO
#define FD_ZERO(set) (memset( (set), 0, sizeof(w32_fd_set)))
#undef FD_SET
#define FD_SET(fd,set) ( (set)->bitmap[(fd) >> 3] |= (0x80 >> ((fd) % 8)))
#undef FD_ISSET
#define FD_ISSET(fd, set) (( (set)->bitmap[(fd) >> 3] & (0x80 >> ((fd) % 8)))?1:0)
#undef FD_CLR
#define FD_CLR(fd, set) ((set)->bitmap[(fd) >> 3] &= (~(0x80 >> ((fd) % 8))))
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
//fcntl commands
#define F_GETFL 0x1
#define F_SETFL 0x2
#define F_GETFD 0x4
#define F_SETFD 0x8
//fd status flags
#define O_NONBLOCK 0x1
//fd flags
#define FD_CLOEXEC 0x1
//open access modes. only one of these specified
#define O_RDONLY 0x1
#define O_WRONLY 0x2
#define O_RDWR 0x4
//open file creation and file status flags // can be bitwise-or'd
#define O_APPEND 0x10 //file is opened in append mode
#define O_CREAT 0x20 //If the file does not exist it will be created
#define O_TRUNC 0x40 //If the file exists and is a regular file, and the file is successfully opened O_RDWR or O_WRONLY, its length shall be truncated to 0, and the mode and owner shall be unchanged
#define O_EXCL 0x80 //If O_CREAT and O_EXCL are set, open() shall fail if the file exists
#define O_BINARY 0x100 //Gives raw data (while O_TEXT normalises line endings
// open modes
#define S_IRUSR 00400 //user has read permission
#define S_IWUSR 00200 //user has write permission
#define S_IRGRP 00040 //group has read permission
#define S_IROTH 00004 //others have read permission

View File

@ -0,0 +1,26 @@
#include "w32fd.h"
#include "defs.h"
int fileio_pipe(struct w32_io* pio[2]) {
return -1;
}
struct w32_io* fileio_open(const char *pathname, int flags, int mode) {
return NULL;
}
int fileio_read(struct w32_io* pio, void *dst, unsigned int max) {
return -1;
}
int fileio_write(struct w32_io* pio, const void *buf, unsigned int max) {
return -1;
}
int fileio_fstat(struct w32_io* pio, struct stat *buf) {
return -1;
}
int fileio_isatty(struct w32_io* pio) {
return 0;
}
FILE* fileio_fdopen(struct w32_io* pio, const char *mode) {
return NULL;
}

View File

@ -5,7 +5,7 @@
#include "w32fd.h" #include "w32fd.h"
#include <stddef.h> #include <stddef.h>
#define INTERNAL_BUFFER_SIZE 100*1024 //100KB #define INTERNAL_SEND_BUFFER_SIZE 70*1024 //70KB
#define INTERNAL_RECV_BUFFER_SIZE 70*1024 //70KB #define INTERNAL_RECV_BUFFER_SIZE 70*1024 //70KB
@ -248,7 +248,7 @@ int socketio_recv(struct w32_io* pio, void *buf, size_t len, int flags) {
BOOL completed = FALSE; BOOL completed = FALSE;
if ((buf == NULL) || (len == 0)){ if ((buf == NULL) || (len == 0)){
errno = EPERM; errno = EINVAL;
return -1; return -1;
} }
@ -371,7 +371,7 @@ int socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags) {
} }
//initialize buffers if needed //initialize buffers if needed
wsabuf.len = INTERNAL_BUFFER_SIZE; wsabuf.len = INTERNAL_SEND_BUFFER_SIZE;
if (pio->write_details.buf == NULL) if (pio->write_details.buf == NULL)
{ {
wsabuf.buf = malloc(wsabuf.len); wsabuf.buf = malloc(wsabuf.len);

View File

@ -127,49 +127,156 @@ int w32_accept(int fd, struct sockaddr* addr, int* addrlen)
} }
int w32_setsockopt(int fd, int level, int optname, const char* optval, int optlen) { int w32_setsockopt(int fd, int level, int optname, const char* optval, int optlen) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return socketio_setsockopt(fd_table.w32_ios[fd], level, optname, optval, optlen); return socketio_setsockopt(fd_table.w32_ios[fd], level, optname, optval, optlen);
} }
int w32_getsockopt(int fd, int level, int optname, char* optval, int* optlen) { int w32_getsockopt(int fd, int level, int optname, char* optval, int* optlen) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return socketio_getsockopt(fd_table.w32_ios[fd], level, optname, optval, optlen); return socketio_getsockopt(fd_table.w32_ios[fd], level, optname, optval, optlen);
} }
int w32_getsockname(int fd, struct sockaddr* name, int* namelen) { int w32_getsockname(int fd, struct sockaddr* name, int* namelen) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return socketio_getsockname(fd_table.w32_ios[fd], name, namelen); return socketio_getsockname(fd_table.w32_ios[fd], name, namelen);
} }
int w32_getpeername(int fd, struct sockaddr* name, int* namelen) { int w32_getpeername(int fd, struct sockaddr* name, int* namelen) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return socketio_getpeername(fd_table.w32_ios[fd], name, namelen); return socketio_getpeername(fd_table.w32_ios[fd], name, namelen);
} }
int w32_listen(int fd, int backlog) { int w32_listen(int fd, int backlog) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return socketio_listen(fd_table.w32_ios[fd], backlog); return socketio_listen(fd_table.w32_ios[fd], backlog);
} }
int w32_bind(int fd, const struct sockaddr *name, int namelen) { int w32_bind(int fd, const struct sockaddr *name, int namelen) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return socketio_bind(fd_table.w32_ios[fd], name, namelen); return socketio_bind(fd_table.w32_ios[fd], name, namelen);
} }
int w32_connect(int fd, const struct sockaddr* name, int namelen) { int w32_connect(int fd, const struct sockaddr* name, int namelen) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return socketio_connect(fd_table.w32_ios[fd], name, namelen); return socketio_connect(fd_table.w32_ios[fd], name, namelen);
} }
int w32_recv(int fd, void *buf, size_t len, int flags) { int w32_recv(int fd, void *buf, size_t len, int flags) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return socketio_recv(fd_table.w32_ios[fd], buf, len, flags); return socketio_recv(fd_table.w32_ios[fd], buf, len, flags);
} }
int w32_send(int fd, const void *buf, size_t len, int flags) { int w32_send(int fd, const void *buf, size_t len, int flags) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return socketio_send(fd_table.w32_ios[fd], buf, len, flags); return socketio_send(fd_table.w32_ios[fd], buf, len, flags);
} }
int w32_shutdown(int fd, int how) { int w32_shutdown(int fd, int how) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return socketio_shutdown(fd_table.w32_ios[fd], how); return socketio_shutdown(fd_table.w32_ios[fd], how);
} }
//file io
int w32_pipe(int *pfds){
pfds[0] = -1;
pfds[1] = -1;
struct w32_io* pio[2];
fileio_pipe(pio);
return -1;
}
int w32_open(const char *pathname, int flags, ...) {
struct w32_io* fileio = fileio_open(pathname, flags, 0);
return -1;
}
int w32_read(int fd, void *dst, unsigned int max) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return fileio_read(fd_table.w32_ios[fd], dst, max);
}
int w32_write(int fd, const void *buf, unsigned int max) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return fileio_write(fd_table.w32_ios[fd], buf, max);
}
int w32_fstat(int fd, struct stat *buf) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return fileio_fstat(fd_table.w32_ios[fd], buf);
}
int w32_isatty(int fd) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
return fileio_isatty(fd_table.w32_ios[fd]);
}
FILE* w32_fdopen(int fd, const char *mode) {
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return NULL;
}
return fileio_fdopen(fd_table.w32_ios[fd], mode);
}
//Common IO
int w32_close(int fd) { int w32_close(int fd) {
struct w32_io* pio = fd_table.w32_ios[fd]; struct w32_io* pio = fd_table.w32_ios[fd];
if (pio == NULL) {
errno = EBADF;
return -1;
}
fd_table_clear(pio->table_index); fd_table_clear(pio->table_index);
if ((pio->type == LISTEN_FD) || (pio->type == SOCK_FD)) { if ((pio->type == LISTEN_FD) || (pio->type == SOCK_FD)) {
return socketio_close(pio); return socketio_close(pio);
@ -182,6 +289,11 @@ int w32_fcntl(int fd, int cmd, ... /* arg */) {
va_list valist; va_list valist;
va_start(valist, cmd); va_start(valist, cmd);
if (fd_table.w32_ios[fd] == NULL) {
errno = EBADF;
return -1;
}
switch (cmd){ switch (cmd){
case F_GETFL: case F_GETFL:
return fd_table.w32_ios[fd]->fd_status_flags; return fd_table.w32_ios[fd]->fd_status_flags;
@ -215,7 +327,7 @@ int w32_select(int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, co
} }
if (!readfds && !writefds && !exceptfds) { if (!readfds && !writefds && !exceptfds) {
errno = EPERM; errno = EINVAL;
return -1; return -1;
} }
@ -225,7 +337,7 @@ int w32_select(int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, co
if (readfds && FD_ISSET(i, readfds)) { if (readfds && FD_ISSET(i, readfds)) {
if (fd_table.w32_ios[i] == NULL) { if (fd_table.w32_ios[i] == NULL) {
errno = EPERM; errno = EBADF;
return -1; return -1;
} }
@ -238,7 +350,7 @@ int w32_select(int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, co
if (writefds && FD_ISSET(i, writefds)) { if (writefds && FD_ISSET(i, writefds)) {
if (fd_table.w32_ios[i] == NULL) { if (fd_table.w32_ios[i] == NULL) {
errno = EPERM; errno = EBADF;
return -1; return -1;
} }
@ -325,3 +437,11 @@ int w32_select(int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, co
} }
int w32_dup(int oldfd) {
return -1;
}
int w32_dup2(int oldfd, int newfd) {
return -1;
}

View File

@ -1,4 +1,5 @@
#include <Windows.h> #include <Windows.h>
#include <stdio.h>
enum w32_io_type { enum w32_io_type {
UNKOWN_FD = 0, UNKOWN_FD = 0,
@ -75,3 +76,13 @@ int socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags);
int socketio_shutdown(struct w32_io* pio, int how); int socketio_shutdown(struct w32_io* pio, int how);
int socketio_close(struct w32_io* pio); int socketio_close(struct w32_io* pio);
//fileio
int fileio_pipe(struct w32_io* pio[2]);
struct w32_io* fileio_open(const char *pathname, int flags, int mode);
int fileio_read(struct w32_io* pio, void *dst, unsigned int max);
int fileio_write(struct w32_io* pio, const void *buf, unsigned int max);
int fileio_fstat(struct w32_io* pio, struct stat *buf);
int fileio_isatty(struct w32_io* pio);
FILE* fileio_fdopen(struct w32_io* pio, const char *mode);

View File

@ -1,9 +1,9 @@
#pragma once #pragma once
#include <memory.h>
#include <WinSock2.h> #include <WinSock2.h>
#include <WS2tcpip.h> #include <WS2tcpip.h>
#include <stdio.h> #include <stdio.h>
#include "defs.h"
//File Descriptor definitions //File Descriptor definitions
#define MAX_FDS 128 //a 2^n number #define MAX_FDS 128 //a 2^n number
@ -12,34 +12,6 @@ typedef struct w32_fd_set_ {
unsigned char bitmap[MAX_FDS >> 3]; unsigned char bitmap[MAX_FDS >> 3];
}w32_fd_set; }w32_fd_set;
#define fd_set w32_fd_set
#undef FD_ZERO
#define FD_ZERO(set) (memset( (set), 0, sizeof(w32_fd_set)))
#undef FD_SET
#define FD_SET(fd,set) ( (set)->bitmap[(fd) >> 3] |= (0x80 >> ((fd) % 8)))
#undef FD_ISSET
#define FD_ISSET(fd, set) (( (set)->bitmap[(fd) >> 3] & (0x80 >> ((fd) % 8)))?1:0)
#undef FD_CLR
#define FD_CLR(fd, set) ((set)->bitmap[(fd) >> 3] &= (~(0x80 >> ((fd) % 8))))
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
//fcntl commands
#define F_GETFL 0x1
#define F_SETFL 0x2
#define F_GETFD 0x4
#define F_SETFD 0x8
//fd status flags
#define O_NONBLOCK 0x1
//fd flags
#define FD_CLOEXEC 0x1
#define socket w32_socket
void w32posix_initialize(); void w32posix_initialize();
void w32posix_done(); void w32posix_done();
@ -69,10 +41,9 @@ int w32_recv(int fd, void *buf, size_t len, int flags);
int w32_send(int fd, const void *buf, size_t len, int flags); int w32_send(int fd, const void *buf, size_t len, int flags);
int w32_shutdown(int fd, int how); int w32_shutdown(int fd, int how);
/*non-network i/o*/ /*non-network (file) i/o*/
#define pipe w32_pipe #define pipe w32_pipe
#define open w32_open #define open w32_open
#define creat w32_creat
#define read w32_read #define read w32_read
#define write w32_write #define write w32_write
#define fstat w32_fstat #define fstat w32_fstat
@ -80,7 +51,6 @@ int w32_shutdown(int fd, int how);
#define fdopen w32_fdopen #define fdopen w32_fdopen
int w32_pipe(int *pfds); int w32_pipe(int *pfds);
int w32_open(const char *pathname, int flags, ...); int w32_open(const char *pathname, int flags, ...);
int w32_creat(const char *pathname, int mode);
int w32_read(int fd, void *dst, unsigned int max); int w32_read(int fd, void *dst, unsigned int max);
int w32_write(int fd, const void *buf, unsigned int max); int w32_write(int fd, const void *buf, unsigned int max);
int w32_fstat(int fd, struct stat *buf); int w32_fstat(int fd, struct stat *buf);

View File

@ -74,11 +74,13 @@
<Text Include="ReadMe.txt" /> <Text Include="ReadMe.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="fileio.cpp" />
<ClCompile Include="signal.c" /> <ClCompile Include="signal.c" />
<ClCompile Include="socketio.c" /> <ClCompile Include="socketio.c" />
<ClCompile Include="w32fd.c" /> <ClCompile Include="w32fd.c" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="defs.h" />
<ClInclude Include="w32fd.h" /> <ClInclude Include="w32fd.h" />
<ClInclude Include="w32posix.h" /> <ClInclude Include="w32posix.h" />
</ItemGroup> </ItemGroup>

View File

@ -27,6 +27,9 @@
<ClCompile Include="signal.c"> <ClCompile Include="signal.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="fileio.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="w32fd.h"> <ClInclude Include="w32fd.h">
@ -35,5 +38,8 @@
<ClInclude Include="w32posix.h"> <ClInclude Include="w32posix.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="defs.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>