Finally Make Server STDIN Code Not Suck As Much

This commit is contained in:
TheBrokenRail 2021-11-20 23:09:25 -05:00
parent d17416421a
commit 7ad36d0ce1

View File

@ -9,6 +9,7 @@
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <sys/ioctl.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
@ -231,17 +232,23 @@ static void *read_stdin_thread(__attribute__((unused)) void *data) {
if (isatty(fileno(stdin))) { if (isatty(fileno(stdin))) {
// Loop // Loop
while (1) { while (1) {
if (!stdin_buffer_complete) { int bytes_available;
// Read Data if (ioctl(fileno(stdin), FIONREAD, &bytes_available) == -1) {
int x = fgetc(stdin); bytes_available = 0;
if (x != EOF) { }
if (x == '\n') { for (int i = 0; i < bytes_available; i++) {
if (stdin_buffer == NULL) { if (!stdin_buffer_complete) {
stdin_buffer = strdup(""); // Read Data
int x = fgetc(stdin);
if (x != EOF) {
if (x == '\n') {
if (stdin_buffer == NULL) {
stdin_buffer = strdup("");
}
stdin_buffer_complete = true;
} else {
string_append((char **) &stdin_buffer, "%c", (char) x);
} }
stdin_buffer_complete = true;
} else {
string_append((char **) &stdin_buffer, "%c", (char) x);
} }
} }
} }