Krolyxon
Commit 160dd62

Branches

Refs →

nsxivutil.c

206 lines · 4.1 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)
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
110 return 0;
111}
112
113int r_closedir(r_dir_t *rdir)
114{
115 int ret = 0;
116
117 if (rdir->stack != NULL) {
118 while (rdir->stlen > 0)
119 free(rdir->stack[--rdir->stlen]);
120 free(rdir->stack);
121 rdir->stack = NULL;
122 }
123
124 if (rdir->dir != NULL) {
125 if ((ret = closedir(rdir->dir)) == 0)
126 rdir->dir = NULL;
127 }
128
129 if (rdir->d != 0) {
130 free(rdir->name);
131 rdir->name = NULL;
132 }
133
134 return ret;
135}
136
137char* r_readdir(r_dir_t *rdir)
138{
139 size_t len;
140 char *filename;
141 struct dirent *dentry;
142 struct stat fstats;
143
144 while (true) {
145 if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
146 if (dentry->d_name[0] == '.')
147 continue;
148
149 len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
150 filename = (char*) emalloc(len);
151 snprintf(filename, len, "%s%s%s", rdir->name,
152 rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
153 dentry->d_name);
154
155 if (stat(filename, &fstats) < 0)
156 continue;
157 if (S_ISDIR(fstats.st_mode)) {
158 /* put subdirectory on the stack */
159 if (rdir->stlen == rdir->stcap) {
160 rdir->stcap *= 2;
161 rdir->stack = (char**) erealloc(rdir->stack,
162 rdir->stcap * sizeof(char*));
163 }
164 rdir->stack[rdir->stlen++] = filename;
165 continue;
166 }
167 return filename;
168 }
169
170 if (rdir->stlen > 0) {
171 /* open next subdirectory */
172 closedir(rdir->dir);
173 if (rdir->d != 0)
174 free(rdir->name);
175 rdir->name = rdir->stack[--rdir->stlen];
176 rdir->d = 1;
177 if ((rdir->dir = opendir(rdir->name)) == NULL)
178 error(0, errno, "%s", rdir->name);
179 continue;
180 }
181 /* no more entries */
182 break;
183 }
184 return NULL;
185}
186
187int r_mkdir(char *path)
188{
189 char c, *s = path;
190 struct stat st;
191
192 while (*s != '\0') {
193 if (*s == '/') {
194 s++;
195 continue;
196 }
197 for (; *s != '\0' && *s != '/'; s++);
198 c = *s;
199 *s = '\0';
200 if (mkdir(path, 0755) == -1)
201 if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode))
202 return -1;
203 *s = c;
204 }
205 return 0;
206}