A Simple X Image Viewer
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if (eval == 0 && options->quiet)
  56. return;
  57. fflush(stdout);
  58. fprintf(stderr, "%s: ", progname);
  59. va_start(ap, fmt);
  60. if (fmt != NULL)
  61. vfprintf(stderr, fmt, ap);
  62. va_end(ap);
  63. if (err != 0)
  64. fprintf(stderr, "%s%s", fmt != NULL ? ": " : "", strerror(err));
  65. fputc('\n', stderr);
  66. if (eval != 0)
  67. exit(eval);
  68. }
  69. void size_readable(float *size, const char **unit)
  70. {
  71. const char *units[] = { "", "K", "M", "G" };
  72. int i;
  73. for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++)
  74. *size /= 1024.0;
  75. *unit = units[MIN(i, ARRLEN(units) - 1)];
  76. }
  77. int r_opendir(r_dir_t *rdir, const char *dirname, bool recursive)
  78. {
  79. if (*dirname == '\0')
  80. return -1;
  81. if ((rdir->dir = opendir(dirname)) == NULL) {
  82. rdir->name = NULL;
  83. rdir->stack = NULL;
  84. return -1;
  85. }
  86. rdir->stcap = 512;
  87. rdir->stack = (char**) emalloc(rdir->stcap * sizeof(char*));
  88. rdir->stlen = 0;
  89. rdir->name = (char*) dirname;
  90. rdir->d = 0;
  91. rdir->recursive = recursive;
  92. return 0;
  93. }
  94. int r_closedir(r_dir_t *rdir)
  95. {
  96. int ret = 0;
  97. if (rdir->stack != NULL) {
  98. while (rdir->stlen > 0)
  99. free(rdir->stack[--rdir->stlen]);
  100. free(rdir->stack);
  101. rdir->stack = NULL;
  102. }
  103. if (rdir->dir != NULL) {
  104. if ((ret = closedir(rdir->dir)) == 0)
  105. rdir->dir = NULL;
  106. }
  107. if (rdir->d != 0) {
  108. free(rdir->name);
  109. rdir->name = NULL;
  110. }
  111. return ret;
  112. }
  113. char* r_readdir(r_dir_t *rdir)
  114. {
  115. size_t len;
  116. char *filename;
  117. struct dirent *dentry;
  118. struct stat fstats;
  119. while (true) {
  120. if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
  121. if (dentry->d_name[0] == '.')
  122. continue;
  123. len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
  124. filename = (char*) emalloc(len);
  125. snprintf(filename, len, "%s%s%s", rdir->name,
  126. rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
  127. dentry->d_name);
  128. if (stat(filename, &fstats) < 0)
  129. continue;
  130. if (S_ISDIR(fstats.st_mode)) {
  131. /* put subdirectory on the stack */
  132. if (rdir->stlen == rdir->stcap) {
  133. rdir->stcap *= 2;
  134. rdir->stack = (char**) erealloc(rdir->stack,
  135. rdir->stcap * sizeof(char*));
  136. }
  137. rdir->stack[rdir->stlen++] = filename;
  138. continue;
  139. }
  140. return filename;
  141. }
  142. if (rdir->recursive && rdir->stlen > 0) {
  143. /* open next subdirectory */
  144. closedir(rdir->dir);
  145. if (rdir->d != 0)
  146. free(rdir->name);
  147. rdir->name = rdir->stack[--rdir->stlen];
  148. rdir->d = 1;
  149. if ((rdir->dir = opendir(rdir->name)) == NULL)
  150. error(0, errno, "%s", rdir->name);
  151. continue;
  152. }
  153. /* no more entries */
  154. break;
  155. }
  156. return NULL;
  157. }
  158. int r_mkdir(char *path)
  159. {
  160. char c, *s = path;
  161. struct stat st;
  162. while (*s != '\0') {
  163. if (*s == '/') {
  164. s++;
  165. continue;
  166. }
  167. for (; *s != '\0' && *s != '/'; s++);
  168. c = *s;
  169. *s = '\0';
  170. if (mkdir(path, 0755) == -1)
  171. if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode))
  172. return -1;
  173. *s = c;
  174. }
  175. return 0;
  176. }