A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

356 wiersze
7.2 KiB

  1. /* sxiv: util.c
  2. * Copyright (c) 2011 Bert Muennich <be.muennich at googlemail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  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. #include "options.h"
  26. #include "util.h"
  27. enum {
  28. BUF_SIZE = 1024,
  29. DNAME_CNT = 512,
  30. FNAME_LEN = 1024
  31. };
  32. void cleanup(void);
  33. void* s_malloc(size_t size) {
  34. void *ptr;
  35. ptr = malloc(size);
  36. if (ptr == NULL)
  37. die("could not allocate memory");
  38. return ptr;
  39. }
  40. void* s_realloc(void *ptr, size_t size) {
  41. ptr = realloc(ptr, size);
  42. if (ptr == NULL)
  43. die("could not allocate memory");
  44. return ptr;
  45. }
  46. char* s_strdup(char *s) {
  47. char *d = NULL;
  48. if (s != NULL) {
  49. d = malloc(strlen(s) + 1);
  50. if (d == NULL)
  51. die("could not allocate memory");
  52. strcpy(d, s);
  53. }
  54. return d;
  55. }
  56. void warn(const char* fmt, ...) {
  57. va_list args;
  58. if (fmt == NULL || options->quiet)
  59. return;
  60. va_start(args, fmt);
  61. fprintf(stderr, "sxiv: warning: ");
  62. vfprintf(stderr, fmt, args);
  63. fprintf(stderr, "\n");
  64. va_end(args);
  65. }
  66. void die(const char* fmt, ...) {
  67. va_list args;
  68. if (fmt == NULL)
  69. return;
  70. va_start(args, fmt);
  71. fprintf(stderr, "sxiv: error: ");
  72. vfprintf(stderr, fmt, args);
  73. fprintf(stderr, "\n");
  74. va_end(args);
  75. cleanup();
  76. exit(EXIT_FAILURE);
  77. }
  78. ssize_t get_line(char **buf, size_t *n, FILE *stream) {
  79. size_t len;
  80. char *s;
  81. if (stream == NULL || feof(stream) || ferror(stream))
  82. return -1;
  83. if (*buf == NULL || *n == 0) {
  84. *n = BUF_SIZE;
  85. *buf = (char*) s_malloc(*n);
  86. }
  87. s = *buf;
  88. while (true) {
  89. if (fgets(s, *n - (s - *buf), stream) == NULL)
  90. return -1;
  91. len = strlen(s);
  92. if (feof(stream))
  93. break;
  94. if (len > 0 && s[len-1] == '\n')
  95. break;
  96. if (len + 1 == *n - (s - *buf)) {
  97. *buf = (char*) s_realloc(*buf, 2 * *n);
  98. s = *buf + *n - 1;
  99. *n *= 2;
  100. } else {
  101. s += len;
  102. }
  103. }
  104. return s - *buf + len;
  105. }
  106. void size_readable(float *size, const char **unit) {
  107. const char *units[] = { "", "K", "M", "G" };
  108. int i;
  109. for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++)
  110. *size /= 1024.0;
  111. *unit = units[MIN(i, ARRLEN(units) - 1)];
  112. }
  113. void time_readable(float *time, const char **unit) {
  114. const char *units[] = { "s", "m", "h" };
  115. int i;
  116. for (i = 0; i < ARRLEN(units) && *time >= 60.0; i++)
  117. *time /= 60.0;
  118. *unit = units[MIN(i, ARRLEN(units) - 1)];
  119. }
  120. char* absolute_path(const char *filename) {
  121. size_t len;
  122. const char *basename;
  123. char *dir, *dirname = NULL, *path = NULL, *s;
  124. char *cwd = NULL, *twd = NULL;
  125. if (filename == NULL || *filename == '\0' || *filename == '/')
  126. return NULL;
  127. len = FNAME_LEN;
  128. cwd = (char*) s_malloc(len);
  129. while ((s = getcwd(cwd, len)) == NULL && errno == ERANGE) {
  130. len *= 2;
  131. cwd = (char*) s_realloc(cwd, len);
  132. }
  133. if (s == NULL)
  134. goto error;
  135. s = strrchr(filename, '/');
  136. if (s != NULL) {
  137. len = s - filename;
  138. dirname = (char*) s_malloc(len + 1);
  139. strncpy(dirname, filename, len);
  140. dirname[len] = '\0';
  141. basename = s + 1;
  142. if (chdir(cwd) < 0)
  143. /* we're not able to come back afterwards */
  144. goto error;
  145. if (chdir(dirname) < 0)
  146. goto error;
  147. len = FNAME_LEN;
  148. twd = (char*) s_malloc(len);
  149. while ((s = getcwd(twd, len)) == NULL && errno == ERANGE) {
  150. len *= 2;
  151. twd = (char*) s_realloc(twd, len);
  152. }
  153. if (chdir(cwd) < 0)
  154. die("could not revert to prior working directory");
  155. if (s == NULL)
  156. goto error;
  157. dir = twd;
  158. } else {
  159. /* only a single filename given */
  160. basename = filename;
  161. dir = cwd;
  162. }
  163. len = strlen(dir) + strlen(basename) + 2;
  164. path = (char*) s_malloc(len);
  165. snprintf(path, len, "%s/%s", dir, basename);
  166. goto end;
  167. error:
  168. if (path != NULL) {
  169. free(path);
  170. path = NULL;
  171. }
  172. end:
  173. if (dirname != NULL)
  174. free(dirname);
  175. if (cwd != NULL)
  176. free(cwd);
  177. if (twd != NULL)
  178. free(twd);
  179. return path;
  180. }
  181. int r_opendir(r_dir_t *rdir, const char *dirname) {
  182. if (rdir == NULL || dirname == NULL || *dirname == '\0')
  183. return -1;
  184. if ((rdir->dir = opendir(dirname)) == NULL) {
  185. rdir->name = NULL;
  186. rdir->stack = NULL;
  187. return -1;
  188. }
  189. rdir->stcap = DNAME_CNT;
  190. rdir->stack = (char**) s_malloc(rdir->stcap * sizeof(char*));
  191. rdir->stlen = 0;
  192. rdir->name = (char*) dirname;
  193. rdir->d = 0;
  194. return 0;
  195. }
  196. int r_closedir(r_dir_t *rdir) {
  197. int ret = 0;
  198. if (rdir == NULL)
  199. return -1;
  200. if (rdir->stack != NULL) {
  201. while (rdir->stlen > 0)
  202. free(rdir->stack[--rdir->stlen]);
  203. free(rdir->stack);
  204. rdir->stack = NULL;
  205. }
  206. if (rdir->dir != NULL) {
  207. if ((ret = closedir(rdir->dir)) == 0)
  208. rdir->dir = NULL;
  209. }
  210. if (rdir->d != 0 && rdir->name != NULL) {
  211. free(rdir->name);
  212. rdir->name = NULL;
  213. }
  214. return ret;
  215. }
  216. char* r_readdir(r_dir_t *rdir) {
  217. size_t len;
  218. char *filename;
  219. struct dirent *dentry;
  220. struct stat fstats;
  221. if (rdir == NULL || rdir->dir == NULL || rdir->name == NULL)
  222. return NULL;
  223. while (true) {
  224. if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
  225. if (STREQ(dentry->d_name, ".") || STREQ(dentry->d_name, ".."))
  226. continue;
  227. len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
  228. filename = (char*) s_malloc(len);
  229. snprintf(filename, len, "%s%s%s", rdir->name,
  230. rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
  231. dentry->d_name);
  232. if (stat(filename, &fstats) < 0)
  233. continue;
  234. if (S_ISDIR(fstats.st_mode)) {
  235. /* put subdirectory on the stack */
  236. if (rdir->stlen == rdir->stcap) {
  237. rdir->stcap *= 2;
  238. rdir->stack = (char**) s_realloc(rdir->stack,
  239. rdir->stcap * sizeof(char*));
  240. }
  241. rdir->stack[rdir->stlen++] = filename;
  242. continue;
  243. }
  244. return filename;
  245. }
  246. if (rdir->stlen > 0) {
  247. /* open next subdirectory */
  248. closedir(rdir->dir);
  249. if (rdir->d != 0)
  250. free(rdir->name);
  251. rdir->name = rdir->stack[--rdir->stlen];
  252. rdir->d = 1;
  253. if ((rdir->dir = opendir(rdir->name)) == NULL)
  254. warn("could not open directory: %s", rdir->name);
  255. continue;
  256. }
  257. /* no more entries */
  258. break;
  259. }
  260. return NULL;
  261. }
  262. int r_mkdir(const char *path) {
  263. char *dir, *d;
  264. struct stat stats;
  265. int err = 0;
  266. if (path == NULL || *path == '\0')
  267. return -1;
  268. if (stat(path, &stats) == 0) {
  269. if (S_ISDIR(stats.st_mode)) {
  270. return 0;
  271. } else {
  272. warn("not a directory: %s", path);
  273. return -1;
  274. }
  275. }
  276. d = dir = (char*) s_malloc(strlen(path) + 1);
  277. strcpy(dir, path);
  278. while (d != NULL && err == 0) {
  279. d = strchr(d + 1, '/');
  280. if (d != NULL)
  281. *d = '\0';
  282. if (access(dir, F_OK) < 0 && errno == ENOENT) {
  283. if (mkdir(dir, 0755) < 0) {
  284. warn("could not create directory: %s", dir);
  285. err = -1;
  286. }
  287. } else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) {
  288. warn("not a directory: %s", dir);
  289. err = -1;
  290. }
  291. if (d != NULL)
  292. *d = '/';
  293. }
  294. free(dir);
  295. return err;
  296. }