From edbe534a1fcbabd8bc10d0d0154e56896e1cc594 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Thu, 9 Feb 2023 18:36:51 +0100 Subject: [PATCH] created read_socket function --- src/chat/server/main.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/chat/server/main.c b/src/chat/server/main.c index d1ae596..b7b03b8 100644 --- a/src/chat/server/main.c +++ b/src/chat/server/main.c @@ -19,6 +19,7 @@ along with this program. If not, see . #include void die(char *exit_message); +char *read_socket(int socket); int main(void) { @@ -59,4 +60,26 @@ void die(char *exit_msg) why2_clean_memory(why2_get_default_memory_identifier()); //GARBAGE COLLECTOR exit(0); +} + +char *read_socket(int socket) +{ + FILE *opened = why2_fdopen(socket, "r"); //OPEN socket + long content_size; + char *content; + + //COUNT content_size + fseek(opened, 0, SEEK_END); + content_size = ftell(opened); + rewind(opened); //REWIND + + //ALLOCATE + content = why2_calloc(content_size, sizeof(char)); + + if (fread(content, content_size, 1, opened) != 1) die("Reading socket failed!"); + + //DEALLOCATION + why2_deallocate(opened); + + return content; } \ No newline at end of file