PageRenderTime 71ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/claws-mail-3.8.1/src/common/utils.c

#
C | 5297 lines | 4253 code | 744 blank | 300 comment | 1185 complexity | 4c6f146a0494bef88f6a540a9ee331dd MD5 | raw file
Possible License(s): GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
  3. * Copyright (C) 1999-2012 Hiroyuki Yamamoto & The Claws Mail Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include "defs.h"
  23. #include <glib.h>
  24. #include <glib/gi18n.h>
  25. #ifdef USE_PTHREAD
  26. #include <pthread.h>
  27. #endif
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <sys/param.h>
  33. #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
  34. # include <wchar.h>
  35. # include <wctype.h>
  36. #endif
  37. #include <stdlib.h>
  38. #include <sys/stat.h>
  39. #include <unistd.h>
  40. #include <stdarg.h>
  41. #include <sys/types.h>
  42. #if HAVE_SYS_WAIT_H
  43. # include <sys/wait.h>
  44. #endif
  45. #include <dirent.h>
  46. #include <time.h>
  47. #include <regex.h>
  48. #ifdef G_OS_UNIX
  49. #include <sys/utsname.h>
  50. #endif
  51. #include <fcntl.h>
  52. #ifdef G_OS_WIN32
  53. # include <direct.h>
  54. # include <io.h>
  55. # include <w32lib.h>
  56. #endif
  57. #ifdef MAEMO
  58. #include <libosso.h>
  59. #ifdef CHINOOK
  60. # include <tablet-browser-interface.h>
  61. #else
  62. # include <osso-browser-interface.h>
  63. #endif
  64. #endif
  65. #include "utils.h"
  66. #include "socket.h"
  67. #include "../codeconv.h"
  68. #define BUFFSIZE 8192
  69. static gboolean debug_mode = FALSE;
  70. #ifdef G_OS_WIN32
  71. static GSList *tempfiles=NULL;
  72. #endif
  73. /* Return true if we are running as root. This function should beused
  74. instead of getuid () == 0. */
  75. gboolean superuser_p (void)
  76. {
  77. #ifdef G_OS_WIN32
  78. return w32_is_administrator ();
  79. #else
  80. return !getuid();
  81. #endif
  82. }
  83. #if !GLIB_CHECK_VERSION(2, 7, 0) && !defined(G_OS_UNIX)
  84. gint g_chdir(const gchar *path)
  85. {
  86. #ifdef G_OS_WIN32
  87. if (G_WIN32_HAVE_WIDECHAR_API()) {
  88. wchar_t *wpath;
  89. gint retval;
  90. gint save_errno;
  91. wpath = g_utf8_to_utf16(path, -1, NULL, NULL, NULL);
  92. if (wpath == NULL) {
  93. errno = EINVAL;
  94. return -1;
  95. }
  96. retval = _wchdir(wpath);
  97. save_errno = errno;
  98. g_free(wpath);
  99. errno = save_errno;
  100. return retval;
  101. } else {
  102. gchar *cp_path;
  103. gint retval;
  104. gint save_errno;
  105. cp_path = g_locale_from_utf8(path, -1, NULL, NULL, NULL);
  106. if (cp_path == NULL) {
  107. errno = EINVAL;
  108. return -1;
  109. }
  110. retval = chdir(cp_path);
  111. save_errno = errno;
  112. g_free(cp_path);
  113. errno = save_errno;
  114. return retval;
  115. }
  116. #else
  117. return chdir(path);
  118. #endif
  119. }
  120. gint g_chmod(const gchar *path, gint mode)
  121. {
  122. #ifdef G_OS_WIN32
  123. if (G_WIN32_HAVE_WIDECHAR_API()) {
  124. wchar_t *wpath;
  125. gint retval;
  126. gint save_errno;
  127. wpath = g_utf8_to_utf16(path, -1, NULL, NULL, NULL);
  128. if (wpath == NULL) {
  129. errno = EINVAL;
  130. return -1;
  131. }
  132. retval = _wchmod(wpath, mode);
  133. save_errno = errno;
  134. g_free(wpath);
  135. errno = save_errno;
  136. return retval;
  137. } else {
  138. gchar *cp_path;
  139. gint retval;
  140. gint save_errno;
  141. cp_path = g_locale_from_utf8(path, -1, NULL, NULL, NULL);
  142. if (cp_path == NULL) {
  143. errno = EINVAL;
  144. return -1;
  145. }
  146. retval = chmod(cp_path, mode);
  147. save_errno = errno;
  148. g_free(cp_path);
  149. errno = save_errno;
  150. return retval;
  151. }
  152. #else
  153. return chmod(path, mode);
  154. #endif
  155. }
  156. FILE* g_fopen(const gchar *filename, const gchar *mode)
  157. {
  158. #ifdef G_OS_WIN32
  159. char *name = g_win32_locale_filename_from_utf8(filename);
  160. FILE* fp = fopen(name, mode);
  161. g_free(name);
  162. return fp;
  163. #else
  164. return fopen(filename, mode);
  165. #endif
  166. }
  167. int g_open(const gchar *filename, int flags, int mode)
  168. {
  169. #ifdef G_OS_WIN32
  170. char *name = g_win32_locale_filename_from_utf8(filename);
  171. int fd = open(name, flags, mode);
  172. g_free(name);
  173. return fp;
  174. #else
  175. return open(filename, flags, mode);
  176. #endif
  177. }
  178. #endif /* GLIB_CHECK_VERSION && G_OS_UNIX */
  179. #ifdef G_OS_WIN32
  180. gint mkstemp_name(gchar *template, gchar **name_used)
  181. {
  182. static gulong count=0; /* W32-_mktemp only supports up to 27
  183. tempfiles... */
  184. int tmpfd;
  185. *name_used = g_strdup_printf("%s.%ld",_mktemp(template),count++);
  186. tmpfd = g_open (*name_used, (O_CREAT | O_RDWR | O_BINARY),
  187. (S_IRUSR | S_IWUSR));
  188. tempfiles=g_slist_append(tempfiles, g_strdup(*name_used));
  189. if (tmpfd<0) {
  190. perror(g_strdup_printf("cant create %s",*name_used));
  191. return -1;
  192. }
  193. else
  194. return tmpfd;
  195. }
  196. #endif /* G_OS_WIN32 */
  197. #ifdef G_OS_WIN32
  198. gint mkstemp(gchar *template)
  199. {
  200. gchar *dummyname;
  201. gint res = mkstemp_name(template, &dummyname);
  202. g_free(dummyname);
  203. return res;
  204. }
  205. #endif /* G_OS_WIN32 */
  206. void list_free_strings(GList *list)
  207. {
  208. list = g_list_first(list);
  209. while (list != NULL) {
  210. g_free(list->data);
  211. list = list->next;
  212. }
  213. }
  214. void slist_free_strings(GSList *list)
  215. {
  216. while (list != NULL) {
  217. g_free(list->data);
  218. list = list->next;
  219. }
  220. }
  221. static void hash_free_strings_func(gpointer key, gpointer value, gpointer data)
  222. {
  223. g_free(key);
  224. }
  225. void hash_free_strings(GHashTable *table)
  226. {
  227. g_hash_table_foreach(table, hash_free_strings_func, NULL);
  228. }
  229. gint str_case_equal(gconstpointer v, gconstpointer v2)
  230. {
  231. return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
  232. }
  233. guint str_case_hash(gconstpointer key)
  234. {
  235. const gchar *p = key;
  236. guint h = *p;
  237. if (h) {
  238. h = g_ascii_tolower(h);
  239. for (p += 1; *p != '\0'; p++)
  240. h = (h << 5) - h + g_ascii_tolower(*p);
  241. }
  242. return h;
  243. }
  244. void ptr_array_free_strings(GPtrArray *array)
  245. {
  246. gint i;
  247. gchar *str;
  248. cm_return_if_fail(array != NULL);
  249. for (i = 0; i < array->len; i++) {
  250. str = g_ptr_array_index(array, i);
  251. g_free(str);
  252. }
  253. }
  254. gint to_number(const gchar *nstr)
  255. {
  256. register const gchar *p;
  257. if (*nstr == '\0') return -1;
  258. for (p = nstr; *p != '\0'; p++)
  259. if (!g_ascii_isdigit(*p)) return -1;
  260. return atoi(nstr);
  261. }
  262. /* convert integer into string,
  263. nstr must be not lower than 11 characters length */
  264. gchar *itos_buf(gchar *nstr, gint n)
  265. {
  266. g_snprintf(nstr, 11, "%d", n);
  267. return nstr;
  268. }
  269. /* convert integer into string */
  270. gchar *itos(gint n)
  271. {
  272. static gchar nstr[11];
  273. return itos_buf(nstr, n);
  274. }
  275. #define divide(num,divisor,i,d) \
  276. { \
  277. i = num >> divisor; \
  278. d = num & ((1<<divisor)-1); \
  279. d = (d*100) >> divisor; \
  280. }
  281. /*!
  282. * \brief Convert a given size in bytes in a human-readable string
  283. *
  284. * \param size The size expressed in bytes to convert in string
  285. * \return The string that respresents the size in an human-readable way
  286. */
  287. gchar *to_human_readable(goffset size)
  288. {
  289. static gchar str[14];
  290. static gchar *b_format = NULL, *kb_format = NULL,
  291. *mb_format = NULL, *gb_format = NULL;
  292. register int t = 0, r = 0;
  293. if (b_format == NULL) {
  294. b_format = _("%dB");
  295. kb_format = _("%d.%02dKB");
  296. mb_format = _("%d.%02dMB");
  297. gb_format = _("%.2fGB");
  298. }
  299. if (size < (goffset)1024) {
  300. g_snprintf(str, sizeof(str), b_format, (gint)size);
  301. return str;
  302. } else if (size >> 10 < (goffset)1024) {
  303. divide(size, 10, t, r);
  304. g_snprintf(str, sizeof(str), kb_format, t, r);
  305. return str;
  306. } else if (size >> 20 < (goffset)1024) {
  307. divide(size, 20, t, r);
  308. g_snprintf(str, sizeof(str), mb_format, t, r);
  309. return str;
  310. } else {
  311. g_snprintf(str, sizeof(str), gb_format, (gfloat)(size >> 30));
  312. return str;
  313. }
  314. }
  315. /* strcmp with NULL-checking */
  316. gint strcmp2(const gchar *s1, const gchar *s2)
  317. {
  318. if (s1 == NULL || s2 == NULL)
  319. return -1;
  320. else
  321. return strcmp(s1, s2);
  322. }
  323. /* strstr with NULL-checking */
  324. gchar *strstr2(const gchar *s1, const gchar *s2)
  325. {
  326. if (s1 == NULL || s2 == NULL)
  327. return NULL;
  328. else
  329. return strstr(s1, s2);
  330. }
  331. /* compare paths */
  332. gint path_cmp(const gchar *s1, const gchar *s2)
  333. {
  334. gint len1, len2;
  335. int rc;
  336. #ifdef G_OS_WIN32
  337. gchar *s1buf, *s2buf;
  338. #endif
  339. if (s1 == NULL || s2 == NULL) return -1;
  340. if (*s1 == '\0' || *s2 == '\0') return -1;
  341. #ifdef G_OS_WIN32
  342. s1buf = g_strdup (s1);
  343. s2buf = g_strdup (s2);
  344. subst_char (s1buf, '/', G_DIR_SEPARATOR);
  345. subst_char (s2buf, '/', G_DIR_SEPARATOR);
  346. s1 = s1buf;
  347. s2 = s2buf;
  348. #endif /* !G_OS_WIN32 */
  349. len1 = strlen(s1);
  350. len2 = strlen(s2);
  351. if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
  352. if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
  353. rc = strncmp(s1, s2, MAX(len1, len2));
  354. #ifdef G_OS_WIN32
  355. g_free (s1buf);
  356. g_free (s2buf);
  357. #endif /* !G_OS_WIN32 */
  358. return rc;
  359. }
  360. /* remove trailing return code */
  361. gchar *strretchomp(gchar *str)
  362. {
  363. register gchar *s;
  364. if (!*str) return str;
  365. for (s = str + strlen(str) - 1;
  366. s >= str && (*s == '\n' || *s == '\r');
  367. s--)
  368. *s = '\0';
  369. return str;
  370. }
  371. /* remove trailing character */
  372. gchar *strtailchomp(gchar *str, gchar tail_char)
  373. {
  374. register gchar *s;
  375. if (!*str) return str;
  376. if (tail_char == '\0') return str;
  377. for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
  378. *s = '\0';
  379. return str;
  380. }
  381. /* remove CR (carriage return) */
  382. gchar *strcrchomp(gchar *str)
  383. {
  384. register gchar *s;
  385. if (!*str) return str;
  386. s = str + strlen(str) - 1;
  387. if (*s == '\n' && s > str && *(s - 1) == '\r') {
  388. *(s - 1) = '\n';
  389. *s = '\0';
  390. }
  391. return str;
  392. }
  393. gint file_strip_crs(const gchar *file)
  394. {
  395. FILE *fp = NULL, *outfp = NULL;
  396. gchar buf[4096];
  397. gchar *out = get_tmp_file();
  398. if (file == NULL)
  399. goto freeout;
  400. fp = g_fopen(file, "rb");
  401. if (!fp)
  402. goto freeout;
  403. outfp = g_fopen(out, "wb");
  404. if (!outfp) {
  405. fclose(fp);
  406. goto freeout;
  407. }
  408. while (fgets(buf, sizeof (buf), fp) != NULL) {
  409. strcrchomp(buf);
  410. if (fputs(buf, outfp) == EOF) {
  411. fclose(fp);
  412. fclose(outfp);
  413. goto unlinkout;
  414. }
  415. }
  416. fclose(fp);
  417. if (fclose(outfp) == EOF) {
  418. goto unlinkout;
  419. }
  420. if (move_file(out, file, TRUE) < 0)
  421. goto unlinkout;
  422. g_free(out);
  423. return 0;
  424. unlinkout:
  425. claws_unlink(out);
  426. freeout:
  427. g_free(out);
  428. return -1;
  429. }
  430. /* Similar to `strstr' but this function ignores the case of both strings. */
  431. gchar *strcasestr(const gchar *haystack, const gchar *needle)
  432. {
  433. register size_t haystack_len, needle_len;
  434. haystack_len = strlen(haystack);
  435. needle_len = strlen(needle);
  436. if (haystack_len < needle_len || needle_len == 0)
  437. return NULL;
  438. while (haystack_len >= needle_len) {
  439. if (!g_ascii_strncasecmp(haystack, needle, needle_len))
  440. return (gchar *)haystack;
  441. else {
  442. haystack++;
  443. haystack_len--;
  444. }
  445. }
  446. return NULL;
  447. }
  448. gpointer my_memmem(gconstpointer haystack, size_t haystacklen,
  449. gconstpointer needle, size_t needlelen)
  450. {
  451. const gchar *haystack_ = (const gchar *)haystack;
  452. const gchar *needle_ = (const gchar *)needle;
  453. const gchar *haystack_cur = (const gchar *)haystack;
  454. size_t haystack_left = haystacklen;
  455. if (needlelen == 1)
  456. return memchr(haystack_, *needle_, haystacklen);
  457. while ((haystack_cur = memchr(haystack_cur, *needle_, haystack_left))
  458. != NULL) {
  459. if (haystacklen - (haystack_cur - haystack_) < needlelen)
  460. break;
  461. if (memcmp(haystack_cur + 1, needle_ + 1, needlelen - 1) == 0)
  462. return (gpointer)haystack_cur;
  463. else{
  464. haystack_cur++;
  465. haystack_left = haystacklen - (haystack_cur - haystack_);
  466. }
  467. }
  468. return NULL;
  469. }
  470. /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
  471. gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
  472. {
  473. register const gchar *s = src;
  474. register gchar *d = dest;
  475. while (--n && *s)
  476. *d++ = *s++;
  477. *d = '\0';
  478. return dest;
  479. }
  480. /* Examine if next block is non-ASCII string */
  481. gboolean is_next_nonascii(const gchar *s)
  482. {
  483. const gchar *p;
  484. /* skip head space */
  485. for (p = s; *p != '\0' && g_ascii_isspace(*p); p++)
  486. ;
  487. for (; *p != '\0' && !g_ascii_isspace(*p); p++) {
  488. if (*(guchar *)p > 127 || *(guchar *)p < 32)
  489. return TRUE;
  490. }
  491. return FALSE;
  492. }
  493. gint get_next_word_len(const gchar *s)
  494. {
  495. gint len = 0;
  496. for (; *s != '\0' && !g_ascii_isspace(*s); s++, len++)
  497. ;
  498. return len;
  499. }
  500. static void trim_subject_for_compare(gchar *str)
  501. {
  502. gchar *srcp;
  503. eliminate_parenthesis(str, '[', ']');
  504. eliminate_parenthesis(str, '(', ')');
  505. g_strstrip(str);
  506. srcp = str + subject_get_prefix_length(str);
  507. if (srcp != str)
  508. memmove(str, srcp, strlen(srcp) + 1);
  509. }
  510. static void trim_subject_for_sort(gchar *str)
  511. {
  512. gchar *srcp;
  513. g_strstrip(str);
  514. srcp = str + subject_get_prefix_length(str);
  515. if (srcp != str)
  516. memmove(str, srcp, strlen(srcp) + 1);
  517. }
  518. /* compare subjects */
  519. gint subject_compare(const gchar *s1, const gchar *s2)
  520. {
  521. gchar *str1, *str2;
  522. if (!s1 || !s2) return -1;
  523. if (!*s1 || !*s2) return -1;
  524. Xstrdup_a(str1, s1, return -1);
  525. Xstrdup_a(str2, s2, return -1);
  526. trim_subject_for_compare(str1);
  527. trim_subject_for_compare(str2);
  528. if (!*str1 || !*str2) return -1;
  529. return strcmp(str1, str2);
  530. }
  531. gint subject_compare_for_sort(const gchar *s1, const gchar *s2)
  532. {
  533. gchar *str1, *str2;
  534. if (!s1 || !s2) return -1;
  535. Xstrdup_a(str1, s1, return -1);
  536. Xstrdup_a(str2, s2, return -1);
  537. trim_subject_for_sort(str1);
  538. trim_subject_for_sort(str2);
  539. return g_utf8_collate(str1, str2);
  540. }
  541. void trim_subject(gchar *str)
  542. {
  543. register gchar *srcp;
  544. gchar op, cl;
  545. gint in_brace;
  546. g_strstrip(str);
  547. srcp = str + subject_get_prefix_length(str);
  548. if (*srcp == '[') {
  549. op = '[';
  550. cl = ']';
  551. } else if (*srcp == '(') {
  552. op = '(';
  553. cl = ')';
  554. } else
  555. op = 0;
  556. if (op) {
  557. ++srcp;
  558. in_brace = 1;
  559. while (*srcp) {
  560. if (*srcp == op)
  561. in_brace++;
  562. else if (*srcp == cl)
  563. in_brace--;
  564. srcp++;
  565. if (in_brace == 0)
  566. break;
  567. }
  568. }
  569. while (g_ascii_isspace(*srcp)) srcp++;
  570. memmove(str, srcp, strlen(srcp) + 1);
  571. }
  572. void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
  573. {
  574. register gchar *srcp, *destp;
  575. gint in_brace;
  576. srcp = destp = str;
  577. while ((destp = strchr(destp, op))) {
  578. in_brace = 1;
  579. srcp = destp + 1;
  580. while (*srcp) {
  581. if (*srcp == op)
  582. in_brace++;
  583. else if (*srcp == cl)
  584. in_brace--;
  585. srcp++;
  586. if (in_brace == 0)
  587. break;
  588. }
  589. while (g_ascii_isspace(*srcp)) srcp++;
  590. memmove(destp, srcp, strlen(srcp) + 1);
  591. }
  592. }
  593. void extract_parenthesis(gchar *str, gchar op, gchar cl)
  594. {
  595. register gchar *srcp, *destp;
  596. gint in_brace;
  597. srcp = destp = str;
  598. while ((srcp = strchr(destp, op))) {
  599. if (destp > str)
  600. *destp++ = ' ';
  601. memmove(destp, srcp + 1, strlen(srcp));
  602. in_brace = 1;
  603. while(*destp) {
  604. if (*destp == op)
  605. in_brace++;
  606. else if (*destp == cl)
  607. in_brace--;
  608. if (in_brace == 0)
  609. break;
  610. destp++;
  611. }
  612. }
  613. *destp = '\0';
  614. }
  615. static void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
  616. gchar op, gchar cl)
  617. {
  618. register gchar *srcp, *destp;
  619. gint in_brace;
  620. gboolean in_quote = FALSE;
  621. srcp = destp = str;
  622. while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
  623. if (destp > str)
  624. *destp++ = ' ';
  625. memmove(destp, srcp + 1, strlen(srcp));
  626. in_brace = 1;
  627. while(*destp) {
  628. if (*destp == op && !in_quote)
  629. in_brace++;
  630. else if (*destp == cl && !in_quote)
  631. in_brace--;
  632. else if (*destp == quote_chr)
  633. in_quote ^= TRUE;
  634. if (in_brace == 0)
  635. break;
  636. destp++;
  637. }
  638. }
  639. *destp = '\0';
  640. }
  641. void extract_quote(gchar *str, gchar quote_chr)
  642. {
  643. register gchar *p;
  644. if ((str = strchr(str, quote_chr))) {
  645. p = str;
  646. while ((p = strchr(p + 1, quote_chr)) && (p[-1] == '\\')) {
  647. memmove(p - 1, p, strlen(p) + 1);
  648. p--;
  649. }
  650. if(p) {
  651. *p = '\0';
  652. memmove(str, str + 1, p - str);
  653. }
  654. }
  655. }
  656. void eliminate_address_comment(gchar *str)
  657. {
  658. register gchar *srcp, *destp;
  659. gint in_brace;
  660. srcp = destp = str;
  661. while ((destp = strchr(destp, '"'))) {
  662. if ((srcp = strchr(destp + 1, '"'))) {
  663. srcp++;
  664. if (*srcp == '@') {
  665. destp = srcp + 1;
  666. } else {
  667. while (g_ascii_isspace(*srcp)) srcp++;
  668. memmove(destp, srcp, strlen(srcp) + 1);
  669. }
  670. } else {
  671. *destp = '\0';
  672. break;
  673. }
  674. }
  675. srcp = destp = str;
  676. while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
  677. in_brace = 1;
  678. srcp = destp + 1;
  679. while (*srcp) {
  680. if (*srcp == '(')
  681. in_brace++;
  682. else if (*srcp == ')')
  683. in_brace--;
  684. srcp++;
  685. if (in_brace == 0)
  686. break;
  687. }
  688. while (g_ascii_isspace(*srcp)) srcp++;
  689. memmove(destp, srcp, strlen(srcp) + 1);
  690. }
  691. }
  692. gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
  693. {
  694. gboolean in_quote = FALSE;
  695. while (*str) {
  696. if (*str == c && !in_quote)
  697. return (gchar *)str;
  698. if (*str == quote_chr)
  699. in_quote ^= TRUE;
  700. str++;
  701. }
  702. return NULL;
  703. }
  704. void extract_address(gchar *str)
  705. {
  706. eliminate_address_comment(str);
  707. if (strchr_with_skip_quote(str, '"', '<'))
  708. extract_parenthesis_with_skip_quote(str, '"', '<', '>');
  709. g_strstrip(str);
  710. }
  711. void extract_list_id_str(gchar *str)
  712. {
  713. if (strchr_with_skip_quote(str, '"', '<'))
  714. extract_parenthesis_with_skip_quote(str, '"', '<', '>');
  715. g_strstrip(str);
  716. }
  717. static GSList *address_list_append_real(GSList *addr_list, const gchar *str, gboolean removecomments)
  718. {
  719. gchar *work;
  720. gchar *workp;
  721. if (!str) return addr_list;
  722. Xstrdup_a(work, str, return addr_list);
  723. if (removecomments)
  724. eliminate_address_comment(work);
  725. workp = work;
  726. while (workp && *workp) {
  727. gchar *p, *next;
  728. if ((p = strchr_with_skip_quote(workp, '"', ','))) {
  729. *p = '\0';
  730. next = p + 1;
  731. } else
  732. next = NULL;
  733. if (removecomments && strchr_with_skip_quote(workp, '"', '<'))
  734. extract_parenthesis_with_skip_quote
  735. (workp, '"', '<', '>');
  736. g_strstrip(workp);
  737. if (*workp)
  738. addr_list = g_slist_append(addr_list, g_strdup(workp));
  739. workp = next;
  740. }
  741. return addr_list;
  742. }
  743. GSList *address_list_append(GSList *addr_list, const gchar *str)
  744. {
  745. return address_list_append_real(addr_list, str, TRUE);
  746. }
  747. GSList *address_list_append_with_comments(GSList *addr_list, const gchar *str)
  748. {
  749. return address_list_append_real(addr_list, str, FALSE);
  750. }
  751. GSList *references_list_prepend(GSList *msgid_list, const gchar *str)
  752. {
  753. const gchar *strp;
  754. if (!str) return msgid_list;
  755. strp = str;
  756. while (strp && *strp) {
  757. const gchar *start, *end;
  758. gchar *msgid;
  759. if ((start = strchr(strp, '<')) != NULL) {
  760. end = strchr(start + 1, '>');
  761. if (!end) break;
  762. } else
  763. break;
  764. msgid = g_strndup(start + 1, end - start - 1);
  765. g_strstrip(msgid);
  766. if (*msgid)
  767. msgid_list = g_slist_prepend(msgid_list, msgid);
  768. else
  769. g_free(msgid);
  770. strp = end + 1;
  771. }
  772. return msgid_list;
  773. }
  774. GSList *references_list_append(GSList *msgid_list, const gchar *str)
  775. {
  776. GSList *list;
  777. list = references_list_prepend(NULL, str);
  778. list = g_slist_reverse(list);
  779. msgid_list = g_slist_concat(msgid_list, list);
  780. return msgid_list;
  781. }
  782. GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
  783. {
  784. gchar *work;
  785. gchar *workp;
  786. if (!str) return group_list;
  787. Xstrdup_a(work, str, return group_list);
  788. workp = work;
  789. while (workp && *workp) {
  790. gchar *p, *next;
  791. if ((p = strchr_with_skip_quote(workp, '"', ','))) {
  792. *p = '\0';
  793. next = p + 1;
  794. } else
  795. next = NULL;
  796. g_strstrip(workp);
  797. if (*workp)
  798. group_list = g_slist_append(group_list,
  799. g_strdup(workp));
  800. workp = next;
  801. }
  802. return group_list;
  803. }
  804. GList *add_history(GList *list, const gchar *str)
  805. {
  806. GList *old;
  807. gchar *oldstr;
  808. cm_return_val_if_fail(str != NULL, list);
  809. old = g_list_find_custom(list, (gpointer)str, (GCompareFunc)strcmp2);
  810. if (old) {
  811. oldstr = old->data;
  812. list = g_list_remove(list, old->data);
  813. g_free(oldstr);
  814. } else if (g_list_length(list) >= MAX_HISTORY_SIZE) {
  815. GList *last;
  816. last = g_list_last(list);
  817. if (last) {
  818. oldstr = last->data;
  819. list = g_list_remove(list, last->data);
  820. g_free(oldstr);
  821. }
  822. }
  823. list = g_list_prepend(list, g_strdup(str));
  824. return list;
  825. }
  826. void remove_return(gchar *str)
  827. {
  828. register gchar *p = str;
  829. while (*p) {
  830. if (*p == '\n' || *p == '\r')
  831. memmove(p, p + 1, strlen(p));
  832. else
  833. p++;
  834. }
  835. }
  836. void remove_space(gchar *str)
  837. {
  838. register gchar *p = str;
  839. register gint spc;
  840. while (*p) {
  841. spc = 0;
  842. while (g_ascii_isspace(*(p + spc)))
  843. spc++;
  844. if (spc)
  845. memmove(p, p + spc, strlen(p + spc) + 1);
  846. else
  847. p++;
  848. }
  849. }
  850. void unfold_line(gchar *str)
  851. {
  852. register gchar *p = str;
  853. register gint spc;
  854. while (*p) {
  855. if (*p == '\n' || *p == '\r') {
  856. *p++ = ' ';
  857. spc = 0;
  858. while (g_ascii_isspace(*(p + spc)))
  859. spc++;
  860. if (spc)
  861. memmove(p, p + spc, strlen(p + spc) + 1);
  862. } else
  863. p++;
  864. }
  865. }
  866. void subst_char(gchar *str, gchar orig, gchar subst)
  867. {
  868. register gchar *p = str;
  869. while (*p) {
  870. if (*p == orig)
  871. *p = subst;
  872. p++;
  873. }
  874. }
  875. void subst_chars(gchar *str, gchar *orig, gchar subst)
  876. {
  877. register gchar *p = str;
  878. while (*p) {
  879. if (strchr(orig, *p) != NULL)
  880. *p = subst;
  881. p++;
  882. }
  883. }
  884. void subst_for_filename(gchar *str)
  885. {
  886. if (!str)
  887. return;
  888. #ifdef G_OS_WIN32
  889. subst_chars(str, "\t\r\n\\/*:", '_');
  890. #else
  891. subst_chars(str, "\t\r\n\\/*", '_');
  892. #endif
  893. }
  894. void subst_for_shellsafe_filename(gchar *str)
  895. {
  896. if (!str)
  897. return;
  898. subst_for_filename(str);
  899. subst_chars(str, " \"'|&;()<>'!{}[]",'_');
  900. }
  901. gboolean is_ascii_str(const gchar *str)
  902. {
  903. const guchar *p = (const guchar *)str;
  904. while (*p != '\0') {
  905. if (*p != '\t' && *p != ' ' &&
  906. *p != '\r' && *p != '\n' &&
  907. (*p < 32 || *p >= 127))
  908. return FALSE;
  909. p++;
  910. }
  911. return TRUE;
  912. }
  913. static const gchar * line_has_quote_char_last(const gchar * str, const gchar *quote_chars)
  914. {
  915. gchar * position = NULL;
  916. gchar * tmp_pos = NULL;
  917. int i;
  918. if (quote_chars == NULL)
  919. return NULL;
  920. for (i = 0; i < strlen(quote_chars); i++) {
  921. tmp_pos = strrchr (str, quote_chars[i]);
  922. if(position == NULL
  923. || (tmp_pos != NULL && position <= tmp_pos) )
  924. position = tmp_pos;
  925. }
  926. return position;
  927. }
  928. gint get_quote_level(const gchar *str, const gchar *quote_chars)
  929. {
  930. const gchar *first_pos;
  931. const gchar *last_pos;
  932. const gchar *p = str;
  933. gint quote_level = -1;
  934. /* speed up line processing by only searching to the last '>' */
  935. if ((first_pos = line_has_quote_char(str, quote_chars)) != NULL) {
  936. /* skip a line if it contains a '<' before the initial '>' */
  937. if (memchr(str, '<', first_pos - str) != NULL)
  938. return -1;
  939. last_pos = line_has_quote_char_last(first_pos, quote_chars);
  940. } else
  941. return -1;
  942. while (p <= last_pos) {
  943. while (p < last_pos) {
  944. if (g_ascii_isspace(*p))
  945. p++;
  946. else
  947. break;
  948. }
  949. if (strchr(quote_chars, *p))
  950. quote_level++;
  951. else if (*p != '-' && !g_ascii_isspace(*p) && p <= last_pos) {
  952. /* any characters are allowed except '-','<' and space */
  953. while (*p != '-' && *p != '<'
  954. && !strchr(quote_chars, *p)
  955. && !g_ascii_isspace(*p)
  956. && p < last_pos)
  957. p++;
  958. if (strchr(quote_chars, *p))
  959. quote_level++;
  960. else
  961. break;
  962. }
  963. p++;
  964. }
  965. return quote_level;
  966. }
  967. gint check_line_length(const gchar *str, gint max_chars, gint *line)
  968. {
  969. const gchar *p = str, *q;
  970. gint cur_line = 0, len;
  971. while ((q = strchr(p, '\n')) != NULL) {
  972. len = q - p + 1;
  973. if (len > max_chars) {
  974. if (line)
  975. *line = cur_line;
  976. return -1;
  977. }
  978. p = q + 1;
  979. ++cur_line;
  980. }
  981. len = strlen(p);
  982. if (len > max_chars) {
  983. if (line)
  984. *line = cur_line;
  985. return -1;
  986. }
  987. return 0;
  988. }
  989. const gchar * line_has_quote_char(const gchar * str, const gchar *quote_chars)
  990. {
  991. gchar * position = NULL;
  992. gchar * tmp_pos = NULL;
  993. int i;
  994. if (quote_chars == NULL)
  995. return FALSE;
  996. for (i = 0; i < strlen(quote_chars); i++) {
  997. tmp_pos = strchr (str, quote_chars[i]);
  998. if(position == NULL
  999. || (tmp_pos != NULL && position >= tmp_pos) )
  1000. position = tmp_pos;
  1001. }
  1002. return position;
  1003. }
  1004. static gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
  1005. {
  1006. register guint haystack_len, needle_len;
  1007. gboolean in_squote = FALSE, in_dquote = FALSE;
  1008. haystack_len = strlen(haystack);
  1009. needle_len = strlen(needle);
  1010. if (haystack_len < needle_len || needle_len == 0)
  1011. return NULL;
  1012. while (haystack_len >= needle_len) {
  1013. if (!in_squote && !in_dquote &&
  1014. !strncmp(haystack, needle, needle_len))
  1015. return (gchar *)haystack;
  1016. /* 'foo"bar"' -> foo"bar"
  1017. "foo'bar'" -> foo'bar' */
  1018. if (*haystack == '\'') {
  1019. if (in_squote)
  1020. in_squote = FALSE;
  1021. else if (!in_dquote)
  1022. in_squote = TRUE;
  1023. } else if (*haystack == '\"') {
  1024. if (in_dquote)
  1025. in_dquote = FALSE;
  1026. else if (!in_squote)
  1027. in_dquote = TRUE;
  1028. }
  1029. haystack++;
  1030. haystack_len--;
  1031. }
  1032. return NULL;
  1033. }
  1034. gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
  1035. gint max_tokens)
  1036. {
  1037. GSList *string_list = NULL, *slist;
  1038. gchar **str_array, *s, *new_str;
  1039. guint i, n = 1, len;
  1040. cm_return_val_if_fail(str != NULL, NULL);
  1041. cm_return_val_if_fail(delim != NULL, NULL);
  1042. if (max_tokens < 1)
  1043. max_tokens = G_MAXINT;
  1044. s = strstr_with_skip_quote(str, delim);
  1045. if (s) {
  1046. guint delimiter_len = strlen(delim);
  1047. do {
  1048. len = s - str;
  1049. new_str = g_strndup(str, len);
  1050. if (new_str[0] == '\'' || new_str[0] == '\"') {
  1051. if (new_str[len - 1] == new_str[0]) {
  1052. new_str[len - 1] = '\0';
  1053. memmove(new_str, new_str + 1, len - 1);
  1054. }
  1055. }
  1056. string_list = g_slist_prepend(string_list, new_str);
  1057. n++;
  1058. str = s + delimiter_len;
  1059. s = strstr_with_skip_quote(str, delim);
  1060. } while (--max_tokens && s);
  1061. }
  1062. if (*str) {
  1063. new_str = g_strdup(str);
  1064. if (new_str[0] == '\'' || new_str[0] == '\"') {
  1065. len = strlen(str);
  1066. if (new_str[len - 1] == new_str[0]) {
  1067. new_str[len - 1] = '\0';
  1068. memmove(new_str, new_str + 1, len - 1);
  1069. }
  1070. }
  1071. string_list = g_slist_prepend(string_list, new_str);
  1072. n++;
  1073. }
  1074. str_array = g_new(gchar*, n);
  1075. i = n - 1;
  1076. str_array[i--] = NULL;
  1077. for (slist = string_list; slist; slist = slist->next)
  1078. str_array[i--] = slist->data;
  1079. g_slist_free(string_list);
  1080. return str_array;
  1081. }
  1082. gchar *get_abbrev_newsgroup_name(const gchar *group, gint len)
  1083. {
  1084. gchar *abbrev_group;
  1085. gchar *ap;
  1086. const gchar *p = group;
  1087. const gchar *last;
  1088. cm_return_val_if_fail(group != NULL, NULL);
  1089. last = group + strlen(group);
  1090. abbrev_group = ap = g_malloc(strlen(group) + 1);
  1091. while (*p) {
  1092. while (*p == '.')
  1093. *ap++ = *p++;
  1094. if ((ap - abbrev_group) + (last - p) > len && strchr(p, '.')) {
  1095. *ap++ = *p++;
  1096. while (*p != '.') p++;
  1097. } else {
  1098. strcpy(ap, p);
  1099. return abbrev_group;
  1100. }
  1101. }
  1102. *ap = '\0';
  1103. return abbrev_group;
  1104. }
  1105. gchar *trim_string(const gchar *str, gint len)
  1106. {
  1107. const gchar *p = str;
  1108. gint mb_len;
  1109. gchar *new_str;
  1110. gint new_len = 0;
  1111. if (!str) return NULL;
  1112. if (strlen(str) <= len)
  1113. return g_strdup(str);
  1114. if (g_utf8_validate(str, -1, NULL) == FALSE)
  1115. return g_strdup(str);
  1116. while (*p != '\0') {
  1117. mb_len = g_utf8_skip[*(guchar *)p];
  1118. if (mb_len == 0)
  1119. break;
  1120. else if (new_len + mb_len > len)
  1121. break;
  1122. new_len += mb_len;
  1123. p += mb_len;
  1124. }
  1125. Xstrndup_a(new_str, str, new_len, return g_strdup(str));
  1126. return g_strconcat(new_str, "...", NULL);
  1127. }
  1128. GList *uri_list_extract_filenames(const gchar *uri_list)
  1129. {
  1130. GList *result = NULL;
  1131. const gchar *p, *q;
  1132. gchar *escaped_utf8uri;
  1133. p = uri_list;
  1134. while (p) {
  1135. if (*p != '#') {
  1136. while (g_ascii_isspace(*p)) p++;
  1137. if (!strncmp(p, "file:", 5)) {
  1138. q = p;
  1139. q += 5;
  1140. while (*q && *q != '\n' && *q != '\r') q++;
  1141. if (q > p) {
  1142. gchar *file, *locale_file = NULL;
  1143. q--;
  1144. while (q > p && g_ascii_isspace(*q))
  1145. q--;
  1146. Xalloca(escaped_utf8uri, q - p + 2,
  1147. return result);
  1148. Xalloca(file, q - p + 2,
  1149. return result);
  1150. *file = '\0';
  1151. strncpy(escaped_utf8uri, p, q - p + 1);
  1152. escaped_utf8uri[q - p + 1] = '\0';
  1153. decode_uri(file, escaped_utf8uri);
  1154. /*
  1155. * g_filename_from_uri() rejects escaped/locale encoded uri
  1156. * string which come from Nautilus.
  1157. */
  1158. #ifndef G_OS_WIN32
  1159. if (g_utf8_validate(file, -1, NULL))
  1160. locale_file
  1161. = conv_codeset_strdup(
  1162. file + 5,
  1163. CS_UTF_8,
  1164. conv_get_locale_charset_str());
  1165. if (!locale_file)
  1166. locale_file = g_strdup(file + 5);
  1167. #else
  1168. locale_file = g_filename_from_uri(file, NULL, NULL);
  1169. #endif
  1170. result = g_list_append(result, locale_file);
  1171. }
  1172. }
  1173. }
  1174. p = strchr(p, '\n');
  1175. if (p) p++;
  1176. }
  1177. return result;
  1178. }
  1179. /* Converts two-digit hexadecimal to decimal. Used for unescaping escaped
  1180. * characters
  1181. */
  1182. static gint axtoi(const gchar *hexstr)
  1183. {
  1184. gint hi, lo, result;
  1185. hi = hexstr[0];
  1186. if ('0' <= hi && hi <= '9') {
  1187. hi -= '0';
  1188. } else
  1189. if ('a' <= hi && hi <= 'f') {
  1190. hi -= ('a' - 10);
  1191. } else
  1192. if ('A' <= hi && hi <= 'F') {
  1193. hi -= ('A' - 10);
  1194. }
  1195. lo = hexstr[1];
  1196. if ('0' <= lo && lo <= '9') {
  1197. lo -= '0';
  1198. } else
  1199. if ('a' <= lo && lo <= 'f') {
  1200. lo -= ('a'-10);
  1201. } else
  1202. if ('A' <= lo && lo <= 'F') {
  1203. lo -= ('A' - 10);
  1204. }
  1205. result = lo + (16 * hi);
  1206. return result;
  1207. }
  1208. gboolean is_uri_string(const gchar *str)
  1209. {
  1210. while (str && *str && g_ascii_isspace(*str))
  1211. str++;
  1212. return (g_ascii_strncasecmp(str, "http://", 7) == 0 ||
  1213. g_ascii_strncasecmp(str, "https://", 8) == 0 ||
  1214. g_ascii_strncasecmp(str, "ftp://", 6) == 0 ||
  1215. g_ascii_strncasecmp(str, "www.", 4) == 0);
  1216. }
  1217. gchar *get_uri_path(const gchar *uri)
  1218. {
  1219. while (uri && *uri && g_ascii_isspace(*uri))
  1220. uri++;
  1221. if (g_ascii_strncasecmp(uri, "http://", 7) == 0)
  1222. return (gchar *)(uri + 7);
  1223. else if (g_ascii_strncasecmp(uri, "https://", 8) == 0)
  1224. return (gchar *)(uri + 8);
  1225. else if (g_ascii_strncasecmp(uri, "ftp://", 6) == 0)
  1226. return (gchar *)(uri + 6);
  1227. else
  1228. return (gchar *)uri;
  1229. }
  1230. gint get_uri_len(const gchar *str)
  1231. {
  1232. const gchar *p;
  1233. if (is_uri_string(str)) {
  1234. for (p = str; *p != '\0'; p++) {
  1235. if (!g_ascii_isgraph(*p) || strchr("()<>\"", *p))
  1236. break;
  1237. }
  1238. return p - str;
  1239. }
  1240. return 0;
  1241. }
  1242. /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
  1243. * plusses, and escape characters are used)
  1244. */
  1245. void decode_uri_with_plus(gchar *decoded_uri, const gchar *encoded_uri, gboolean with_plus)
  1246. {
  1247. gchar *dec = decoded_uri;
  1248. const gchar *enc = encoded_uri;
  1249. while (*enc) {
  1250. if (*enc == '%') {
  1251. enc++;
  1252. if (isxdigit((guchar)enc[0]) &&
  1253. isxdigit((guchar)enc[1])) {
  1254. *dec = axtoi(enc);
  1255. dec++;
  1256. enc += 2;
  1257. }
  1258. } else {
  1259. if (with_plus && *enc == '+')
  1260. *dec = ' ';
  1261. else
  1262. *dec = *enc;
  1263. dec++;
  1264. enc++;
  1265. }
  1266. }
  1267. *dec = '\0';
  1268. }
  1269. void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
  1270. {
  1271. decode_uri_with_plus(decoded_uri, encoded_uri, TRUE);
  1272. }
  1273. static gchar *decode_uri_gdup(const gchar *encoded_uri)
  1274. {
  1275. gchar *buffer = g_malloc(strlen(encoded_uri)+1);
  1276. decode_uri_with_plus(buffer, encoded_uri, FALSE);
  1277. return buffer;
  1278. }
  1279. gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc, gchar **bcc,
  1280. gchar **subject, gchar **body, gchar ***attach, gchar **inreplyto)
  1281. {
  1282. gchar *tmp_mailto;
  1283. gchar *p;
  1284. const gchar *forbidden_uris[] = { ".gnupg/",
  1285. "/etc/passwd",
  1286. "/etc/shadow",
  1287. ".ssh/",
  1288. "../",
  1289. NULL };
  1290. gint num_attach = 0;
  1291. gchar **my_att = NULL;
  1292. Xstrdup_a(tmp_mailto, mailto, return -1);
  1293. if (!strncmp(tmp_mailto, "mailto:", 7))
  1294. tmp_mailto += 7;
  1295. p = strchr(tmp_mailto, '?');
  1296. if (p) {
  1297. *p = '\0';
  1298. p++;
  1299. }
  1300. if (to && !*to)
  1301. *to = decode_uri_gdup(tmp_mailto);
  1302. my_att = g_malloc(sizeof(char *));
  1303. my_att[0] = NULL;
  1304. while (p) {
  1305. gchar *field, *value;
  1306. field = p;
  1307. p = strchr(p, '=');
  1308. if (!p) break;
  1309. *p = '\0';
  1310. p++;
  1311. value = p;
  1312. p = strchr(p, '&');
  1313. if (p) {
  1314. *p = '\0';
  1315. p++;
  1316. }
  1317. if (*value == '\0') continue;
  1318. if (from && !g_ascii_strcasecmp(field, "from")) {
  1319. if (!*from) {
  1320. *from = decode_uri_gdup(value);
  1321. } else {
  1322. gchar *tmp = decode_uri_gdup(value);
  1323. gchar *new_from = g_strdup_printf("%s, %s", *from, tmp);
  1324. g_free(*from);
  1325. *from = new_from;
  1326. }
  1327. } else if (cc && !g_ascii_strcasecmp(field, "cc")) {
  1328. if (!*cc) {
  1329. *cc = decode_uri_gdup(value);
  1330. } else {
  1331. gchar *tmp = decode_uri_gdup(value);
  1332. gchar *new_cc = g_strdup_printf("%s, %s", *cc, tmp);
  1333. g_free(*cc);
  1334. *cc = new_cc;
  1335. }
  1336. } else if (bcc && !g_ascii_strcasecmp(field, "bcc")) {
  1337. if (!*bcc) {
  1338. *bcc = decode_uri_gdup(value);
  1339. } else {
  1340. gchar *tmp = decode_uri_gdup(value);
  1341. gchar *new_bcc = g_strdup_printf("%s, %s", *bcc, tmp);
  1342. g_free(*bcc);
  1343. *bcc = new_bcc;
  1344. }
  1345. } else if (subject && !*subject &&
  1346. !g_ascii_strcasecmp(field, "subject")) {
  1347. *subject = decode_uri_gdup(value);
  1348. } else if (body && !*body && !g_ascii_strcasecmp(field, "body")) {
  1349. *body = decode_uri_gdup(value);
  1350. } else if (body && !*body && !g_ascii_strcasecmp(field, "insert")) {
  1351. gchar *tmp = decode_uri_gdup(value);
  1352. if (!g_file_get_contents(tmp, body, NULL, NULL)) {
  1353. g_warning("Error: couldn't set insert file '%s' in body\n", value);
  1354. }
  1355. g_free(tmp);
  1356. tmp = NULL;
  1357. } else if (attach && !g_ascii_strcasecmp(field, "attach")) {
  1358. int i = 0;
  1359. gchar *tmp = decode_uri_gdup(value);
  1360. for (; forbidden_uris[i]; i++) {
  1361. if (strstr(tmp, forbidden_uris[i])) {
  1362. g_print("Refusing to attach '%s', potential private data leak\n",
  1363. tmp);
  1364. g_free(tmp);
  1365. tmp = NULL;
  1366. break;
  1367. }
  1368. }
  1369. if (tmp) {
  1370. /* attach is correct */
  1371. num_attach++;
  1372. my_att = g_realloc(my_att, (sizeof(char *))*(num_attach+1));
  1373. my_att[num_attach-1] = tmp;
  1374. my_att[num_attach] = NULL;
  1375. }
  1376. } else if (inreplyto && !*inreplyto &&
  1377. !g_ascii_strcasecmp(field, "in-reply-to")) {
  1378. *inreplyto = decode_uri_gdup(value);
  1379. }
  1380. }
  1381. if (attach)
  1382. *attach = my_att;
  1383. return 0;
  1384. }
  1385. #ifdef G_OS_WIN32
  1386. #include <windows.h>
  1387. #ifndef CSIDL_APPDATA
  1388. #define CSIDL_APPDATA 0x001a
  1389. #endif
  1390. #ifndef CSIDL_LOCAL_APPDATA
  1391. #define CSIDL_LOCAL_APPDATA 0x001c
  1392. #endif
  1393. #ifndef CSIDL_FLAG_CREATE
  1394. #define CSIDL_FLAG_CREATE 0x8000
  1395. #endif
  1396. #define DIM(v) (sizeof(v)/sizeof((v)[0]))
  1397. #define RTLD_LAZY 0
  1398. const char *
  1399. w32_strerror (int w32_errno)
  1400. {
  1401. static char strerr[256];
  1402. int ec = (int)GetLastError ();
  1403. if (w32_errno == 0)
  1404. w32_errno = ec;
  1405. FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, w32_errno,
  1406. MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
  1407. strerr, DIM (strerr)-1, NULL);
  1408. return strerr;
  1409. }
  1410. static __inline__ void *
  1411. dlopen (const char * name, int flag)
  1412. {
  1413. void * hd = LoadLibrary (name);
  1414. return hd;
  1415. }
  1416. static __inline__ void *
  1417. dlsym (void * hd, const char * sym)
  1418. {
  1419. if (hd && sym)
  1420. {
  1421. void * fnc = GetProcAddress (hd, sym);
  1422. if (!fnc)
  1423. return NULL;
  1424. return fnc;
  1425. }
  1426. return NULL;
  1427. }
  1428. static __inline__ const char *
  1429. dlerror (void)
  1430. {
  1431. return w32_strerror (0);
  1432. }
  1433. static __inline__ int
  1434. dlclose (void * hd)
  1435. {
  1436. if (hd)
  1437. {
  1438. FreeLibrary (hd);
  1439. return 0;
  1440. }
  1441. return -1;
  1442. }
  1443. static HRESULT
  1444. w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e)
  1445. {
  1446. static int initialized;
  1447. static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPSTR);
  1448. if (!initialized)
  1449. {
  1450. static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
  1451. void *handle;
  1452. int i;
  1453. initialized = 1;
  1454. for (i=0, handle = NULL; !handle && dllnames[i]; i++)
  1455. {
  1456. handle = dlopen (dllnames[i], RTLD_LAZY);
  1457. if (handle)
  1458. {
  1459. func = dlsym (handle, "SHGetFolderPathW");
  1460. if (!func)
  1461. {
  1462. dlclose (handle);
  1463. handle = NULL;
  1464. }
  1465. }
  1466. }
  1467. }
  1468. if (func)
  1469. return func (a,b,c,d,e);
  1470. else
  1471. return -1;
  1472. }
  1473. /* Returns a static string with the directroy from which the module
  1474. has been loaded. Returns an empty string on error. */
  1475. static char *w32_get_module_dir(void)
  1476. {
  1477. static char *moddir;
  1478. if (!moddir) {
  1479. char name[MAX_PATH+10];
  1480. char *p;
  1481. if ( !GetModuleFileNameA (0, name, sizeof (name)-10) )
  1482. *name = 0;
  1483. else {
  1484. p = strrchr (name, '\\');
  1485. if (p)
  1486. *p = 0;
  1487. else
  1488. *name = 0;
  1489. }
  1490. moddir = g_strdup (name);
  1491. }
  1492. return moddir;
  1493. }
  1494. #endif /* G_OS_WIN32 */
  1495. /* Return a static string with the locale dir. */
  1496. const gchar *get_locale_dir(void)
  1497. {
  1498. static gchar *loc_dir;
  1499. #ifdef G_OS_WIN32
  1500. if (!loc_dir)
  1501. loc_dir = g_strconcat(w32_get_module_dir(), G_DIR_SEPARATOR_S,
  1502. "\\share\\locale", NULL);
  1503. #endif
  1504. if (!loc_dir)
  1505. loc_dir = LOCALEDIR;
  1506. return loc_dir;
  1507. }
  1508. const gchar *get_home_dir(void)
  1509. {
  1510. #ifdef G_OS_WIN32
  1511. static char home_dir_utf16[MAX_PATH] = "";
  1512. static gchar *home_dir_utf8 = NULL;
  1513. if (home_dir_utf16[0] == '\0') {
  1514. if (w32_shgetfolderpath
  1515. (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
  1516. NULL, 0, home_dir_utf16) < 0)
  1517. strcpy (home_dir_utf16, "C:\\Sylpheed");
  1518. home_dir_utf8 = g_utf16_to_utf8 ((const gunichar *)home_dir_utf16, -1, NULL, NULL, NULL);
  1519. }
  1520. return home_dir_utf8;
  1521. #else
  1522. static const gchar *homeenv = NULL;
  1523. if (homeenv)
  1524. return homeenv;
  1525. if (!homeenv && g_getenv("HOME") != NULL)
  1526. homeenv = g_strdup(g_getenv("HOME"));
  1527. if (!homeenv)
  1528. homeenv = g_get_home_dir();
  1529. return homeenv;
  1530. #endif
  1531. }
  1532. static gchar *claws_rc_dir = NULL;
  1533. static gboolean rc_dir_alt = FALSE;
  1534. const gchar *get_rc_dir(void)
  1535. {
  1536. if (!claws_rc_dir)
  1537. claws_rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
  1538. RC_DIR, NULL);
  1539. return claws_rc_dir;
  1540. }
  1541. void set_rc_dir(const gchar *dir)
  1542. {
  1543. if (claws_rc_dir != NULL) {
  1544. g_print("Error: rc_dir already set\n");
  1545. } else {
  1546. rc_dir_alt = TRUE;
  1547. if (g_path_is_absolute(dir))
  1548. claws_rc_dir = g_strdup(dir);
  1549. else {
  1550. claws_rc_dir = g_strconcat(g_get_current_dir(),
  1551. G_DIR_SEPARATOR_S, dir, NULL);
  1552. }
  1553. debug_print("set rc_dir to %s\n", claws_rc_dir);
  1554. if (!is_dir_exist(claws_rc_dir)) {
  1555. if (make_dir_hier(claws_rc_dir) != 0) {
  1556. g_print("Error: can't create %s\n",
  1557. claws_rc_dir);
  1558. }
  1559. }
  1560. }
  1561. }
  1562. gboolean rc_dir_is_alt(void) {
  1563. return rc_dir_alt;
  1564. }
  1565. const gchar *get_mail_base_dir(void)
  1566. {
  1567. return get_home_dir();
  1568. }
  1569. #ifdef MAEMO
  1570. const gchar *prefs_common_get_data_root(void);
  1571. gchar *last_data_root = NULL;
  1572. #endif
  1573. const gchar *get_news_cache_dir(void)
  1574. {
  1575. static gchar *news_cache_dir = NULL;
  1576. #ifdef MAEMO
  1577. const gchar *data_root = prefs_common_get_data_root();
  1578. if (strcmp2(data_root, last_data_root)) {
  1579. g_free(news_cache_dir);
  1580. news_cache_dir = NULL;
  1581. }
  1582. #endif
  1583. if (!news_cache_dir)
  1584. #ifndef MAEMO
  1585. news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
  1586. NEWS_CACHE_DIR, NULL);
  1587. #else
  1588. {
  1589. if (data_root) {
  1590. news_cache_dir = g_strconcat(data_root, G_DIR_SEPARATOR_S,
  1591. "Claws", G_DIR_SEPARATOR_S,
  1592. g_get_user_name(), G_DIR_SEPARATOR_S,
  1593. NEWS_CACHE_DIR, NULL);
  1594. g_free(last_data_root);
  1595. last_data_root = g_strdup(last_data_root);
  1596. } else {
  1597. news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
  1598. NEWS_CACHE_DIR, NULL);
  1599. g_free(last_data_root);
  1600. last_data_root = NULL;
  1601. }
  1602. }
  1603. #endif
  1604. return news_cache_dir;
  1605. }
  1606. const gchar *get_imap_cache_dir(void)
  1607. {
  1608. static gchar *imap_cache_dir = NULL;
  1609. #ifdef MAEMO
  1610. const gchar *data_root = prefs_common_get_data_root();
  1611. if (strcmp2(data_root, last_data_root)) {
  1612. g_free(imap_cache_dir);
  1613. imap_cache_dir = NULL;
  1614. }
  1615. #endif
  1616. if (!imap_cache_dir)
  1617. #ifndef MAEMO
  1618. imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
  1619. IMAP_CACHE_DIR, NULL);
  1620. #else
  1621. {
  1622. if (data_root) {
  1623. imap_cache_dir = g_strconcat(data_root, G_DIR_SEPARATOR_S,
  1624. "Claws", G_DIR_SEPARATOR_S,
  1625. g_get_user_name(), G_DIR_SEPARATOR_S,
  1626. IMAP_CACHE_DIR, NULL);
  1627. g_free(last_data_root);
  1628. last_data_root = g_strdup(last_data_root);
  1629. } else {
  1630. imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
  1631. IMAP_CACHE_DIR, NULL);
  1632. g_free(last_data_root);
  1633. last_data_root = NULL;
  1634. }
  1635. }
  1636. #endif
  1637. return imap_cache_dir;
  1638. }
  1639. const gchar *get_mime_tmp_dir(void)
  1640. {
  1641. static gchar *mime_tmp_dir = NULL;
  1642. if (!mime_tmp_dir)
  1643. mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
  1644. MIME_TMP_DIR, NULL);
  1645. return mime_tmp_dir;
  1646. }
  1647. const gchar *get_template_dir(void)
  1648. {
  1649. static gchar *template_dir = NULL;
  1650. if (!template_dir)
  1651. template_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
  1652. TEMPLATE_DIR, NULL);
  1653. return template_dir;
  1654. }
  1655. #ifdef G_OS_WIN32
  1656. const gchar *get_cert_file(void)
  1657. {
  1658. const gchar *cert_file = NULL;
  1659. if (!cert_file)
  1660. cert_file = g_strconcat(w32_get_module_dir(),
  1661. "\\share\\claws-mail\\",
  1662. "ca-certificates.crt",
  1663. NULL);
  1664. return cert_file;
  1665. }
  1666. #endif
  1667. /* Return the filepath of the claws-mail.desktop file */
  1668. const gchar *get_desktop_file(void)
  1669. {
  1670. #ifdef DESKTOPFILEPATH
  1671. return DESKTOPFILEPATH;
  1672. #else
  1673. return NULL;
  1674. #endif
  1675. }
  1676. /* Return the default directory for Plugins. */
  1677. const gchar *get_plugin_dir(void)
  1678. {
  1679. #ifdef G_OS_WIN32
  1680. static gchar *plugin_dir = NULL;
  1681. if (!plugin_dir)
  1682. plugin_dir = g_strconcat(w32_get_module_dir(),
  1683. "\\lib\\claws-mail\\plugins\\",
  1684. NULL);
  1685. return plugin_dir;
  1686. #else
  1687. if (is_dir_exist(PLUGINDIR))
  1688. return PLUGINDIR;
  1689. else {
  1690. static gchar *plugin_dir = NULL;
  1691. if (!plugin_dir)
  1692. plugin_dir = g_strconcat(get_rc_dir(),
  1693. G_DIR_SEPARATOR_S, "plugins",
  1694. G_DIR_SEPARATOR_S, NULL);
  1695. return plugin_dir;
  1696. }
  1697. #endif
  1698. }
  1699. #ifdef G_OS_WIN32
  1700. /* Return the default directory for Themes. */
  1701. const gchar *get_themes_dir(void)
  1702. {
  1703. static gchar *themes_dir = NULL;
  1704. if (!themes_dir)
  1705. themes_dir = g_strconcat(w32_get_module_dir(),
  1706. "\\share\\claws-mail\\themes",
  1707. NULL);
  1708. return themes_dir;
  1709. }
  1710. #endif
  1711. const gchar *get_tmp_dir(void)
  1712. {
  1713. static gchar *tmp_dir = NULL;
  1714. if (!tmp_dir)
  1715. tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
  1716. TMP_DIR, NULL);
  1717. return tmp_dir;
  1718. }
  1719. gchar *get_tmp_file(void)
  1720. {
  1721. gchar *tmp_file;
  1722. static guint32 id = 0;
  1723. tmp_file = g_strdup_printf("%s%ctmpfile.%08x",
  1724. get_tmp_dir(), G_DIR_SEPARATOR, id++);
  1725. return tmp_file;
  1726. }
  1727. const gchar *get_domain_name(void)
  1728. {
  1729. #ifdef G_OS_UNIX
  1730. static gchar *domain_name = NULL;
  1731. if (!domain_name) {
  1732. struct hostent *hp;
  1733. char hostname[256];
  1734. if (gethostname(hostname, sizeof(hostname)) != 0) {
  1735. perror("gethostname");
  1736. domain_name = "unknown";
  1737. } else {
  1738. hostname[sizeof(hostname) - 1] = '\0';
  1739. if ((hp = my_gethostbyname(hostname)) == NULL) {
  1740. perror("gethostbyname");
  1741. domain_name = g_strdup(hostname);
  1742. } else {
  1743. domain_name = g_strdup(hp->h_name);
  1744. }
  1745. }
  1746. debug_print("domain name = %s\n", domain_name);
  1747. }
  1748. return domain_name;
  1749. #else
  1750. return "unknown";
  1751. #endif
  1752. }
  1753. off_t get_file_size(const gchar *file)
  1754. {
  1755. struct stat s;
  1756. if (g_stat(file, &s) < 0) {
  1757. FILE_OP_ERROR(file, "stat");
  1758. return -1;
  1759. }
  1760. return s.st_size;
  1761. }
  1762. time_t get_file_mtime(const gchar *file)
  1763. {
  1764. struct stat s;
  1765. if (g_stat(file, &s) < 0) {
  1766. FILE_OP_ERROR(file, "stat");
  1767. return -1;
  1768. }
  1769. return s.st_mtime;
  1770. }
  1771. off_t get_file_size_as_crlf(const gchar *file)
  1772. {
  1773. FILE *fp;
  1774. off_t size = 0;
  1775. gchar buf[BUFFSIZE];
  1776. if ((fp = g_fopen(file, "rb")) == NULL) {
  1777. FILE_OP_ERROR(file, "g_fopen");
  1778. return -1;
  1779. }
  1780. while (fgets(buf, sizeof(buf), fp) != NULL) {
  1781. strretchomp(buf);
  1782. size += strlen(buf) + 2;
  1783. }
  1784. if (ferror(fp)) {
  1785. FILE_OP_ERROR(file, "fgets");
  1786. size = -1;
  1787. }
  1788. fclose(fp);
  1789. return size;
  1790. }
  1791. gboolean file_exist(const gchar *file, gboolean allow_fifo)
  1792. {
  1793. struct stat s;
  1794. if (file == NULL)
  1795. return FALSE;
  1796. if (g_stat(file, &s) < 0) {
  1797. if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
  1798. return FALSE;
  1799. }
  1800. if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
  1801. return TRUE;
  1802. return FALSE;
  1803. }
  1804. /* Test on whether FILE is a relative file name. This is
  1805. * straightforward for Unix but more complex for Windows. */
  1806. gboolean is_relative_filename(const gchar *file)
  1807. {
  1808. if (!file)
  1809. return TRUE;
  1810. #ifdef G_OS_WIN32
  1811. if ( *file == '\\' && file[1] == '\\' && strchr (file+2, '\\') )
  1812. return FALSE; /* Prefixed with a hostname - this can't
  1813. * be a relative name. */
  1814. if ( ((*file >= 'a' && *file <= 'z')
  1815. || (*file >= 'A' && *file <= 'Z'))
  1816. && file[1] == ':')
  1817. file += 2; /* Skip drive letter. */
  1818. return !(*file == '\\' || *file == '/');
  1819. #else
  1820. return !(*file == G_DIR_SEPARATOR);
  1821. #endif
  1822. }
  1823. gboolean is_dir_exist(const gchar *dir)
  1824. {
  1825. if (dir == NULL)
  1826. return FALSE;
  1827. return g_file_test(dir, G_FILE_TEST_IS_DIR);
  1828. }
  1829. gboolean is_file_entry_exist(const gchar *file)
  1830. {
  1831. if (file == NULL)
  1832. return FALSE;
  1833. return g_file_test(file, G_FILE_TEST_EXISTS);
  1834. }
  1835. gboolean dirent_is_regular_file(struct dirent *d)
  1836. {
  1837. #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
  1838. if (d->d_type == DT_REG)
  1839. return TRUE;
  1840. else if (d->d_type != DT_UNKNOWN)
  1841. return FALSE;
  1842. #endif
  1843. return g_file_test(d->d_name, G_FILE_TEST_IS_REGULAR);
  1844. }
  1845. gint change_dir(const gchar *dir)
  1846. {
  1847. gchar *prevdir = NULL;
  1848. if (debug_mode)
  1849. prevdir = g_get_current_dir();
  1850. if (g_chdir(dir) < 0) {
  1851. FILE_OP_ERROR(dir, "chdir");
  1852. if (debug_mode) g_free(prevdir);
  1853. return -1;
  1854. } else if (debug_mode) {
  1855. gchar *cwd;
  1856. cwd = g_get_current_dir();
  1857. if (strcmp(prevdir, cwd) != 0)
  1858. g_print("current dir: %s\n", cwd);
  1859. g_free(cwd);
  1860. g_free(prevdir);
  1861. }
  1862. return 0;
  1863. }
  1864. gint make_dir(const gchar *dir)
  1865. {
  1866. if (g_mkdir(dir, S_IRWXU) < 0) {
  1867. FILE_OP_ERROR(dir, "mkdir");
  1868. return -1;
  1869. }
  1870. if (g_chmod(dir, S_IRWXU) < 0)
  1871. FILE_OP_ERROR(dir, "chmod");
  1872. return 0;
  1873. }
  1874. gint make_dir_hier(const gchar *dir)
  1875. {
  1876. gchar *parent_dir;
  1877. const gchar *p;
  1878. for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
  1879. parent_dir = g_strndup(dir, p - dir);
  1880. if (*parent_dir != '\0') {
  1881. if (!is_dir_exist(parent_dir)) {
  1882. if (make_dir(parent_dir) < 0) {
  1883. g_free(parent_dir);
  1884. return -1;
  1885. }
  1886. }
  1887. }
  1888. g_free(parent_dir);
  1889. }
  1890. if (!is_dir_exist(dir)) {
  1891. if (make_dir(dir) < 0)
  1892. return -1;
  1893. }
  1894. return 0;
  1895. }
  1896. gint remove_all_files(const gchar *dir)
  1897. {
  1898. GDir *dp;
  1899. const gchar *dir_name;
  1900. gchar *prev_dir;
  1901. prev_dir = g_get_current_dir();
  1902. if (g_chdir(dir) < 0) {
  1903. FILE_OP_ERROR(dir, "chdir");
  1904. g_free(prev_dir);
  1905. return -1;
  1906. }
  1907. if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
  1908. g_warning("failed to open directory: %s\n", dir);
  1909. g_free(prev_dir);
  1910. return -1;
  1911. }
  1912. while ((dir_name = g_dir_read_name(dp)) != NULL) {
  1913. if (claws_unlink(dir_name) < 0)
  1914. FILE_OP_ERROR(dir_name, "unlink");
  1915. }
  1916. g_dir_close(dp);
  1917. if (g_chdir(prev_dir) < 0) {
  1918. FILE_OP_ERROR(prev_dir, "chdir");
  1919. g_free(prev_dir);
  1920. return -1;
  1921. }
  1922. g_free(prev_dir);
  1923. return 0;
  1924. }
  1925. gint remove_numbered_files(const gchar *dir, guint first, guint last)
  1926. {
  1927. GDir *dp;
  1928. const gchar *dir_name;
  1929. gchar *prev_dir;
  1930. gint file_no;
  1931. if (first == last) {
  1932. /* Skip all the dir reading part. */
  1933. gchar *filename = g_strdup_printf("%s%s%u", dir, G_DIR_SEPARATOR_S, first);
  1934. if (claws_unlink(filename) < 0) {
  1935. FILE_OP_ERROR(filename, "unlink");
  1936. g_free(filename);
  1937. return -1;
  1938. }
  1939. g_free(filename);
  1940. return 0;
  1941. }
  1942. prev_dir = g_get_current_dir();
  1943. if (g_chdir(dir) < 0) {
  1944. FILE_OP_ERROR(dir, "chdir");
  1945. g_free(prev_dir);
  1946. return -1;
  1947. }
  1948. if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
  1949. g_warning("failed to open directory: %s\n", dir);
  1950. g_free(prev_dir);
  1951. return -1;
  1952. }
  1953. while ((dir_name = g_dir_read_name(dp)) != NULL) {
  1954. file_no = to_number(dir_name);
  1955. if (file_no > 0 && first <= file_no && file_no <= last) {
  1956. if (is_dir_exist(dir_name))
  1957. continue;
  1958. if (claws_unlink(dir_name) < 0)
  1959. FILE_OP_ERROR(dir_name, "unlink");
  1960. }
  1961. }
  1962. g_dir_close(dp);
  1963. if (g_chdir(prev_dir) < 0) {
  1964. FILE_OP_ERROR(prev_dir, "chdir");
  1965. g_free(prev_dir);
  1966. return -1;
  1967. }
  1968. g_free(prev_dir);
  1969. return 0;
  1970. }
  1971. gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
  1972. {
  1973. GDir *dp;
  1974. const gchar *dir_name;
  1975. gchar *prev_dir;
  1976. gint file_no;
  1977. prev_dir = g_get_current_dir();
  1978. if (g_chdir(dir) < 0) {
  1979. FILE_OP_ERROR(dir, "chdir");
  1980. g_free(prev_dir);
  1981. return -1;
  1982. }
  1983. if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
  1984. FILE_OP_ERROR(dir, "opendir");
  1985. g_free(prev_dir);
  1986. return -1;
  1987. }
  1988. while ((dir_name = g_dir_read_name(dp)) != NULL) {
  1989. file_no = to_number(dir_name);
  1990. if (file_no > 0 && (g_slist_find(numberlist, GINT_TO_POINTER(file_no)) == NULL)) {
  1991. debug_print("removing unwanted file %d from %s\n", file_no, dir);
  1992. if (is_dir_exist(dir_name))
  1993. continue;
  1994. if (claws_unlink(dir_name) < 0)
  1995. FILE_OP_ERROR(dir_name, "unlink");
  1996. }
  1997. }
  1998. g_dir_close(dp);
  1999. if (g_chdir(prev_dir) < 0) {
  2000. FILE_OP_ERROR(prev_dir, "chdir");
  2001. g_free(prev_dir);
  2002. return -1;
  2003. }
  2004. g_free(prev_dir);
  2005. return 0;
  2006. }
  2007. gint remove_all_numbered_files(const gchar *dir)
  2008. {
  2009. return remove_numbered_files(dir, 0, UINT_MAX);
  2010. }
  2011. gint remove_dir_recursive(const gchar *dir)
  2012. {
  2013. struct stat s;
  2014. GDir *dp;
  2015. const gchar *dir_name;
  2016. gchar *prev_dir;
  2017. if (g_stat(dir, &s) < 0) {
  2018. FILE_OP_ERROR(dir, "stat");
  2019. if (ENOENT == errno) return 0;
  2020. return -1;
  2021. }
  2022. if (!S_ISDIR(s.st_mode)) {
  2023. if (claws_unlink(dir) < 0) {
  2024. FILE_OP_ERROR(dir, "unlink");
  2025. return -1;
  2026. }
  2027. return 0;
  2028. }
  2029. prev_dir = g_get_current_dir();
  2030. /* g_print("prev_dir = %s\n", prev_dir); */
  2031. if (!path_cmp(prev_dir, dir)) {
  2032. g_free(prev_dir);
  2033. if (g_chdir("..") < 0) {
  2034. FILE_OP_ERROR(dir, "chdir");
  2035. return -1;
  2036. }
  2037. prev_dir = g_get_current_dir();
  2038. }
  2039. if (g_chdir(dir) < 0) {
  2040. FILE_OP_ERROR(dir, "chdir");
  2041. g_free(prev_dir);
  2042. return -1;
  2043. }
  2044. if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
  2045. g_warning("failed to open directory: %s\n", dir);
  2046. g_chdir(prev_dir);
  2047. g_free(prev_dir);
  2048. return -1;
  2049. }
  2050. /* remove all files in the directory */
  2051. while ((dir_name = g_dir_read_name(dp)) != NULL) {
  2052. /* g_print("removing %s\n", dir_name); */
  2053. if (is_dir_exist(dir_name)) {
  2054. if (remove_dir_recursive(dir_name) < 0) {
  2055. g_warning("can't remove directory\n");
  2056. return -1;
  2057. }
  2058. } else {
  2059. if (claws_unlink(dir_name) < 0)
  2060. FILE_OP_ERROR(dir_name, "unlink");
  2061. }
  2062. }
  2063. g_dir_close(dp);
  2064. if (g_chdir(prev_dir) < 0) {
  2065. FILE_OP_ERROR(prev_dir, "chdir");
  2066. g_free(prev_dir);
  2067. return -1;
  2068. }
  2069. g_free(prev_dir);
  2070. if (g_rmdir(dir) < 0) {
  2071. FILE_OP_ERROR(dir, "rmdir");
  2072. return -1;
  2073. }
  2074. return 0;
  2075. }
  2076. gint rename_force(const gchar *oldpath, const gchar *newpath)
  2077. {
  2078. #ifndef G_OS_UNIX
  2079. if (!is_file_entry_exist(oldpath)) {
  2080. errno = ENOENT;
  2081. return -1;
  2082. }
  2083. if (is_file_exist(newpath)) {
  2084. if (claws_unlink(newpath) < 0)
  2085. FILE_OP_ERROR(newpath, "unlink");
  2086. }
  2087. #endif
  2088. return g_rename(oldpath, newpath);
  2089. }
  2090. /*
  2091. * Append src file body to the tail of dest file.
  2092. * Now keep_backup has no effects.
  2093. */
  2094. gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
  2095. {
  2096. FILE *src_fp, *dest_fp;
  2097. gint n_read;
  2098. gchar buf[BUFSIZ];
  2099. gboolean err = FALSE;
  2100. if ((src_fp = g_fopen(src, "rb")) == NULL) {
  2101. FILE_OP_ERROR(src, "g_fopen");
  2102. return -1;
  2103. }
  2104. if ((dest_fp = g_fopen(dest, "ab")) == NULL) {
  2105. FILE_OP_ERROR(dest, "g_fopen");
  2106. fclose(src_fp);
  2107. return -1;
  2108. }
  2109. if (change_file_mode_rw(dest_fp, dest) < 0) {
  2110. FILE_OP_ERROR(dest, "chmod");
  2111. g_warning("can't change file mode\n");
  2112. }
  2113. while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
  2114. if (n_read < sizeof(buf) && ferror(src_fp))
  2115. break;
  2116. if (fwrite(b

Large files files are truncated, but you can click here to view the full file