aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/stdlib.c13
-rw-r--r--std/stdlib.sloth2
-rw-r--r--std/stdsocket.c48
-rw-r--r--std/stdsocket.sloth5
4 files changed, 67 insertions, 1 deletions
diff --git a/std/stdlib.c b/std/stdlib.c
index 4a650a4..ed848c3 100644
--- a/std/stdlib.c
+++ b/std/stdlib.c
@@ -3,6 +3,7 @@
#include <string.h>
#include <time.h>
#include <stdio.h>
+#include <stdbool.h>
int wait(int msec) {
struct timespec ts;
@@ -40,6 +41,18 @@ int as_int(float x) {
return (int) x;
}
+bool sequals(char* a, char* b) {
+ if (strlen(a) != strlen(b)) {
+ return false;
+ }
+ for (int i=0; i<strlen(a); i++) {
+ if (a[i] != b[i]) {
+ return false;
+ }
+ }
+ return true;
+}
+
char* istr(int x) {
char* snum = malloc(12);
sprintf(snum, "%d", x);
diff --git a/std/stdlib.sloth b/std/stdlib.sloth
index 4d3617f..48a92bd 100644
--- a/std/stdlib.sloth
+++ b/std/stdlib.sloth
@@ -7,5 +7,5 @@ foreign fn termpos(x: Int, y: Int);
foreign fn as_int(x: Float) Int;
foreign fn istr(x: Int) String;
foreign fn system(cmd: String) Int;
-
+foreign fn sequals(a: String, b: String) Bool;
foreign fn termclear() Void;
diff --git a/std/stdsocket.c b/std/stdsocket.c
new file mode 100644
index 0000000..592291d
--- /dev/null
+++ b/std/stdsocket.c
@@ -0,0 +1,48 @@
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#define PORT 8080
+
+int serversock() {
+ int opt = 1;
+ int sock = socket(AF_INET, SOCK_STREAM, 0);
+ struct sockaddr_in address;
+ int addrlen = sizeof(address);
+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
+ address.sin_family = AF_INET;
+ address.sin_addr.s_addr = INADDR_ANY;
+ address.sin_port = htons(PORT);
+
+ bind(sock, (struct sockaddr*)&address, sizeof(address));
+ listen(sock, 3);
+ int new_sock = accept(sock, (struct sockaddr*)&address, (socklen_t*)&addrlen);
+ return new_sock;
+}
+
+int clientsock() {
+ struct sockaddr_in serv_addr;
+ int sock = socket(AF_INET, SOCK_STREAM, 0);
+ serv_addr.sin_family = AF_INET;
+ serv_addr.sin_port = htons(PORT);
+
+ inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
+ int status = connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
+ return sock;
+}
+
+char* recvsock(int soc) {
+ char* buf = malloc(1024);
+ int valread = read(soc, buf, 1024);
+ return buf;
+}
+
+void sendsock(char* msg, int soc) {
+ send(soc, msg, strlen(msg), 0);
+}
+
+void closesock(int soc) {
+ close(soc);
+}
diff --git a/std/stdsocket.sloth b/std/stdsocket.sloth
new file mode 100644
index 0000000..46a5328
--- /dev/null
+++ b/std/stdsocket.sloth
@@ -0,0 +1,5 @@
+foreign fn serversock() Int;
+foreign fn clientsock() Int;
+foreign fn closesock(soc: Int);
+foreign fn sendsock(msg: String, soc: Int);
+foreign fn recvsock(soc: Int) String;