| 1 | /* sxiv: util.c |
| 2 | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 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 | #define FNAME_LEN 1024 |
| 30 | |
| 31 | void cleanup(); |
| 32 | |
| 33 | void* s_malloc(size_t size) { |
| 34 | void *ptr; |
| 35 | |
| 36 | if (!(ptr = malloc(size))) |
| 37 | die("could not allocate memory"); |
| 38 | return ptr; |
| 39 | } |
| 40 | |
| 41 | void* s_realloc(void *ptr, size_t size) { |
| 42 | if (!(ptr = realloc(ptr, size))) |
| 43 | die("could not allocate memory"); |
| 44 | return ptr; |
| 45 | } |
| 46 | |
| 47 | void warn(const char* fmt, ...) { |
| 48 | va_list args; |
| 49 | |
| 50 | if (!fmt || options->quiet) |
| 51 | return; |
| 52 | |
| 53 | va_start(args, fmt); |
| 54 | fprintf(stderr, "sxiv: warning: "); |
| 55 | vfprintf(stderr, fmt, args); |
| 56 | fprintf(stderr, "\n"); |
| 57 | va_end(args); |
| 58 | } |
| 59 | |
| 60 | void die(const char* fmt, ...) { |
| 61 | va_list args; |
| 62 | |
| 63 | if (!fmt) |
| 64 | return; |
| 65 | |
| 66 | va_start(args, fmt); |
| 67 | fprintf(stderr, "sxiv: error: "); |
| 68 | vfprintf(stderr, fmt, args); |
| 69 | fprintf(stderr, "\n"); |
| 70 | va_end(args); |
| 71 | |
| 72 | cleanup(); |
| 73 | exit(1); |
| 74 | } |
| 75 | |
| 76 | void size_readable(float *size, const char **unit) { |
| 77 | const char *units[] = { "", "K", "M", "G" }; |
| 78 | int i; |
| 79 | |
| 80 | for (i = 0; i < LEN(units) && *size > 1024; ++i) |
| 81 | *size /= 1024; |
| 82 | *unit = units[MIN(i, LEN(units) - 1)]; |
| 83 | } |
| 84 | |
| 85 | char* absolute_path(const char *filename) { |
| 86 | size_t len; |
| 87 | char *path = NULL; |
| 88 | const char *basename; |
| 89 | char *dirname = NULL; |
| 90 | char *cwd = NULL; |
| 91 | char *twd = NULL; |
| 92 | char *dir; |
| 93 | char *s; |
| 94 | |
| 95 | if (!filename || *filename == '\0' || *filename == '/') |
| 96 | return NULL; |
| 97 | |
| 98 | len = FNAME_LEN; |
| 99 | cwd = (char*) s_malloc(len); |
| 100 | while (!(s = getcwd(cwd, len)) && errno == ERANGE) { |
| 101 | len *= 2; |
| 102 | cwd = (char*) s_realloc(cwd, len); |
| 103 | } |
| 104 | if (!s) |
| 105 | goto error; |
| 106 | |
| 107 | s = strrchr(filename, '/'); |
| 108 | if (s) { |
| 109 | len = s - filename; |
| 110 | dirname = (char*) s_malloc(len + 1); |
| 111 | strncpy(dirname, filename, len); |
| 112 | dirname[len] = '\0'; |
| 113 | basename = s + 1; |
| 114 | |
| 115 | if (chdir(cwd)) |
| 116 | /* we're not able to come back afterwards */ |
| 117 | goto error; |
| 118 | if (chdir(dirname)) |
| 119 | goto error; |
| 120 | |
| 121 | len = FNAME_LEN; |
| 122 | twd = (char*) s_malloc(len); |
| 123 | while (!(s = getcwd(twd, len)) && errno == ERANGE) { |
| 124 | len *= 2; |
| 125 | twd = (char*) s_realloc(twd, len); |
| 126 | } |
| 127 | if (chdir(cwd)) |
| 128 | die("could not revert to prior working directory"); |
| 129 | if (!s) |
| 130 | goto error; |
| 131 | dir = twd; |
| 132 | } else { |
| 133 | /* only a single filename given */ |
| 134 | basename = filename; |
| 135 | dir = cwd; |
| 136 | } |
| 137 | |
| 138 | len = strlen(dir) + strlen(basename) + 2; |
| 139 | path = (char*) s_malloc(len); |
| 140 | snprintf(path, len, "%s/%s", dir, basename); |
| 141 | |
| 142 | goto end; |
| 143 | |
| 144 | error: |
| 145 | if (path) { |
| 146 | free(path); |
| 147 | path = NULL; |
| 148 | } |
| 149 | |
| 150 | end: |
| 151 | if (dirname) |
| 152 | free(dirname); |
| 153 | if (cwd) |
| 154 | free(cwd); |
| 155 | if (twd) |
| 156 | free(twd); |
| 157 | |
| 158 | return path; |
| 159 | } |
| 160 | |
| 161 | int create_dir_rec(const char *path) { |
| 162 | char *dir, *d; |
| 163 | struct stat stats; |
| 164 | int err = 0; |
| 165 | |
| 166 | if (!path || !*path) |
| 167 | return -1; |
| 168 | |
| 169 | if (!stat(path, &stats)) { |
| 170 | if (S_ISDIR(stats.st_mode)) { |
| 171 | return 0; |
| 172 | } else { |
| 173 | warn("not a directory: %s", path); |
| 174 | return -1; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | d = dir = (char*) s_malloc(strlen(path) + 1); |
| 179 | strcpy(dir, path); |
| 180 | |
| 181 | while (d != NULL && !err) { |
| 182 | d = strchr(d + 1, '/'); |
| 183 | if (d != NULL) |
| 184 | *d = '\0'; |
| 185 | if (access(dir, F_OK) && errno == ENOENT) { |
| 186 | if (mkdir(dir, 0755)) { |
| 187 | warn("could not create directory: %s", dir); |
| 188 | err = -1; |
| 189 | } |
| 190 | } else if (stat(dir, &stats) || !S_ISDIR(stats.st_mode)) { |
| 191 | warn("not a directory: %s", dir); |
| 192 | err = -1; |
| 193 | } |
| 194 | if (d != NULL) |
| 195 | *d = '/'; |
| 196 | } |
| 197 | free(dir); |
| 198 | |
| 199 | return err; |
| 200 | } |
| 201 | |
| 202 | char* readline(FILE *stream) { |
| 203 | size_t len; |
| 204 | char *buf, *s, *end; |
| 205 | |
| 206 | if (!stream || feof(stream) || ferror(stream)) |
| 207 | return NULL; |
| 208 | |
| 209 | len = FNAME_LEN; |
| 210 | s = buf = (char*) s_malloc(len * sizeof(char)); |
| 211 | |
| 212 | do { |
| 213 | *s = '\0'; |
| 214 | fgets(s, len - (s - buf), stream); |
| 215 | if ((end = strchr(s, '\n'))) { |
| 216 | *end = '\0'; |
| 217 | } else if (strlen(s) + 1 == len - (s - buf)) { |
| 218 | buf = (char*) s_realloc(buf, 2 * len * sizeof(char)); |
| 219 | s = buf + len - 1; |
| 220 | len *= 2; |
| 221 | } else { |
| 222 | s += strlen(s); |
| 223 | } |
| 224 | } while (!end && !feof(stream) && !ferror(stream)); |
| 225 | |
| 226 | if (ferror(stream)) { |
| 227 | s = NULL; |
| 228 | } else { |
| 229 | s = (char*) s_malloc((strlen(buf) + 1) * sizeof(char)); |
| 230 | strcpy(s, buf); |
| 231 | } |
| 232 | |
| 233 | free(buf); |
| 234 | |
| 235 | return s; |
| 236 | } |