commit af41b95

sewn  ·  2026-08-22 11:00:33 +0000 UTC
parent f7a5940
scan recursively
1 files changed,  +9, -5
M wawa.c
M wawa.c
+9, -5
 1@@ -417,16 +417,20 @@ main(int argc, char *argv[])
 2 		if (stat(argv[2], &st) < 0) die("fopen %s:", argv[2]);
 3 
 4 		const char *path = argv[2];
 5-		struct dirent **namelist = NULL;
 6+		struct dirent **namelist = NULL, *name;
 7 		int dirn = 0;
 8 
 9 		if (S_ISDIR(st.st_mode)) {
10 			srand((intptr_t)path | (unsigned int)time(NULL));
11-
12-			if (chdir(path) < 0) die("chdir:");
13-			if (!(dirn = scandir(".", &namelist, NULL, alphasort))) die("scandir:");
14+scan:
15+			if (chdir(path) < 0) die("chdir:"); // slower & easier than strncpy PATH_MAX
16+			if (namelist) free(namelist);
17+			if ((dirn = scandir(".", &namelist, NULL, alphasort)) < 0) die("scandir:");
18 			if (dirn < 3) die("no images");
19-			path = namelist[rand() % dirn]->d_name;
20+			int i = 2 + rand() % (dirn - 2);
21+			name = namelist[i];
22+			path = name->d_name;
23+			if (name->d_type == DT_DIR) goto scan;
24 		}
25 
26 		if (!(image.fp = fopen(path, "rb"))) die("fopen %s:", path);