about summary refs log tree commit diff
path: root/seq.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2019-01-10 16:43:25 +0100
committerLeah Neukirchen <leah@vuxu.org>2019-01-10 16:43:25 +0100
commitc5cd4df026e8f8b7a8334028dda21af13e205ce3 (patch)
tree593c90b07713a4b8645c431e53d60ace3a5720fc /seq.c
parent518ff4c14880fd6f460a15670d29abd00f0bfdcc (diff)
downloadmblaze-c5cd4df026e8f8b7a8334028dda21af13e205ce3.tar.gz
mblaze-c5cd4df026e8f8b7a8334028dda21af13e205ce3.tar.xz
mblaze-c5cd4df026e8f8b7a8334028dda21af13e205ce3.zip
seq: sort dir file lists numerically
Diffstat (limited to 'seq.c')
-rw-r--r--seq.c59
1 files changed, 34 insertions, 25 deletions
diff --git a/seq.c b/seq.c
index b8186af..0272e31 100644
--- a/seq.c
+++ b/seq.c
@@ -467,44 +467,53 @@ blaze822_seq_next(char *map, char *range, struct blaze822_seq_iter *iter)
 	return r;
 }
 
+int mystrverscmp(const char *, const char *);
+
+static int
+mailsort(const struct dirent **a, const struct dirent **b)
+{
+	return mystrverscmp((*a)->d_name, (*b)->d_name);
+}
+
 static long
 iterdir(char *dir, void (*cb)(char *))
 {
-	DIR *fd, *fd2;
-	struct dirent *d;
+	struct dirent **namelist;
 
-	long i = 0;
+	int n;
+
+	char sub[PATH_MAX];
+	snprintf(sub, sizeof sub, "%s/cur", dir);
 
-	fd = opendir(dir);
-	if (!fd) {
+	char *m = "/cur";
+
+	n = scandir(sub, &namelist, 0, mailsort);
+	if (n == -1 && (errno == ENOENT || errno == ENOTDIR)) {
+		m = "";
+		n = scandir(dir, &namelist, 0, mailsort);
+	}
+		
+	if (n == -1) {	
 		if (errno == ENOTDIR)
 			cb(dir);
 		return 1;
 	}
 
-	char sub[PATH_MAX];
-	snprintf(sub, sizeof sub, "%s/cur", dir);
-	fd2 = opendir(sub);
-	if (fd2) {
-		closedir(fd);
-		fd = fd2;
-	}
-
-	while ((d = readdir(fd))) {
+	long i = 0;
+	for (i = 0; i < n; i++) {
+		if (namelist[i]->d_name[0] != '.')
 #if defined(DT_REG) && defined(DT_UNKNOWN)
-		if (d->d_type != DT_REG && d->d_type != DT_UNKNOWN)
-			continue;
+		if (namelist[i]->d_type == DT_REG ||
+		    namelist[i]->d_type == DT_UNKNOWN)
 #endif
-		if (d->d_name[0] == '.')
-			continue;
-		if (fd2)
-			snprintf(sub, sizeof sub, "%s/cur/%s", dir, d->d_name);
-		else
-			snprintf(sub, sizeof sub, "%s/%s", dir, d->d_name);
-		cb(sub);
-		i++;
+		{
+			snprintf(sub, sizeof sub, "%s%s/%s",
+			    dir, m, namelist[i]->d_name);
+			cb(sub);
+		}
+		free(namelist[i]);
 	}
-	closedir(fd);
+	free(namelist);
 
 	return i;
 }