From d8b44c12818db535eaf000dbf71ccc0ea51dd5e1 Mon Sep 17 00:00:00 2001 From: snarmph Date: Sun, 3 Oct 2021 17:03:11 +0200 Subject: [PATCH] added some stuff --- socket.c | 12 +++++++++-- socket.h | 8 ++++++- strstream.c | 2 ++ strutils.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ strutils.h | 15 ++++++++----- 5 files changed, 91 insertions(+), 8 deletions(-) diff --git a/socket.c b/socket.c index 798bb1e..b639036 100644 --- a/socket.c +++ b/socket.c @@ -121,11 +121,19 @@ bool skConnectPro(socket_t sock, const struct sockaddr *name, socket_len_t namel } int skSend(socket_t sock, char *buf, int len) { - return send(sock, buf, len, 0); + return skSendPro(sock, buf, len, 0); +} + +int skSendPro(socket_t sock, char *buf, int len, int flags) { + return send(sock, buf, len, flags); } int skReceive(socket_t sock, char *buf, int len) { - return recv(sock, buf, len, 0); + return skReceivePro(sock, buf, len, 0); +} + +int skReceivePro(socket_t sock, char *buf, int len, int flags) { + return recv(sock, buf, len, flags); } bool skIsValid(socket_t sock) { diff --git a/socket.h b/socket.h index f9ed90b..c45ad5a 100644 --- a/socket.h +++ b/socket.h @@ -16,11 +16,13 @@ extern "C" { struct sockaddr; #if SOCK_WINDOWS - typedef uintptr_t socket_t; + #include + typedef SOCKET socket_t; typedef int socket_len_t; #define INVALID_SOCKET (socket_t)(~0) #define SOCKET_ERROR (-1) #elif SOCK_POSIX + #include typedef int socket_t; typedef uint32_t socket_len_t; #define INVALID_SOCKET (-1) @@ -66,8 +68,12 @@ bool skConnectPro(socket_t sock, const struct sockaddr *name, socket_len_t namel // Sends data on a socket, returns true on success int skSend(socket_t sock, char *buf, int len); +// Sends data on a socket, returns true on success +int skSendPro(socket_t sock, char *buf, int len, int flags); // Receives data from a socket, returns byte count on success, 0 on connection close or -1 on error int skReceive(socket_t sock, char *buf, int len); +// Sends data on a socket, returns true on success +int skReceivePro(socket_t sock, char *buf, int len, int flags); // Checks that a opened socket is valid, returns true on success bool skIsValid(socket_t sock); diff --git a/strstream.c b/strstream.c index 87daa11..bc6d921 100644 --- a/strstream.c +++ b/strstream.c @@ -324,6 +324,8 @@ str_ostream_t ostrInitStr(const char *cstr, size_t len) { str_ostream_t stream; stream.buf = malloc(len + 1); memcpy(stream.buf, cstr, len); + stream.size = len; + stream.allocated = len + 1; return stream; } diff --git a/strutils.c b/strutils.c index f727800..f80ad73 100644 --- a/strutils.c +++ b/strutils.c @@ -4,6 +4,68 @@ #include #include +#ifdef _WIN32 +#define _BUFSZ 128 + +// modified from netbsd source http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/pkgtools/libnbcompat/files/getdelim.c?only_with_tag=MAIN +ssize_t getdelim(char **buf, size_t *bufsz, int delimiter, FILE *fp) { + char *ptr, *eptr; + + if(*buf == NULL || *bufsz == 0) { + *bufsz = _BUFSZ; + if((*buf = malloc(*bufsz)) == NULL) { + return -1; + } + } + + ssize_t result = -1; + // usually fgetc locks every read, using windows-specific + // _lock_file and _unlock_file will be faster + _lock_file(fp); + + for(ptr = *buf, eptr = *buf + *bufsz;;) { + int c = _getc_nolock(fp); + if(c == -1) { + if(feof(fp)) { + ssize_t diff = (ssize_t)(ptr - *buf); + if(diff != 0) { + *ptr = '\0'; + result = diff; + break; + } + } + break; + } + *ptr++ = c; + if(c == delimiter) { + *ptr = '\0'; + result = ptr - *buf; + break; + } + if((ptr + 2) >= eptr) { + char *nbuf; + size_t nbufsz = *bufsz * 2; + ssize_t d = ptr - *buf; + if((nbuf = realloc(*buf, nbufsz)) == NULL) { + break; + } + *buf = nbuf; + *bufsz = nbufsz; + eptr = nbuf + nbufsz; + ptr = nbuf + d; + } + } + + _unlock_file(fp); + return result; +} + +// taken from netbsd source http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/pkgtools/libnbcompat/files/getline.c?only_with_tag=MAIN +ssize_t getline(char **line_ptr, size_t *n, FILE *stream) { + return getdelim(line_ptr, n, '\n', stream); +} +#endif + void strToLower(char *str) { for(char *beg = str; *beg; ++beg) { *beg = tolower(*beg); diff --git a/strutils.h b/strutils.h index 0474775..60fbb55 100644 --- a/strutils.h +++ b/strutils.h @@ -7,12 +7,17 @@ extern "C" { #include #include -#if _WIN32 -#define stricmp _stricmp -#elif __unix__ || __APPLE__ -#define stricmp strcasecmp +#ifdef _WIN32 + #include + #include + typedef SSIZE_T ssize_t; + ssize_t getdelim(char **buf, size_t *bufsz, int delimiter, FILE *fp); + ssize_t getline(char **line_ptr, size_t *n, FILE *stream); + #define stricmp _stricmp #else -#error "stricmp is not supported" + #define stricmp strcasecmp + #define _GNU_SOURCE + #include #endif // prefix str -> changes string