|
10 | 10 | #include <unistd.h>
|
11 | 11 | #include "kfc.h"
|
12 | 12 |
|
13 |
| -struct dirent *de; |
| 13 | +struct dirent **de; |
14 | 14 |
|
15 | 15 | static int i, cval, randf;
|
16 | 16 | static int rflag, lflag, pflag = 0; /* argument flags */
|
17 | 17 | static size_t len = BUFF;
|
18 | 18 | static char *line, *sval, *mode, *conf, *evar, *eval, *seq = NULL;
|
19 | 19 | static char sel[BUFF];
|
20 | 20 | static FILE *fp;
|
21 |
| -static DIR *dr; |
22 | 21 |
|
23 | 22 | static void
|
24 | 23 | find_palettes(void)
|
@@ -68,56 +67,38 @@ select_palette(void)
|
68 | 67 | static void
|
69 | 68 | list_palette(void)
|
70 | 69 | {
|
71 |
| - dr = opendir(sel); |
| 70 | + int n = scandir(sel, &de, NULL, alphasort); |
72 | 71 |
|
73 |
| - if ( dr == NULL ) |
| 72 | + while (n--) |
74 | 73 | {
|
75 |
| - fprintf(stderr, "Could not open directory\n"); |
76 |
| - exit(1); |
77 |
| - } |
78 |
| - |
79 |
| - while( (de = readdir(dr)) != NULL ) |
80 |
| - { |
81 |
| - if ( de->d_type == 8 ) |
| 74 | + if (de[n]->d_type == 8) |
82 | 75 | {
|
83 |
| - printf("%s/%s\n", mode, de->d_name); |
| 76 | + printf("%s/%s\n", mode, de[n]->d_name); |
84 | 77 | }
|
85 | 78 | }
|
86 | 79 |
|
87 |
| - closedir(dr); |
| 80 | + free(de); |
88 | 81 | }
|
89 | 82 |
|
90 | 83 | static void
|
91 | 84 | random_palette(void)
|
92 | 85 | {
|
93 |
| - dr = opendir(sel); |
| 86 | + int n = scandir(sel, &de, NULL, alphasort); |
94 | 87 | randf = 0;
|
95 | 88 |
|
96 |
| - if ( dr == NULL ) |
97 |
| - { |
98 |
| - fprintf(stderr, "Could not open directory\n"); |
99 |
| - exit(1); |
100 |
| - } |
101 |
| - |
102 |
| - while( (de = readdir(dr)) != NULL ) |
103 |
| - { |
104 |
| - randf++; |
105 |
| - } |
106 |
| - |
107 |
| - closedir(dr); |
108 | 89 | srand(time(0));
|
109 |
| - i = (rand() % (randf - 0 + 1)) + 0; |
110 |
| - randf = 0; |
111 |
| - dr = opendir(sel); |
112 |
| - while( (de = readdir(dr)) != NULL ) |
| 90 | + i = (rand() % (n - 0 + 1)) + 0; |
| 91 | + |
| 92 | + while(n--) |
113 | 93 | {
|
114 | 94 | if (i == randf)
|
115 | 95 | {
|
116 | 96 | strcat(sel, "/");
|
117 |
| - strcat(sel, de->d_name); |
| 97 | + strcat(sel, de[n]->d_name); |
118 | 98 | }
|
119 | 99 | randf++;
|
120 | 100 | }
|
| 101 | + free(de); |
121 | 102 | }
|
122 | 103 |
|
123 | 104 | static void
|
@@ -157,14 +138,14 @@ usage: kfc [-L] [-r|-s palette] [-l|-p|-v]\n \
|
157 | 138 | int
|
158 | 139 | main(int argc, char **argv)
|
159 | 140 | {
|
| 141 | + extern char *optarg; |
| 142 | + extern int optind, optopt; |
| 143 | + |
160 | 144 | if (argc == 1)
|
161 | 145 | {
|
162 | 146 | fprintf(stderr, "No argument(s) provided\n");
|
163 | 147 | exit(1);
|
164 | 148 | }
|
165 |
| - |
166 |
| - extern char *optarg; |
167 |
| - extern int optind, optopt; |
168 | 149 |
|
169 | 150 | mode = "dark";
|
170 | 151 |
|
|
0 commit comments