| 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 | |
| 32 | extern char **environ; |
| 33 | const char *progname = "nsxiv"; |
| 34 | |
| 35 | void* 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 | |
| 45 | void* 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 | |
| 55 | void* 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 | |
| 63 | char* estrdup(const char *s) |
| 64 | { |
| 65 | size_t n = strlen(s) + 1; |
| 66 | return memcpy(emalloc(n), s, n); |
| 67 | } |
| 68 | |
| 69 | void 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 | |
| 90 | int 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 | |
| 112 | int 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 | |
| 136 | char* 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 | |
| 195 | int 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 | |
| 220 | void 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 | assert(argv[len-1] == NULL && "argv should be NULL terminated"); |
| 230 | } |
| 231 | |
| 232 | static int mkspawn_pipe(posix_spawn_file_actions_t *fa, const char *cmd, int *pfd, int dupidx) |
| 233 | { |
| 234 | int err; |
| 235 | if (pipe(pfd) < 0) { |
| 236 | error(0, errno, "pipe: %s", cmd); |
| 237 | return -1; |
| 238 | } |
| 239 | err = posix_spawn_file_actions_adddup2(fa, pfd[dupidx], dupidx); |
| 240 | err = err ? err : posix_spawn_file_actions_addclose(fa, pfd[0]); |
| 241 | err = err ? err : posix_spawn_file_actions_addclose(fa, pfd[1]); |
| 242 | if (err) { |
| 243 | error(0, err, "posix_spawn_file_actions: %s", cmd); |
| 244 | close(pfd[0]); |
| 245 | close(pfd[1]); |
| 246 | } |
| 247 | return err ? -1 : 0; |
| 248 | } |
| 249 | |
| 250 | pid_t spawn(int *readfd, int *writefd, char *const argv[]) |
| 251 | { |
| 252 | pid_t pid = -1; |
| 253 | const char *cmd; |
| 254 | int err, pfd_read[2], pfd_write[2]; |
| 255 | posix_spawn_file_actions_t fa; |
| 256 | |
| 257 | assert(argv != NULL && argv[0] != NULL); |
| 258 | cmd = argv[0]; |
| 259 | |
| 260 | if ((err = posix_spawn_file_actions_init(&fa)) != 0) { |
| 261 | error(0, err, "spawn: %s", cmd); |
| 262 | return pid; |
| 263 | } |
| 264 | |
| 265 | if (readfd != NULL && mkspawn_pipe(&fa, cmd, pfd_read, 1) < 0) |
| 266 | goto err_destroy_fa; |
| 267 | if (writefd != NULL && mkspawn_pipe(&fa, cmd, pfd_write, 0) < 0) |
| 268 | goto err_close_readfd; |
| 269 | |
| 270 | if ((err = posix_spawnp(&pid, cmd, &fa, NULL, argv, environ)) != 0) { |
| 271 | error(0, err, "spawn: %s", cmd); |
| 272 | } else { |
| 273 | if (readfd != NULL) |
| 274 | *readfd = pfd_read[0]; |
| 275 | if (writefd != NULL) |
| 276 | *writefd = pfd_write[1]; |
| 277 | } |
| 278 | |
| 279 | if (writefd != NULL) { |
| 280 | close(pfd_write[0]); |
| 281 | if (pid < 0) |
| 282 | close(pfd_write[1]); |
| 283 | } |
| 284 | err_close_readfd: |
| 285 | if (readfd != NULL) { |
| 286 | if (pid < 0) |
| 287 | close(pfd_read[0]); |
| 288 | close(pfd_read[1]); |
| 289 | } |
| 290 | err_destroy_fa: |
| 291 | posix_spawn_file_actions_destroy(&fa); |
| 292 | return pid; |
| 293 | } |