A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

218 строки
4.3 KiB

  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. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include "options.h"
  25. #include "util.h"
  26. const char *progname;
  27. void* emalloc(size_t size)
  28. {
  29. void *ptr;
  30. ptr = malloc(size);
  31. if (ptr == NULL)
  32. error(EXIT_FAILURE, errno, NULL);
  33. return ptr;
  34. }
  35. void* erealloc(void *ptr, size_t size)
  36. {
  37. ptr = realloc(ptr, size);
  38. if (ptr == NULL)
  39. error(EXIT_FAILURE, errno, NULL);
  40. return ptr;
  41. }
  42. char* estrdup(const char *s)
  43. {
  44. char *d;
  45. size_t n = strlen(s) + 1;
  46. d = malloc(n);
  47. if (d == NULL)
  48. error(EXIT_FAILURE, errno, NULL);
  49. memcpy(d, s, n);
  50. return d;
  51. }
  52. void error(int eval, int err, const char* fmt, ...)
  53. {
  54. va_list ap;
  55. fflush(stdout);
  56. fprintf(stderr, "%s: ", progname);
  57. va_start(ap, fmt);
  58. if (fmt != NULL)
  59. vfprintf(stderr, fmt, ap);
  60. va_end(ap);
  61. if (err != 0)
  62. fprintf(stderr, "%s%s", fmt != NULL ? ": " : "", strerror(err));
  63. fputc('\n', stderr);
  64. if (eval != 0)
  65. exit(eval);
  66. }
  67. void size_readable(float *size, const char **unit)
  68. {
  69. const char *units[] = { "", "K", "M", "G" };
  70. int i;
  71. for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++)
  72. *size /= 1024.0;
  73. *unit = units[MIN(i, ARRLEN(units) - 1)];
  74. }
  75. int r_opendir(r_dir_t *rdir, const char *dirname)
  76. {
  77. if (*dirname == '\0')
  78. return -1;
  79. if ((rdir->dir = opendir(dirname)) == NULL) {
  80. rdir->name = NULL;
  81. rdir->stack = NULL;
  82. return -1;
  83. }
  84. rdir->stcap = 512;
  85. rdir->stack = (char**) emalloc(rdir->stcap * sizeof(char*));
  86. rdir->stlen = 0;
  87. rdir->name = (char*) dirname;
  88. rdir->d = 0;
  89. return 0;
  90. }
  91. int r_closedir(r_dir_t *rdir)
  92. {
  93. int ret = 0;
  94. if (rdir->stack != NULL) {
  95. while (rdir->stlen > 0)
  96. free(rdir->stack[--rdir->stlen]);
  97. free(rdir->stack);
  98. rdir->stack = NULL;
  99. }
  100. if (rdir->dir != NULL) {
  101. if ((ret = closedir(rdir->dir)) == 0)
  102. rdir->dir = NULL;
  103. }
  104. if (rdir->d != 0) {
  105. free(rdir->name);
  106. rdir->name = NULL;
  107. }
  108. return ret;
  109. }
  110. char* r_readdir(r_dir_t *rdir)
  111. {
  112. size_t len;
  113. char *filename;
  114. struct dirent *dentry;
  115. struct stat fstats;
  116. while (true) {
  117. if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
  118. if (dentry->d_name[0] == '.')
  119. continue;
  120. len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
  121. filename = (char*) emalloc(len);
  122. snprintf(filename, len, "%s%s%s", rdir->name,
  123. rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
  124. dentry->d_name);
  125. if (stat(filename, &fstats) < 0)
  126. continue;
  127. if (S_ISDIR(fstats.st_mode)) {
  128. /* put subdirectory on the stack */
  129. if (rdir->stlen == rdir->stcap) {
  130. rdir->stcap *= 2;
  131. rdir->stack = (char**) erealloc(rdir->stack,
  132. rdir->stcap * sizeof(char*));
  133. }
  134. rdir->stack[rdir->stlen++] = filename;
  135. continue;
  136. }
  137. return filename;
  138. }
  139. if (rdir->stlen > 0) {
  140. /* open next subdirectory */
  141. closedir(rdir->dir);
  142. if (rdir->d != 0)
  143. free(rdir->name);
  144. rdir->name = rdir->stack[--rdir->stlen];
  145. rdir->d = 1;
  146. if ((rdir->dir = opendir(rdir->name)) == NULL)
  147. error(0, errno, "%s", rdir->name);
  148. continue;
  149. }
  150. /* no more entries */
  151. break;
  152. }
  153. return NULL;
  154. }
  155. int r_mkdir(const char *path)
  156. {
  157. char *dir, *d;
  158. struct stat stats;
  159. int err = 0;
  160. if (*path == '\0')
  161. return -1;
  162. if (stat(path, &stats) == 0)
  163. return S_ISDIR(stats.st_mode) ? 0 : -1;
  164. d = dir = (char*) emalloc(strlen(path) + 1);
  165. strcpy(dir, path);
  166. while (d != NULL && err == 0) {
  167. d = strchr(d + 1, '/');
  168. if (d != NULL)
  169. *d = '\0';
  170. if (access(dir, F_OK) < 0 && errno == ENOENT) {
  171. if (mkdir(dir, 0755) < 0) {
  172. error(0, errno, "%s", dir);
  173. err = -1;
  174. }
  175. } else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) {
  176. err = -1;
  177. }
  178. if (d != NULL)
  179. *d = '/';
  180. }
  181. free(dir);
  182. return err;
  183. }