Krolyxon
Commit 1f788a3

Branches

Refs →

nsxivutil.c

294 lines · 6.2 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 <assert.h>
23#include <errno.h>
24#include <spawn.h>
25#include <stdarg.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/stat.h>
30#include <unistd.h>
31
32extern char **environ;
33const char *progname = "nsxiv";
34
35void* emalloc(size_t size)
36{
37 void *ptr;
38
39 ptr = malloc(size);
40 if (ptr == NULL)
41 error(EXIT_FAILURE, errno, NULL);
42 return ptr;
43}
44
45void* ecalloc(size_t nmemb, size_t size)
46{
47 void *ptr;
48
49 ptr = calloc(nmemb, size);
50 if (ptr == NULL)
51 error(EXIT_FAILURE, errno, NULL);
52 return ptr;
53}
54
55void* erealloc(void *ptr, size_t size)
56{
57 ptr = realloc(ptr, size);
58 if (ptr == NULL)
59 error(EXIT_FAILURE, errno, NULL);
60 return ptr;
61}
62
63char* estrdup(const char *s)
64{
65 size_t n = strlen(s) + 1;
66 return memcpy(emalloc(n), s, n);
67}
68
69void error(int eval, int err, const char* fmt, ...)
70{
71 va_list ap;
72
73 if (eval == 0 && options->quiet)
74 return;
75
76 fflush(stdout);
77 fprintf(stderr, "%s: ", progname);
78 va_start(ap, fmt);
79 if (fmt != NULL)
80 vfprintf(stderr, fmt, ap);
81 va_end(ap);
82 if (err != 0)
83 fprintf(stderr, "%s%s", fmt != NULL ? ": " : "", strerror(err));
84 fputc('\n', stderr);
85
86 if (eval != 0)
87 exit(eval);
88}
89
90int r_opendir(r_dir_t *rdir, const char *dirname, bool recursive)
91{
92 if (*dirname == '\0')
93 return -1;
94
95 if ((rdir->dir = opendir(dirname)) == NULL) {
96 rdir->name = NULL;
97 rdir->stack = NULL;
98 return -1;
99 }
100
101 rdir->stcap = 512;
102 rdir->stack = emalloc(rdir->stcap * sizeof(*rdir->stack));
103 rdir->stlen = 0;
104
105 rdir->name = (char*) dirname;
106 rdir->d = 0;
107 rdir->recursive = recursive;
108
109 return 0;
110}
111
112int r_closedir(r_dir_t *rdir)
113{
114 int ret = 0;
115
116 if (rdir->stack != NULL) {
117 while (rdir->stlen > 0)
118 free(rdir->stack[--rdir->stlen]);
119 free(rdir->stack);
120 rdir->stack = NULL;
121 }
122
123 if (rdir->dir != NULL) {
124 if ((ret = closedir(rdir->dir)) == 0)
125 rdir->dir = NULL;
126 }
127
128 if (rdir->d != 0) {
129 free(rdir->name);
130 rdir->name = NULL;
131 }
132
133 return ret;
134}
135
136char* r_readdir(r_dir_t *rdir, bool skip_dotfiles)
137{
138 size_t len;
139 char *filename;
140 struct dirent *dentry;
141 struct stat fstats;
142
143 while (true) {
144 if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
145 if (dentry->d_name[0] == '.') {
146 if (skip_dotfiles)
147 continue;
148 if (dentry->d_name[1] == '\0')
149 continue;
150 if (dentry->d_name[1] == '.' && dentry->d_name[2] == '\0')
151 continue;
152 }
153
154 len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
155 filename = emalloc(len);
156 snprintf(filename, len, "%s%s%s", rdir->name,
157 rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
158 dentry->d_name);
159
160 if (stat(filename, &fstats) < 0) {
161 free(filename);
162 continue;
163 }
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(*rdir->stack));
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 assert(rdir->dir != NULL);
180 closedir(rdir->dir);
181 if (rdir->d != 0)
182 free(rdir->name);
183 rdir->name = rdir->stack[--rdir->stlen];
184 rdir->d = 1;
185 if ((rdir->dir = opendir(rdir->name)) == NULL)
186 error(0, errno, "%s", rdir->name);
187 continue;
188 }
189 /* no more entries */
190 break;
191 }
192 return NULL;
193}
194
195int r_mkdir(char *path)
196{
197 int rc = 0;
198 char c, *s = path;
199 struct stat st;
200
201 while (*s != '\0' && rc == 0) {
202 if (*s == '/') {
203 s++;
204 continue;
205 }
206 for (; *s != '\0' && *s != '/'; s++);
207 c = *s;
208 *s = '\0';
209 if (mkdir(path, 0755) == -1) {
210 if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
211 error(0, errno, "%s", path);
212 rc = -1;
213 }
214 }
215 *s = c;
216 }
217 return rc;
218}
219
220void construct_argv(char **argv, unsigned int len, ...)
221{
222 unsigned int i;
223 va_list args;
224
225 va_start(args, len);
226 for (i = 0; i < len; ++i)
227 argv[i] = va_arg(args, char *);
228 va_end(args);
229 if (argv[len-1] != NULL)
230 error(EXIT_FAILURE, 0, "argv not NULL terminated");
231}
232
233static int mkspawn_pipe(posix_spawn_file_actions_t *fa, const char *cmd, int *pfd, int dupidx)
234{
235 int err;
236 if (pipe(pfd) < 0) {
237 error(0, errno, "pipe: %s", cmd);
238 return -1;
239 }
240 err = posix_spawn_file_actions_adddup2(fa, pfd[dupidx], dupidx);
241 err = err ? err : posix_spawn_file_actions_addclose(fa, pfd[0]);
242 err = err ? err : posix_spawn_file_actions_addclose(fa, pfd[1]);
243 if (err) {
244 error(0, err, "posix_spawn_file_actions: %s", cmd);
245 close(pfd[0]);
246 close(pfd[1]);
247 }
248 return err ? -1 : 0;
249}
250
251pid_t spawn(int *readfd, int *writefd, char *const argv[])
252{
253 pid_t pid = -1;
254 const char *cmd;
255 int err, pfd_read[2], pfd_write[2];
256 posix_spawn_file_actions_t fa;
257
258 assert(argv != NULL && argv[0] != NULL);
259 cmd = argv[0];
260
261 if ((err = posix_spawn_file_actions_init(&fa)) != 0) {
262 error(0, err, "spawn: %s", cmd);
263 return pid;
264 }
265
266 if (readfd != NULL && mkspawn_pipe(&fa, cmd, pfd_read, 1) < 0)
267 goto err_destroy_fa;
268 if (writefd != NULL && mkspawn_pipe(&fa, cmd, pfd_write, 0) < 0)
269 goto err_close_readfd;
270
271 if ((err = posix_spawnp(&pid, cmd, &fa, NULL, argv, environ)) != 0) {
272 error(0, err, "spawn: %s", cmd);
273 } else {
274 if (readfd != NULL)
275 *readfd = pfd_read[0];
276 if (writefd != NULL)
277 *writefd = pfd_write[1];
278 }
279
280 if (writefd != NULL) {
281 close(pfd_write[0]);
282 if (pid < 0)
283 close(pfd_write[1]);
284 }
285err_close_readfd:
286 if (readfd != NULL) {
287 if (pid < 0)
288 close(pfd_read[0]);
289 close(pfd_read[1]);
290 }
291err_destroy_fa:
292 posix_spawn_file_actions_destroy(&fa);
293 return pid;
294}