A Simple X Image Viewer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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