A Simple X Image Viewer
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

237 lines
4.6 KiB

  1. /* sxiv: util.c
  2. * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  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. #define FNAME_LEN 1024
  27. void cleanup();
  28. void* s_malloc(size_t size) {
  29. void *ptr;
  30. if (!(ptr = malloc(size)))
  31. die("could not allocate memory");
  32. return ptr;
  33. }
  34. void* s_realloc(void *ptr, size_t size) {
  35. if (!(ptr = realloc(ptr, size)))
  36. die("could not allocate memory");
  37. return ptr;
  38. }
  39. void warn(const char* fmt, ...) {
  40. va_list args;
  41. if (!fmt || options->quiet)
  42. return;
  43. va_start(args, fmt);
  44. fprintf(stderr, "sxiv: warning: ");
  45. vfprintf(stderr, fmt, args);
  46. fprintf(stderr, "\n");
  47. va_end(args);
  48. }
  49. void die(const char* fmt, ...) {
  50. va_list args;
  51. if (!fmt)
  52. return;
  53. va_start(args, fmt);
  54. fprintf(stderr, "sxiv: error: ");
  55. vfprintf(stderr, fmt, args);
  56. fprintf(stderr, "\n");
  57. va_end(args);
  58. cleanup();
  59. exit(1);
  60. }
  61. void size_readable(float *size, const char **unit) {
  62. const char *units[] = { "", "K", "M", "G" };
  63. int i;
  64. for (i = 0; i < LEN(units) && *size > 1024; ++i)
  65. *size /= 1024;
  66. *unit = units[MIN(i, LEN(units) - 1)];
  67. }
  68. char* absolute_path(const char *filename) {
  69. size_t len;
  70. char *path = NULL;
  71. const char *basename;
  72. char *dirname = NULL;
  73. char *cwd = NULL;
  74. char *twd = NULL;
  75. char *dir;
  76. char *s;
  77. if (!filename || *filename == '\0' || *filename == '/')
  78. return NULL;
  79. len = FNAME_LEN;
  80. cwd = (char*) s_malloc(len);
  81. while (!(s = getcwd(cwd, len)) && errno == ERANGE) {
  82. len *= 2;
  83. cwd = (char*) s_realloc(cwd, len);
  84. }
  85. if (!s)
  86. goto error;
  87. s = strrchr(filename, '/');
  88. if (s) {
  89. len = s - filename;
  90. dirname = (char*) s_malloc(len + 1);
  91. strncpy(dirname, filename, len);
  92. dirname[len] = '\0';
  93. basename = s + 1;
  94. if (chdir(cwd))
  95. /* we're not able to come back afterwards */
  96. goto error;
  97. if (chdir(dirname))
  98. goto error;
  99. len = FNAME_LEN;
  100. twd = (char*) s_malloc(len);
  101. while (!(s = getcwd(twd, len)) && errno == ERANGE) {
  102. len *= 2;
  103. twd = (char*) s_realloc(twd, len);
  104. }
  105. if (chdir(cwd))
  106. die("could not revert to prior working directory");
  107. if (!s)
  108. goto error;
  109. dir = twd;
  110. } else {
  111. /* only a single filename given */
  112. basename = filename;
  113. dir = cwd;
  114. }
  115. len = strlen(dir) + strlen(basename) + 2;
  116. path = (char*) s_malloc(len);
  117. snprintf(path, len, "%s/%s", dir, basename);
  118. goto end;
  119. error:
  120. if (path) {
  121. free(path);
  122. path = NULL;
  123. }
  124. end:
  125. if (dirname)
  126. free(dirname);
  127. if (cwd)
  128. free(cwd);
  129. if (twd)
  130. free(twd);
  131. return path;
  132. }
  133. int create_dir_rec(const char *path) {
  134. char *dir, *d;
  135. struct stat stats;
  136. int err = 0;
  137. if (!path || !*path)
  138. return -1;
  139. if (!stat(path, &stats)) {
  140. if (S_ISDIR(stats.st_mode)) {
  141. return 0;
  142. } else {
  143. warn("not a directory: %s", path);
  144. return -1;
  145. }
  146. }
  147. d = dir = (char*) s_malloc(strlen(path) + 1);
  148. strcpy(dir, path);
  149. while (d != NULL && !err) {
  150. d = strchr(d + 1, '/');
  151. if (d != NULL)
  152. *d = '\0';
  153. if (access(dir, F_OK) && errno == ENOENT) {
  154. if (mkdir(dir, 0755)) {
  155. warn("could not create directory: %s", dir);
  156. err = -1;
  157. }
  158. } else if (stat(dir, &stats) || !S_ISDIR(stats.st_mode)) {
  159. warn("not a directory: %s", dir);
  160. err = -1;
  161. }
  162. if (d != NULL)
  163. *d = '/';
  164. }
  165. free(dir);
  166. return err;
  167. }
  168. char* readline(FILE *stream) {
  169. size_t len;
  170. char *buf, *s, *end;
  171. if (!stream || feof(stream) || ferror(stream))
  172. return NULL;
  173. len = FNAME_LEN;
  174. s = buf = (char*) s_malloc(len * sizeof(char));
  175. do {
  176. *s = '\0';
  177. fgets(s, len - (s - buf), stream);
  178. if ((end = strchr(s, '\n'))) {
  179. *end = '\0';
  180. } else if (strlen(s) + 1 == len - (s - buf)) {
  181. buf = (char*) s_realloc(buf, 2 * len * sizeof(char));
  182. s = buf + len - 1;
  183. len *= 2;
  184. } else {
  185. s += strlen(s);
  186. }
  187. } while (!end && !feof(stream) && !ferror(stream));
  188. if (ferror(stream)) {
  189. s = NULL;
  190. } else {
  191. s = (char*) s_malloc((strlen(buf) + 1) * sizeof(char));
  192. strcpy(s, buf);
  193. }
  194. free(buf);
  195. return s;
  196. }