Krolyxon
Commit 09d2d73

Branches

Refs →

nsxivutil.c

289 lines · 5.7 KB · C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021-2022 nsxiv contributors
3 *
4 * This file is a part of nsxiv.
5 *
6 * nsxiv is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
10 *
11 * nsxiv is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "nsxiv.h"
21
22#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27#include <errno.h>
28
29const char *progname;
30
31void* emalloc(size_t size)
32{
33 void *ptr;
34
35 ptr = malloc(size);
36 if (ptr == NULL)
37 error(EXIT_FAILURE, errno, NULL);
38 return ptr;
39}
40
41void* ecalloc(size_t nmemb, size_t size)
42{
43 void *ptr;
44
45 ptr = calloc(nmemb, size);
46 if (ptr == NULL)
47 error(EXIT_FAILURE, errno, NULL);
48 return ptr;
49}
50
51void* erealloc(void *ptr, size_t size)
52{
53 ptr = realloc(ptr, size);
54 if (ptr == NULL)
55 error(EXIT_FAILURE, errno, NULL);
56 return ptr;
57}
58
59char* estrdup(const char *s)
60{
61 char *d;
62 size_t n = strlen(s) + 1;
63
64 d = malloc(n);
65 if (d == NULL)
66 error(EXIT_FAILURE, errno, NULL);
67 memcpy(d, s, n);
68 return d;
69}
70
71void error(int eval, int err, const char* fmt, ...)
72{
73 va_list ap;
74
75 if (eval == 0 && options->quiet)
76 return;
77
78 fflush(stdout);
79 fprintf(stderr, "%s: ", progname);
80 va_start(ap, fmt);
81 if (fmt != NULL)
82 vfprintf(stderr, fmt, ap);
83 va_end(ap);
84 if (err != 0)
85 fprintf(stderr, "%s%s", fmt != NULL ? ": " : "", strerror(err));
86 fputc('\n', stderr);
87
88 if (eval != 0)
89 exit(eval);
90}
91
92int r_opendir(r_dir_t *rdir, const char *dirname, bool recursive)
93{
94 if (*dirname == '\0')
95 return -1;
96
97 if ((rdir->dir = opendir(dirname)) == NULL) {
98 rdir->name = NULL;
99 rdir->stack = NULL;
100 return -1;
101 }
102
103 rdir->stcap = 512;
104 rdir->stack = emalloc(rdir->stcap * sizeof(char*));
105 rdir->stlen = 0;
106
107 rdir->name = (char*) dirname;
108 rdir->d = 0;
109 rdir->recursive = recursive;
110
111 return 0;
112}
113
114int r_closedir(r_dir_t *rdir)
115{
116 int ret = 0;
117
118 if (rdir->stack != NULL) {
119 while (rdir->stlen > 0)
120 free(rdir->stack[--rdir->stlen]);
121 free(rdir->stack);
122 rdir->stack = NULL;
123 }
124
125 if (rdir->dir != NULL) {
126 if ((ret = closedir(rdir->dir)) == 0)
127 rdir->dir = NULL;
128 }
129
130 if (rdir->d != 0) {
131 free(rdir->name);
132 rdir->name = NULL;
133 }
134
135 return ret;
136}
137
138char* r_readdir(r_dir_t *rdir, bool skip_dotfiles)
139{
140 size_t len;
141 char *filename;
142 struct dirent *dentry;
143 struct stat fstats;
144
145 while (true) {
146 if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
147 if (dentry->d_name[0] == '.') {
148 if (skip_dotfiles)
149 continue;
150 if (dentry->d_name[1] == '\0')
151 continue;
152 if (dentry->d_name[1] == '.' && dentry->d_name[2] == '\0')
153 continue;
154 }
155
156 len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
157 filename = emalloc(len);
158 snprintf(filename, len, "%s%s%s", rdir->name,
159 rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
160 dentry->d_name);
161
162 if (stat(filename, &fstats) < 0)
163 continue;
164 if (S_ISDIR(fstats.st_mode)) {
165 /* put subdirectory on the stack */
166 if (rdir->stlen == rdir->stcap) {
167 rdir->stcap *= 2;
168 rdir->stack = erealloc(rdir->stack,
169 rdir->stcap * sizeof(char*));
170 }
171 rdir->stack[rdir->stlen++] = filename;
172 continue;
173 }
174 return filename;
175 }
176
177 if (rdir->recursive && rdir->stlen > 0) {
178 /* open next subdirectory */
179 closedir(rdir->dir);
180 if (rdir->d != 0)
181 free(rdir->name);
182 rdir->name = rdir->stack[--rdir->stlen];
183 rdir->d = 1;
184 if ((rdir->dir = opendir(rdir->name)) == NULL)
185 error(0, errno, "%s", rdir->name);
186 continue;
187 }
188 /* no more entries */
189 break;
190 }
191 return NULL;
192}
193
194int r_mkdir(char *path)
195{
196 char c, *s = path;
197 struct stat st;
198
199 while (*s != '\0') {
200 if (*s == '/') {
201 s++;
202 continue;
203 }
204 for (; *s != '\0' && *s != '/'; s++);
205 c = *s;
206 *s = '\0';
207 if (mkdir(path, 0755) == -1)
208 if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode))
209 return -1;
210 *s = c;
211 }
212 return 0;
213}
214
215void construct_argv(char **argv, unsigned int len, ...)
216{
217 unsigned int i;
218 va_list args;
219
220 va_start(args, len);
221 for (i = 0; i < len; ++i)
222 argv[i] = va_arg(args, char *);
223 va_end(args);
224 if (argv[len-1] != NULL)
225 error(EXIT_FAILURE, 0, "argv not NULL terminated");
226}
227
228spawn_t spawn(const char *cmd, char *const argv[], unsigned int flags)
229{
230 pid_t pid;
231 spawn_t status = { -1, -1, -1 };
232 int pfd_read[2] = { -1, -1 };
233 int pfd_write[2] = { -1, -1 };
234 const bool r = flags & X_READ;
235 const bool w = flags & X_WRITE;
236
237 if (cmd == NULL || argv == NULL || flags == 0)
238 return status;
239
240 if (r && pipe(pfd_read) < 0) {
241 error(0, errno, "pipe: %s", cmd);
242 return status;
243 }
244
245 if (w && pipe(pfd_write) < 0) {
246 if (r) {
247 close(pfd_read[0]);
248 close(pfd_read[1]);
249 }
250 error(0, errno, "pipe: %s", cmd);
251 return status;
252 }
253
254 if ((pid = fork()) == 0) {
255 bool err = (r && dup2(pfd_read[1], 1) < 0) || (w && dup2(pfd_write[0], 0) < 0);
256 if (r) {
257 close(pfd_read[0]);
258 close(pfd_read[1]);
259 }
260 if (w) {
261 close(pfd_write[0]);
262 close(pfd_write[1]);
263 }
264
265 if (err)
266 error(EXIT_FAILURE, errno, "dup2: %s", cmd);
267 execv(cmd, argv);
268 error(EXIT_FAILURE, errno, "exec: %s", cmd);
269 }
270
271 if (r)
272 close(pfd_read[1]);
273 if (w)
274 close(pfd_write[0]);
275
276 if (pid < 0) {
277 if (r)
278 close(pfd_read[0]);
279 if (w)
280 close(pfd_write[1]);
281 error(0, errno, "fork: %s", cmd);
282 return status;
283 }
284
285 status.pid = pid;
286 status.readfd = pfd_read[0];
287 status.writefd = pfd_write[1];
288 return status;
289}