http app
This commit is contained in:
51
os_linux.c
51
os_linux.c
@@ -5,15 +5,15 @@
|
||||
|
||||
#include "sys/mman.h"
|
||||
#include "sys/stat.h"
|
||||
#include "unistd.h" // POSIX Standard
|
||||
#include "stdio.h"
|
||||
#include "string.h" // memcpy TODO(dledda): replace memcpy with custom impl?
|
||||
#include "unistd.h" // POSIX Standard, read, write, close, open, etc.
|
||||
|
||||
#include "pthread.h"
|
||||
|
||||
#include "fcntl.h"
|
||||
#include "sys/epoll.h"
|
||||
|
||||
#include "sys/socket.h"
|
||||
#include "arpa/inet.h"
|
||||
#include "string.h" // memcpy
|
||||
|
||||
|
||||
void *os_alloc(uint64 capacity) {
|
||||
@@ -34,17 +34,21 @@ void os_free(void *ptr, uint64 size) {
|
||||
string os_readEntireFile(Arena *arena, string filename) {
|
||||
Scratch temp = scratchStart(&arena, 1);
|
||||
|
||||
FILE *input = fopen(cstring(temp.arena, filename), "r");
|
||||
int input = open(cstring(temp.arena, filename), O_RDONLY);
|
||||
string readBuffer;
|
||||
if (input) {
|
||||
struct stat st;
|
||||
stat((char *)filename.str, &st);
|
||||
uint64 fsize = st.st_size;
|
||||
readBuffer = PushString(arena, fsize);
|
||||
fread(readBuffer.str, sizeof(byte), readBuffer.length, input);
|
||||
fclose(input);
|
||||
int64 bytesRead = read(input, readBuffer.str, readBuffer.length);
|
||||
close(input);
|
||||
if (bytesRead == -1) {
|
||||
arenaPopTo(arena, readBuffer.str);
|
||||
return s("");
|
||||
}
|
||||
} else {
|
||||
readBuffer = PushString(arena, 0);
|
||||
readBuffer = s("");
|
||||
}
|
||||
|
||||
scratchEnd(temp);
|
||||
@@ -55,11 +59,13 @@ bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, uin
|
||||
Scratch temp = scratchStart(&arena, 1);
|
||||
|
||||
bool result = false;
|
||||
FILE *output = fopen(cstring(temp.arena, filename), "w");
|
||||
int output = open(cstring(temp.arena, filename), O_WRONLY);
|
||||
if (output) {
|
||||
fwrite(contents, sizeof(byte), contentsLength, output);
|
||||
fclose(output);
|
||||
result = true;
|
||||
int64 bytesWritten = write(output, contents, contentsLength);
|
||||
if (bytesWritten != -1) {
|
||||
result = true;
|
||||
}
|
||||
close(output);
|
||||
}
|
||||
|
||||
scratchEnd(temp);
|
||||
@@ -70,11 +76,13 @@ bool os_fileAppend(Arena *arena, string filename, const byte *contents, uint64 c
|
||||
Scratch temp = scratchStart(&arena, 1);
|
||||
|
||||
bool result = false;
|
||||
FILE *output = fopen(cstring(temp.arena, filename), "a");
|
||||
int output = open(cstring(temp.arena, filename), O_APPEND);
|
||||
if (output) {
|
||||
fwrite(contents, sizeof(byte), contentsLength, output);
|
||||
fclose(output);
|
||||
result = true;
|
||||
int bytesWritten = write(output, contents, contentsLength);
|
||||
if (bytesWritten != -1) {
|
||||
result = true;
|
||||
}
|
||||
close(output);
|
||||
}
|
||||
|
||||
scratchEnd(temp);
|
||||
@@ -98,12 +106,10 @@ void os_println(StdStream target, const char *fmt, va_list argList) {
|
||||
write(0, (const void *)result.str, result.length);
|
||||
break;
|
||||
case StdStream_stderr:
|
||||
fflush(stderr);
|
||||
write(2, (const void *)result.str, result.length);
|
||||
break;
|
||||
case StdStream_stdout:
|
||||
default:
|
||||
fflush(stdout);
|
||||
write(1, (const void *)result.str, result.length);
|
||||
break;
|
||||
}
|
||||
@@ -121,12 +127,10 @@ void os_print(StdStream target, const char *fmt, va_list argList) {
|
||||
write(0, (const void *)result.str, result.length);
|
||||
break;
|
||||
case StdStream_stderr:
|
||||
fflush(stderr);
|
||||
write(2, (const void *)result.str, result.length);
|
||||
break;
|
||||
case StdStream_stdout:
|
||||
default:
|
||||
fflush(stdout);
|
||||
write(1, (const void *)result.str, result.length);
|
||||
break;
|
||||
}
|
||||
@@ -211,8 +215,6 @@ Socket *serverAccept(Server *s) {
|
||||
uint64 clientSockHandle = accept((int)(uint64)s->handle, (struct sockaddr *)clientAddr, &clientAddrLen);
|
||||
if (clientSockHandle == -1) {
|
||||
clientSockHandle = (uint64)NULL;
|
||||
println("ERR server accept");
|
||||
perror("accept");
|
||||
} else {
|
||||
fcntl((uint64)clientSockHandle, F_SETFL, fcntl((uint64)clientSockHandle, F_GETFL, 0) | O_NONBLOCK);
|
||||
}
|
||||
@@ -339,7 +341,9 @@ void socketClose(Socket *s) {
|
||||
|
||||
Socket socketConnect(Arena *arena, SocketConnectInfo info) {
|
||||
int socketFd = socket(AF_INET6, SOCK_STREAM, 0 /* IPPROTO_TCP */);
|
||||
fcntl(socketFd, F_SETFL, fcntl(socketFd, F_GETFL, 0) | O_NONBLOCK);
|
||||
if (!info.blocking) {
|
||||
fcntl(socketFd, F_SETFL, fcntl(socketFd, F_GETFL, 0) | O_NONBLOCK);
|
||||
}
|
||||
|
||||
struct sockaddr_in6 *remoteAddr = PushStructZero(arena, struct sockaddr_in6);
|
||||
remoteAddr->sin6_family = AF_INET6;
|
||||
@@ -352,6 +356,7 @@ Socket socketConnect(Arena *arena, SocketConnectInfo info) {
|
||||
.address=(Address *)remoteAddr,
|
||||
.closed=false,
|
||||
//.closed=connectErr == -1,
|
||||
// TODO(dledda): investigate error behaviour
|
||||
};
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user