Krolyxon
Commit 5c3a796

Branches

Refs →

nsxivutil.c

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