Krolyxon
Commit 8bce80f

Branches

Refs →

nsxivutil.c

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