commit 6236025
sewn
·
2026-08-21 13:03:02 +0000 UTC
parent bee2421
pick randomly from if given directory i mean, i can just add this. i have a script sitting around my bin/ that does just this, but since i have my own wallpaper setter, i might as well add this feature only because i can, and it doesn't add many lines.
1 files changed,
+22,
-2
M
wawa.c
M
wawa.c
+22,
-2
1@@ -1,8 +1,11 @@
2 /* See LICENSE file for copyright and license details. */
3 #include <byteswap.h>
4+#include <dirent.h>
5 #include <fcntl.h>
6 #include <stdbool.h>
7 #include <sys/mman.h>
8+#include <sys/stat.h>
9+#include <time.h>
10 #include <unistd.h>
11 #include <wayland-client-protocol.h>
12 #include <wayland-client.h>
13@@ -410,7 +413,24 @@ main(int argc, char *argv[])
14 else if (!strcmp(argv[1], "spread")) image_modify = image_spread;
15 else goto usage;
16
17- if (!(image.fp = fopen(argv[2], "rb"))) die("fopen %s:", argv[2]);
18+ struct stat st;
19+ if (stat(argv[2], &st) < 0) die("fopen %s:", argv[2]);
20+
21+ const char *path = argv[2];
22+ struct dirent **namelist = NULL;
23+ int dirn = 0;
24+
25+ if (S_ISDIR(st.st_mode)) {
26+ srand((intptr_t)path | (unsigned int)time(NULL));
27+
28+ if (chdir(path) < 0) die("chdir:");
29+ if (!(dirn = scandir(".", &namelist, NULL, alphasort))) die("scandir:");
30+ if (dirn < 3) die("no images");
31+ path = namelist[rand() % dirn]->d_name;
32+ }
33+
34+ if (!(image.fp = fopen(path, "rb"))) die("fopen %s:", path);
35+ if (namelist) free(namelist);
36 break;
37 default:
38 goto usage;
39@@ -435,7 +455,7 @@ main(int argc, char *argv[])
40 return EXIT_SUCCESS;
41
42 usage:
43- fprintf(stderr, "usage: %s fill|fit|spread|stretch|tile filename\n"
44+ fprintf(stderr, "usage: %s fill|fit|spread|stretch|tile filename|dir\n"
45 " %s RRGGBBAA\n",
46 argv[0], argv[0]);
47 return EXIT_FAILURE;