From af2e194ad4bd255c5984c488e2b75b390232ff3f Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Wed, 9 Jul 2025 18:02:45 -0600 Subject: [PATCH] bug fixes --- Makefile | 26 ++++++++++-- list_adders.c | 112 ++++++++++++++++++++++++++++---------------------- main.c | 9 ++-- 3 files changed, 88 insertions(+), 59 deletions(-) diff --git a/Makefile b/Makefile index 1bf9676..8d97806 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,29 @@ CC = gcc CFLAGS = -g CPPFLAGS = -Wall -Wextra -pedantic -std=gnu90 -chatty: main.o - $(CC) $(CFLAGS) -o chatty main.o -lpthread +chatty: main.o liblist.a + $(CC) $(CFLAGS) -L. -o chatty main.o -llist -lpthread main.o: main.c - $(CC) $(CFLAGS) $(CPPFLAGS) -c -o main.o main.c + $(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o main.o main.c + +liblist.a: list_adders.o list_removers.o list_movers.o + ar -rcs liblist.a list_adders.o list_removers.o list_movers.o + +list_adders.o: list_adders.c + $(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o list_adders.o list_adders.c + +list_movers.o: list_movers.c + $(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o list_movers.o list_movers.c + +list_removers.o: list_removers.c + $(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o list_removers.o list_removers.c + +listtest: listtest.o liblist.a + $(CC) $(CFLAGS) -L. -o listtest listtest.o -llist + +listtest.o: listtest.c + $(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o listtest.o listtest.c clean: - rm -f *.o chatty + rm -f *.o chatty liblist.a diff --git a/list_adders.c b/list_adders.c index e873a90..1dd7534 100644 --- a/list_adders.c +++ b/list_adders.c @@ -3,19 +3,19 @@ #include LIST *ListCreate(void) { - LIST *new_list; + LIST *new_node_list; - new_list = malloc(sizeof(LIST)); - if (!new_list) { + new_node_list = malloc(sizeof(LIST)); + if (!new_node_list) { return NULL; } - new_list->size = 0; - new_list->current = 0; - new_list->head = 0; - new_list->tail = 0; + new_node_list->size = 0; + new_node_list->current = 0; + new_node_list->head = 0; + new_node_list->tail = 0; - return new_list; + return new_node_list; } int ListAdd(LIST *list, void *item) { @@ -24,28 +24,28 @@ int ListAdd(LIST *list, void *item) { * 2. Insert when cursor is at the tail (& list not empty) * 3. Insert when cursor is anywhere but the tail (& list not empty) */ - NODE *new = malloc(sizeof(NODE)); - if (new == NULL || (list->current == NULL && list->size != 0)) { + NODE *new_node = malloc(sizeof(NODE)); + if (new_node == NULL || (list->current == NULL && list->size != 0)) { return -1; } - new->data = item; + new_node->data = item; if (list->size == 0) { - list->current = new; - list->head = new; - list->tail = new; + list->current = new_node; + list->head = new_node; + list->tail = new_node; } else if (list->current == list->tail) { - new->previous = list->tail; - list->tail->next = new; - list->tail = new; - list->current = new; + new_node->previous = list->tail; + list->tail->next = new_node; + list->tail = new_node; + list->current = new_node; } else if (list->current != list->tail && list->size > 0) { - new->next = list->current->next; - new->previous = list->current; - list->current->next->previous = new; - list->current->next = new; - list->current = new; + new_node->next = list->current->next; + new_node->previous = list->current; + list->current->next->previous = new_node; + list->current->next = new_node; + list->current = new_node; } else { return -1; @@ -61,29 +61,29 @@ int ListInsert(LIST *list, void *item) { * 2. Insert when cursor is at the head (& list not empty) * 3. Insert when cursor is anywhere but the head (& list not empty) */ - NODE *new = malloc(sizeof(NODE)); - if (new == NULL || (list->current == NULL && list->size != 0)) { + NODE *new_node = malloc(sizeof(NODE)); + if (new_node == NULL || (list->current == NULL && list->size != 0)) { return -1; } - new->data = item; + new_node->data = item; if (list->size == 0) { - list->current = new; - list->head = new; - list->tail = new; + list->current = new_node; + list->head = new_node; + list->tail = new_node; } else if (list->current == list->head) { - new->next = list->head; - list->head->previous = new; - list->head = new; - list->current = new; + new_node->next = list->head; + list->head->previous = new_node; + list->head = new_node; + list->current = new_node; } else if (list->current != list->head && list->size > 0) { - new->next = list->current; - new->previous = list->current->previous; - list->current->previous->next = new; - list->current->previous = new; - list->current = new; + new_node->next = list->current; + new_node->previous = list->current->previous; + list->current->previous->next = new_node; + list->current->previous = new_node; + list->current = new_node; } else { return -1; @@ -94,28 +94,40 @@ int ListInsert(LIST *list, void *item) { } int ListPrepend(LIST *list, void *item) { - NODE *new = malloc(sizeof(NODE)); - if (new == NULL) { + NODE *new_node = malloc(sizeof(NODE)); + if (new_node == NULL) { return -1; } - new->data = item; - new->next = list->head; - list->head->previous = new; - list->head = new; + new_node->data = item; + if (list->size == 0) { + list->current = new_node; + list->head = new_node; + list->tail = new_node; + } + new_node->next = list->head; + list->head->previous = new_node; + list->head = new_node; list->size++; return 0; } int ListAppend(LIST *list, void *item) { - NODE *new = malloc(sizeof(NODE)); - if (new == NULL) { + NODE *add = malloc(sizeof(NODE)); + if (add == NULL) { return -1; } - new->data = item; - new->previous = list->tail; - list->tail->next = new; - list->tail = new; + add->data = item; + if (list->size == 0) { + list->current = add; + list->head = add; + list->tail = add; + } + else { + add->previous = list->tail; + list->tail->next = add; + list->tail = add; + } list->size++; return 0; diff --git a/main.c b/main.c index 0e59c20..cb305ff 100644 --- a/main.c +++ b/main.c @@ -25,13 +25,12 @@ LIST *receivelist, *sendlist; pthread_mutex_t sendmut, receivemut; void *input(void *arg) { - char msg[512]; - + char msg[30]; while (1) { printf("Type a message: "); - scanf("%s", msg); + fgets(msg, sizeof(msg), stdin); pthread_mutex_lock(&sendmut); - ListAppend(sendlist, msg); + ListAppend(sendlist, &msg); pthread_mutex_unlock(&sendmut); } return 0; @@ -180,7 +179,7 @@ int main(int argc, char* argv[]) { } pthread_create(&input_thread, NULL, input, (void*) msg); pthread_join(input_thread, NULL); - + close(socketfd); close(confd); return 0;