/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
- /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Hiroyuki Yamamoto & The Claws Mail Team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- #ifdef HAVE_CONFIG_H
- # include "config.h"
- #endif
- #include "defs.h"
- #include <glib.h>
- #include <glib/gi18n.h>
- #ifdef USE_PTHREAD
- #include <pthread.h>
- #endif
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <errno.h>
- #include <sys/param.h>
- #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
- # include <wchar.h>
- # include <wctype.h>
- #endif
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <sys/types.h>
- #if HAVE_SYS_WAIT_H
- # include <sys/wait.h>
- #endif
- #include <dirent.h>
- #include <time.h>
- #include <regex.h>
- #ifdef G_OS_UNIX
- #include <sys/utsname.h>
- #endif
- #include <fcntl.h>
- #ifdef G_OS_WIN32
- # include <direct.h>
- # include <io.h>
- # include <w32lib.h>
- #endif
- #ifdef MAEMO
- #include <libosso.h>
- #ifdef CHINOOK
- # include <tablet-browser-interface.h>
- #else
- # include <osso-browser-interface.h>
- #endif
- #endif
- #include "utils.h"
- #include "socket.h"
- #include "../codeconv.h"
- #define BUFFSIZE 8192
- static gboolean debug_mode = FALSE;
- #ifdef G_OS_WIN32
- static GSList *tempfiles=NULL;
- #endif
- /* Return true if we are running as root. This function should beused
- instead of getuid () == 0. */
- gboolean superuser_p (void)
- {
- #ifdef G_OS_WIN32
- return w32_is_administrator ();
- #else
- return !getuid();
- #endif
- }
- #if !GLIB_CHECK_VERSION(2, 7, 0) && !defined(G_OS_UNIX)
- gint g_chdir(const gchar *path)
- {
- #ifdef G_OS_WIN32
- if (G_WIN32_HAVE_WIDECHAR_API()) {
- wchar_t *wpath;
- gint retval;
- gint save_errno;
- wpath = g_utf8_to_utf16(path, -1, NULL, NULL, NULL);
- if (wpath == NULL) {
- errno = EINVAL;
- return -1;
- }
- retval = _wchdir(wpath);
- save_errno = errno;
- g_free(wpath);
- errno = save_errno;
- return retval;
- } else {
- gchar *cp_path;
- gint retval;
- gint save_errno;
- cp_path = g_locale_from_utf8(path, -1, NULL, NULL, NULL);
- if (cp_path == NULL) {
- errno = EINVAL;
- return -1;
- }
- retval = chdir(cp_path);
- save_errno = errno;
- g_free(cp_path);
- errno = save_errno;
- return retval;
- }
- #else
- return chdir(path);
- #endif
- }
- gint g_chmod(const gchar *path, gint mode)
- {
- #ifdef G_OS_WIN32
- if (G_WIN32_HAVE_WIDECHAR_API()) {
- wchar_t *wpath;
- gint retval;
- gint save_errno;
- wpath = g_utf8_to_utf16(path, -1, NULL, NULL, NULL);
- if (wpath == NULL) {
- errno = EINVAL;
- return -1;
- }
- retval = _wchmod(wpath, mode);
- save_errno = errno;
- g_free(wpath);
- errno = save_errno;
- return retval;
- } else {
- gchar *cp_path;
- gint retval;
- gint save_errno;
- cp_path = g_locale_from_utf8(path, -1, NULL, NULL, NULL);
- if (cp_path == NULL) {
- errno = EINVAL;
- return -1;
- }
- retval = chmod(cp_path, mode);
- save_errno = errno;
- g_free(cp_path);
- errno = save_errno;
- return retval;
- }
- #else
- return chmod(path, mode);
- #endif
- }
- FILE* g_fopen(const gchar *filename, const gchar *mode)
- {
- #ifdef G_OS_WIN32
- char *name = g_win32_locale_filename_from_utf8(filename);
- FILE* fp = fopen(name, mode);
- g_free(name);
- return fp;
- #else
- return fopen(filename, mode);
- #endif
- }
- int g_open(const gchar *filename, int flags, int mode)
- {
- #ifdef G_OS_WIN32
- char *name = g_win32_locale_filename_from_utf8(filename);
- int fd = open(name, flags, mode);
- g_free(name);
- return fp;
- #else
- return open(filename, flags, mode);
- #endif
- }
- #endif /* GLIB_CHECK_VERSION && G_OS_UNIX */
- #ifdef G_OS_WIN32
- gint mkstemp_name(gchar *template, gchar **name_used)
- {
- static gulong count=0; /* W32-_mktemp only supports up to 27
- tempfiles... */
- int tmpfd;
- *name_used = g_strdup_printf("%s.%ld",_mktemp(template),count++);
- tmpfd = g_open (*name_used, (O_CREAT | O_RDWR | O_BINARY),
- (S_IRUSR | S_IWUSR));
- tempfiles=g_slist_append(tempfiles, g_strdup(*name_used));
- if (tmpfd<0) {
- perror(g_strdup_printf("cant create %s",*name_used));
- return -1;
- }
- else
- return tmpfd;
- }
- #endif /* G_OS_WIN32 */
- #ifdef G_OS_WIN32
- gint mkstemp(gchar *template)
- {
- gchar *dummyname;
- gint res = mkstemp_name(template, &dummyname);
- g_free(dummyname);
- return res;
- }
- #endif /* G_OS_WIN32 */
- void list_free_strings(GList *list)
- {
- list = g_list_first(list);
- while (list != NULL) {
- g_free(list->data);
- list = list->next;
- }
- }
- void slist_free_strings(GSList *list)
- {
- while (list != NULL) {
- g_free(list->data);
- list = list->next;
- }
- }
- static void hash_free_strings_func(gpointer key, gpointer value, gpointer data)
- {
- g_free(key);
- }
- void hash_free_strings(GHashTable *table)
- {
- g_hash_table_foreach(table, hash_free_strings_func, NULL);
- }
- gint str_case_equal(gconstpointer v, gconstpointer v2)
- {
- return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
- }
- guint str_case_hash(gconstpointer key)
- {
- const gchar *p = key;
- guint h = *p;
- if (h) {
- h = g_ascii_tolower(h);
- for (p += 1; *p != '\0'; p++)
- h = (h << 5) - h + g_ascii_tolower(*p);
- }
- return h;
- }
- void ptr_array_free_strings(GPtrArray *array)
- {
- gint i;
- gchar *str;
- cm_return_if_fail(array != NULL);
- for (i = 0; i < array->len; i++) {
- str = g_ptr_array_index(array, i);
- g_free(str);
- }
- }
- gint to_number(const gchar *nstr)
- {
- register const gchar *p;
- if (*nstr == '\0') return -1;
- for (p = nstr; *p != '\0'; p++)
- if (!g_ascii_isdigit(*p)) return -1;
- return atoi(nstr);
- }
- /* convert integer into string,
- nstr must be not lower than 11 characters length */
- gchar *itos_buf(gchar *nstr, gint n)
- {
- g_snprintf(nstr, 11, "%d", n);
- return nstr;
- }
- /* convert integer into string */
- gchar *itos(gint n)
- {
- static gchar nstr[11];
- return itos_buf(nstr, n);
- }
- #define divide(num,divisor,i,d) \
- { \
- i = num >> divisor; \
- d = num & ((1<<divisor)-1); \
- d = (d*100) >> divisor; \
- }
- /*!
- * \brief Convert a given size in bytes in a human-readable string
- *
- * \param size The size expressed in bytes to convert in string
- * \return The string that respresents the size in an human-readable way
- */
- gchar *to_human_readable(goffset size)
- {
- static gchar str[14];
- static gchar *b_format = NULL, *kb_format = NULL,
- *mb_format = NULL, *gb_format = NULL;
- register int t = 0, r = 0;
- if (b_format == NULL) {
- b_format = _("%dB");
- kb_format = _("%d.%02dKB");
- mb_format = _("%d.%02dMB");
- gb_format = _("%.2fGB");
- }
-
- if (size < (goffset)1024) {
- g_snprintf(str, sizeof(str), b_format, (gint)size);
- return str;
- } else if (size >> 10 < (goffset)1024) {
- divide(size, 10, t, r);
- g_snprintf(str, sizeof(str), kb_format, t, r);
- return str;
- } else if (size >> 20 < (goffset)1024) {
- divide(size, 20, t, r);
- g_snprintf(str, sizeof(str), mb_format, t, r);
- return str;
- } else {
- g_snprintf(str, sizeof(str), gb_format, (gfloat)(size >> 30));
- return str;
- }
- }
- /* strcmp with NULL-checking */
- gint strcmp2(const gchar *s1, const gchar *s2)
- {
- if (s1 == NULL || s2 == NULL)
- return -1;
- else
- return strcmp(s1, s2);
- }
- /* strstr with NULL-checking */
- gchar *strstr2(const gchar *s1, const gchar *s2)
- {
- if (s1 == NULL || s2 == NULL)
- return NULL;
- else
- return strstr(s1, s2);
- }
- /* compare paths */
- gint path_cmp(const gchar *s1, const gchar *s2)
- {
- gint len1, len2;
- int rc;
- #ifdef G_OS_WIN32
- gchar *s1buf, *s2buf;
- #endif
- if (s1 == NULL || s2 == NULL) return -1;
- if (*s1 == '\0' || *s2 == '\0') return -1;
- #ifdef G_OS_WIN32
- s1buf = g_strdup (s1);
- s2buf = g_strdup (s2);
- subst_char (s1buf, '/', G_DIR_SEPARATOR);
- subst_char (s2buf, '/', G_DIR_SEPARATOR);
- s1 = s1buf;
- s2 = s2buf;
- #endif /* !G_OS_WIN32 */
- len1 = strlen(s1);
- len2 = strlen(s2);
- if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
- if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
- rc = strncmp(s1, s2, MAX(len1, len2));
- #ifdef G_OS_WIN32
- g_free (s1buf);
- g_free (s2buf);
- #endif /* !G_OS_WIN32 */
- return rc;
- }
- /* remove trailing return code */
- gchar *strretchomp(gchar *str)
- {
- register gchar *s;
- if (!*str) return str;
- for (s = str + strlen(str) - 1;
- s >= str && (*s == '\n' || *s == '\r');
- s--)
- *s = '\0';
- return str;
- }
- /* remove trailing character */
- gchar *strtailchomp(gchar *str, gchar tail_char)
- {
- register gchar *s;
- if (!*str) return str;
- if (tail_char == '\0') return str;
- for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
- *s = '\0';
- return str;
- }
- /* remove CR (carriage return) */
- gchar *strcrchomp(gchar *str)
- {
- register gchar *s;
- if (!*str) return str;
- s = str + strlen(str) - 1;
- if (*s == '\n' && s > str && *(s - 1) == '\r') {
- *(s - 1) = '\n';
- *s = '\0';
- }
- return str;
- }
- gint file_strip_crs(const gchar *file)
- {
- FILE *fp = NULL, *outfp = NULL;
- gchar buf[4096];
- gchar *out = get_tmp_file();
- if (file == NULL)
- goto freeout;
- fp = g_fopen(file, "rb");
- if (!fp)
- goto freeout;
- outfp = g_fopen(out, "wb");
- if (!outfp) {
- fclose(fp);
- goto freeout;
- }
- while (fgets(buf, sizeof (buf), fp) != NULL) {
- strcrchomp(buf);
- if (fputs(buf, outfp) == EOF) {
- fclose(fp);
- fclose(outfp);
- goto unlinkout;
- }
- }
- fclose(fp);
- if (fclose(outfp) == EOF) {
- goto unlinkout;
- }
-
- if (move_file(out, file, TRUE) < 0)
- goto unlinkout;
-
- g_free(out);
- return 0;
- unlinkout:
- claws_unlink(out);
- freeout:
- g_free(out);
- return -1;
- }
- /* Similar to `strstr' but this function ignores the case of both strings. */
- gchar *strcasestr(const gchar *haystack, const gchar *needle)
- {
- register size_t haystack_len, needle_len;
- haystack_len = strlen(haystack);
- needle_len = strlen(needle);
- if (haystack_len < needle_len || needle_len == 0)
- return NULL;
- while (haystack_len >= needle_len) {
- if (!g_ascii_strncasecmp(haystack, needle, needle_len))
- return (gchar *)haystack;
- else {
- haystack++;
- haystack_len--;
- }
- }
- return NULL;
- }
- gpointer my_memmem(gconstpointer haystack, size_t haystacklen,
- gconstpointer needle, size_t needlelen)
- {
- const gchar *haystack_ = (const gchar *)haystack;
- const gchar *needle_ = (const gchar *)needle;
- const gchar *haystack_cur = (const gchar *)haystack;
- size_t haystack_left = haystacklen;
- if (needlelen == 1)
- return memchr(haystack_, *needle_, haystacklen);
- while ((haystack_cur = memchr(haystack_cur, *needle_, haystack_left))
- != NULL) {
- if (haystacklen - (haystack_cur - haystack_) < needlelen)
- break;
- if (memcmp(haystack_cur + 1, needle_ + 1, needlelen - 1) == 0)
- return (gpointer)haystack_cur;
- else{
- haystack_cur++;
- haystack_left = haystacklen - (haystack_cur - haystack_);
- }
- }
- return NULL;
- }
- /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
- gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
- {
- register const gchar *s = src;
- register gchar *d = dest;
- while (--n && *s)
- *d++ = *s++;
- *d = '\0';
- return dest;
- }
- /* Examine if next block is non-ASCII string */
- gboolean is_next_nonascii(const gchar *s)
- {
- const gchar *p;
- /* skip head space */
- for (p = s; *p != '\0' && g_ascii_isspace(*p); p++)
- ;
- for (; *p != '\0' && !g_ascii_isspace(*p); p++) {
- if (*(guchar *)p > 127 || *(guchar *)p < 32)
- return TRUE;
- }
- return FALSE;
- }
- gint get_next_word_len(const gchar *s)
- {
- gint len = 0;
- for (; *s != '\0' && !g_ascii_isspace(*s); s++, len++)
- ;
- return len;
- }
- static void trim_subject_for_compare(gchar *str)
- {
- gchar *srcp;
- eliminate_parenthesis(str, '[', ']');
- eliminate_parenthesis(str, '(', ')');
- g_strstrip(str);
- srcp = str + subject_get_prefix_length(str);
- if (srcp != str)
- memmove(str, srcp, strlen(srcp) + 1);
- }
- static void trim_subject_for_sort(gchar *str)
- {
- gchar *srcp;
- g_strstrip(str);
- srcp = str + subject_get_prefix_length(str);
- if (srcp != str)
- memmove(str, srcp, strlen(srcp) + 1);
- }
- /* compare subjects */
- gint subject_compare(const gchar *s1, const gchar *s2)
- {
- gchar *str1, *str2;
- if (!s1 || !s2) return -1;
- if (!*s1 || !*s2) return -1;
- Xstrdup_a(str1, s1, return -1);
- Xstrdup_a(str2, s2, return -1);
- trim_subject_for_compare(str1);
- trim_subject_for_compare(str2);
- if (!*str1 || !*str2) return -1;
- return strcmp(str1, str2);
- }
- gint subject_compare_for_sort(const gchar *s1, const gchar *s2)
- {
- gchar *str1, *str2;
- if (!s1 || !s2) return -1;
- Xstrdup_a(str1, s1, return -1);
- Xstrdup_a(str2, s2, return -1);
- trim_subject_for_sort(str1);
- trim_subject_for_sort(str2);
- return g_utf8_collate(str1, str2);
- }
- void trim_subject(gchar *str)
- {
- register gchar *srcp;
- gchar op, cl;
- gint in_brace;
- g_strstrip(str);
- srcp = str + subject_get_prefix_length(str);
- if (*srcp == '[') {
- op = '[';
- cl = ']';
- } else if (*srcp == '(') {
- op = '(';
- cl = ')';
- } else
- op = 0;
- if (op) {
- ++srcp;
- in_brace = 1;
- while (*srcp) {
- if (*srcp == op)
- in_brace++;
- else if (*srcp == cl)
- in_brace--;
- srcp++;
- if (in_brace == 0)
- break;
- }
- }
- while (g_ascii_isspace(*srcp)) srcp++;
- memmove(str, srcp, strlen(srcp) + 1);
- }
- void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
- {
- register gchar *srcp, *destp;
- gint in_brace;
- srcp = destp = str;
- while ((destp = strchr(destp, op))) {
- in_brace = 1;
- srcp = destp + 1;
- while (*srcp) {
- if (*srcp == op)
- in_brace++;
- else if (*srcp == cl)
- in_brace--;
- srcp++;
- if (in_brace == 0)
- break;
- }
- while (g_ascii_isspace(*srcp)) srcp++;
- memmove(destp, srcp, strlen(srcp) + 1);
- }
- }
- void extract_parenthesis(gchar *str, gchar op, gchar cl)
- {
- register gchar *srcp, *destp;
- gint in_brace;
- srcp = destp = str;
- while ((srcp = strchr(destp, op))) {
- if (destp > str)
- *destp++ = ' ';
- memmove(destp, srcp + 1, strlen(srcp));
- in_brace = 1;
- while(*destp) {
- if (*destp == op)
- in_brace++;
- else if (*destp == cl)
- in_brace--;
- if (in_brace == 0)
- break;
- destp++;
- }
- }
- *destp = '\0';
- }
- static void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
- gchar op, gchar cl)
- {
- register gchar *srcp, *destp;
- gint in_brace;
- gboolean in_quote = FALSE;
- srcp = destp = str;
- while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
- if (destp > str)
- *destp++ = ' ';
- memmove(destp, srcp + 1, strlen(srcp));
- in_brace = 1;
- while(*destp) {
- if (*destp == op && !in_quote)
- in_brace++;
- else if (*destp == cl && !in_quote)
- in_brace--;
- else if (*destp == quote_chr)
- in_quote ^= TRUE;
- if (in_brace == 0)
- break;
- destp++;
- }
- }
- *destp = '\0';
- }
- void extract_quote(gchar *str, gchar quote_chr)
- {
- register gchar *p;
- if ((str = strchr(str, quote_chr))) {
- p = str;
- while ((p = strchr(p + 1, quote_chr)) && (p[-1] == '\\')) {
- memmove(p - 1, p, strlen(p) + 1);
- p--;
- }
- if(p) {
- *p = '\0';
- memmove(str, str + 1, p - str);
- }
- }
- }
- void eliminate_address_comment(gchar *str)
- {
- register gchar *srcp, *destp;
- gint in_brace;
- srcp = destp = str;
- while ((destp = strchr(destp, '"'))) {
- if ((srcp = strchr(destp + 1, '"'))) {
- srcp++;
- if (*srcp == '@') {
- destp = srcp + 1;
- } else {
- while (g_ascii_isspace(*srcp)) srcp++;
- memmove(destp, srcp, strlen(srcp) + 1);
- }
- } else {
- *destp = '\0';
- break;
- }
- }
- srcp = destp = str;
- while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
- in_brace = 1;
- srcp = destp + 1;
- while (*srcp) {
- if (*srcp == '(')
- in_brace++;
- else if (*srcp == ')')
- in_brace--;
- srcp++;
- if (in_brace == 0)
- break;
- }
- while (g_ascii_isspace(*srcp)) srcp++;
- memmove(destp, srcp, strlen(srcp) + 1);
- }
- }
- gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
- {
- gboolean in_quote = FALSE;
- while (*str) {
- if (*str == c && !in_quote)
- return (gchar *)str;
- if (*str == quote_chr)
- in_quote ^= TRUE;
- str++;
- }
- return NULL;
- }
- void extract_address(gchar *str)
- {
- eliminate_address_comment(str);
- if (strchr_with_skip_quote(str, '"', '<'))
- extract_parenthesis_with_skip_quote(str, '"', '<', '>');
- g_strstrip(str);
- }
- void extract_list_id_str(gchar *str)
- {
- if (strchr_with_skip_quote(str, '"', '<'))
- extract_parenthesis_with_skip_quote(str, '"', '<', '>');
- g_strstrip(str);
- }
- static GSList *address_list_append_real(GSList *addr_list, const gchar *str, gboolean removecomments)
- {
- gchar *work;
- gchar *workp;
- if (!str) return addr_list;
- Xstrdup_a(work, str, return addr_list);
- if (removecomments)
- eliminate_address_comment(work);
- workp = work;
- while (workp && *workp) {
- gchar *p, *next;
- if ((p = strchr_with_skip_quote(workp, '"', ','))) {
- *p = '\0';
- next = p + 1;
- } else
- next = NULL;
- if (removecomments && strchr_with_skip_quote(workp, '"', '<'))
- extract_parenthesis_with_skip_quote
- (workp, '"', '<', '>');
- g_strstrip(workp);
- if (*workp)
- addr_list = g_slist_append(addr_list, g_strdup(workp));
- workp = next;
- }
- return addr_list;
- }
- GSList *address_list_append(GSList *addr_list, const gchar *str)
- {
- return address_list_append_real(addr_list, str, TRUE);
- }
- GSList *address_list_append_with_comments(GSList *addr_list, const gchar *str)
- {
- return address_list_append_real(addr_list, str, FALSE);
- }
- GSList *references_list_prepend(GSList *msgid_list, const gchar *str)
- {
- const gchar *strp;
- if (!str) return msgid_list;
- strp = str;
- while (strp && *strp) {
- const gchar *start, *end;
- gchar *msgid;
- if ((start = strchr(strp, '<')) != NULL) {
- end = strchr(start + 1, '>');
- if (!end) break;
- } else
- break;
- msgid = g_strndup(start + 1, end - start - 1);
- g_strstrip(msgid);
- if (*msgid)
- msgid_list = g_slist_prepend(msgid_list, msgid);
- else
- g_free(msgid);
- strp = end + 1;
- }
- return msgid_list;
- }
- GSList *references_list_append(GSList *msgid_list, const gchar *str)
- {
- GSList *list;
- list = references_list_prepend(NULL, str);
- list = g_slist_reverse(list);
- msgid_list = g_slist_concat(msgid_list, list);
- return msgid_list;
- }
- GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
- {
- gchar *work;
- gchar *workp;
- if (!str) return group_list;
- Xstrdup_a(work, str, return group_list);
- workp = work;
- while (workp && *workp) {
- gchar *p, *next;
- if ((p = strchr_with_skip_quote(workp, '"', ','))) {
- *p = '\0';
- next = p + 1;
- } else
- next = NULL;
- g_strstrip(workp);
- if (*workp)
- group_list = g_slist_append(group_list,
- g_strdup(workp));
- workp = next;
- }
- return group_list;
- }
- GList *add_history(GList *list, const gchar *str)
- {
- GList *old;
- gchar *oldstr;
- cm_return_val_if_fail(str != NULL, list);
- old = g_list_find_custom(list, (gpointer)str, (GCompareFunc)strcmp2);
- if (old) {
- oldstr = old->data;
- list = g_list_remove(list, old->data);
- g_free(oldstr);
- } else if (g_list_length(list) >= MAX_HISTORY_SIZE) {
- GList *last;
- last = g_list_last(list);
- if (last) {
- oldstr = last->data;
- list = g_list_remove(list, last->data);
- g_free(oldstr);
- }
- }
- list = g_list_prepend(list, g_strdup(str));
- return list;
- }
- void remove_return(gchar *str)
- {
- register gchar *p = str;
- while (*p) {
- if (*p == '\n' || *p == '\r')
- memmove(p, p + 1, strlen(p));
- else
- p++;
- }
- }
- void remove_space(gchar *str)
- {
- register gchar *p = str;
- register gint spc;
- while (*p) {
- spc = 0;
- while (g_ascii_isspace(*(p + spc)))
- spc++;
- if (spc)
- memmove(p, p + spc, strlen(p + spc) + 1);
- else
- p++;
- }
- }
- void unfold_line(gchar *str)
- {
- register gchar *p = str;
- register gint spc;
- while (*p) {
- if (*p == '\n' || *p == '\r') {
- *p++ = ' ';
- spc = 0;
- while (g_ascii_isspace(*(p + spc)))
- spc++;
- if (spc)
- memmove(p, p + spc, strlen(p + spc) + 1);
- } else
- p++;
- }
- }
- void subst_char(gchar *str, gchar orig, gchar subst)
- {
- register gchar *p = str;
- while (*p) {
- if (*p == orig)
- *p = subst;
- p++;
- }
- }
- void subst_chars(gchar *str, gchar *orig, gchar subst)
- {
- register gchar *p = str;
- while (*p) {
- if (strchr(orig, *p) != NULL)
- *p = subst;
- p++;
- }
- }
- void subst_for_filename(gchar *str)
- {
- if (!str)
- return;
- #ifdef G_OS_WIN32
- subst_chars(str, "\t\r\n\\/*:", '_');
- #else
- subst_chars(str, "\t\r\n\\/*", '_');
- #endif
- }
- void subst_for_shellsafe_filename(gchar *str)
- {
- if (!str)
- return;
- subst_for_filename(str);
- subst_chars(str, " \"'|&;()<>'!{}[]",'_');
- }
- gboolean is_ascii_str(const gchar *str)
- {
- const guchar *p = (const guchar *)str;
- while (*p != '\0') {
- if (*p != '\t' && *p != ' ' &&
- *p != '\r' && *p != '\n' &&
- (*p < 32 || *p >= 127))
- return FALSE;
- p++;
- }
- return TRUE;
- }
- static const gchar * line_has_quote_char_last(const gchar * str, const gchar *quote_chars)
- {
- gchar * position = NULL;
- gchar * tmp_pos = NULL;
- int i;
- if (quote_chars == NULL)
- return NULL;
- for (i = 0; i < strlen(quote_chars); i++) {
- tmp_pos = strrchr (str, quote_chars[i]);
- if(position == NULL
- || (tmp_pos != NULL && position <= tmp_pos) )
- position = tmp_pos;
- }
- return position;
- }
- gint get_quote_level(const gchar *str, const gchar *quote_chars)
- {
- const gchar *first_pos;
- const gchar *last_pos;
- const gchar *p = str;
- gint quote_level = -1;
- /* speed up line processing by only searching to the last '>' */
- if ((first_pos = line_has_quote_char(str, quote_chars)) != NULL) {
- /* skip a line if it contains a '<' before the initial '>' */
- if (memchr(str, '<', first_pos - str) != NULL)
- return -1;
- last_pos = line_has_quote_char_last(first_pos, quote_chars);
- } else
- return -1;
- while (p <= last_pos) {
- while (p < last_pos) {
- if (g_ascii_isspace(*p))
- p++;
- else
- break;
- }
- if (strchr(quote_chars, *p))
- quote_level++;
- else if (*p != '-' && !g_ascii_isspace(*p) && p <= last_pos) {
- /* any characters are allowed except '-','<' and space */
- while (*p != '-' && *p != '<'
- && !strchr(quote_chars, *p)
- && !g_ascii_isspace(*p)
- && p < last_pos)
- p++;
- if (strchr(quote_chars, *p))
- quote_level++;
- else
- break;
- }
- p++;
- }
- return quote_level;
- }
- gint check_line_length(const gchar *str, gint max_chars, gint *line)
- {
- const gchar *p = str, *q;
- gint cur_line = 0, len;
- while ((q = strchr(p, '\n')) != NULL) {
- len = q - p + 1;
- if (len > max_chars) {
- if (line)
- *line = cur_line;
- return -1;
- }
- p = q + 1;
- ++cur_line;
- }
- len = strlen(p);
- if (len > max_chars) {
- if (line)
- *line = cur_line;
- return -1;
- }
- return 0;
- }
- const gchar * line_has_quote_char(const gchar * str, const gchar *quote_chars)
- {
- gchar * position = NULL;
- gchar * tmp_pos = NULL;
- int i;
- if (quote_chars == NULL)
- return FALSE;
- for (i = 0; i < strlen(quote_chars); i++) {
- tmp_pos = strchr (str, quote_chars[i]);
- if(position == NULL
- || (tmp_pos != NULL && position >= tmp_pos) )
- position = tmp_pos;
- }
- return position;
- }
- static gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
- {
- register guint haystack_len, needle_len;
- gboolean in_squote = FALSE, in_dquote = FALSE;
- haystack_len = strlen(haystack);
- needle_len = strlen(needle);
- if (haystack_len < needle_len || needle_len == 0)
- return NULL;
- while (haystack_len >= needle_len) {
- if (!in_squote && !in_dquote &&
- !strncmp(haystack, needle, needle_len))
- return (gchar *)haystack;
- /* 'foo"bar"' -> foo"bar"
- "foo'bar'" -> foo'bar' */
- if (*haystack == '\'') {
- if (in_squote)
- in_squote = FALSE;
- else if (!in_dquote)
- in_squote = TRUE;
- } else if (*haystack == '\"') {
- if (in_dquote)
- in_dquote = FALSE;
- else if (!in_squote)
- in_dquote = TRUE;
- }
- haystack++;
- haystack_len--;
- }
- return NULL;
- }
- gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
- gint max_tokens)
- {
- GSList *string_list = NULL, *slist;
- gchar **str_array, *s, *new_str;
- guint i, n = 1, len;
- cm_return_val_if_fail(str != NULL, NULL);
- cm_return_val_if_fail(delim != NULL, NULL);
- if (max_tokens < 1)
- max_tokens = G_MAXINT;
- s = strstr_with_skip_quote(str, delim);
- if (s) {
- guint delimiter_len = strlen(delim);
- do {
- len = s - str;
- new_str = g_strndup(str, len);
- if (new_str[0] == '\'' || new_str[0] == '\"') {
- if (new_str[len - 1] == new_str[0]) {
- new_str[len - 1] = '\0';
- memmove(new_str, new_str + 1, len - 1);
- }
- }
- string_list = g_slist_prepend(string_list, new_str);
- n++;
- str = s + delimiter_len;
- s = strstr_with_skip_quote(str, delim);
- } while (--max_tokens && s);
- }
- if (*str) {
- new_str = g_strdup(str);
- if (new_str[0] == '\'' || new_str[0] == '\"') {
- len = strlen(str);
- if (new_str[len - 1] == new_str[0]) {
- new_str[len - 1] = '\0';
- memmove(new_str, new_str + 1, len - 1);
- }
- }
- string_list = g_slist_prepend(string_list, new_str);
- n++;
- }
- str_array = g_new(gchar*, n);
- i = n - 1;
- str_array[i--] = NULL;
- for (slist = string_list; slist; slist = slist->next)
- str_array[i--] = slist->data;
- g_slist_free(string_list);
- return str_array;
- }
- gchar *get_abbrev_newsgroup_name(const gchar *group, gint len)
- {
- gchar *abbrev_group;
- gchar *ap;
- const gchar *p = group;
- const gchar *last;
- cm_return_val_if_fail(group != NULL, NULL);
- last = group + strlen(group);
- abbrev_group = ap = g_malloc(strlen(group) + 1);
- while (*p) {
- while (*p == '.')
- *ap++ = *p++;
- if ((ap - abbrev_group) + (last - p) > len && strchr(p, '.')) {
- *ap++ = *p++;
- while (*p != '.') p++;
- } else {
- strcpy(ap, p);
- return abbrev_group;
- }
- }
- *ap = '\0';
- return abbrev_group;
- }
- gchar *trim_string(const gchar *str, gint len)
- {
- const gchar *p = str;
- gint mb_len;
- gchar *new_str;
- gint new_len = 0;
- if (!str) return NULL;
- if (strlen(str) <= len)
- return g_strdup(str);
- if (g_utf8_validate(str, -1, NULL) == FALSE)
- return g_strdup(str);
- while (*p != '\0') {
- mb_len = g_utf8_skip[*(guchar *)p];
- if (mb_len == 0)
- break;
- else if (new_len + mb_len > len)
- break;
- new_len += mb_len;
- p += mb_len;
- }
- Xstrndup_a(new_str, str, new_len, return g_strdup(str));
- return g_strconcat(new_str, "...", NULL);
- }
- GList *uri_list_extract_filenames(const gchar *uri_list)
- {
- GList *result = NULL;
- const gchar *p, *q;
- gchar *escaped_utf8uri;
- p = uri_list;
- while (p) {
- if (*p != '#') {
- while (g_ascii_isspace(*p)) p++;
- if (!strncmp(p, "file:", 5)) {
- q = p;
- q += 5;
- while (*q && *q != '\n' && *q != '\r') q++;
- if (q > p) {
- gchar *file, *locale_file = NULL;
- q--;
- while (q > p && g_ascii_isspace(*q))
- q--;
- Xalloca(escaped_utf8uri, q - p + 2,
- return result);
- Xalloca(file, q - p + 2,
- return result);
- *file = '\0';
- strncpy(escaped_utf8uri, p, q - p + 1);
- escaped_utf8uri[q - p + 1] = '\0';
- decode_uri(file, escaped_utf8uri);
- /*
- * g_filename_from_uri() rejects escaped/locale encoded uri
- * string which come from Nautilus.
- */
- #ifndef G_OS_WIN32
- if (g_utf8_validate(file, -1, NULL))
- locale_file
- = conv_codeset_strdup(
- file + 5,
- CS_UTF_8,
- conv_get_locale_charset_str());
- if (!locale_file)
- locale_file = g_strdup(file + 5);
- #else
- locale_file = g_filename_from_uri(file, NULL, NULL);
- #endif
- result = g_list_append(result, locale_file);
- }
- }
- }
- p = strchr(p, '\n');
- if (p) p++;
- }
- return result;
- }
- /* Converts two-digit hexadecimal to decimal. Used for unescaping escaped
- * characters
- */
- static gint axtoi(const gchar *hexstr)
- {
- gint hi, lo, result;
- hi = hexstr[0];
- if ('0' <= hi && hi <= '9') {
- hi -= '0';
- } else
- if ('a' <= hi && hi <= 'f') {
- hi -= ('a' - 10);
- } else
- if ('A' <= hi && hi <= 'F') {
- hi -= ('A' - 10);
- }
- lo = hexstr[1];
- if ('0' <= lo && lo <= '9') {
- lo -= '0';
- } else
- if ('a' <= lo && lo <= 'f') {
- lo -= ('a'-10);
- } else
- if ('A' <= lo && lo <= 'F') {
- lo -= ('A' - 10);
- }
- result = lo + (16 * hi);
- return result;
- }
- gboolean is_uri_string(const gchar *str)
- {
- while (str && *str && g_ascii_isspace(*str))
- str++;
- return (g_ascii_strncasecmp(str, "http://", 7) == 0 ||
- g_ascii_strncasecmp(str, "https://", 8) == 0 ||
- g_ascii_strncasecmp(str, "ftp://", 6) == 0 ||
- g_ascii_strncasecmp(str, "www.", 4) == 0);
- }
- gchar *get_uri_path(const gchar *uri)
- {
- while (uri && *uri && g_ascii_isspace(*uri))
- uri++;
- if (g_ascii_strncasecmp(uri, "http://", 7) == 0)
- return (gchar *)(uri + 7);
- else if (g_ascii_strncasecmp(uri, "https://", 8) == 0)
- return (gchar *)(uri + 8);
- else if (g_ascii_strncasecmp(uri, "ftp://", 6) == 0)
- return (gchar *)(uri + 6);
- else
- return (gchar *)uri;
- }
- gint get_uri_len(const gchar *str)
- {
- const gchar *p;
- if (is_uri_string(str)) {
- for (p = str; *p != '\0'; p++) {
- if (!g_ascii_isgraph(*p) || strchr("()<>\"", *p))
- break;
- }
- return p - str;
- }
- return 0;
- }
- /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
- * plusses, and escape characters are used)
- */
- void decode_uri_with_plus(gchar *decoded_uri, const gchar *encoded_uri, gboolean with_plus)
- {
- gchar *dec = decoded_uri;
- const gchar *enc = encoded_uri;
- while (*enc) {
- if (*enc == '%') {
- enc++;
- if (isxdigit((guchar)enc[0]) &&
- isxdigit((guchar)enc[1])) {
- *dec = axtoi(enc);
- dec++;
- enc += 2;
- }
- } else {
- if (with_plus && *enc == '+')
- *dec = ' ';
- else
- *dec = *enc;
- dec++;
- enc++;
- }
- }
- *dec = '\0';
- }
- void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
- {
- decode_uri_with_plus(decoded_uri, encoded_uri, TRUE);
- }
- static gchar *decode_uri_gdup(const gchar *encoded_uri)
- {
- gchar *buffer = g_malloc(strlen(encoded_uri)+1);
- decode_uri_with_plus(buffer, encoded_uri, FALSE);
- return buffer;
- }
- gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc, gchar **bcc,
- gchar **subject, gchar **body, gchar ***attach, gchar **inreplyto)
- {
- gchar *tmp_mailto;
- gchar *p;
- const gchar *forbidden_uris[] = { ".gnupg/",
- "/etc/passwd",
- "/etc/shadow",
- ".ssh/",
- "../",
- NULL };
- gint num_attach = 0;
- gchar **my_att = NULL;
- Xstrdup_a(tmp_mailto, mailto, return -1);
- if (!strncmp(tmp_mailto, "mailto:", 7))
- tmp_mailto += 7;
- p = strchr(tmp_mailto, '?');
- if (p) {
- *p = '\0';
- p++;
- }
- if (to && !*to)
- *to = decode_uri_gdup(tmp_mailto);
- my_att = g_malloc(sizeof(char *));
- my_att[0] = NULL;
- while (p) {
- gchar *field, *value;
- field = p;
- p = strchr(p, '=');
- if (!p) break;
- *p = '\0';
- p++;
- value = p;
- p = strchr(p, '&');
- if (p) {
- *p = '\0';
- p++;
- }
- if (*value == '\0') continue;
- if (from && !g_ascii_strcasecmp(field, "from")) {
- if (!*from) {
- *from = decode_uri_gdup(value);
- } else {
- gchar *tmp = decode_uri_gdup(value);
- gchar *new_from = g_strdup_printf("%s, %s", *from, tmp);
- g_free(*from);
- *from = new_from;
- }
- } else if (cc && !g_ascii_strcasecmp(field, "cc")) {
- if (!*cc) {
- *cc = decode_uri_gdup(value);
- } else {
- gchar *tmp = decode_uri_gdup(value);
- gchar *new_cc = g_strdup_printf("%s, %s", *cc, tmp);
- g_free(*cc);
- *cc = new_cc;
- }
- } else if (bcc && !g_ascii_strcasecmp(field, "bcc")) {
- if (!*bcc) {
- *bcc = decode_uri_gdup(value);
- } else {
- gchar *tmp = decode_uri_gdup(value);
- gchar *new_bcc = g_strdup_printf("%s, %s", *bcc, tmp);
- g_free(*bcc);
- *bcc = new_bcc;
- }
- } else if (subject && !*subject &&
- !g_ascii_strcasecmp(field, "subject")) {
- *subject = decode_uri_gdup(value);
- } else if (body && !*body && !g_ascii_strcasecmp(field, "body")) {
- *body = decode_uri_gdup(value);
- } else if (body && !*body && !g_ascii_strcasecmp(field, "insert")) {
- gchar *tmp = decode_uri_gdup(value);
- if (!g_file_get_contents(tmp, body, NULL, NULL)) {
- g_warning("Error: couldn't set insert file '%s' in body\n", value);
- }
- g_free(tmp);
- tmp = NULL;
- } else if (attach && !g_ascii_strcasecmp(field, "attach")) {
- int i = 0;
- gchar *tmp = decode_uri_gdup(value);
- for (; forbidden_uris[i]; i++) {
- if (strstr(tmp, forbidden_uris[i])) {
- g_print("Refusing to attach '%s', potential private data leak\n",
- tmp);
- g_free(tmp);
- tmp = NULL;
- break;
- }
- }
- if (tmp) {
- /* attach is correct */
- num_attach++;
- my_att = g_realloc(my_att, (sizeof(char *))*(num_attach+1));
- my_att[num_attach-1] = tmp;
- my_att[num_attach] = NULL;
- }
- } else if (inreplyto && !*inreplyto &&
- !g_ascii_strcasecmp(field, "in-reply-to")) {
- *inreplyto = decode_uri_gdup(value);
- }
- }
- if (attach)
- *attach = my_att;
- return 0;
- }
- #ifdef G_OS_WIN32
- #include <windows.h>
- #ifndef CSIDL_APPDATA
- #define CSIDL_APPDATA 0x001a
- #endif
- #ifndef CSIDL_LOCAL_APPDATA
- #define CSIDL_LOCAL_APPDATA 0x001c
- #endif
- #ifndef CSIDL_FLAG_CREATE
- #define CSIDL_FLAG_CREATE 0x8000
- #endif
- #define DIM(v) (sizeof(v)/sizeof((v)[0]))
- #define RTLD_LAZY 0
- const char *
- w32_strerror (int w32_errno)
- {
- static char strerr[256];
- int ec = (int)GetLastError ();
- if (w32_errno == 0)
- w32_errno = ec;
- FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, w32_errno,
- MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
- strerr, DIM (strerr)-1, NULL);
- return strerr;
- }
- static __inline__ void *
- dlopen (const char * name, int flag)
- {
- void * hd = LoadLibrary (name);
- return hd;
- }
- static __inline__ void *
- dlsym (void * hd, const char * sym)
- {
- if (hd && sym)
- {
- void * fnc = GetProcAddress (hd, sym);
- if (!fnc)
- return NULL;
- return fnc;
- }
- return NULL;
- }
- static __inline__ const char *
- dlerror (void)
- {
- return w32_strerror (0);
- }
- static __inline__ int
- dlclose (void * hd)
- {
- if (hd)
- {
- FreeLibrary (hd);
- return 0;
- }
- return -1;
- }
- static HRESULT
- w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e)
- {
- static int initialized;
- static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPSTR);
- if (!initialized)
- {
- static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
- void *handle;
- int i;
- initialized = 1;
- for (i=0, handle = NULL; !handle && dllnames[i]; i++)
- {
- handle = dlopen (dllnames[i], RTLD_LAZY);
- if (handle)
- {
- func = dlsym (handle, "SHGetFolderPathW");
- if (!func)
- {
- dlclose (handle);
- handle = NULL;
- }
- }
- }
- }
- if (func)
- return func (a,b,c,d,e);
- else
- return -1;
- }
- /* Returns a static string with the directroy from which the module
- has been loaded. Returns an empty string on error. */
- static char *w32_get_module_dir(void)
- {
- static char *moddir;
- if (!moddir) {
- char name[MAX_PATH+10];
- char *p;
- if ( !GetModuleFileNameA (0, name, sizeof (name)-10) )
- *name = 0;
- else {
- p = strrchr (name, '\\');
- if (p)
- *p = 0;
- else
- *name = 0;
- }
- moddir = g_strdup (name);
- }
- return moddir;
- }
- #endif /* G_OS_WIN32 */
- /* Return a static string with the locale dir. */
- const gchar *get_locale_dir(void)
- {
- static gchar *loc_dir;
- #ifdef G_OS_WIN32
- if (!loc_dir)
- loc_dir = g_strconcat(w32_get_module_dir(), G_DIR_SEPARATOR_S,
- "\\share\\locale", NULL);
- #endif
- if (!loc_dir)
- loc_dir = LOCALEDIR;
-
- return loc_dir;
- }
- const gchar *get_home_dir(void)
- {
- #ifdef G_OS_WIN32
- static char home_dir_utf16[MAX_PATH] = "";
- static gchar *home_dir_utf8 = NULL;
- if (home_dir_utf16[0] == '\0') {
- if (w32_shgetfolderpath
- (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
- NULL, 0, home_dir_utf16) < 0)
- strcpy (home_dir_utf16, "C:\\Sylpheed");
- home_dir_utf8 = g_utf16_to_utf8 ((const gunichar *)home_dir_utf16, -1, NULL, NULL, NULL);
- }
- return home_dir_utf8;
- #else
- static const gchar *homeenv = NULL;
- if (homeenv)
- return homeenv;
- if (!homeenv && g_getenv("HOME") != NULL)
- homeenv = g_strdup(g_getenv("HOME"));
- if (!homeenv)
- homeenv = g_get_home_dir();
- return homeenv;
- #endif
- }
- static gchar *claws_rc_dir = NULL;
- static gboolean rc_dir_alt = FALSE;
- const gchar *get_rc_dir(void)
- {
- if (!claws_rc_dir)
- claws_rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
- RC_DIR, NULL);
- return claws_rc_dir;
- }
- void set_rc_dir(const gchar *dir)
- {
- if (claws_rc_dir != NULL) {
- g_print("Error: rc_dir already set\n");
- } else {
- rc_dir_alt = TRUE;
- if (g_path_is_absolute(dir))
- claws_rc_dir = g_strdup(dir);
- else {
- claws_rc_dir = g_strconcat(g_get_current_dir(),
- G_DIR_SEPARATOR_S, dir, NULL);
- }
- debug_print("set rc_dir to %s\n", claws_rc_dir);
- if (!is_dir_exist(claws_rc_dir)) {
- if (make_dir_hier(claws_rc_dir) != 0) {
- g_print("Error: can't create %s\n",
- claws_rc_dir);
- }
- }
- }
- }
- gboolean rc_dir_is_alt(void) {
- return rc_dir_alt;
- }
- const gchar *get_mail_base_dir(void)
- {
- return get_home_dir();
- }
- #ifdef MAEMO
- const gchar *prefs_common_get_data_root(void);
- gchar *last_data_root = NULL;
- #endif
- const gchar *get_news_cache_dir(void)
- {
- static gchar *news_cache_dir = NULL;
- #ifdef MAEMO
- const gchar *data_root = prefs_common_get_data_root();
- if (strcmp2(data_root, last_data_root)) {
- g_free(news_cache_dir);
- news_cache_dir = NULL;
- }
- #endif
- if (!news_cache_dir)
- #ifndef MAEMO
- news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- NEWS_CACHE_DIR, NULL);
- #else
- {
- if (data_root) {
- news_cache_dir = g_strconcat(data_root, G_DIR_SEPARATOR_S,
- "Claws", G_DIR_SEPARATOR_S,
- g_get_user_name(), G_DIR_SEPARATOR_S,
- NEWS_CACHE_DIR, NULL);
- g_free(last_data_root);
- last_data_root = g_strdup(last_data_root);
- } else {
- news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- NEWS_CACHE_DIR, NULL);
- g_free(last_data_root);
- last_data_root = NULL;
- }
- }
- #endif
- return news_cache_dir;
- }
- const gchar *get_imap_cache_dir(void)
- {
- static gchar *imap_cache_dir = NULL;
- #ifdef MAEMO
- const gchar *data_root = prefs_common_get_data_root();
- if (strcmp2(data_root, last_data_root)) {
- g_free(imap_cache_dir);
- imap_cache_dir = NULL;
- }
- #endif
- if (!imap_cache_dir)
- #ifndef MAEMO
- imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- IMAP_CACHE_DIR, NULL);
- #else
- {
- if (data_root) {
- imap_cache_dir = g_strconcat(data_root, G_DIR_SEPARATOR_S,
- "Claws", G_DIR_SEPARATOR_S,
- g_get_user_name(), G_DIR_SEPARATOR_S,
- IMAP_CACHE_DIR, NULL);
- g_free(last_data_root);
- last_data_root = g_strdup(last_data_root);
- } else {
- imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- IMAP_CACHE_DIR, NULL);
- g_free(last_data_root);
- last_data_root = NULL;
- }
- }
- #endif
- return imap_cache_dir;
- }
- const gchar *get_mime_tmp_dir(void)
- {
- static gchar *mime_tmp_dir = NULL;
- if (!mime_tmp_dir)
- mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- MIME_TMP_DIR, NULL);
- return mime_tmp_dir;
- }
- const gchar *get_template_dir(void)
- {
- static gchar *template_dir = NULL;
- if (!template_dir)
- template_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- TEMPLATE_DIR, NULL);
- return template_dir;
- }
- #ifdef G_OS_WIN32
- const gchar *get_cert_file(void)
- {
- const gchar *cert_file = NULL;
- if (!cert_file)
- cert_file = g_strconcat(w32_get_module_dir(),
- "\\share\\claws-mail\\",
- "ca-certificates.crt",
- NULL);
- return cert_file;
- }
- #endif
- /* Return the filepath of the claws-mail.desktop file */
- const gchar *get_desktop_file(void)
- {
- #ifdef DESKTOPFILEPATH
- return DESKTOPFILEPATH;
- #else
- return NULL;
- #endif
- }
- /* Return the default directory for Plugins. */
- const gchar *get_plugin_dir(void)
- {
- #ifdef G_OS_WIN32
- static gchar *plugin_dir = NULL;
- if (!plugin_dir)
- plugin_dir = g_strconcat(w32_get_module_dir(),
- "\\lib\\claws-mail\\plugins\\",
- NULL);
- return plugin_dir;
- #else
- if (is_dir_exist(PLUGINDIR))
- return PLUGINDIR;
- else {
- static gchar *plugin_dir = NULL;
- if (!plugin_dir)
- plugin_dir = g_strconcat(get_rc_dir(),
- G_DIR_SEPARATOR_S, "plugins",
- G_DIR_SEPARATOR_S, NULL);
- return plugin_dir;
- }
- #endif
- }
- #ifdef G_OS_WIN32
- /* Return the default directory for Themes. */
- const gchar *get_themes_dir(void)
- {
- static gchar *themes_dir = NULL;
- if (!themes_dir)
- themes_dir = g_strconcat(w32_get_module_dir(),
- "\\share\\claws-mail\\themes",
- NULL);
- return themes_dir;
- }
- #endif
- const gchar *get_tmp_dir(void)
- {
- static gchar *tmp_dir = NULL;
- if (!tmp_dir)
- tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- TMP_DIR, NULL);
- return tmp_dir;
- }
- gchar *get_tmp_file(void)
- {
- gchar *tmp_file;
- static guint32 id = 0;
- tmp_file = g_strdup_printf("%s%ctmpfile.%08x",
- get_tmp_dir(), G_DIR_SEPARATOR, id++);
- return tmp_file;
- }
- const gchar *get_domain_name(void)
- {
- #ifdef G_OS_UNIX
- static gchar *domain_name = NULL;
- if (!domain_name) {
- struct hostent *hp;
- char hostname[256];
- if (gethostname(hostname, sizeof(hostname)) != 0) {
- perror("gethostname");
- domain_name = "unknown";
- } else {
- hostname[sizeof(hostname) - 1] = '\0';
- if ((hp = my_gethostbyname(hostname)) == NULL) {
- perror("gethostbyname");
- domain_name = g_strdup(hostname);
- } else {
- domain_name = g_strdup(hp->h_name);
- }
- }
- debug_print("domain name = %s\n", domain_name);
- }
- return domain_name;
- #else
- return "unknown";
- #endif
- }
- off_t get_file_size(const gchar *file)
- {
- struct stat s;
- if (g_stat(file, &s) < 0) {
- FILE_OP_ERROR(file, "stat");
- return -1;
- }
- return s.st_size;
- }
- time_t get_file_mtime(const gchar *file)
- {
- struct stat s;
- if (g_stat(file, &s) < 0) {
- FILE_OP_ERROR(file, "stat");
- return -1;
- }
- return s.st_mtime;
- }
- off_t get_file_size_as_crlf(const gchar *file)
- {
- FILE *fp;
- off_t size = 0;
- gchar buf[BUFFSIZE];
- if ((fp = g_fopen(file, "rb")) == NULL) {
- FILE_OP_ERROR(file, "g_fopen");
- return -1;
- }
- while (fgets(buf, sizeof(buf), fp) != NULL) {
- strretchomp(buf);
- size += strlen(buf) + 2;
- }
- if (ferror(fp)) {
- FILE_OP_ERROR(file, "fgets");
- size = -1;
- }
- fclose(fp);
- return size;
- }
- gboolean file_exist(const gchar *file, gboolean allow_fifo)
- {
- struct stat s;
- if (file == NULL)
- return FALSE;
- if (g_stat(file, &s) < 0) {
- if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
- return FALSE;
- }
- if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
- return TRUE;
- return FALSE;
- }
- /* Test on whether FILE is a relative file name. This is
- * straightforward for Unix but more complex for Windows. */
- gboolean is_relative_filename(const gchar *file)
- {
- if (!file)
- return TRUE;
- #ifdef G_OS_WIN32
- if ( *file == '\\' && file[1] == '\\' && strchr (file+2, '\\') )
- return FALSE; /* Prefixed with a hostname - this can't
- * be a relative name. */
- if ( ((*file >= 'a' && *file <= 'z')
- || (*file >= 'A' && *file <= 'Z'))
- && file[1] == ':')
- file += 2; /* Skip drive letter. */
- return !(*file == '\\' || *file == '/');
- #else
- return !(*file == G_DIR_SEPARATOR);
- #endif
- }
- gboolean is_dir_exist(const gchar *dir)
- {
- if (dir == NULL)
- return FALSE;
- return g_file_test(dir, G_FILE_TEST_IS_DIR);
- }
- gboolean is_file_entry_exist(const gchar *file)
- {
- if (file == NULL)
- return FALSE;
- return g_file_test(file, G_FILE_TEST_EXISTS);
- }
- gboolean dirent_is_regular_file(struct dirent *d)
- {
- #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
- if (d->d_type == DT_REG)
- return TRUE;
- else if (d->d_type != DT_UNKNOWN)
- return FALSE;
- #endif
- return g_file_test(d->d_name, G_FILE_TEST_IS_REGULAR);
- }
- gint change_dir(const gchar *dir)
- {
- gchar *prevdir = NULL;
- if (debug_mode)
- prevdir = g_get_current_dir();
- if (g_chdir(dir) < 0) {
- FILE_OP_ERROR(dir, "chdir");
- if (debug_mode) g_free(prevdir);
- return -1;
- } else if (debug_mode) {
- gchar *cwd;
- cwd = g_get_current_dir();
- if (strcmp(prevdir, cwd) != 0)
- g_print("current dir: %s\n", cwd);
- g_free(cwd);
- g_free(prevdir);
- }
- return 0;
- }
- gint make_dir(const gchar *dir)
- {
- if (g_mkdir(dir, S_IRWXU) < 0) {
- FILE_OP_ERROR(dir, "mkdir");
- return -1;
- }
- if (g_chmod(dir, S_IRWXU) < 0)
- FILE_OP_ERROR(dir, "chmod");
- return 0;
- }
- gint make_dir_hier(const gchar *dir)
- {
- gchar *parent_dir;
- const gchar *p;
- for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
- parent_dir = g_strndup(dir, p - dir);
- if (*parent_dir != '\0') {
- if (!is_dir_exist(parent_dir)) {
- if (make_dir(parent_dir) < 0) {
- g_free(parent_dir);
- return -1;
- }
- }
- }
- g_free(parent_dir);
- }
- if (!is_dir_exist(dir)) {
- if (make_dir(dir) < 0)
- return -1;
- }
- return 0;
- }
- gint remove_all_files(const gchar *dir)
- {
- GDir *dp;
- const gchar *dir_name;
- gchar *prev_dir;
- prev_dir = g_get_current_dir();
- if (g_chdir(dir) < 0) {
- FILE_OP_ERROR(dir, "chdir");
- g_free(prev_dir);
- return -1;
- }
- if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
- g_warning("failed to open directory: %s\n", dir);
- g_free(prev_dir);
- return -1;
- }
- while ((dir_name = g_dir_read_name(dp)) != NULL) {
- if (claws_unlink(dir_name) < 0)
- FILE_OP_ERROR(dir_name, "unlink");
- }
- g_dir_close(dp);
- if (g_chdir(prev_dir) < 0) {
- FILE_OP_ERROR(prev_dir, "chdir");
- g_free(prev_dir);
- return -1;
- }
- g_free(prev_dir);
- return 0;
- }
- gint remove_numbered_files(const gchar *dir, guint first, guint last)
- {
- GDir *dp;
- const gchar *dir_name;
- gchar *prev_dir;
- gint file_no;
- if (first == last) {
- /* Skip all the dir reading part. */
- gchar *filename = g_strdup_printf("%s%s%u", dir, G_DIR_SEPARATOR_S, first);
- if (claws_unlink(filename) < 0) {
- FILE_OP_ERROR(filename, "unlink");
- g_free(filename);
- return -1;
- }
- g_free(filename);
- return 0;
- }
- prev_dir = g_get_current_dir();
- if (g_chdir(dir) < 0) {
- FILE_OP_ERROR(dir, "chdir");
- g_free(prev_dir);
- return -1;
- }
- if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
- g_warning("failed to open directory: %s\n", dir);
- g_free(prev_dir);
- return -1;
- }
- while ((dir_name = g_dir_read_name(dp)) != NULL) {
- file_no = to_number(dir_name);
- if (file_no > 0 && first <= file_no && file_no <= last) {
- if (is_dir_exist(dir_name))
- continue;
- if (claws_unlink(dir_name) < 0)
- FILE_OP_ERROR(dir_name, "unlink");
- }
- }
- g_dir_close(dp);
- if (g_chdir(prev_dir) < 0) {
- FILE_OP_ERROR(prev_dir, "chdir");
- g_free(prev_dir);
- return -1;
- }
- g_free(prev_dir);
- return 0;
- }
- gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
- {
- GDir *dp;
- const gchar *dir_name;
- gchar *prev_dir;
- gint file_no;
- prev_dir = g_get_current_dir();
- if (g_chdir(dir) < 0) {
- FILE_OP_ERROR(dir, "chdir");
- g_free(prev_dir);
- return -1;
- }
- if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
- FILE_OP_ERROR(dir, "opendir");
- g_free(prev_dir);
- return -1;
- }
- while ((dir_name = g_dir_read_name(dp)) != NULL) {
- file_no = to_number(dir_name);
- if (file_no > 0 && (g_slist_find(numberlist, GINT_TO_POINTER(file_no)) == NULL)) {
- debug_print("removing unwanted file %d from %s\n", file_no, dir);
- if (is_dir_exist(dir_name))
- continue;
- if (claws_unlink(dir_name) < 0)
- FILE_OP_ERROR(dir_name, "unlink");
- }
- }
- g_dir_close(dp);
- if (g_chdir(prev_dir) < 0) {
- FILE_OP_ERROR(prev_dir, "chdir");
- g_free(prev_dir);
- return -1;
- }
- g_free(prev_dir);
- return 0;
- }
- gint remove_all_numbered_files(const gchar *dir)
- {
- return remove_numbered_files(dir, 0, UINT_MAX);
- }
- gint remove_dir_recursive(const gchar *dir)
- {
- struct stat s;
- GDir *dp;
- const gchar *dir_name;
- gchar *prev_dir;
- if (g_stat(dir, &s) < 0) {
- FILE_OP_ERROR(dir, "stat");
- if (ENOENT == errno) return 0;
- return -1;
- }
- if (!S_ISDIR(s.st_mode)) {
- if (claws_unlink(dir) < 0) {
- FILE_OP_ERROR(dir, "unlink");
- return -1;
- }
- return 0;
- }
- prev_dir = g_get_current_dir();
- /* g_print("prev_dir = %s\n", prev_dir); */
- if (!path_cmp(prev_dir, dir)) {
- g_free(prev_dir);
- if (g_chdir("..") < 0) {
- FILE_OP_ERROR(dir, "chdir");
- return -1;
- }
- prev_dir = g_get_current_dir();
- }
- if (g_chdir(dir) < 0) {
- FILE_OP_ERROR(dir, "chdir");
- g_free(prev_dir);
- return -1;
- }
- if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
- g_warning("failed to open directory: %s\n", dir);
- g_chdir(prev_dir);
- g_free(prev_dir);
- return -1;
- }
- /* remove all files in the directory */
- while ((dir_name = g_dir_read_name(dp)) != NULL) {
- /* g_print("removing %s\n", dir_name); */
- if (is_dir_exist(dir_name)) {
- if (remove_dir_recursive(dir_name) < 0) {
- g_warning("can't remove directory\n");
- return -1;
- }
- } else {
- if (claws_unlink(dir_name) < 0)
- FILE_OP_ERROR(dir_name, "unlink");
- }
- }
- g_dir_close(dp);
- if (g_chdir(prev_dir) < 0) {
- FILE_OP_ERROR(prev_dir, "chdir");
- g_free(prev_dir);
- return -1;
- }
- g_free(prev_dir);
- if (g_rmdir(dir) < 0) {
- FILE_OP_ERROR(dir, "rmdir");
- return -1;
- }
- return 0;
- }
- gint rename_force(const gchar *oldpath, const gchar *newpath)
- {
- #ifndef G_OS_UNIX
- if (!is_file_entry_exist(oldpath)) {
- errno = ENOENT;
- return -1;
- }
- if (is_file_exist(newpath)) {
- if (claws_unlink(newpath) < 0)
- FILE_OP_ERROR(newpath, "unlink");
- }
- #endif
- return g_rename(oldpath, newpath);
- }
- /*
- * Append src file body to the tail of dest file.
- * Now keep_backup has no effects.
- */
- gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
- {
- FILE *src_fp, *dest_fp;
- gint n_read;
- gchar buf[BUFSIZ];
- gboolean err = FALSE;
- if ((src_fp = g_fopen(src, "rb")) == NULL) {
- FILE_OP_ERROR(src, "g_fopen");
- return -1;
- }
- if ((dest_fp = g_fopen(dest, "ab")) == NULL) {
- FILE_OP_ERROR(dest, "g_fopen");
- fclose(src_fp);
- return -1;
- }
- if (change_file_mode_rw(dest_fp, dest) < 0) {
- FILE_OP_ERROR(dest, "chmod");
- g_warning("can't change file mode\n");
- }
- while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
- if (n_read < sizeof(buf) && ferror(src_fp))
- break;
- if (fwrite(b…
Large files files are truncated, but you can click here to view the full file