| 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 | enum { |
| 30 | BUF_SIZE = 1024, |
| 31 | DNAME_CNT = 512, |
| 32 | FNAME_LEN = 1024 |
| 33 | }; |
| 34 | |
| 35 | void cleanup(void); |
| 36 | |
| 37 | void* s_malloc(size_t size) |
| 38 | { |
| 39 | void *ptr; |
| 40 | |
| 41 | ptr = malloc(size); |
| 42 | if (ptr == NULL) |
| 43 | die("could not allocate memory"); |
| 44 | return ptr; |
| 45 | } |
| 46 | |
| 47 | void* s_realloc(void *ptr, size_t size) |
| 48 | { |
| 49 | ptr = realloc(ptr, size); |
| 50 | if (ptr == NULL) |
| 51 | die("could not allocate memory"); |
| 52 | return ptr; |
| 53 | } |
| 54 | |
| 55 | char* s_strdup(const char *s) |
| 56 | { |
| 57 | char *d = NULL; |
| 58 | |
| 59 | if (s != NULL) { |
| 60 | d = malloc(strlen(s) + 1); |
| 61 | if (d == NULL) |
| 62 | die("could not allocate memory"); |
| 63 | strcpy(d, s); |
| 64 | } |
| 65 | return d; |
| 66 | } |
| 67 | |
| 68 | void warn(const char* fmt, ...) |
| 69 | { |
| 70 | va_list args; |
| 71 | |
| 72 | if (fmt == NULL || options->quiet) |
| 73 | return; |
| 74 | |
| 75 | va_start(args, fmt); |
| 76 | fprintf(stderr, "sxiv: warning: "); |
| 77 | vfprintf(stderr, fmt, args); |
| 78 | fprintf(stderr, "\n"); |
| 79 | va_end(args); |
| 80 | } |
| 81 | |
| 82 | void die(const char* fmt, ...) |
| 83 | { |
| 84 | va_list args; |
| 85 | |
| 86 | if (fmt == NULL) |
| 87 | return; |
| 88 | |
| 89 | va_start(args, fmt); |
| 90 | fprintf(stderr, "sxiv: error: "); |
| 91 | vfprintf(stderr, fmt, args); |
| 92 | fprintf(stderr, "\n"); |
| 93 | va_end(args); |
| 94 | |
| 95 | cleanup(); |
| 96 | exit(EXIT_FAILURE); |
| 97 | } |
| 98 | |
| 99 | void size_readable(float *size, const char **unit) |
| 100 | { |
| 101 | const char *units[] = { "", "K", "M", "G" }; |
| 102 | int i; |
| 103 | |
| 104 | for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++) |
| 105 | *size /= 1024.0; |
| 106 | *unit = units[MIN(i, ARRLEN(units) - 1)]; |
| 107 | } |
| 108 | |
| 109 | int r_opendir(r_dir_t *rdir, const char *dirname) |
| 110 | { |
| 111 | if (*dirname == '\0') |
| 112 | return -1; |
| 113 | |
| 114 | if ((rdir->dir = opendir(dirname)) == NULL) { |
| 115 | rdir->name = NULL; |
| 116 | rdir->stack = NULL; |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | rdir->stcap = DNAME_CNT; |
| 121 | rdir->stack = (char**) s_malloc(rdir->stcap * sizeof(char*)); |
| 122 | rdir->stlen = 0; |
| 123 | |
| 124 | rdir->name = (char*) dirname; |
| 125 | rdir->d = 0; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int r_closedir(r_dir_t *rdir) |
| 131 | { |
| 132 | int ret = 0; |
| 133 | |
| 134 | if (rdir->stack != NULL) { |
| 135 | while (rdir->stlen > 0) |
| 136 | free(rdir->stack[--rdir->stlen]); |
| 137 | free(rdir->stack); |
| 138 | rdir->stack = NULL; |
| 139 | } |
| 140 | |
| 141 | if (rdir->dir != NULL) { |
| 142 | if ((ret = closedir(rdir->dir)) == 0) |
| 143 | rdir->dir = NULL; |
| 144 | } |
| 145 | |
| 146 | if (rdir->d != 0) { |
| 147 | free(rdir->name); |
| 148 | rdir->name = NULL; |
| 149 | } |
| 150 | |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | char* r_readdir(r_dir_t *rdir) |
| 155 | { |
| 156 | size_t len; |
| 157 | char *filename; |
| 158 | struct dirent *dentry; |
| 159 | struct stat fstats; |
| 160 | |
| 161 | while (true) { |
| 162 | if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) { |
| 163 | if (dentry->d_name[0] == '.') |
| 164 | continue; |
| 165 | |
| 166 | len = strlen(rdir->name) + strlen(dentry->d_name) + 2; |
| 167 | filename = (char*) s_malloc(len); |
| 168 | snprintf(filename, len, "%s%s%s", rdir->name, |
| 169 | rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/", |
| 170 | dentry->d_name); |
| 171 | |
| 172 | if (stat(filename, &fstats) < 0) |
| 173 | continue; |
| 174 | if (S_ISDIR(fstats.st_mode)) { |
| 175 | /* put subdirectory on the stack */ |
| 176 | if (rdir->stlen == rdir->stcap) { |
| 177 | rdir->stcap *= 2; |
| 178 | rdir->stack = (char**) s_realloc(rdir->stack, |
| 179 | rdir->stcap * sizeof(char*)); |
| 180 | } |
| 181 | rdir->stack[rdir->stlen++] = filename; |
| 182 | continue; |
| 183 | } |
| 184 | return filename; |
| 185 | } |
| 186 | |
| 187 | if (rdir->stlen > 0) { |
| 188 | /* open next subdirectory */ |
| 189 | closedir(rdir->dir); |
| 190 | if (rdir->d != 0) |
| 191 | free(rdir->name); |
| 192 | rdir->name = rdir->stack[--rdir->stlen]; |
| 193 | rdir->d = 1; |
| 194 | if ((rdir->dir = opendir(rdir->name)) == NULL) |
| 195 | warn("could not open directory: %s", rdir->name); |
| 196 | continue; |
| 197 | } |
| 198 | /* no more entries */ |
| 199 | break; |
| 200 | } |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | int r_mkdir(const char *path) |
| 205 | { |
| 206 | char *dir, *d; |
| 207 | struct stat stats; |
| 208 | int err = 0; |
| 209 | |
| 210 | if (*path == '\0') |
| 211 | return -1; |
| 212 | |
| 213 | if (stat(path, &stats) == 0) |
| 214 | return S_ISDIR(stats.st_mode) ? 0 : -1; |
| 215 | |
| 216 | d = dir = (char*) s_malloc(strlen(path) + 1); |
| 217 | strcpy(dir, path); |
| 218 | |
| 219 | while (d != NULL && err == 0) { |
| 220 | d = strchr(d + 1, '/'); |
| 221 | if (d != NULL) |
| 222 | *d = '\0'; |
| 223 | if (access(dir, F_OK) < 0 && errno == ENOENT) { |
| 224 | if (mkdir(dir, 0755) < 0) { |
| 225 | warn("could not create directory: %s", dir); |
| 226 | err = -1; |
| 227 | } |
| 228 | } else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) { |
| 229 | err = -1; |
| 230 | } |
| 231 | if (d != NULL) |
| 232 | *d = '/'; |
| 233 | } |
| 234 | free(dir); |
| 235 | |
| 236 | return err; |
| 237 | } |