Krolyxon
Commit a9c6cbc

Branches

Refs →

nsxivutil.c

203 lines · 4.1 KB · C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021 nsxiv contributors
3 *
4 * This file is a part of nsxiv.
5 *
6 * nsxiv is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
10 *
11 * nsxiv is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "nsxiv.h"
21
22#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27#include <errno.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
82int r_opendir(r_dir_t *rdir, const char *dirname, bool recursive)
83{
84 if (*dirname == '\0')
85 return -1;
86
87 if ((rdir->dir = opendir(dirname)) == NULL) {
88 rdir->name = NULL;
89 rdir->stack = NULL;
90 return -1;
91 }
92
93 rdir->stcap = 512;
94 rdir->stack = emalloc(rdir->stcap * sizeof(char*));
95 rdir->stlen = 0;
96
97 rdir->name = (char*) dirname;
98 rdir->d = 0;
99 rdir->recursive = recursive;
100
101 return 0;
102}
103
104int r_closedir(r_dir_t *rdir)
105{
106 int ret = 0;
107
108 if (rdir->stack != NULL) {
109 while (rdir->stlen > 0)
110 free(rdir->stack[--rdir->stlen]);
111 free(rdir->stack);
112 rdir->stack = NULL;
113 }
114
115 if (rdir->dir != NULL) {
116 if ((ret = closedir(rdir->dir)) == 0)
117 rdir->dir = NULL;
118 }
119
120 if (rdir->d != 0) {
121 free(rdir->name);
122 rdir->name = NULL;
123 }
124
125 return ret;
126}
127
128char* r_readdir(r_dir_t *rdir, bool skip_dotfiles)
129{
130 size_t len;
131 char *filename;
132 struct dirent *dentry;
133 struct stat fstats;
134
135 while (true) {
136 if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
137 if (dentry->d_name[0] == '.') {
138 if (skip_dotfiles)
139 continue;
140 if (dentry->d_name[1] == '\0')
141 continue;
142 if (dentry->d_name[1] == '.' && dentry->d_name[2] == '\0')
143 continue;
144 }
145
146 len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
147 filename = emalloc(len);
148 snprintf(filename, len, "%s%s%s", rdir->name,
149 rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
150 dentry->d_name);
151
152 if (stat(filename, &fstats) < 0)
153 continue;
154 if (S_ISDIR(fstats.st_mode)) {
155 /* put subdirectory on the stack */
156 if (rdir->stlen == rdir->stcap) {
157 rdir->stcap *= 2;
158 rdir->stack = erealloc(rdir->stack,
159 rdir->stcap * sizeof(char*));
160 }
161 rdir->stack[rdir->stlen++] = filename;
162 continue;
163 }
164 return filename;
165 }
166
167 if (rdir->recursive && rdir->stlen > 0) {
168 /* open next subdirectory */
169 closedir(rdir->dir);
170 if (rdir->d != 0)
171 free(rdir->name);
172 rdir->name = rdir->stack[--rdir->stlen];
173 rdir->d = 1;
174 if ((rdir->dir = opendir(rdir->name)) == NULL)
175 error(0, errno, "%s", rdir->name);
176 continue;
177 }
178 /* no more entries */
179 break;
180 }
181 return NULL;
182}
183
184int r_mkdir(char *path)
185{
186 char c, *s = path;
187 struct stat st;
188
189 while (*s != '\0') {
190 if (*s == '/') {
191 s++;
192 continue;
193 }
194 for (; *s != '\0' && *s != '/'; s++);
195 c = *s;
196 *s = '\0';
197 if (mkdir(path, 0755) == -1)
198 if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode))
199 return -1;
200 *s = c;
201 }
202 return 0;
203}