Krolyxon
Commit e574a6d

Branches

Refs →

nsxivutil.c

335 lines · 6.4 KB · C

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
29enum {
30 BUF_SIZE = 1024,
31 DNAME_CNT = 512,
32 FNAME_LEN = 1024
33};
34
35void cleanup(void);
36
37void* 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
47void* 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
55char* 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
68void 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
82void 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
99ssize_t get_line(char **buf, size_t *n, FILE *stream)
100{
101 size_t len;
102 char *s;
103
104 if (*buf == NULL || *n == 0) {
105 *n = BUF_SIZE;
106 *buf = (char*) s_malloc(*n);
107 }
108 s = *buf;
109
110 while (true) {
111 if (fgets(s, *n - (s - *buf), stream) == NULL)
112 return -1;
113 len = strlen(s);
114 if (feof(stream))
115 break;
116 if (len > 0 && s[len-1] == '\n')
117 break;
118 if (len + 1 == *n - (s - *buf)) {
119 *buf = (char*) s_realloc(*buf, 2 * *n);
120 s = *buf + *n - 1;
121 *n *= 2;
122 } else {
123 s += len;
124 }
125 }
126 return s - *buf + len;
127}
128
129void size_readable(float *size, const char **unit)
130{
131 const char *units[] = { "", "K", "M", "G" };
132 int i;
133
134 for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++)
135 *size /= 1024.0;
136 *unit = units[MIN(i, ARRLEN(units) - 1)];
137}
138
139char* absolute_path(const char *filename)
140{
141 size_t len;
142 const char *basename;
143 char *dir, *dirname = NULL, *path = NULL, *s;
144 char *cwd = NULL, *twd = NULL;
145
146 if (*filename == '\0' || *filename == '/')
147 return NULL;
148
149 len = FNAME_LEN;
150 cwd = (char*) s_malloc(len);
151 while ((s = getcwd(cwd, len)) == NULL && errno == ERANGE) {
152 len *= 2;
153 cwd = (char*) s_realloc(cwd, len);
154 }
155 if (s == NULL)
156 goto error;
157
158 s = strrchr(filename, '/');
159 if (s != NULL) {
160 len = s - filename;
161 dirname = (char*) s_malloc(len + 1);
162 strncpy(dirname, filename, len);
163 dirname[len] = '\0';
164 basename = s + 1;
165
166 if (chdir(cwd) < 0)
167 /* we're not able to come back afterwards */
168 goto error;
169 if (chdir(dirname) < 0)
170 goto error;
171
172 len = FNAME_LEN;
173 twd = (char*) s_malloc(len);
174 while ((s = getcwd(twd, len)) == NULL && errno == ERANGE) {
175 len *= 2;
176 twd = (char*) s_realloc(twd, len);
177 }
178 if (chdir(cwd) < 0)
179 die("could not revert to prior working directory");
180 if (s == NULL)
181 goto error;
182 dir = twd;
183 } else {
184 /* only a single filename given */
185 basename = filename;
186 dir = cwd;
187 }
188
189 len = strlen(dir) + strlen(basename) + 2;
190 path = (char*) s_malloc(len);
191 snprintf(path, len, "%s/%s", dir, basename);
192
193 goto end;
194
195error:
196 free(path);
197 path = NULL;
198
199end:
200 free(dirname);
201 free(cwd);
202 free(twd);
203
204 return path;
205}
206
207int r_opendir(r_dir_t *rdir, const char *dirname)
208{
209 if (*dirname == '\0')
210 return -1;
211
212 if ((rdir->dir = opendir(dirname)) == NULL) {
213 rdir->name = NULL;
214 rdir->stack = NULL;
215 return -1;
216 }
217
218 rdir->stcap = DNAME_CNT;
219 rdir->stack = (char**) s_malloc(rdir->stcap * sizeof(char*));
220 rdir->stlen = 0;
221
222 rdir->name = (char*) dirname;
223 rdir->d = 0;
224
225 return 0;
226}
227
228int r_closedir(r_dir_t *rdir)
229{
230 int ret = 0;
231
232 if (rdir->stack != NULL) {
233 while (rdir->stlen > 0)
234 free(rdir->stack[--rdir->stlen]);
235 free(rdir->stack);
236 rdir->stack = NULL;
237 }
238
239 if (rdir->dir != NULL) {
240 if ((ret = closedir(rdir->dir)) == 0)
241 rdir->dir = NULL;
242 }
243
244 if (rdir->d != 0) {
245 free(rdir->name);
246 rdir->name = NULL;
247 }
248
249 return ret;
250}
251
252char* r_readdir(r_dir_t *rdir)
253{
254 size_t len;
255 char *filename;
256 struct dirent *dentry;
257 struct stat fstats;
258
259 while (true) {
260 if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
261 if (dentry->d_name[0] == '.')
262 continue;
263
264 len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
265 filename = (char*) s_malloc(len);
266 snprintf(filename, len, "%s%s%s", rdir->name,
267 rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
268 dentry->d_name);
269
270 if (stat(filename, &fstats) < 0)
271 continue;
272 if (S_ISDIR(fstats.st_mode)) {
273 /* put subdirectory on the stack */
274 if (rdir->stlen == rdir->stcap) {
275 rdir->stcap *= 2;
276 rdir->stack = (char**) s_realloc(rdir->stack,
277 rdir->stcap * sizeof(char*));
278 }
279 rdir->stack[rdir->stlen++] = filename;
280 continue;
281 }
282 return filename;
283 }
284
285 if (rdir->stlen > 0) {
286 /* open next subdirectory */
287 closedir(rdir->dir);
288 if (rdir->d != 0)
289 free(rdir->name);
290 rdir->name = rdir->stack[--rdir->stlen];
291 rdir->d = 1;
292 if ((rdir->dir = opendir(rdir->name)) == NULL)
293 warn("could not open directory: %s", rdir->name);
294 continue;
295 }
296 /* no more entries */
297 break;
298 }
299 return NULL;
300}
301
302int r_mkdir(const char *path)
303{
304 char *dir, *d;
305 struct stat stats;
306 int err = 0;
307
308 if (*path == '\0')
309 return -1;
310
311 if (stat(path, &stats) == 0)
312 return S_ISDIR(stats.st_mode) ? 0 : -1;
313
314 d = dir = (char*) s_malloc(strlen(path) + 1);
315 strcpy(dir, path);
316
317 while (d != NULL && err == 0) {
318 d = strchr(d + 1, '/');
319 if (d != NULL)
320 *d = '\0';
321 if (access(dir, F_OK) < 0 && errno == ENOENT) {
322 if (mkdir(dir, 0755) < 0) {
323 warn("could not create directory: %s", dir);
324 err = -1;
325 }
326 } else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) {
327 err = -1;
328 }
329 if (d != NULL)
330 *d = '/';
331 }
332 free(dir);
333
334 return err;
335}