From 6ab937c3e37b71315ab8661c6188ded49dfa182b Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 10 Feb 2015 20:39:57 +0000 Subject: [PATCH] Copy list when sorting windows for /wins --- src/common.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/common.c b/src/common.c index 95deecbb..3af4f550 100644 --- a/src/common.c +++ b/src/common.c @@ -488,25 +488,34 @@ cmp_win_num(gconstpointer a, gconstpointer b) int get_next_available_win_num(GList *used) { - used = g_list_sort(used, cmp_win_num); // only console used if (g_list_length(used) == 1) { return 2; } else { + GList *sorted = NULL; + GList *curr = used; + while (curr) { + sorted = g_list_insert_sorted(sorted, curr->data, cmp_win_num); + curr = g_list_next(curr); + } + int result = 0; int last_num = 1; - GList *curr = used; + curr = sorted; // skip console curr = g_list_next(curr); while (curr != NULL) { int curr_num = GPOINTER_TO_INT(curr->data); + if (((last_num != 9) && ((last_num + 1) != curr_num)) || ((last_num == 9) && (curr_num != 0))) { result = last_num + 1; if (result == 10) { result = 0; } + g_list_free(sorted); return (result); + } else { last_num = curr_num; if (last_num == 0) { @@ -520,6 +529,7 @@ get_next_available_win_num(GList *used) result = 0; } + g_list_free(sorted); return result; } }