From 7059385ca136e50a7f2d3866c0e1f37b350b7cc7 Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Mon, 14 Jul 2025 23:22:04 -0600 Subject: [PATCH] update output function and fix segfault --- list_movers.c | 16 +++++++++++----- main.c | 12 ++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/list_movers.c b/list_movers.c index 18b9cab..12c220b 100644 --- a/list_movers.c +++ b/list_movers.c @@ -6,13 +6,19 @@ int ListCount(LIST *list) { } void *ListFirst(LIST *list) { - list->current = list->head; - return list->current->data; -} + if (list->size != 0) { + list->current = list->head; + return list->current->data; + } + return NULL; +} void *ListLast(LIST *list) { - list->current = list->tail; - return list->current->data; + if (list->size != 0) { + list->current = list->tail; + return list->current->data; + } + return NULL; } void *ListNext(LIST *list) { diff --git a/main.c b/main.c index bd73135..4bdd83f 100644 --- a/main.c +++ b/main.c @@ -69,11 +69,15 @@ void *output(void *arg) { char msg[30]; while (1) { pthread_mutex_lock(&receivemut); - ListFirst(receivelist); - strcpy(msg, ListCurr(receivelist)); - ListRemove(receivelist); + if (ListCount(receivelist) != 0) { + ListFirst(receivelist); + strcpy(msg, ListCurr(receivelist)); + ListRemove(receivelist); + } pthread_mutex_unlock(&receivemut); - printf("%s\n", msg); + if (ListCount(receivelist) != 0) { + printf("%s\n", msg); + } } return 0;