Krolyxon
Commit 851e428

Branches

Refs →

nsxivutil.c

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