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