Krolyxon
Commit 66c3c55

Branches

Refs →

nsxivutil.c

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