Krolyxon
Commit 7efcf55

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 "sxiv.h"
20
21#include <stdlib.h>
22#include <string.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <unistd.h>
26#include <errno.h>
27
28const char *progname;
29
30void* emalloc(size_t size)
31{
32 void *ptr;
33
34 ptr = malloc(size);
35 if (ptr == NULL)
36 error(EXIT_FAILURE, errno, NULL);
37 return ptr;
38}
39
40void* erealloc(void *ptr, size_t size)
41{
42 ptr = realloc(ptr, size);
43 if (ptr == NULL)
44 error(EXIT_FAILURE, errno, NULL);
45 return ptr;
46}
47
48char* estrdup(const char *s)
49{
50 char *d;
51 size_t n = strlen(s) + 1;
52
53 d = malloc(n);
54 if (d == NULL)
55 error(EXIT_FAILURE, errno, NULL);
56 memcpy(d, s, n);
57 return d;
58}
59
60void error(int eval, int err, const char* fmt, ...)
61{
62 va_list ap;
63
64 if (eval == 0 && options->quiet)
65 return;
66
67 fflush(stdout);
68 fprintf(stderr, "%s: ", progname);
69 va_start(ap, fmt);
70 if (fmt != NULL)
71 vfprintf(stderr, fmt, ap);
72 va_end(ap);
73 if (err != 0)
74 fprintf(stderr, "%s%s", fmt != NULL ? ": " : "", strerror(err));
75 fputc('\n', stderr);
76
77 if (eval != 0)
78 exit(eval);
79}
80
81void size_readable(float *size, const char **unit)
82{
83 const char *units[] = { "", "K", "M", "G" };
84 int i;
85
86 for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++)
87 *size /= 1024.0;
88 *unit = units[MIN(i, ARRLEN(units) - 1)];
89}
90
91int r_opendir(r_dir_t *rdir, const char *dirname, bool recursive)
92{
93 if (*dirname == '\0')
94 return -1;
95
96 if ((rdir->dir = opendir(dirname)) == NULL) {
97 rdir->name = NULL;
98 rdir->stack = NULL;
99 return -1;
100 }
101
102 rdir->stcap = 512;
103 rdir->stack = (char**) emalloc(rdir->stcap * sizeof(char*));
104 rdir->stlen = 0;
105
106 rdir->name = (char*) dirname;
107 rdir->d = 0;
108 rdir->recursive = recursive;
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->recursive && 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}
207