A Simple X Image Viewer
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

347 satır
7.0 KiB

  1. /* sxiv: util.c
  2. * Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  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. #include "options.h"
  26. #include "util.h"
  27. enum {
  28. BUF_SIZE = 1024,
  29. DNAME_CNT = 512,
  30. FNAME_LEN = 1024
  31. };
  32. void cleanup(void);
  33. void* s_malloc(size_t size) {
  34. void *ptr;
  35. ptr = malloc(size);
  36. if (ptr == NULL)
  37. die("could not allocate memory");
  38. return ptr;
  39. }
  40. void* s_realloc(void *ptr, size_t size) {
  41. ptr = realloc(ptr, size);
  42. if (ptr == NULL)
  43. die("could not allocate memory");
  44. return ptr;
  45. }
  46. char* s_strdup(char *s) {
  47. char *d = NULL;
  48. if (s != NULL) {
  49. d = malloc(strlen(s) + 1);
  50. if (d == NULL)
  51. die("could not allocate memory");
  52. strcpy(d, s);
  53. }
  54. return d;
  55. }
  56. void warn(const char* fmt, ...) {
  57. va_list args;
  58. if (fmt == NULL || options->quiet)
  59. return;
  60. va_start(args, fmt);
  61. fprintf(stderr, "sxiv: warning: ");
  62. vfprintf(stderr, fmt, args);
  63. fprintf(stderr, "\n");
  64. va_end(args);
  65. }
  66. void die(const char* fmt, ...) {
  67. va_list args;
  68. if (fmt == NULL)
  69. return;
  70. va_start(args, fmt);
  71. fprintf(stderr, "sxiv: error: ");
  72. vfprintf(stderr, fmt, args);
  73. fprintf(stderr, "\n");
  74. va_end(args);
  75. cleanup();
  76. exit(EXIT_FAILURE);
  77. }
  78. ssize_t get_line(char **buf, size_t *n, FILE *stream) {
  79. size_t len;
  80. char *s;
  81. if (stream == NULL || feof(stream) || ferror(stream))
  82. return -1;
  83. if (*buf == NULL || *n == 0) {
  84. *n = BUF_SIZE;
  85. *buf = (char*) s_malloc(*n);
  86. }
  87. s = *buf;
  88. while (true) {
  89. if (fgets(s, *n - (s - *buf), stream) == NULL)
  90. return -1;
  91. len = strlen(s);
  92. if (feof(stream))
  93. break;
  94. if (len > 0 && s[len-1] == '\n')
  95. break;
  96. if (len + 1 == *n - (s - *buf)) {
  97. *buf = (char*) s_realloc(*buf, 2 * *n);
  98. s = *buf + *n - 1;
  99. *n *= 2;
  100. } else {
  101. s += len;
  102. }
  103. }
  104. return s - *buf + len;
  105. }
  106. void size_readable(float *size, const char **unit) {
  107. const char *units[] = { "", "K", "M", "G" };
  108. int i;
  109. for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++)
  110. *size /= 1024.0;
  111. *unit = units[MIN(i, ARRLEN(units) - 1)];
  112. }
  113. char* absolute_path(const char *filename) {
  114. size_t len;
  115. const char *basename;
  116. char *dir, *dirname = NULL, *path = NULL, *s;
  117. char *cwd = NULL, *twd = NULL;
  118. if (filename == NULL || *filename == '\0' || *filename == '/')
  119. return NULL;
  120. len = FNAME_LEN;
  121. cwd = (char*) s_malloc(len);
  122. while ((s = getcwd(cwd, len)) == NULL && errno == ERANGE) {
  123. len *= 2;
  124. cwd = (char*) s_realloc(cwd, len);
  125. }
  126. if (s == NULL)
  127. goto error;
  128. s = strrchr(filename, '/');
  129. if (s != NULL) {
  130. len = s - filename;
  131. dirname = (char*) s_malloc(len + 1);
  132. strncpy(dirname, filename, len);
  133. dirname[len] = '\0';
  134. basename = s + 1;
  135. if (chdir(cwd) < 0)
  136. /* we're not able to come back afterwards */
  137. goto error;
  138. if (chdir(dirname) < 0)
  139. goto error;
  140. len = FNAME_LEN;
  141. twd = (char*) s_malloc(len);
  142. while ((s = getcwd(twd, len)) == NULL && errno == ERANGE) {
  143. len *= 2;
  144. twd = (char*) s_realloc(twd, len);
  145. }
  146. if (chdir(cwd) < 0)
  147. die("could not revert to prior working directory");
  148. if (s == NULL)
  149. goto error;
  150. dir = twd;
  151. } else {
  152. /* only a single filename given */
  153. basename = filename;
  154. dir = cwd;
  155. }
  156. len = strlen(dir) + strlen(basename) + 2;
  157. path = (char*) s_malloc(len);
  158. snprintf(path, len, "%s/%s", dir, basename);
  159. goto end;
  160. error:
  161. if (path != NULL) {
  162. free(path);
  163. path = NULL;
  164. }
  165. end:
  166. if (dirname != NULL)
  167. free(dirname);
  168. if (cwd != NULL)
  169. free(cwd);
  170. if (twd != NULL)
  171. free(twd);
  172. return path;
  173. }
  174. int r_opendir(r_dir_t *rdir, const char *dirname) {
  175. if (rdir == NULL || dirname == NULL || *dirname == '\0')
  176. return -1;
  177. if ((rdir->dir = opendir(dirname)) == NULL) {
  178. rdir->name = NULL;
  179. rdir->stack = NULL;
  180. return -1;
  181. }
  182. rdir->stcap = DNAME_CNT;
  183. rdir->stack = (char**) s_malloc(rdir->stcap * sizeof(char*));
  184. rdir->stlen = 0;
  185. rdir->name = (char*) dirname;
  186. rdir->d = 0;
  187. return 0;
  188. }
  189. int r_closedir(r_dir_t *rdir) {
  190. int ret = 0;
  191. if (rdir == NULL)
  192. return -1;
  193. if (rdir->stack != NULL) {
  194. while (rdir->stlen > 0)
  195. free(rdir->stack[--rdir->stlen]);
  196. free(rdir->stack);
  197. rdir->stack = NULL;
  198. }
  199. if (rdir->dir != NULL) {
  200. if ((ret = closedir(rdir->dir)) == 0)
  201. rdir->dir = NULL;
  202. }
  203. if (rdir->d != 0 && rdir->name != NULL) {
  204. free(rdir->name);
  205. rdir->name = NULL;
  206. }
  207. return ret;
  208. }
  209. char* r_readdir(r_dir_t *rdir) {
  210. size_t len;
  211. char *filename;
  212. struct dirent *dentry;
  213. struct stat fstats;
  214. if (rdir == NULL || rdir->dir == NULL || rdir->name == NULL)
  215. return NULL;
  216. while (true) {
  217. if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
  218. if (STREQ(dentry->d_name, ".") || STREQ(dentry->d_name, ".."))
  219. continue;
  220. len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
  221. filename = (char*) s_malloc(len);
  222. snprintf(filename, len, "%s%s%s", rdir->name,
  223. rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
  224. dentry->d_name);
  225. if (stat(filename, &fstats) < 0)
  226. continue;
  227. if (S_ISDIR(fstats.st_mode)) {
  228. /* put subdirectory on the stack */
  229. if (rdir->stlen == rdir->stcap) {
  230. rdir->stcap *= 2;
  231. rdir->stack = (char**) s_realloc(rdir->stack,
  232. rdir->stcap * sizeof(char*));
  233. }
  234. rdir->stack[rdir->stlen++] = filename;
  235. continue;
  236. }
  237. return filename;
  238. }
  239. if (rdir->stlen > 0) {
  240. /* open next subdirectory */
  241. closedir(rdir->dir);
  242. if (rdir->d != 0)
  243. free(rdir->name);
  244. rdir->name = rdir->stack[--rdir->stlen];
  245. rdir->d = 1;
  246. if ((rdir->dir = opendir(rdir->name)) == NULL)
  247. warn("could not open directory: %s", rdir->name);
  248. continue;
  249. }
  250. /* no more entries */
  251. break;
  252. }
  253. return NULL;
  254. }
  255. int r_mkdir(const char *path) {
  256. char *dir, *d;
  257. struct stat stats;
  258. int err = 0;
  259. if (path == NULL || *path == '\0')
  260. return -1;
  261. if (stat(path, &stats) == 0) {
  262. if (S_ISDIR(stats.st_mode)) {
  263. return 0;
  264. } else {
  265. warn("not a directory: %s", path);
  266. return -1;
  267. }
  268. }
  269. d = dir = (char*) s_malloc(strlen(path) + 1);
  270. strcpy(dir, path);
  271. while (d != NULL && err == 0) {
  272. d = strchr(d + 1, '/');
  273. if (d != NULL)
  274. *d = '\0';
  275. if (access(dir, F_OK) < 0 && errno == ENOENT) {
  276. if (mkdir(dir, 0755) < 0) {
  277. warn("could not create directory: %s", dir);
  278. err = -1;
  279. }
  280. } else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) {
  281. warn("not a directory: %s", dir);
  282. err = -1;
  283. }
  284. if (d != NULL)
  285. *d = '/';
  286. }
  287. free(dir);
  288. return err;
  289. }