#include #include "../ipc.c" struct str { char msg[126]; int len; }; void set_output(struct str* output, char* msg) { int i = 0; while (msg[i] != 0) { assert(i < 126); output->msg[i] = msg[i]; i++; } output->len = i; } struct strs { struct str* data; int capacity; int count; }; struct strs init_strs() { return (struct strs) { .data = malloc(sizeof(struct str) * 10), .count = 0, .capacity = 10, }; } void add_str(struct strs* strs, char* str) { if (strs->count + 1 > strs->capacity) { strs->capacity += 30; strs->data = realloc(strs->data, sizeof(struct str) * strs->capacity); } struct str item; set_output(&item, str); strs->data[strs->count] = item; strs->count++; } struct strs handle_input(mqd_t tx, mqd_t rx, char* payload, int len) { printf("Message: %.*s", len, payload); fflush(stdout); struct strs output = init_strs(); int n = get_num_messages(rx); while (n > 0) { char* buf = read_queue(rx); add_str(&output, buf); free(buf); n--; if (n == 0) n = get_num_messages(rx); } printf(" -> done\n"); // if (buf) free(buf); // // buf = read_queue(rx); // printf("%p\n", buf); // add_str(&output, buf); // if (buf) free(buf); return output; }