From c1847394828b08393e5d6f1750d7143be598a226 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Windows 11)" <rob@synchro.net>
Date: Fri, 3 May 2024 19:27:55 -0700
Subject: [PATCH] Re-assign the dirnum and subnum fields after sorting dirs or
 subs

The dirnum and subnum are used for the next/previous (left and right) arrow
nav through sub-boards and directories. For message groups or file libs that
are configured to auto-sort the sub-boards or directory list, when importing
an unsorted list, the dirnum and subnum values would then be wrong after the
qsort() at the end of the import thus making the left/right nav thing broken
(jump to other groups/libs and such).

This fixes that.

Could I have fixed this by passing an index value to the next/prev_dirnum()
and _subnum() functions and returning the next/previous index value instead of
relying on the subnum/dirnum elemment to be correct? Maybe. <shrug>
Do I like second guessing myself? Not really.
---
 src/sbbs3/scfg/scfg.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/sbbs3/scfg/scfg.c b/src/sbbs3/scfg/scfg.c
index f97f2cafab..c4678175d5 100644
--- a/src/sbbs3/scfg/scfg.c
+++ b/src/sbbs3/scfg/scfg.c
@@ -125,6 +125,13 @@ void sort_subs(int grpnum)
 {
 	sort_group = grpnum;
 	qsort(cfg.sub, cfg.total_subs, sizeof(sub_t*), sub_compare);
+
+	// Re-number the sub-boards after sorting:
+	for(int i = 0; i < cfg.total_subs; ++i) {
+		if(cfg.sub[i]->grp != grpnum)
+			continue;
+		cfg.sub[i]->subnum = i;
+	}
 }
 
 static int sort_lib = 0;
@@ -152,6 +159,13 @@ void sort_dirs(int libnum)
 {
 	sort_lib = libnum;
 	qsort(cfg.dir, cfg.total_dirs, sizeof(dir_t*), dir_compare);
+
+	// Re-number the directories after sorting:
+	for(int i = 0; i < cfg.total_dirs; ++i) {
+		if(cfg.dir[i]->lib != libnum)
+			continue;
+		cfg.dir[i]->dirnum = i;
+	}
 }
 
 void wizard_msg(int page, int total, const char* text)
-- 
GitLab