fix: using file descriptors instead of FILE*

we can directly write to a socket using dprintf, no need for a buffer
This commit is contained in:
2024-01-20 17:53:36 +01:00
parent b4560bd748
commit 8880fae4aa
11 changed files with 75 additions and 97 deletions

View File

@@ -1,5 +1,4 @@
SRC = lib_use.c \
fmemopen.c
SRC = lib_use.c
OBJ = $(SRC:.c=.o)
NAME = lib_use
CC = gcc

View File

@@ -1,43 +0,0 @@
#include "fmemopen.h"
typedef int make_iso_compilers_happy; //...
#ifdef _WIN32
#include <windows.h>
#include <share.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
/* https://github.com/Arryboom/fmemopen_windows */
FILE *fmemopen(void *buf, size_t len, __attribute__((unused)) const char *type)
{
int fd;
FILE *fp;
char tp[MAX_PATH - 13];
char fn[MAX_PATH + 1];
int * pfd = &fd;
int retner = -1;
char tfname[] = "MemTF_";
if (!GetTempPathA(sizeof(tp), tp))
return NULL;
if (!GetTempFileNameA(tp, tfname, 0, fn))
return NULL;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration" //msys not happy
retner = _sopen_s(pfd, fn, _O_CREAT | _O_SHORT_LIVED | _O_TEMPORARY | _O_RDWR | _O_BINARY | _O_NOINHERIT, _SH_DENYRW, _S_IREAD | _S_IWRITE);
#pragma GCC diagnostic pop
if (retner != 0)
return NULL;
if (fd == -1)
return NULL;
fp = _fdopen(fd, "wb+");
if (!fp) {
_close(fd);
return NULL;
}
/*File descriptors passed into _fdopen are owned by the returned FILE * stream.If _fdopen is successful, do not call _close on the file descriptor.Calling fclose on the returned FILE * also closes the file descriptor.*/
fwrite(buf, len, 1, fp);
rewind(fp);
return fp;
}
#endif

View File

@@ -1,6 +0,0 @@
#pragma once
#ifdef _WIN32
#include <stdio.h>
FILE *fmemopen(void *buf, size_t len, const char *type);
#endif

View File

@@ -8,5 +8,5 @@ int main(int argc, char **argv) {
fprintf(stderr, "Usage : %s filename\n", argv[0]);
return 1;
}
return pasm_run_script(argv[1], 0, 0, stderr);
return pasm_run_script(argv[1], 0, 0, 1);
}

View File

@@ -1,24 +1,52 @@
#include "../include/pasm.h"
#include "fmemopen.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(void) {
char output[1024] = {0};
FILE *memfile = fmemopen(output, sizeof(output), "w+");
if (memfile != NULL) {
pasm_run_script("../examples/test.pasm", 0, 0, memfile);
#ifdef _WIN32
fseek(memfile, 0, SEEK_SET);
fread(output, sizeof(char), sizeof(output), memfile);
printf("%s", output);
fclose(memfile);
#else
fclose(memfile);
printf("%s", output);
#endif
} else {
printf("null\n");
#define SERVER_IP "192.168.1.16"
#define SERVER_PORT 1337
#define BUFFER_SIZE 1024
int main() {
int clientSocket;
struct sockaddr_in serverAddr;
char buffer[BUFFER_SIZE];
if ((clientSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "Socket creation failed");
return 1;
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, SERVER_IP, &serverAddr.sin_addr) <= 0) {
fprintf(stderr, "Invalid address/ Address not supported");
return 1;
}
if (connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == -1) {
fprintf(stderr, "Connection failed");
return 1;
}
while (1) {
// Receive data from the server
int bytesRead = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0);
if (bytesRead == -1) {
fprintf(stderr, "Receive failed");
close(clientSocket);
return 1;
}
buffer[bytesRead] = '\0'; // Null-terminate the received data
if (bytesRead > 0 && buffer[bytesRead - 1] == '\n')
buffer[bytesRead - 1] = '\0'; //deletes \n
printf("script path: %s\n", buffer);
pasm_run_script(buffer, 0, 0, clientSocket); //considering buffer is the path of a valid .pasm script
}
close(clientSocket);
return 0;
}