2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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 2 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
40 #include <sys/types.h>
47 #include "statusbar.h"
48 #include "logwindow.h"
52 extern gboolean debug_mode;
54 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data);
56 void list_free_strings(GList *list)
58 list = g_list_first(list);
60 while (list != NULL) {
66 void slist_free_strings(GSList *list)
68 while (list != NULL) {
74 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data)
79 void hash_free_strings(GHashTable *table)
81 g_hash_table_foreach(table, hash_free_strings_func, NULL);
84 static void hash_free_value_mem_func(gpointer key, gpointer value,
90 void hash_free_value_mem(GHashTable *table)
92 g_hash_table_foreach(table, hash_free_value_mem_func, NULL);
95 void ptr_array_free_strings(GPtrArray *array)
100 g_return_if_fail(array != NULL);
102 for (i = 0; i < array->len; i++) {
103 str = g_ptr_array_index(array, i);
108 gint to_number(const gchar *nstr)
110 register const gchar *p;
112 if (*nstr == '\0') return -1;
114 for (p = nstr; *p != '\0'; p++)
115 if (!isdigit(*p)) return -1;
120 /* convert integer into string,
121 nstr must be not lower than 11 characters length */
122 gchar *itos_buf(gchar *nstr, gint n)
124 g_snprintf(nstr, 11, "%d", n);
128 /* convert integer into string */
131 static gchar nstr[11];
133 return itos_buf(nstr, n);
136 gchar *to_human_readable(off_t size)
138 static gchar str[10];
141 g_snprintf(str, sizeof(str), "%dB", (gint)size);
142 else if (size >> 10 < 1024)
143 g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / (1 << 10));
144 else if (size >> 20 < 1024)
145 g_snprintf(str, sizeof(str), "%.2fMB", (gfloat)size / (1 << 20));
147 g_snprintf(str, sizeof(str), "%.2fGB", (gfloat)size / (1 << 30));
152 /* strcmp with NULL-checking */
153 gint strcmp2(const gchar *s1, const gchar *s2)
155 if (s1 == NULL || s2 == NULL)
158 return strcmp(s1, s2);
160 /* strstr with NULL-checking */
161 gchar *strstr2(const gchar *s1, const gchar *s2)
163 if (s1 == NULL || s2 == NULL)
166 return strstr(s1, s2);
169 gint path_cmp(const gchar *s1, const gchar *s2)
173 if (s1 == NULL || s2 == NULL) return -1;
174 if (*s1 == '\0' || *s2 == '\0') return -1;
179 if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
180 if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
182 return strncmp(s1, s2, MAX(len1, len2));
185 /* remove trailing return code */
186 gchar *strretchomp(gchar *str)
190 if (!*str) return str;
192 for (s = str + strlen(str) - 1;
193 s >= str && (*s == '\n' || *s == '\r');
200 /* remove trailing character */
201 gchar *strtailchomp(gchar *str, gchar tail_char)
205 if (!*str) return str;
206 if (tail_char == '\0') return str;
208 for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
214 /* remove CR (carriage return) */
215 gchar *strcrchomp(gchar *str)
219 if (!*str) return str;
221 s = str + strlen(str) - 1;
222 if (*s == '\n' && s > str && *(s - 1) == '\r') {
230 /* Similar to `strstr' but this function ignores the case of both strings. */
231 gchar *strcasestr(const gchar *haystack, const gchar *needle)
233 register size_t haystack_len, needle_len;
235 haystack_len = strlen(haystack);
236 needle_len = strlen(needle);
238 if (haystack_len < needle_len || needle_len == 0)
241 while (haystack_len >= needle_len) {
242 if (!strncasecmp(haystack, needle, needle_len))
243 return (gchar *)haystack;
253 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
254 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
268 /* don't do zero fill */
273 int iswalnum(wint_t wc)
275 return isalnum((int)wc);
280 int iswspace(wint_t wc)
282 return isspace((int)wc);
287 wint_t towlower(wint_t wc)
289 if (wc >= L'A' && wc <= L'Z')
290 return wc + L'a' - L'A';
297 size_t wcslen(const wchar_t *s)
309 /* Copy SRC to DEST. */
310 wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
318 } while (c != L'\0');
325 /* Copy no more than N wide-characters of SRC to DEST. */
326 wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
336 } while (c != L'\0');
347 /* Duplicate S, returning an identical malloc'd string. */
348 wchar_t *wcsdup(const wchar_t *s)
353 new_str = g_new(wchar_t, wcslen(s) + 1);
361 /* Duplicate no more than N wide-characters of S,
362 returning an identical malloc'd string. */
363 wchar_t *wcsndup(const wchar_t *s, size_t n)
368 new_str = g_new(wchar_t, n + 1);
369 wcsncpy(new_str, s, n);
370 new_str[n] = (wchar_t)0;
377 wchar_t *strdup_mbstowcs(const gchar *s)
382 new_str = g_new(wchar_t, strlen(s) + 1);
383 if (mbstowcs(new_str, s, strlen(s) + 1) < 0) {
387 new_str = g_realloc(new_str,
388 sizeof(wchar_t) * (wcslen(new_str) + 1));
395 gchar *strdup_wcstombs(const wchar_t *s)
401 len = wcslen(s) * MB_CUR_MAX + 1;
402 new_str = g_new(gchar, len);
403 if (wcstombs(new_str, s, len) < 0) {
407 new_str = g_realloc(new_str, strlen(new_str) + 1);
414 /* Compare S1 and S2, ignoring case. */
415 gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
421 c1 = towlower(*s1++);
422 c2 = towlower(*s2++);
425 else if (c1 == 0 && c2 == 0)
432 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
433 wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
435 register size_t haystack_len, needle_len;
437 haystack_len = wcslen(haystack);
438 needle_len = wcslen(needle);
440 if (haystack_len < needle_len || needle_len == 0)
443 while (haystack_len >= needle_len) {
444 if (!wcsncasecmp(haystack, needle, needle_len))
445 return (wchar_t *)haystack;
455 /* Examine if next block is non-ASCII string */
456 gboolean is_next_nonascii(const wchar_t *s)
460 /* skip head space */
461 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
463 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
471 /* Examine if next block is multi-byte string */
472 gboolean is_next_mbs(const wchar_t *s)
476 gchar tmp[MB_LEN_MAX];
478 /* skip head space */
479 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
481 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
482 mbl = wctomb(tmp, *wp);
490 wchar_t *find_wspace(const wchar_t *s)
494 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
496 for (; *wp != (wchar_t)0; wp++) {
498 return (wchar_t *)wp;
504 /* compare subjects */
505 gint subject_compare(const gchar *s1, const gchar *s2)
509 if (!s1 || !s2) return -1;
510 if (!*s1 || !*s2) return -1;
512 Xstrdup_a(str1, s1, return -1);
513 Xstrdup_a(str2, s2, return -1);
518 if (!*str1 || !*str2) return -1;
520 return strcmp(str1, str2);
523 void trim_subject(gchar *str)
527 eliminate_parenthesis(str, '[', ']');
528 eliminate_parenthesis(str, '(', ')');
531 while (!strncasecmp(str, "Re:", 3)) {
533 while (isspace(*srcp)) srcp++;
534 memmove(str, srcp, strlen(srcp) + 1);
538 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
540 register gchar *srcp, *destp;
545 while ((destp = strchr(destp, op))) {
551 else if (*srcp == cl)
557 while (isspace(*srcp)) srcp++;
558 memmove(destp, srcp, strlen(srcp) + 1);
562 void extract_parenthesis(gchar *str, gchar op, gchar cl)
564 register gchar *srcp, *destp;
569 while ((srcp = strchr(destp, op))) {
572 memmove(destp, srcp + 1, strlen(srcp));
577 else if (*destp == cl)
589 void extract_one_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
592 register gchar *srcp, *destp;
594 gboolean in_quote = FALSE;
598 if ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
599 memmove(destp, srcp + 1, strlen(srcp));
602 if (*destp == op && !in_quote)
604 else if (*destp == cl && !in_quote)
606 else if (*destp == quote_chr)
618 void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
621 register gchar *srcp, *destp;
623 gboolean in_quote = FALSE;
627 while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
630 memmove(destp, srcp + 1, strlen(srcp));
633 if (*destp == op && !in_quote)
635 else if (*destp == cl && !in_quote)
637 else if (*destp == quote_chr)
649 void eliminate_quote(gchar *str, gchar quote_chr)
651 register gchar *srcp, *destp;
655 while ((destp = strchr(destp, quote_chr))) {
656 if ((srcp = strchr(destp + 1, quote_chr))) {
658 while (isspace(*srcp)) srcp++;
659 memmove(destp, srcp, strlen(srcp) + 1);
667 void extract_quote(gchar *str, gchar quote_chr)
671 if ((str = strchr(str, quote_chr))) {
673 while ((p = strchr(p + 1, quote_chr)) && (p[-1] == '\\')) {
674 memmove(p - 1, p, strlen(p) + 1);
679 memmove(str, str + 1, p - str);
684 void eliminate_address_comment(gchar *str)
686 register gchar *srcp, *destp;
691 while ((destp = strchr(destp, '"'))) {
692 if ((srcp = strchr(destp + 1, '"'))) {
697 while (isspace(*srcp)) srcp++;
698 memmove(destp, srcp, strlen(srcp) + 1);
708 while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
714 else if (*srcp == ')')
720 while (isspace(*srcp)) srcp++;
721 memmove(destp, srcp, strlen(srcp) + 1);
725 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
727 gboolean in_quote = FALSE;
730 if (*str == c && !in_quote)
732 if (*str == quote_chr)
740 gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
742 gboolean in_quote = FALSE;
745 p = str + strlen(str) - 1;
747 if (*p == c && !in_quote)
757 void extract_address(gchar *str)
759 eliminate_address_comment(str);
760 if (strchr_with_skip_quote(str, '"', '<'))
761 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
765 GSList *address_list_append(GSList *addr_list, const gchar *str)
770 if (!str) return addr_list;
772 Xstrdup_a(work, str, return addr_list);
774 eliminate_address_comment(work);
777 while (workp && *workp) {
780 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
786 if (strchr_with_skip_quote(workp, '"', '<'))
787 extract_parenthesis_with_skip_quote
788 (workp, '"', '<', '>');
792 addr_list = g_slist_append(addr_list, g_strdup(workp));
800 GSList *references_list_append(GSList *msgid_list, const gchar *str)
804 if (!str) return msgid_list;
807 while (strp && *strp) {
808 const gchar *start, *end;
811 if ((start = strchr(strp, '<')) != NULL) {
812 end = strchr(start + 1, '>');
817 msgid = g_strndup(start + 1, end - start - 1);
820 msgid_list = g_slist_append(msgid_list, msgid);
830 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
835 if (!str) return group_list;
837 Xstrdup_a(work, str, return group_list);
841 while (workp && *workp) {
844 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
852 group_list = g_slist_append(group_list,
861 GList *add_history(GList *list, const gchar *str)
865 g_return_val_if_fail(str != NULL, list);
867 old = g_list_find_custom(list, (gpointer)str, (GCompareFunc)strcmp2);
870 list = g_list_remove(list, old->data);
871 } else if (g_list_length(list) >= MAX_HISTORY_SIZE) {
874 last = g_list_last(list);
877 g_list_remove(list, last->data);
881 list = g_list_prepend(list, g_strdup(str));
886 void remove_return(gchar *str)
888 register gchar *p = str;
891 if (*p == '\n' || *p == '\r')
892 memmove(p, p + 1, strlen(p));
898 void remove_space(gchar *str)
900 register gchar *p = str;
905 while (isspace(*(p + spc)))
908 memmove(p, p + spc, strlen(p + spc) + 1);
914 void unfold_line(gchar *str)
916 register gchar *p = str;
920 if (*p == '\n' || *p == '\r') {
923 while (isspace(*(p + spc)))
926 memmove(p, p + spc, strlen(p + spc) + 1);
932 void subst_char(gchar *str, gchar orig, gchar subst)
934 register gchar *p = str;
943 void subst_chars(gchar *str, gchar *orig, gchar subst)
945 register gchar *p = str;
948 if (strchr(orig, *p) != NULL)
954 void subst_for_filename(gchar *str)
956 subst_chars(str, " \t\r\n\"/\\", '_');
959 gboolean is_header_line(const gchar *str)
961 if (str[0] == ':') return FALSE;
963 while (*str != '\0' && *str != ' ') {
972 gboolean is_ascii_str(const guchar *str)
974 while (*str != '\0') {
975 if (*str != '\t' && *str != ' ' &&
976 *str != '\r' && *str != '\n' &&
977 (*str < 32 || *str >= 127))
985 gint get_quote_level(const gchar *str)
987 const gchar *first_pos;
988 const gchar *last_pos;
989 const gchar *p = str;
990 gint quote_level = -1;
992 /* speed up line processing by only searching to the last '>' */
993 if ((first_pos = strchr(str, '>')) != NULL) {
994 /* skip a line if it contains a '<' before the initial '>' */
995 if (memchr(str, '<', first_pos - str) != NULL)
997 last_pos = strrchr(first_pos, '>');
1001 while (p <= last_pos) {
1002 while (p < last_pos) {
1011 else if (*p != '-' && !isspace(*p) && p <= last_pos) {
1012 /* any characters are allowed except '-' and space */
1013 while (*p != '-' && *p != '>' && !isspace(*p) &&
1028 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
1030 register guint haystack_len, needle_len;
1031 gboolean in_squote = FALSE, in_dquote = FALSE;
1033 haystack_len = strlen(haystack);
1034 needle_len = strlen(needle);
1036 if (haystack_len < needle_len || needle_len == 0)
1039 while (haystack_len >= needle_len) {
1040 if (!in_squote && !in_dquote &&
1041 !strncmp(haystack, needle, needle_len))
1042 return (gchar *)haystack;
1044 /* 'foo"bar"' -> foo"bar"
1045 "foo'bar'" -> foo'bar' */
1046 if (*haystack == '\'') {
1049 else if (!in_dquote)
1051 } else if (*haystack == '\"') {
1054 else if (!in_squote)
1065 /* this fuction was taken from gstrfuncs.c in glib. */
1066 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1069 GSList *string_list = NULL, *slist;
1070 gchar **str_array, *s, *new_str;
1071 guint i, n = 1, len;
1073 g_return_val_if_fail(str != NULL, NULL);
1074 g_return_val_if_fail(delim != NULL, NULL);
1077 max_tokens = G_MAXINT;
1079 s = strstr_with_skip_quote(str, delim);
1081 guint delimiter_len = strlen(delim);
1085 new_str = g_strndup(str, len);
1087 if (new_str[0] == '\'' || new_str[0] == '\"') {
1088 if (new_str[len - 1] == new_str[0]) {
1089 new_str[len - 1] = '\0';
1090 memmove(new_str, new_str + 1, len - 1);
1093 string_list = g_slist_prepend(string_list, new_str);
1095 str = s + delimiter_len;
1096 s = strstr_with_skip_quote(str, delim);
1097 } while (--max_tokens && s);
1101 new_str = g_strdup(str);
1102 if (new_str[0] == '\'' || new_str[0] == '\"') {
1104 if (new_str[len - 1] == new_str[0]) {
1105 new_str[len - 1] = '\0';
1106 memmove(new_str, new_str + 1, len - 1);
1109 string_list = g_slist_prepend(string_list, new_str);
1113 str_array = g_new(gchar*, n);
1117 str_array[i--] = NULL;
1118 for (slist = string_list; slist; slist = slist->next)
1119 str_array[i--] = slist->data;
1121 g_slist_free(string_list);
1126 gchar *get_abbrev_newsgroup_name(const gchar *group)
1128 gchar *abbrev_group;
1130 const gchar *p = group;
1132 abbrev_group = ap = g_malloc(strlen(group) + 1);
1137 if (strchr(p, '.')) {
1139 while (*p != '.') p++;
1143 return abbrev_group;
1148 return abbrev_group;
1151 GList *uri_list_extract_filenames(const gchar *uri_list)
1153 GList *result = NULL;
1161 while (isspace(*p)) p++;
1162 if (!strncmp(p, "file:", 5)) {
1165 while (*q && *q != '\n' && *q != '\r') q++;
1169 while (q > p && isspace(*q)) q--;
1170 file = g_malloc(q - p + 2);
1171 strncpy(file, p, q - p + 1);
1172 file[q - p + 1] = '\0';
1173 result = g_list_append(result,file);
1177 p = strchr(p, '\n');
1184 #define HEX_TO_INT(val, hex) \
1188 if ('0' <= c && c <= '9') { \
1190 } else if ('a' <= c && c <= 'f') { \
1191 val = c - 'a' + 10; \
1192 } else if ('A' <= c && c <= 'F') { \
1193 val = c - 'A' + 10; \
1200 * We need this wrapper around g_get_home_dir(), so that
1201 * we can fix some Windoze things here. Should be done in glibc of course
1202 * but as long as we are not able to do our own extensions to glibc, we do
1205 gchar *get_home_dir(void)
1207 #if HAVE_DOSISH_SYSTEM
1208 static gchar *home_dir;
1211 home_dir = read_w32_registry_string(NULL,
1212 "Software\\Sylpheed", "HomeDir" );
1213 if (!home_dir || !*home_dir) {
1214 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1215 const char *s = g_get_home_dir();
1217 home_dir = g_strdup (s);
1219 if (!home_dir || !*home_dir)
1220 home_dir = g_strdup ("c:\\sylpheed");
1222 debug_print("initialized home_dir to `%s'\n", home_dir);
1225 #else /* standard glib */
1226 return g_get_home_dir();
1230 gchar *get_rc_dir(void)
1232 static gchar *rc_dir = NULL;
1235 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1241 gchar *get_news_cache_dir(void)
1243 static gchar *news_cache_dir = NULL;
1245 if (!news_cache_dir)
1246 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1247 NEWS_CACHE_DIR, NULL);
1249 return news_cache_dir;
1252 gchar *get_imap_cache_dir(void)
1254 static gchar *imap_cache_dir = NULL;
1256 if (!imap_cache_dir)
1257 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1258 IMAP_CACHE_DIR, NULL);
1260 return imap_cache_dir;
1263 gchar *get_mbox_cache_dir(void)
1265 static gchar *mbox_cache_dir = NULL;
1267 if (!mbox_cache_dir)
1268 mbox_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1269 MBOX_CACHE_DIR, NULL);
1271 return mbox_cache_dir;
1274 gchar *get_mime_tmp_dir(void)
1276 static gchar *mime_tmp_dir = NULL;
1279 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1280 MIME_TMP_DIR, NULL);
1282 return mime_tmp_dir;
1285 gchar *get_template_dir(void)
1287 static gchar *template_dir = NULL;
1290 template_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1291 TEMPLATE_DIR, NULL);
1293 return template_dir;
1296 gchar *get_header_cache_dir(void)
1298 static gchar *header_dir = NULL;
1301 header_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1302 HEADER_CACHE_DIR, NULL);
1307 gchar *get_tmp_file(void)
1309 static gchar *tmp_file = NULL;
1312 tmp_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1318 gchar *get_domain_name(void)
1320 static gchar *domain_name = NULL;
1323 gchar buf[BUFFSIZE] = "";
1325 if (gethostname(buf, sizeof(buf)) < 0) {
1326 perror("gethostname");
1327 strcpy(buf, "unknown");
1330 domain_name = g_strdup(buf);
1336 off_t get_file_size(const gchar *file)
1340 if (stat(file, &s) < 0) {
1341 FILE_OP_ERROR(file, "stat");
1348 off_t get_file_size_as_crlf(const gchar *file)
1352 gchar buf[BUFFSIZE];
1354 if ((fp = fopen(file, "rb")) == NULL) {
1355 FILE_OP_ERROR(file, "fopen");
1359 while (fgets(buf, sizeof(buf), fp) != NULL) {
1361 size += strlen(buf) + 2;
1365 FILE_OP_ERROR(file, "fgets");
1374 off_t get_left_file_size(FILE *fp)
1380 if ((pos = ftell(fp)) < 0) {
1384 if (fseek(fp, 0L, SEEK_END) < 0) {
1388 if ((end = ftell(fp)) < 0) {
1393 if (fseek(fp, pos, SEEK_SET) < 0) {
1401 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1405 if (stat(file, &s) < 0) {
1406 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1410 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1416 gboolean is_dir_exist(const gchar *dir)
1420 if (stat(dir, &s) < 0) {
1421 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1425 if (S_ISDIR(s.st_mode))
1431 gint change_dir(const gchar *dir)
1433 gchar *prevdir = NULL;
1436 prevdir = g_get_current_dir();
1438 if (chdir(dir) < 0) {
1439 FILE_OP_ERROR(dir, "chdir");
1440 if (debug_mode) g_free(prevdir);
1442 } else if (debug_mode) {
1445 cwd = g_get_current_dir();
1446 if (strcmp(prevdir, cwd) != 0)
1447 g_print("current dir: %s\n", cwd);
1455 gint make_dir_hier(const gchar *dir)
1460 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1461 parent_dir = g_strndup(dir, p - dir);
1462 if (*parent_dir != '\0') {
1463 if (!is_dir_exist(parent_dir)) {
1464 if (mkdir(parent_dir, S_IRWXU) < 0) {
1465 FILE_OP_ERROR(parent_dir, "mkdir");
1469 if (chmod(parent_dir, S_IRWXU) < 0)
1470 FILE_OP_ERROR(parent_dir, "chmod");
1475 if (!is_dir_exist(dir)) {
1476 if (mkdir(dir, S_IRWXU) < 0) {
1477 FILE_OP_ERROR(dir, "mkdir");
1480 if (chmod(dir, S_IRWXU) < 0)
1481 FILE_OP_ERROR(dir, "chmod");
1487 gint remove_all_files(const gchar *dir)
1493 prev_dir = g_get_current_dir();
1495 if (chdir(dir) < 0) {
1496 FILE_OP_ERROR(dir, "chdir");
1500 if ((dp = opendir(".")) == NULL) {
1501 FILE_OP_ERROR(dir, "opendir");
1505 while ((d = readdir(dp)) != NULL) {
1506 if (!strcmp(d->d_name, ".") ||
1507 !strcmp(d->d_name, ".."))
1510 if (unlink(d->d_name) < 0)
1511 FILE_OP_ERROR(d->d_name, "unlink");
1516 if (chdir(prev_dir) < 0) {
1517 FILE_OP_ERROR(prev_dir, "chdir");
1527 gint remove_numbered_files(const gchar *dir, guint first, guint last)
1534 prev_dir = g_get_current_dir();
1536 if (chdir(dir) < 0) {
1537 FILE_OP_ERROR(dir, "chdir");
1541 if ((dp = opendir(".")) == NULL) {
1542 FILE_OP_ERROR(dir, "opendir");
1546 while ((d = readdir(dp)) != NULL) {
1547 fileno = to_number(d->d_name);
1548 if (fileno >= 0 && first <= fileno && fileno <= last) {
1549 if (unlink(d->d_name) < 0)
1550 FILE_OP_ERROR(d->d_name, "unlink");
1556 if (chdir(prev_dir) < 0) {
1557 FILE_OP_ERROR(prev_dir, "chdir");
1567 gint remove_all_numbered_files(const gchar *dir)
1569 return remove_numbered_files(dir, 0, UINT_MAX);
1572 gint remove_dir_recursive(const gchar *dir)
1579 /* g_print("dir = %s\n", dir); */
1581 if (stat(dir, &s) < 0) {
1582 FILE_OP_ERROR(dir, "stat");
1583 if (ENOENT == errno) return 0;
1587 if (!S_ISDIR(s.st_mode)) {
1588 if (unlink(dir) < 0) {
1589 FILE_OP_ERROR(dir, "unlink");
1596 prev_dir = g_get_current_dir();
1597 /* g_print("prev_dir = %s\n", prev_dir); */
1599 if (!path_cmp(prev_dir, dir)) {
1601 if (chdir("..") < 0) {
1602 FILE_OP_ERROR(dir, "chdir");
1605 prev_dir = g_get_current_dir();
1608 if (chdir(dir) < 0) {
1609 FILE_OP_ERROR(dir, "chdir");
1614 if ((dp = opendir(".")) == NULL) {
1615 FILE_OP_ERROR(dir, "opendir");
1621 /* remove all files in the directory */
1622 while ((d = readdir(dp)) != NULL) {
1623 if (!strcmp(d->d_name, ".") ||
1624 !strcmp(d->d_name, ".."))
1627 if (stat(d->d_name, &s) < 0) {
1628 FILE_OP_ERROR(d->d_name, "stat");
1632 /* g_print("removing %s\n", d->d_name); */
1634 if (S_ISDIR(s.st_mode)) {
1635 if (remove_dir_recursive(d->d_name) < 0) {
1636 g_warning("can't remove directory\n");
1640 if (unlink(d->d_name) < 0)
1641 FILE_OP_ERROR(d->d_name, "unlink");
1647 if (chdir(prev_dir) < 0) {
1648 FILE_OP_ERROR(prev_dir, "chdir");
1655 if (rmdir(dir) < 0) {
1656 FILE_OP_ERROR(dir, "rmdir");
1664 /* this seems to be slower than the stdio version... */
1665 gint copy_file(const gchar *src, const gchar *dest)
1667 gint src_fd, dest_fd;
1671 gchar *dest_bak = NULL;
1673 if ((src_fd = open(src, O_RDONLY)) < 0) {
1674 FILE_OP_ERROR(src, "open");
1678 if (is_file_exist(dest)) {
1679 dest_bak = g_strconcat(dest, ".bak", NULL);
1680 if (rename(dest, dest_bak) < 0) {
1681 FILE_OP_ERROR(dest, "rename");
1688 if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
1689 FILE_OP_ERROR(dest, "open");
1692 if (rename(dest_bak, dest) < 0)
1693 FILE_OP_ERROR(dest_bak, "rename");
1699 while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
1704 n_write = write(dest_fd, bufp, len);
1706 g_warning(_("writing to %s failed.\n"), dest);
1711 if (rename(dest_bak, dest) < 0)
1712 FILE_OP_ERROR(dest_bak, "rename");
1725 if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
1726 g_warning(_("File copy from %s to %s failed.\n"), src, dest);
1729 if (rename(dest_bak, dest) < 0)
1730 FILE_OP_ERROR(dest_bak, "rename");
1741 gint copy_file(const gchar *src, const gchar *dest)
1743 FILE *src_fp, *dest_fp;
1746 gchar *dest_bak = NULL;
1747 gboolean err = FALSE;
1749 if ((src_fp = fopen(src, "rb")) == NULL) {
1750 FILE_OP_ERROR(src, "fopen");
1753 if (is_file_exist(dest)) {
1754 dest_bak = g_strconcat(dest, ".bak", NULL);
1755 if (rename(dest, dest_bak) < 0) {
1756 FILE_OP_ERROR(dest, "rename");
1763 if ((dest_fp = fopen(dest, "wb")) == NULL) {
1764 FILE_OP_ERROR(dest, "fopen");
1767 if (rename(dest_bak, dest) < 0)
1768 FILE_OP_ERROR(dest_bak, "rename");
1774 if (change_file_mode_rw(dest_fp, dest) < 0) {
1775 FILE_OP_ERROR(dest, "chmod");
1776 g_warning(_("can't change file mode\n"));
1779 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
1780 if (n_read < sizeof(buf) && ferror(src_fp))
1782 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
1783 g_warning(_("writing to %s failed.\n"), dest);
1788 if (rename(dest_bak, dest) < 0)
1789 FILE_OP_ERROR(dest_bak, "rename");
1796 if (ferror(src_fp)) {
1797 FILE_OP_ERROR(src, "fread");
1801 if (fclose(dest_fp) == EOF) {
1802 FILE_OP_ERROR(dest, "fclose");
1809 if (rename(dest_bak, dest) < 0)
1810 FILE_OP_ERROR(dest_bak, "rename");
1821 gint move_file(const gchar *src, const gchar *dest)
1823 if (is_file_exist(dest)) {
1824 g_warning(_("move_file(): file %s already exists."), dest);
1828 if (rename(src, dest) == 0) return 0;
1830 if (EXDEV != errno) {
1831 FILE_OP_ERROR(src, "rename");
1835 if (copy_file(src, dest) < 0) return -1;
1842 gint change_file_mode_rw(FILE *fp, const gchar *file)
1845 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
1847 return chmod(file, S_IRUSR|S_IWUSR);
1851 FILE *my_tmpfile(void)
1854 const gchar suffix[] = ".XXXXXX";
1855 const gchar *tmpdir;
1857 const gchar *progname;
1863 tmpdir = g_get_tmp_dir();
1864 tmplen = strlen(tmpdir);
1865 progname = g_get_prgname();
1866 proglen = strlen(progname);
1867 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
1870 memcpy(fname, tmpdir, tmplen);
1871 fname[tmplen] = G_DIR_SEPARATOR;
1872 memcpy(fname + tmplen + 1, progname, proglen);
1873 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
1875 fd = mkstemp(fname);
1881 fp = fdopen(fd, "w+b");
1886 #endif /* HAVE_MKSTEMP */
1891 FILE *str_open_as_stream(const gchar *str)
1896 g_return_val_if_fail(str != NULL, NULL);
1900 FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
1905 if (len == 0) return fp;
1907 if (fwrite(str, len, 1, fp) != 1) {
1908 FILE_OP_ERROR("str_open_as_stream", "fwrite");
1917 gint execute_async(gchar *const argv[])
1921 if ((pid = fork()) < 0) {
1926 if (pid == 0) { /* child process */
1929 if ((gch_pid = fork()) < 0) {
1934 if (gch_pid == 0) { /* grandchild process */
1935 execvp(argv[0], argv);
1944 waitpid(pid, NULL, 0);
1949 gint execute_sync(gchar *const argv[])
1953 if ((pid = fork()) < 0) {
1958 if (pid == 0) { /* child process */
1959 execvp(argv[0], argv);
1965 waitpid(pid, NULL, 0);
1970 gint execute_command_line(const gchar *cmdline, gboolean async)
1975 argv = strsplit_with_quote(cmdline, " ", 0);
1978 ret = execute_async(argv);
1980 ret = execute_sync(argv);
1986 static gint is_unchanged_uri_char(char c)
1998 void encode_uri(gchar *encoded_uri, gint bufsize, const gchar *uri)
2004 for(i = 0; i < strlen(uri) ; i++) {
2005 if (is_unchanged_uri_char(uri[i])) {
2006 if (k + 2 >= bufsize)
2008 encoded_uri[k++] = uri[i];
2011 char * hexa = "0123456789ABCDEF";
2013 if (k + 4 >= bufsize)
2015 encoded_uri[k++] = '%';
2016 encoded_uri[k++] = hexa[uri[i] / 16];
2017 encoded_uri[k++] = hexa[uri[i] % 16];
2023 /* Converts two-digit hexadecimal to decimal. Used for unescaping escaped
2026 static gint axtoi(const gchar *hexstr)
2028 gint hi, lo, result;
2031 if ('0' <= hi && hi <= '9') {
2034 if ('a' <= hi && hi <= 'f') {
2037 if ('A' <= hi && hi <= 'F') {
2042 if ('0' <= lo && lo <= '9') {
2045 if ('a' <= lo && lo <= 'f') {
2048 if ('A' <= lo && lo <= 'F') {
2051 result = lo + (16 * hi);
2056 /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
2057 * plusses, and escape characters are used)
2060 void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
2062 const gchar *encoded;
2065 encoded = encoded_uri;
2066 decoded = decoded_uri;
2069 if (*encoded == '%') {
2071 if (isxdigit(encoded[0])
2072 && isxdigit(encoded[1])) {
2073 *decoded = (gchar) axtoi(encoded);
2078 else if (*encoded == '+') {
2084 *decoded = *encoded;
2094 gint open_uri(const gchar *uri, const gchar *cmdline)
2096 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
2097 gchar buf[BUFFSIZE];
2099 gchar encoded_uri[BUFFSIZE];
2101 g_return_val_if_fail(uri != NULL, -1);
2103 /* an option to choose whether to use encode_uri or not ? */
2104 encode_uri(encoded_uri, BUFFSIZE, uri);
2107 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
2108 !strchr(p + 2, '%'))
2109 g_snprintf(buf, sizeof(buf), cmdline, encoded_uri);
2112 g_warning(_("Open URI command line is invalid: `%s'"),
2114 g_snprintf(buf, sizeof(buf), default_cmdline, encoded_uri);
2117 execute_command_line(buf, TRUE);
2122 time_t remote_tzoffset_sec(const gchar *zone)
2124 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
2130 time_t remoteoffset;
2132 strncpy(zone3, zone, 3);
2136 if (sscanf(zone, "%c%d", &c, &offset) == 2 &&
2137 (c == '+' || c == '-')) {
2138 remoteoffset = ((offset / 100) * 60 + (offset % 100)) * 60;
2140 remoteoffset = -remoteoffset;
2141 } else if (!strncmp(zone, "UT" , 2) ||
2142 !strncmp(zone, "GMT", 2)) {
2144 } else if (strlen(zone3) == 3 &&
2145 (p = strstr(ustzstr, zone3)) != NULL &&
2146 (p - ustzstr) % 3 == 0) {
2147 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
2148 remoteoffset = iustz * 3600;
2149 } else if (strlen(zone3) == 1) {
2151 case 'Z': remoteoffset = 0; break;
2152 case 'A': remoteoffset = -1; break;
2153 case 'B': remoteoffset = -2; break;
2154 case 'C': remoteoffset = -3; break;
2155 case 'D': remoteoffset = -4; break;
2156 case 'E': remoteoffset = -5; break;
2157 case 'F': remoteoffset = -6; break;
2158 case 'G': remoteoffset = -7; break;
2159 case 'H': remoteoffset = -8; break;
2160 case 'I': remoteoffset = -9; break;
2161 case 'K': remoteoffset = -10; break; /* J is not used */
2162 case 'L': remoteoffset = -11; break;
2163 case 'M': remoteoffset = -12; break;
2164 case 'N': remoteoffset = 1; break;
2165 case 'O': remoteoffset = 2; break;
2166 case 'P': remoteoffset = 3; break;
2167 case 'Q': remoteoffset = 4; break;
2168 case 'R': remoteoffset = 5; break;
2169 case 'S': remoteoffset = 6; break;
2170 case 'T': remoteoffset = 7; break;
2171 case 'U': remoteoffset = 8; break;
2172 case 'V': remoteoffset = 9; break;
2173 case 'W': remoteoffset = 10; break;
2174 case 'X': remoteoffset = 11; break;
2175 case 'Y': remoteoffset = 12; break;
2176 default: remoteoffset = 0; break;
2178 remoteoffset = remoteoffset * 3600;
2181 return remoteoffset;
2184 time_t tzoffset_sec(time_t *now)
2190 lt = localtime(now);
2192 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
2194 if (lt->tm_year < gmt.tm_year)
2196 else if (lt->tm_year > gmt.tm_year)
2198 else if (lt->tm_yday < gmt.tm_yday)
2200 else if (lt->tm_yday > gmt.tm_yday)
2203 if (off >= 24 * 60) /* should be impossible */
2204 off = 23 * 60 + 59; /* if not, insert silly value */
2205 if (off <= -24 * 60)
2206 off = -(23 * 60 + 59);
2215 /* calculate timezone offset */
2216 gchar *tzoffset(time_t *now)
2218 static gchar offset_string[6];
2224 lt = localtime(now);
2226 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
2228 if (lt->tm_year < gmt.tm_year)
2230 else if (lt->tm_year > gmt.tm_year)
2232 else if (lt->tm_yday < gmt.tm_yday)
2234 else if (lt->tm_yday > gmt.tm_yday)
2242 if (off >= 24 * 60) /* should be impossible */
2243 off = 23 * 60 + 59; /* if not, insert silly value */
2245 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
2247 return offset_string;
2250 void get_rfc822_date(gchar *buf, gint len)
2254 gchar day[4], mon[4];
2255 gint dd, hh, mm, ss, yyyy;
2260 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
2261 day, mon, &dd, &hh, &mm, &ss, &yyyy);
2262 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
2263 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
2266 static FILE *log_fp = NULL;
2268 void set_log_file(const gchar *filename)
2271 log_fp = fopen(filename, "wb");
2273 FILE_OP_ERROR(filename, "fopen");
2276 void close_log_file(void)
2284 static guint log_verbosity_count = 0;
2286 void log_verbosity_set(gboolean verbose)
2289 log_verbosity_count++;
2290 else if (log_verbosity_count > 0)
2291 log_verbosity_count--;
2294 void debug_print_real(const gchar *format, ...)
2297 gchar buf[BUFFSIZE];
2299 if (!debug_mode) return;
2301 va_start(args, format);
2302 g_vsnprintf(buf, sizeof(buf), format, args);
2308 void log_print(const gchar *format, ...)
2311 gchar buf[BUFFSIZE];
2316 va_start(args, format);
2317 g_vsnprintf(buf, sizeof(buf), format, args);
2321 strftime(timestr, 6, "%H:%M", localtime(&t));
2322 logbuf = g_strdup_printf("[%s] %s", timestr, buf);
2324 if (debug_mode) fputs(logbuf, stdout);
2325 log_window_append(logbuf, LOG_NORMAL);
2327 fputs(logbuf, log_fp);
2330 if (log_verbosity_count)
2331 statusbar_puts_all(buf);
2335 void log_message(const gchar *format, ...)
2338 gchar buf[BUFFSIZE];
2340 va_start(args, format);
2341 g_vsnprintf(buf, sizeof(buf), format, args);
2344 if (debug_mode) g_message("%s", buf);
2345 log_window_append(buf, LOG_MSG);
2347 fputs("message: ", log_fp);
2351 statusbar_puts_all(buf);
2354 void log_warning(const gchar *format, ...)
2357 gchar buf[BUFFSIZE];
2359 va_start(args, format);
2360 g_vsnprintf(buf, sizeof(buf), format, args);
2363 g_warning("%s", buf);
2364 log_window_append(buf, LOG_WARN);
2366 fputs("*** warning: ", log_fp);
2372 void log_error(const gchar *format, ...)
2375 gchar buf[BUFFSIZE];
2377 va_start(args, format);
2378 g_vsnprintf(buf, sizeof(buf), format, args);
2381 g_warning("%s", buf);
2382 log_window_append(buf, LOG_ERROR);
2384 fputs("*** error: ", log_fp);
2391 void * subject_table_lookup(GHashTable *subject_table, gchar * subject)
2393 if (subject == NULL)
2396 if (g_strncasecmp(subject, "Re: ", 4) == 0)
2397 return g_hash_table_lookup(subject_table, subject + 4);
2399 return g_hash_table_lookup(subject_table, subject);
2402 void subject_table_insert(GHashTable *subject_table, gchar * subject,
2405 if (subject == NULL)
2409 if (g_strcasecmp(subject, "Re:") == 0)
2411 if (g_strcasecmp(subject, "Re: ") == 0)
2414 if (g_strncasecmp(subject, "Re: ", 4) == 0)
2415 g_hash_table_insert(subject_table, subject + 4, data);
2417 g_hash_table_insert(subject_table, subject, data);
2420 void subject_table_remove(GHashTable *subject_table, gchar * subject)
2422 if (subject == NULL)
2425 if (g_strncasecmp(subject, "Re: ", 4) == 0)
2426 g_hash_table_remove(subject_table, subject + 4);
2428 g_hash_table_remove(subject_table, subject);
2431 gboolean subject_is_reply(const gchar *subject)
2433 /* XXX: just simply here so someone can handle really
2434 * advanced Re: detection like "Re[4]", "ANTW:" or
2435 * Re: Re: Re: Re: Re: Re: Re: Re:" stuff. */
2436 if (subject == NULL) return FALSE;
2437 else return 0 == g_strncasecmp(subject, "Re: ", 4);