| 1 | /* Copyright 2011 Bert Muennich |
| 2 | * |
| 3 | * This file is part of sxiv. |
| 4 | * |
| 5 | * sxiv is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published |
| 7 | * by the Free Software Foundation; either version 2 of the License, |
| 8 | * or (at your option) any later version. |
| 9 | * |
| 10 | * sxiv is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with sxiv. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
| 24 | #include <errno.h> |
| 25 | |
| 26 | #include "options.h" |
| 27 | #include "util.h" |
| 28 | |
| 29 | const char *progname; |
| 30 | |
| 31 | void* 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 | |
| 41 | void* erealloc(void *ptr, size_t size) |
| 42 | { |
| 43 | ptr = realloc(ptr, size); |
| 44 | if (ptr == NULL) |
| 45 | error(EXIT_FAILURE, errno, NULL); |
| 46 | return ptr; |
| 47 | } |
| 48 | |
| 49 | char* estrdup(const char *s) |
| 50 | { |
| 51 | char *d; |
| 52 | size_t n = strlen(s) + 1; |
| 53 | |
| 54 | d = malloc(n); |
| 55 | if (d == NULL) |
| 56 | error(EXIT_FAILURE, errno, NULL); |
| 57 | memcpy(d, s, n); |
| 58 | return d; |
| 59 | } |
| 60 | |
| 61 | void error(int eval, int err, const char* fmt, ...) |
| 62 | { |
| 63 | va_list ap; |
| 64 | |
| 65 | fflush(stdout); |
| 66 | fprintf(stderr, "%s: ", progname); |
| 67 | va_start(ap, fmt); |
| 68 | if (fmt != NULL) |
| 69 | vfprintf(stderr, fmt, ap); |
| 70 | va_end(ap); |
| 71 | if (err != 0) |
| 72 | fprintf(stderr, "%s%s", fmt != NULL ? ": " : "", strerror(err)); |
| 73 | fputc('\n', stderr); |
| 74 | |
| 75 | if (eval != 0) |
| 76 | exit(eval); |
| 77 | } |
| 78 | |
| 79 | void size_readable(float *size, const char **unit) |
| 80 | { |
| 81 | const char *units[] = { "", "K", "M", "G" }; |
| 82 | int i; |
| 83 | |
| 84 | for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++) |
| 85 | *size /= 1024.0; |
| 86 | *unit = units[MIN(i, ARRLEN(units) - 1)]; |
| 87 | } |
| 88 | |
| 89 | int r_opendir(r_dir_t *rdir, const char *dirname) |
| 90 | { |
| 91 | if (*dirname == '\0') |
| 92 | return -1; |
| 93 | |
| 94 | if ((rdir->dir = opendir(dirname)) == NULL) { |
| 95 | rdir->name = NULL; |
| 96 | rdir->stack = NULL; |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | rdir->stcap = 512; |
| 101 | rdir->stack = (char**) emalloc(rdir->stcap * sizeof(char*)); |
| 102 | rdir->stlen = 0; |
| 103 | |
| 104 | rdir->name = (char*) dirname; |
| 105 | rdir->d = 0; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int r_closedir(r_dir_t *rdir) |
| 111 | { |
| 112 | int ret = 0; |
| 113 | |
| 114 | if (rdir->stack != NULL) { |
| 115 | while (rdir->stlen > 0) |
| 116 | free(rdir->stack[--rdir->stlen]); |
| 117 | free(rdir->stack); |
| 118 | rdir->stack = NULL; |
| 119 | } |
| 120 | |
| 121 | if (rdir->dir != NULL) { |
| 122 | if ((ret = closedir(rdir->dir)) == 0) |
| 123 | rdir->dir = NULL; |
| 124 | } |
| 125 | |
| 126 | if (rdir->d != 0) { |
| 127 | free(rdir->name); |
| 128 | rdir->name = NULL; |
| 129 | } |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | char* r_readdir(r_dir_t *rdir) |
| 135 | { |
| 136 | size_t len; |
| 137 | char *filename; |
| 138 | struct dirent *dentry; |
| 139 | struct stat fstats; |
| 140 | |
| 141 | while (true) { |
| 142 | if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) { |
| 143 | if (dentry->d_name[0] == '.') |
| 144 | continue; |
| 145 | |
| 146 | len = strlen(rdir->name) + strlen(dentry->d_name) + 2; |
| 147 | filename = (char*) emalloc(len); |
| 148 | snprintf(filename, len, "%s%s%s", rdir->name, |
| 149 | rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/", |
| 150 | dentry->d_name); |
| 151 | |
| 152 | if (stat(filename, &fstats) < 0) |
| 153 | continue; |
| 154 | if (S_ISDIR(fstats.st_mode)) { |
| 155 | /* put subdirectory on the stack */ |
| 156 | if (rdir->stlen == rdir->stcap) { |
| 157 | rdir->stcap *= 2; |
| 158 | rdir->stack = (char**) erealloc(rdir->stack, |
| 159 | rdir->stcap * sizeof(char*)); |
| 160 | } |
| 161 | rdir->stack[rdir->stlen++] = filename; |
| 162 | continue; |
| 163 | } |
| 164 | return filename; |
| 165 | } |
| 166 | |
| 167 | if (rdir->stlen > 0) { |
| 168 | /* open next subdirectory */ |
| 169 | closedir(rdir->dir); |
| 170 | if (rdir->d != 0) |
| 171 | free(rdir->name); |
| 172 | rdir->name = rdir->stack[--rdir->stlen]; |
| 173 | rdir->d = 1; |
| 174 | if ((rdir->dir = opendir(rdir->name)) == NULL) |
| 175 | error(0, errno, "%s", rdir->name); |
| 176 | continue; |
| 177 | } |
| 178 | /* no more entries */ |
| 179 | break; |
| 180 | } |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | int r_mkdir(char *path) |
| 185 | { |
| 186 | char c, *s = path; |
| 187 | struct stat st; |
| 188 | |
| 189 | while (*s != '\0') { |
| 190 | if (*s == '/') { |
| 191 | s++; |
| 192 | continue; |
| 193 | } |
| 194 | for (; *s != '\0' && *s != '/'; s++); |
| 195 | c = *s; |
| 196 | *s = '\0'; |
| 197 | if (mkdir(path, 0755) == -1) |
| 198 | if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) |
| 199 | return -1; |
| 200 | *s = c; |
| 201 | } |
| 202 | return 0; |
| 203 | } |