util.c 4.3 KB

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