2005-01-03 [colin] 0.9.13cvs25.4
[claws.git] / src / common / utils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto & The Sylpheed-Claws 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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <errno.h>
31
32 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
33 #  include <wchar.h>
34 #  include <wctype.h>
35 #endif
36 #include <stdlib.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <stdarg.h>
40 #include <sys/types.h>
41 #include <sys/wait.h>
42 #include <dirent.h>
43 #include <time.h>
44 #include <regex.h>
45 #include <sys/utsname.h>
46
47 #include "intl.h"
48 #include "utils.h"
49 #include "socket.h"
50 #include "../codeconv.h"
51
52 #define BUFFSIZE        8192
53
54 static gboolean debug_mode = FALSE;
55
56 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data);
57
58 void list_free_strings(GList *list)
59 {
60         list = g_list_first(list);
61
62         while (list != NULL) {
63                 g_free(list->data);
64                 list = list->next;
65         }
66 }
67
68 void slist_free_strings(GSList *list)
69 {
70         while (list != NULL) {
71                 g_free(list->data);
72                 list = list->next;
73         }
74 }
75
76 GSList *slist_concat_unique (GSList *first, GSList *second)
77 {
78         GSList *tmp, *ret;
79         if (first == NULL) {
80                 if (second == NULL)
81                         return NULL;
82                 else 
83                         return second;
84         } else if (second == NULL)
85                 return first;
86         ret = first;
87         for (tmp = second; tmp != NULL; tmp = g_slist_next(tmp)) {
88                 if (g_slist_find(ret, tmp->data) == NULL)
89                         ret = g_slist_prepend(ret, tmp->data);
90         }
91         return ret;
92 }
93  
94 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data)
95 {
96         g_free(key);
97 }
98
99 void hash_free_strings(GHashTable *table)
100 {
101         g_hash_table_foreach(table, hash_free_strings_func, NULL);
102 }
103
104 static void hash_free_value_mem_func(gpointer key, gpointer value,
105                                      gpointer data)
106 {
107         g_free(value);
108 }
109
110 void hash_free_value_mem(GHashTable *table)
111 {
112         g_hash_table_foreach(table, hash_free_value_mem_func, NULL);
113 }
114
115 gint str_case_equal(gconstpointer v, gconstpointer v2)
116 {
117         return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
118 }
119
120 guint str_case_hash(gconstpointer key)
121 {
122         const gchar *p = key;
123         guint h = *p;
124
125         if (h) {
126                 h = tolower(h);
127                 for (p += 1; *p != '\0'; p++)
128                         h = (h << 5) - h + tolower(*p);
129         }
130
131         return h;
132 }
133
134 void ptr_array_free_strings(GPtrArray *array)
135 {
136         gint i;
137         gchar *str;
138
139         g_return_if_fail(array != NULL);
140
141         for (i = 0; i < array->len; i++) {
142                 str = g_ptr_array_index(array, i);
143                 g_free(str);
144         }
145 }
146
147 gboolean str_find(const gchar *haystack, const gchar *needle)
148 {
149         return strstr(haystack, needle) != NULL ? TRUE : FALSE;
150 }
151
152 gboolean str_case_find(const gchar *haystack, const gchar *needle)
153 {
154         return strcasestr(haystack, needle) != NULL ? TRUE : FALSE;
155 }
156
157 gboolean str_find_equal(const gchar *haystack, const gchar *needle)
158 {
159         return strcmp(haystack, needle) == 0;
160 }
161
162 gboolean str_case_find_equal(const gchar *haystack, const gchar *needle)
163 {
164         return strcasecmp(haystack, needle) == 0;
165 }
166
167 gint to_number(const gchar *nstr)
168 {
169         register const guchar *p;
170
171         if (*nstr == '\0') return -1;
172
173         for (p = nstr; *p != '\0'; p++)
174                 if (!isdigit(*p)) return -1;
175
176         return atoi(nstr);
177 }
178
179 /* convert integer into string,
180    nstr must be not lower than 11 characters length */
181 gchar *itos_buf(gchar *nstr, gint n)
182 {
183         g_snprintf(nstr, 11, "%d", n);
184         return nstr;
185 }
186
187 /* convert integer into string */
188 gchar *itos(gint n)
189 {
190         static gchar nstr[11];
191
192         return itos_buf(nstr, n);
193 }
194
195 gchar *to_human_readable(off_t size)
196 {
197         static gchar str[10];
198
199         if (size < 1024)
200                 g_snprintf(str, sizeof(str), _("%dB"), (gint)size);
201         else if (size >> 10 < 1024)
202                 g_snprintf(str, sizeof(str), _("%.1fKB"), (gfloat)size / (1 << 10));
203         else if (size >> 20 < 1024)
204                 g_snprintf(str, sizeof(str), _("%.2fMB"), (gfloat)size / (1 << 20));
205         else
206                 g_snprintf(str, sizeof(str), _("%.2fGB"), (gfloat)size / (1 << 30));
207
208         return str;
209 }
210
211 /* strcmp with NULL-checking */
212 gint strcmp2(const gchar *s1, const gchar *s2)
213 {
214         if (s1 == NULL || s2 == NULL)
215                 return -1;
216         else
217                 return strcmp(s1, s2);
218 }
219 /* strstr with NULL-checking */
220 gchar *strstr2(const gchar *s1, const gchar *s2)
221 {
222         if (s1 == NULL || s2 == NULL)
223                 return NULL;
224         else
225                 return strstr(s1, s2);
226 }
227 /* compare paths */
228 gint path_cmp(const gchar *s1, const gchar *s2)
229 {
230         gint len1, len2;
231
232         if (s1 == NULL || s2 == NULL) return -1;
233         if (*s1 == '\0' || *s2 == '\0') return -1;
234
235         len1 = strlen(s1);
236         len2 = strlen(s2);
237
238         if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
239         if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
240
241         return strncmp(s1, s2, MAX(len1, len2));
242 }
243
244 /* remove trailing return code */
245 gchar *strretchomp(gchar *str)
246 {
247         register gchar *s;
248
249         if (!*str) return str;
250
251         for (s = str + strlen(str) - 1;
252              s >= str && (*s == '\n' || *s == '\r');
253              s--)
254                 *s = '\0';
255
256         return str;
257 }
258
259 /* remove trailing character */
260 gchar *strtailchomp(gchar *str, gchar tail_char)
261 {
262         register gchar *s;
263
264         if (!*str) return str;
265         if (tail_char == '\0') return str;
266
267         for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
268                 *s = '\0';
269
270         return str;
271 }
272
273 /* remove CR (carriage return) */
274 gchar *strcrchomp(gchar *str)
275 {
276         register gchar *s;
277
278         if (!*str) return str;
279
280         s = str + strlen(str) - 1;
281         if (*s == '\n' && s > str && *(s - 1) == '\r') {
282                 *(s - 1) = '\n';
283                 *s = '\0';
284         }
285
286         return str;
287 }
288
289 /* Similar to `strstr' but this function ignores the case of both strings.  */
290 gchar *strcasestr(const gchar *haystack, const gchar *needle)
291 {
292         register size_t haystack_len, needle_len;
293
294         haystack_len = strlen(haystack);
295         needle_len   = strlen(needle);
296
297         if (haystack_len < needle_len || needle_len == 0)
298                 return NULL;
299
300         while (haystack_len >= needle_len) {
301                 if (!g_ascii_strncasecmp(haystack, needle, needle_len))
302                         return (gchar *)haystack;
303                 else {
304                         haystack++;
305                         haystack_len--;
306                 }
307         }
308
309         return NULL;
310 }
311
312 gpointer my_memmem(gconstpointer haystack, size_t haystacklen,
313                    gconstpointer needle, size_t needlelen)
314 {
315         const gchar *haystack_ = (const gchar *)haystack;
316         const gchar *needle_ = (const gchar *)needle;
317         const gchar *haystack_cur = (const gchar *)haystack;
318
319         if (needlelen == 1)
320                 return memchr(haystack_, *needle_, haystacklen);
321
322         while ((haystack_cur = memchr(haystack_cur, *needle_, haystacklen))
323                != NULL) {
324                 if (haystacklen - (haystack_cur - haystack_) < needlelen)
325                         break;
326                 if (memcmp(haystack_cur + 1, needle_ + 1, needlelen - 1) == 0)
327                         return (gpointer)haystack_cur;
328                 else
329                         haystack_cur++;
330         }
331
332         return NULL;
333 }
334
335 /* Copy no more than N characters of SRC to DEST, with NULL terminating.  */
336 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
337 {
338         register gchar c;
339         gchar *s = dest;
340
341         do {
342                 if (--n == 0) {
343                         *dest = '\0';
344                         return s;
345                 }
346                 c = *src++;
347                 *dest++ = c;
348         } while (c != '\0');
349
350         /* don't do zero fill */
351         return s;
352 }
353
354 #if !HAVE_ISWALNUM
355 int iswalnum(wint_t wc)
356 {
357         return isalnum((int)wc);
358 }
359 #endif
360
361 #if !HAVE_ISWSPACE
362 int iswspace(wint_t wc)
363 {
364         return isspace((int)wc);
365 }
366 #endif
367
368 #if !HAVE_TOWLOWER
369 wint_t towlower(wint_t wc)
370 {
371         if (wc >= L'A' && wc <= L'Z')
372                 return wc + L'a' - L'A';
373
374         return wc;
375 }
376 #endif
377
378 #if !HAVE_WCSLEN
379 size_t wcslen(const wchar_t *s)
380 {
381         size_t len = 0;
382
383         while (*s != L'\0')
384                 ++len, ++s;
385
386         return len;
387 }
388 #endif
389
390 #if !HAVE_WCSCPY
391 /* Copy SRC to DEST.  */
392 wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
393 {
394         wint_t c;
395         wchar_t *s = dest;
396
397         do {
398                 c = *src++;
399                 *dest++ = c;
400         } while (c != L'\0');
401
402         return s;
403 }
404 #endif
405
406 #if !HAVE_WCSNCPY
407 /* Copy no more than N wide-characters of SRC to DEST.  */
408 wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
409 {
410         wint_t c;
411         wchar_t *s = dest;
412
413         do {
414                 c = *src++;
415                 *dest++ = c;
416                 if (--n == 0)
417                         return s;
418         } while (c != L'\0');
419
420         /* zero fill */
421         do
422                 *dest++ = L'\0';
423         while (--n > 0);
424
425         return s;
426 }
427 #endif
428
429 /* Duplicate S, returning an identical malloc'd string. */
430 wchar_t *wcsdup(const wchar_t *s)
431 {
432         wchar_t *new_str;
433
434         if (s) {
435                 new_str = g_new(wchar_t, wcslen(s) + 1);
436                 wcscpy(new_str, s);
437         } else
438                 new_str = NULL;
439
440         return new_str;
441 }
442
443 /* Duplicate no more than N wide-characters of S,
444    returning an identical malloc'd string. */
445 wchar_t *wcsndup(const wchar_t *s, size_t n)
446 {
447         wchar_t *new_str;
448
449         if (s) {
450                 new_str = g_new(wchar_t, n + 1);
451                 wcsncpy(new_str, s, n);
452                 new_str[n] = (wchar_t)0;
453         } else
454                 new_str = NULL;
455
456         return new_str;
457 }
458
459 wchar_t *strdup_mbstowcs(const gchar *s)
460 {
461         wchar_t *new_str;
462
463         if (s) {
464                 new_str = g_new(wchar_t, strlen(s) + 1);
465                 if (mbstowcs(new_str, s, strlen(s) + 1) < 0) {
466                         g_free(new_str);
467                         new_str = NULL;
468                 } else
469                         new_str = g_realloc(new_str,
470                                             sizeof(wchar_t) * (wcslen(new_str) + 1));
471         } else
472                 new_str = NULL;
473
474         return new_str;
475 }
476
477 gchar *strdup_wcstombs(const wchar_t *s)
478 {
479         gchar *new_str;
480         size_t len;
481
482         if (s) {
483                 len = wcslen(s) * MB_CUR_MAX + 1;
484                 new_str = g_new(gchar, len);
485                 if (wcstombs(new_str, s, len) < 0) {
486                         g_free(new_str);
487                         new_str = NULL;
488                 } else
489                         new_str = g_realloc(new_str, strlen(new_str) + 1);
490         } else
491                 new_str = NULL;
492
493         return new_str;
494 }
495
496 /* Compare S1 and S2, ignoring case.  */
497 gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
498 {
499         wint_t c1;
500         wint_t c2;
501
502         while (n--) {
503                 c1 = towlower(*s1++);
504                 c2 = towlower(*s2++);
505                 if (c1 != c2)
506                         return c1 - c2;
507                 else if (c1 == 0 && c2 == 0)
508                         break;
509         }
510
511         return 0;
512 }
513
514 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case.  */
515 wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
516 {
517         register size_t haystack_len, needle_len;
518
519         haystack_len = wcslen(haystack);
520         needle_len   = wcslen(needle);
521
522         if (haystack_len < needle_len || needle_len == 0)
523                 return NULL;
524
525         while (haystack_len >= needle_len) {
526                 if (!wcsncasecmp(haystack, needle, needle_len))
527                         return (wchar_t *)haystack;
528                 else {
529                         haystack++;
530                         haystack_len--;
531                 }
532         }
533
534         return NULL;
535 }
536
537 gint get_mbs_len(const gchar *s)
538 {
539         const gchar *p = s;
540         gint mb_len;
541         gint len = 0;
542
543         if (!p)
544                 return -1;
545
546         while (*p != '\0') {
547                 mb_len = mblen(p, MB_LEN_MAX);
548                 if (mb_len == 0)
549                         break;
550                 else if (mb_len < 0)
551                         return -1;
552                 else
553                         len++;
554
555                 p += mb_len;
556         }
557
558         return len;
559 }
560
561 /* Examine if next block is non-ASCII string */
562 gboolean is_next_nonascii(const guchar *s)
563 {
564         const guchar *p;
565
566         /* skip head space */
567         for (p = s; *p != '\0' && isspace(*p); p++)
568                 ;
569         for (; *p != '\0' && !isspace(*p); p++) {
570                 if (*p > 127 || *p < 32)
571                         return TRUE;
572         }
573
574         return FALSE;
575 }
576
577 gint get_next_word_len(const guchar *s)
578 {
579         gint len = 0;
580
581         for (; *s != '\0' && !isspace(*s); s++, len++)
582                 ;
583
584         return len;
585 }
586
587 /* compare subjects */
588 gint subject_compare(const gchar *s1, const gchar *s2)
589 {
590         gchar *str1, *str2;
591
592         if (!s1 || !s2) return -1;
593         if (!*s1 || !*s2) return -1;
594
595         Xstrdup_a(str1, s1, return -1);
596         Xstrdup_a(str2, s2, return -1);
597
598         trim_subject_for_compare(str1);
599         trim_subject_for_compare(str2);
600
601         if (!*str1 || !*str2) return -1;
602
603         return strcmp(str1, str2);
604 }
605
606 gint subject_compare_for_sort(const gchar *s1, const gchar *s2)
607 {
608         gchar *str1, *str2;
609
610         if (!s1 || !s2) return -1;
611
612         Xstrdup_a(str1, s1, return -1);
613         Xstrdup_a(str2, s2, return -1);
614
615         trim_subject_for_sort(str1);
616         trim_subject_for_sort(str2);
617
618         return g_utf8_collate(str1, str2);
619 }
620
621 void trim_subject_for_compare(gchar *str)
622 {
623         gchar *srcp;
624
625         eliminate_parenthesis(str, '[', ']');
626         eliminate_parenthesis(str, '(', ')');
627         g_strstrip(str);
628
629         srcp = str + subject_get_prefix_length(str);
630         if (srcp != str)
631                 memmove(str, srcp, strlen(srcp) + 1);
632 }
633
634 void trim_subject_for_sort(gchar *str)
635 {
636         gchar *srcp;
637
638         g_strstrip(str);
639
640         srcp = str + subject_get_prefix_length(str);
641         if (srcp != str)        
642                 memmove(str, srcp, strlen(srcp) + 1);
643 }
644
645 void trim_subject(gchar *str)
646 {
647         register guchar *srcp, *destp;
648         gchar op, cl;
649         gint in_brace;
650
651         destp = str + subject_get_prefix_length(str);
652
653         if (*destp == '[') {
654                 op = '[';
655                 cl = ']';
656         } else if (*destp == '(') {
657                 op = '(';
658                 cl = ')';
659         } else
660                 return;
661
662         srcp = destp + 1;
663         in_brace = 1;
664         while (*srcp) {
665                 if (*srcp == op)
666                         in_brace++;
667                 else if (*srcp == cl)
668                         in_brace--;
669                 srcp++;
670                 if (in_brace == 0)
671                         break;
672         }
673         while (isspace(*srcp)) srcp++;
674         memmove(destp, srcp, strlen(srcp) + 1);
675 }
676
677 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
678 {
679         register guchar *srcp, *destp;
680         gint in_brace;
681
682         srcp = destp = str;
683
684         while ((destp = strchr(destp, op))) {
685                 in_brace = 1;
686                 srcp = destp + 1;
687                 while (*srcp) {
688                         if (*srcp == op)
689                                 in_brace++;
690                         else if (*srcp == cl)
691                                 in_brace--;
692                         srcp++;
693                         if (in_brace == 0)
694                                 break;
695                 }
696                 while (isspace(*srcp)) srcp++;
697                 memmove(destp, srcp, strlen(srcp) + 1);
698         }
699 }
700
701 void extract_parenthesis(gchar *str, gchar op, gchar cl)
702 {
703         register gchar *srcp, *destp;
704         gint in_brace;
705
706         srcp = destp = str;
707
708         while ((srcp = strchr(destp, op))) {
709                 if (destp > str)
710                         *destp++ = ' ';
711                 memmove(destp, srcp + 1, strlen(srcp));
712                 in_brace = 1;
713                 while(*destp) {
714                         if (*destp == op)
715                                 in_brace++;
716                         else if (*destp == cl)
717                                 in_brace--;
718
719                         if (in_brace == 0)
720                                 break;
721
722                         destp++;
723                 }
724         }
725         *destp = '\0';
726 }
727
728 void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
729                                          gchar op, gchar cl)
730 {
731         register gchar *srcp, *destp;
732         gint in_brace;
733         gboolean in_quote = FALSE;
734
735         srcp = destp = str;
736
737         while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
738                 if (destp > str)
739                         *destp++ = ' ';
740                 memmove(destp, srcp + 1, strlen(srcp));
741                 in_brace = 1;
742                 while(*destp) {
743                         if (*destp == op && !in_quote)
744                                 in_brace++;
745                         else if (*destp == cl && !in_quote)
746                                 in_brace--;
747                         else if (*destp == quote_chr)
748                                 in_quote ^= TRUE;
749
750                         if (in_brace == 0)
751                                 break;
752
753                         destp++;
754                 }
755         }
756         *destp = '\0';
757 }
758
759 void eliminate_quote(gchar *str, gchar quote_chr)
760 {
761         register guchar *srcp, *destp;
762
763         srcp = destp = str;
764
765         while ((destp = strchr(destp, quote_chr))) {
766                 if ((srcp = strchr(destp + 1, quote_chr))) {
767                         srcp++;
768                         while (isspace(*srcp)) srcp++;
769                         memmove(destp, srcp, strlen(srcp) + 1);
770                 } else {
771                         *destp = '\0';
772                         break;
773                 }
774         }
775 }
776
777 void extract_quote(gchar *str, gchar quote_chr)
778 {
779         register gchar *p;
780
781         if ((str = strchr(str, quote_chr))) {
782                 p = str;
783                 while ((p = strchr(p + 1, quote_chr)) && (p[-1] == '\\')) {
784                         memmove(p - 1, p, strlen(p) + 1);
785                         p--;
786                 }
787                 if(p) {
788                         *p = '\0';
789                         memmove(str, str + 1, p - str);
790                 }
791         }
792 }
793
794 void eliminate_address_comment(gchar *str)
795 {
796         register guchar *srcp, *destp;
797         gint in_brace;
798
799         srcp = destp = str;
800
801         while ((destp = strchr(destp, '"'))) {
802                 if ((srcp = strchr(destp + 1, '"'))) {
803                         srcp++;
804                         if (*srcp == '@') {
805                                 destp = srcp + 1;
806                         } else {
807                                 while (isspace(*srcp)) srcp++;
808                                 memmove(destp, srcp, strlen(srcp) + 1);
809                         }
810                 } else {
811                         *destp = '\0';
812                         break;
813                 }
814         }
815
816         srcp = destp = str;
817
818         while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
819                 in_brace = 1;
820                 srcp = destp + 1;
821                 while (*srcp) {
822                         if (*srcp == '(')
823                                 in_brace++;
824                         else if (*srcp == ')')
825                                 in_brace--;
826                         srcp++;
827                         if (in_brace == 0)
828                                 break;
829                 }
830                 while (isspace(*srcp)) srcp++;
831                 memmove(destp, srcp, strlen(srcp) + 1);
832         }
833 }
834
835 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
836 {
837         gboolean in_quote = FALSE;
838
839         while (*str) {
840                 if (*str == c && !in_quote)
841                         return (gchar *)str;
842                 if (*str == quote_chr)
843                         in_quote ^= TRUE;
844                 str++;
845         }
846
847         return NULL;
848 }
849
850 gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
851 {
852         gboolean in_quote = FALSE;
853         const gchar *p;
854
855         p = str + strlen(str) - 1;
856         while (p >= str) {
857                 if (*p == c && !in_quote)
858                         return (gchar *)p;
859                 if (*p == quote_chr)
860                         in_quote ^= TRUE;
861                 p--;
862         }
863
864         return NULL;
865 }
866
867 void extract_address(gchar *str)
868 {
869         eliminate_address_comment(str);
870         if (strchr_with_skip_quote(str, '"', '<'))
871                 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
872         g_strstrip(str);
873 }
874
875 void extract_list_id_str(gchar *str)
876 {
877         if (strchr_with_skip_quote(str, '"', '<'))
878                 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
879         g_strstrip(str);
880 }
881
882 static GSList *address_list_append_real(GSList *addr_list, const gchar *str, gboolean removecomments)
883 {
884         gchar *work;
885         gchar *workp;
886
887         if (!str) return addr_list;
888
889         Xstrdup_a(work, str, return addr_list);
890
891         if (removecomments)
892                 eliminate_address_comment(work);
893         workp = work;
894
895         while (workp && *workp) {
896                 gchar *p, *next;
897
898                 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
899                         *p = '\0';
900                         next = p + 1;
901                 } else
902                         next = NULL;
903
904                 if (removecomments && strchr_with_skip_quote(workp, '"', '<'))
905                         extract_parenthesis_with_skip_quote
906                                 (workp, '"', '<', '>');
907
908                 g_strstrip(workp);
909                 if (*workp)
910                         addr_list = g_slist_append(addr_list, g_strdup(workp));
911
912                 workp = next;
913         }
914
915         return addr_list;
916 }
917
918 GSList *address_list_append(GSList *addr_list, const gchar *str)
919 {
920         return address_list_append_real(addr_list, str, TRUE);
921 }
922
923 GSList *address_list_append_with_comments(GSList *addr_list, const gchar *str)
924 {
925         return address_list_append_real(addr_list, str, FALSE);
926 }
927
928 GSList *references_list_append(GSList *msgid_list, const gchar *str)
929 {
930         const gchar *strp;
931
932         if (!str) return msgid_list;
933         strp = str;
934
935         while (strp && *strp) {
936                 const gchar *start, *end;
937                 gchar *msgid;
938
939                 if ((start = strchr(strp, '<')) != NULL) {
940                         end = strchr(start + 1, '>');
941                         if (!end) break;
942                 } else
943                         break;
944
945                 msgid = g_strndup(start + 1, end - start - 1);
946                 g_strstrip(msgid);
947                 if (*msgid)
948                         msgid_list = g_slist_append(msgid_list, msgid);
949                 else
950                         g_free(msgid);
951
952                 strp = end + 1;
953         }
954
955         return msgid_list;
956 }
957
958 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
959 {
960         gchar *work;
961         gchar *workp;
962
963         if (!str) return group_list;
964
965         Xstrdup_a(work, str, return group_list);
966
967         workp = work;
968
969         while (workp && *workp) {
970                 gchar *p, *next;
971
972                 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
973                         *p = '\0';
974                         next = p + 1;
975                 } else
976                         next = NULL;
977
978                 g_strstrip(workp);
979                 if (*workp)
980                         group_list = g_slist_append(group_list,
981                                                     g_strdup(workp));
982
983                 workp = next;
984         }
985
986         return group_list;
987 }
988
989 GList *add_history(GList *list, const gchar *str)
990 {
991         GList *old;
992
993         g_return_val_if_fail(str != NULL, list);
994
995         old = g_list_find_custom(list, (gpointer)str, (GCompareFunc)strcmp2);
996         if (old) {
997                 g_free(old->data);
998                 list = g_list_remove(list, old->data);
999         } else if (g_list_length(list) >= MAX_HISTORY_SIZE) {
1000                 GList *last;
1001
1002                 last = g_list_last(list);
1003                 if (last) {
1004                         g_free(last->data);
1005                         g_list_remove(list, last->data);
1006                 }
1007         }
1008
1009         list = g_list_prepend(list, g_strdup(str));
1010
1011         return list;
1012 }
1013
1014 void remove_return(gchar *str)
1015 {
1016         register gchar *p = str;
1017
1018         while (*p) {
1019                 if (*p == '\n' || *p == '\r')
1020                         memmove(p, p + 1, strlen(p));
1021                 else
1022                         p++;
1023         }
1024 }
1025
1026 void remove_space(gchar *str)
1027 {
1028         register guchar *p = str;
1029         register gint spc;
1030
1031         while (*p) {
1032                 spc = 0;
1033                 while (isspace(*(p + spc)))
1034                         spc++;
1035                 if (spc)
1036                         memmove(p, p + spc, strlen(p + spc) + 1);
1037                 else
1038                         p++;
1039         }
1040 }
1041
1042 void unfold_line(gchar *str)
1043 {
1044         register guchar *p = str;
1045         register gint spc;
1046
1047         while (*p) {
1048                 if (*p == '\n' || *p == '\r') {
1049                         *p++ = ' ';
1050                         spc = 0;
1051                         while (isspace(*(p + spc)))
1052                                 spc++;
1053                         if (spc)
1054                                 memmove(p, p + spc, strlen(p + spc) + 1);
1055                 } else
1056                         p++;
1057         }
1058 }
1059
1060 void subst_char(gchar *str, gchar orig, gchar subst)
1061 {
1062         register gchar *p = str;
1063
1064         while (*p) {
1065                 if (*p == orig)
1066                         *p = subst;
1067                 p++;
1068         }
1069 }
1070
1071 void subst_chars(gchar *str, gchar *orig, gchar subst)
1072 {
1073         register gchar *p = str;
1074
1075         while (*p) {
1076                 if (strchr(orig, *p) != NULL)
1077                         *p = subst;
1078                 p++;
1079         }
1080 }
1081
1082 void subst_for_filename(gchar *str)
1083 {
1084         subst_chars(str, " \t\r\n\"'/\\", '_');
1085 }
1086
1087 void subst_for_shellsafe_filename(gchar *str)
1088 {
1089         subst_for_filename(str);
1090         subst_chars(str, "|&;()<>'!{}[]",'_');
1091 }
1092
1093 gboolean is_header_line(const gchar *str)
1094 {
1095         if (str[0] == ':') return FALSE;
1096
1097         while (*str != '\0' && *str != ' ') {
1098                 if (*str == ':')
1099                         return TRUE;
1100                 str++;
1101         }
1102
1103         return FALSE;
1104 }
1105
1106 gboolean is_ascii_str(const guchar *str)
1107 {
1108         g_return_val_if_fail(str, FALSE);
1109
1110         while (*str != '\0') {
1111                 if (*str != '\t' && *str != ' ' &&
1112                     *str != '\r' && *str != '\n' &&
1113                     (*str < 32 || *str >= 127))
1114                         return FALSE;
1115                 str++;
1116         }
1117
1118         return TRUE;
1119 }
1120
1121 gint get_quote_level(const gchar *str, const gchar *quote_chars)
1122 {
1123         const guchar *first_pos;
1124         const guchar *last_pos;
1125         const guchar *p = str;
1126         gint quote_level = -1;
1127
1128         /* speed up line processing by only searching to the last '>' */
1129         if ((first_pos = line_has_quote_char(str, quote_chars)) != NULL) {
1130                 /* skip a line if it contains a '<' before the initial '>' */
1131                 if (memchr(str, '<', first_pos - (const guchar *)str) != NULL)
1132                         return -1;
1133                 last_pos = line_has_quote_char_last(first_pos, quote_chars);
1134         } else
1135                 return -1;
1136
1137         while (p <= last_pos) {
1138                 while (p < last_pos) {
1139                         if (isspace(*p))
1140                                 p++;
1141                         else
1142                                 break;
1143                 }
1144
1145                 if (strchr(quote_chars, *p))
1146                         quote_level++;
1147                 else if (*p != '-' && !isspace(*p) && p <= last_pos) {
1148                         /* any characters are allowed except '-' and space */
1149                         while (*p != '-' 
1150                                && !strchr(quote_chars, *p) 
1151                                && !isspace(*p) 
1152                                && p < last_pos)
1153                                 p++;
1154                         if (strchr(quote_chars, *p))
1155                                 quote_level++;
1156                         else
1157                                 break;
1158                 }
1159
1160                 p++;
1161         }
1162
1163         return quote_level;
1164 }
1165
1166 const gchar * line_has_quote_char(const gchar * str, const gchar *quote_chars) 
1167 {
1168         gchar * position = NULL;
1169         gchar * tmp_pos = NULL;
1170         int i;
1171
1172         if (quote_chars == NULL)
1173                 return FALSE;
1174         
1175         for (i = 0; i < strlen(quote_chars); i++) {
1176                 tmp_pos = strchr (str,  quote_chars[i]);
1177                 if(position == NULL 
1178                    || (tmp_pos != NULL && position >= tmp_pos) )
1179                         position = tmp_pos;
1180         }
1181         return position; 
1182 }
1183
1184 const gchar * line_has_quote_char_last(const gchar * str, const gchar *quote_chars) 
1185 {
1186         gchar * position = NULL;
1187         gchar * tmp_pos = NULL;
1188         int i;
1189
1190         if (quote_chars == NULL)
1191                 return FALSE;
1192         
1193         for (i = 0; i < strlen(quote_chars); i++) {
1194                 tmp_pos = strrchr (str, quote_chars[i]);
1195                 if(position == NULL 
1196                    || (tmp_pos != NULL && position <= tmp_pos) )
1197                         position = tmp_pos;
1198         }
1199         return position; 
1200 }
1201
1202 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
1203 {
1204         register guint haystack_len, needle_len;
1205         gboolean in_squote = FALSE, in_dquote = FALSE;
1206
1207         haystack_len = strlen(haystack);
1208         needle_len   = strlen(needle);
1209
1210         if (haystack_len < needle_len || needle_len == 0)
1211                 return NULL;
1212
1213         while (haystack_len >= needle_len) {
1214                 if (!in_squote && !in_dquote &&
1215                     !strncmp(haystack, needle, needle_len))
1216                         return (gchar *)haystack;
1217
1218                 /* 'foo"bar"' -> foo"bar"
1219                    "foo'bar'" -> foo'bar' */
1220                 if (*haystack == '\'') {
1221                         if (in_squote)
1222                                 in_squote = FALSE;
1223                         else if (!in_dquote)
1224                                 in_squote = TRUE;
1225                 } else if (*haystack == '\"') {
1226                         if (in_dquote)
1227                                 in_dquote = FALSE;
1228                         else if (!in_squote)
1229                                 in_dquote = TRUE;
1230                 }
1231
1232                 haystack++;
1233                 haystack_len--;
1234         }
1235
1236         return NULL;
1237 }
1238
1239 gchar *strchr_parenthesis_close(const gchar *str, gchar op, gchar cl)
1240 {
1241         const gchar *p;
1242         gchar quote_chr = '"';
1243         gint in_brace;
1244         gboolean in_quote = FALSE;
1245
1246         p = str;
1247
1248         if ((p = strchr_with_skip_quote(p, quote_chr, op))) {
1249                 p++;
1250                 in_brace = 1;
1251                 while (*p) {
1252                         if (*p == op && !in_quote)
1253                                 in_brace++;
1254                         else if (*p == cl && !in_quote)
1255                                 in_brace--;
1256                         else if (*p == quote_chr)
1257                                 in_quote ^= TRUE;
1258
1259                         if (in_brace == 0)
1260                                 return (gchar *)p;
1261
1262                         p++;
1263                 }
1264         }
1265
1266         return NULL;
1267 }
1268
1269 gchar **strsplit_parenthesis(const gchar *str, gchar op, gchar cl,
1270                              gint max_tokens)
1271 {
1272         GSList *string_list = NULL, *slist;
1273         gchar **str_array;
1274         const gchar *s_op, *s_cl;
1275         guint i, n = 1;
1276
1277         g_return_val_if_fail(str != NULL, NULL);
1278
1279         if (max_tokens < 1)
1280                 max_tokens = G_MAXINT;
1281
1282         s_op = strchr_with_skip_quote(str, '"', op);
1283         if (!s_op) return NULL;
1284         str = s_op;
1285         s_cl = strchr_parenthesis_close(str, op, cl);
1286         if (s_cl) {
1287                 do {
1288                         guint len;
1289                         gchar *new_string;
1290
1291                         str++;
1292                         len = s_cl - str;
1293                         new_string = g_new(gchar, len + 1);
1294                         strncpy(new_string, str, len);
1295                         new_string[len] = 0;
1296                         string_list = g_slist_prepend(string_list, new_string);
1297                         n++;
1298                         str = s_cl + 1;
1299
1300                         while (*str && isspace(*(guchar *)str)) str++;
1301                         if (*str != op) {
1302                                 string_list = g_slist_prepend(string_list,
1303                                                               g_strdup(""));
1304                                 n++;
1305                                 s_op = strchr_with_skip_quote(str, '"', op);
1306                                 if (!--max_tokens || !s_op) break;
1307                                 str = s_op;
1308                         } else
1309                                 s_op = str;
1310                         s_cl = strchr_parenthesis_close(str, op, cl);
1311                 } while (--max_tokens && s_cl);
1312         }
1313
1314         str_array = g_new(gchar*, n);
1315
1316         i = n - 1;
1317
1318         str_array[i--] = NULL;
1319         for (slist = string_list; slist; slist = slist->next)
1320                 str_array[i--] = slist->data;
1321
1322         g_slist_free(string_list);
1323
1324         return str_array;
1325 }
1326
1327 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1328                             gint max_tokens)
1329 {
1330         GSList *string_list = NULL, *slist;
1331         gchar **str_array, *s, *new_str;
1332         guint i, n = 1, len;
1333
1334         g_return_val_if_fail(str != NULL, NULL);
1335         g_return_val_if_fail(delim != NULL, NULL);
1336
1337         if (max_tokens < 1)
1338                 max_tokens = G_MAXINT;
1339
1340         s = strstr_with_skip_quote(str, delim);
1341         if (s) {
1342                 guint delimiter_len = strlen(delim);
1343
1344                 do {
1345                         len = s - str;
1346                         new_str = g_strndup(str, len);
1347
1348                         if (new_str[0] == '\'' || new_str[0] == '\"') {
1349                                 if (new_str[len - 1] == new_str[0]) {
1350                                         new_str[len - 1] = '\0';
1351                                         memmove(new_str, new_str + 1, len - 1);
1352                                 }
1353                         }
1354                         string_list = g_slist_prepend(string_list, new_str);
1355                         n++;
1356                         str = s + delimiter_len;
1357                         s = strstr_with_skip_quote(str, delim);
1358                 } while (--max_tokens && s);
1359         }
1360
1361         if (*str) {
1362                 new_str = g_strdup(str);
1363                 if (new_str[0] == '\'' || new_str[0] == '\"') {
1364                         len = strlen(str);
1365                         if (new_str[len - 1] == new_str[0]) {
1366                                 new_str[len - 1] = '\0';
1367                                 memmove(new_str, new_str + 1, len - 1);
1368                         }
1369                 }
1370                 string_list = g_slist_prepend(string_list, new_str);
1371                 n++;
1372         }
1373
1374         str_array = g_new(gchar*, n);
1375
1376         i = n - 1;
1377
1378         str_array[i--] = NULL;
1379         for (slist = string_list; slist; slist = slist->next)
1380                 str_array[i--] = slist->data;
1381
1382         g_slist_free(string_list);
1383
1384         return str_array;
1385 }
1386
1387 gchar *get_abbrev_newsgroup_name(const gchar *group, gint len)
1388 {
1389         gchar *abbrev_group;
1390         gchar *ap;
1391         const gchar *p = group;
1392         const gchar *last;
1393
1394         g_return_val_if_fail(group != NULL, NULL);
1395
1396         last = group + strlen(group);
1397         abbrev_group = ap = g_malloc(strlen(group) + 1);
1398
1399         while (*p) {
1400                 while (*p == '.')
1401                         *ap++ = *p++;
1402                 if ((ap - abbrev_group) + (last - p) > len && strchr(p, '.')) {
1403                         *ap++ = *p++;
1404                         while (*p != '.') p++;
1405                 } else {
1406                         strcpy(ap, p);
1407                         return abbrev_group;
1408                 }
1409         }
1410
1411         *ap = '\0';
1412         return abbrev_group;
1413 }
1414
1415 gchar *trim_string(const gchar *str, gint len)
1416 {
1417         const gchar *p = str;
1418         gint mb_len;
1419         gchar *new_str;
1420         gint new_len = 0;
1421
1422         if (!str) return NULL;
1423         if (strlen(str) <= len)
1424                 return g_strdup(str);
1425
1426         while (*p != '\0') {
1427                 mb_len = mblen(p, MB_LEN_MAX);
1428                 if (mb_len == 0)
1429                         break;
1430                 else if (mb_len < 0)
1431                         return g_strdup(str);
1432                 else if (new_len + mb_len > len)
1433                         break;
1434                 else
1435                         new_len += mb_len;
1436                 p += mb_len;
1437         }
1438
1439         Xstrndup_a(new_str, str, new_len, return g_strdup(str));
1440         return g_strconcat(new_str, "...", NULL);
1441 }
1442
1443 GList *uri_list_extract_filenames(const gchar *uri_list)
1444 {
1445         GList *result = NULL;
1446         const gchar *p, *q;
1447         gchar *escaped_utf8uri;
1448         gchar *file;
1449
1450         p = uri_list;
1451
1452         while (p) {
1453                 if (*p != '#') {
1454                         while (isspace(*p)) p++;
1455                         if (!strncmp(p, "file:", 5)) {
1456                                 q = p;
1457                                 q += 5;
1458                                 while (*q && *q != '\n' && *q != '\r') q++;
1459
1460                                 if (q > p) {
1461                                         gchar *file, *locale_file = NULL;
1462                                         q--;
1463                                         while (q > p && isspace(*q)) q--;
1464                                         Xalloca(escaped_utf8uri, q - p + 2,
1465                                                 return result);
1466                                         Xalloca(file, q - p + 2,
1467                                                 return result);
1468                                         *file = '\0';
1469                                         strncpy(escaped_utf8uri, p, q - p + 1);
1470                                         escaped_utf8uri[q - p + 1] = '\0';
1471                                         decode_uri(file, escaped_utf8uri);
1472 #warning FIXME_GTK2 /* should we use g_filename_from_utf8()? */
1473                     /*
1474                      * g_filename_from_uri() rejects escaped/locale encoded uri
1475                      * string which come from Nautilus.
1476                      */
1477                                         if (g_utf8_validate(file, -1, NULL))
1478                                                 locale_file
1479                                                         = conv_codeset_strdup(
1480                                                                 file + 5,
1481                                                                 CS_UTF_8,
1482                                                                 conv_get_current_charset_str());
1483                                         if (!locale_file)
1484                                                 locale_file = g_strdup(file + 5);
1485                                         result = g_list_append(result, locale_file);
1486                                 }
1487                         }
1488                 }
1489                 p = strchr(p, '\n');
1490                 if (p) p++;
1491         }
1492
1493         return result;
1494 }
1495
1496 /* Converts two-digit hexadecimal to decimal.  Used for unescaping escaped 
1497  * characters
1498  */
1499 static gint axtoi(const gchar *hexstr)
1500 {
1501         gint hi, lo, result;
1502        
1503         hi = hexstr[0];
1504         if ('0' <= hi && hi <= '9') {
1505                 hi -= '0';
1506         } else
1507                 if ('a' <= hi && hi <= 'f') {
1508                         hi -= ('a' - 10);
1509                 } else
1510                         if ('A' <= hi && hi <= 'F') {
1511                                 hi -= ('A' - 10);
1512                         }
1513
1514         lo = hexstr[1];
1515         if ('0' <= lo && lo <= '9') {
1516                 lo -= '0';
1517         } else
1518                 if ('a' <= lo && lo <= 'f') {
1519                         lo -= ('a'-10);
1520                 } else
1521                         if ('A' <= lo && lo <= 'F') {
1522                                 lo -= ('A' - 10);
1523                         }
1524         result = lo + (16 * hi);
1525         return result;
1526 }
1527
1528 gboolean is_uri_string(const gchar *str)
1529 {
1530         return (g_ascii_strncasecmp(str, "http://", 7) == 0 ||
1531                 g_ascii_strncasecmp(str, "https://", 8) == 0 ||
1532                 g_ascii_strncasecmp(str, "ftp://", 6) == 0 ||
1533                 g_ascii_strncasecmp(str, "www.", 4) == 0);
1534 }
1535
1536 gchar *get_uri_path(const gchar *uri)
1537 {
1538         if (g_ascii_strncasecmp(uri, "http://", 7) == 0)
1539                 return (gchar *)(uri + 7);
1540         else if (g_ascii_strncasecmp(uri, "https://", 8) == 0)
1541                 return (gchar *)(uri + 8);
1542         else if (g_ascii_strncasecmp(uri, "ftp://", 6) == 0)
1543                 return (gchar *)(uri + 6);
1544         else
1545                 return (gchar *)uri;
1546 }
1547
1548 /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
1549  * plusses, and escape characters are used)
1550  */
1551 void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
1552 {
1553         gchar *dec = decoded_uri;
1554         const gchar *enc = encoded_uri;
1555
1556         while (*enc) {
1557                 if (*enc == '%') {
1558                         enc++;
1559                         if (isxdigit((guchar)enc[0]) &&
1560                             isxdigit((guchar)enc[1])) {
1561                                 *dec = axtoi(enc);
1562                                 dec++;
1563                                 enc += 2;
1564                         }
1565                 } else {
1566                         if (*enc == '+')
1567                                 *dec = ' ';
1568                         else
1569                                 *dec = *enc;
1570                         dec++;
1571                         enc++;
1572                 }
1573         }
1574
1575         *dec = '\0';
1576 }
1577
1578 gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
1579                      gchar **subject, gchar **body)
1580 {
1581         gchar *tmp_mailto;
1582         gchar *p;
1583
1584         Xstrdup_a(tmp_mailto, mailto, return -1);
1585
1586         if (!strncmp(tmp_mailto, "mailto:", 7))
1587                 tmp_mailto += 7;
1588
1589         p = strchr(tmp_mailto, '?');
1590         if (p) {
1591                 *p = '\0';
1592                 p++;
1593         }
1594
1595         if (to && !*to)
1596                 *to = g_strdup(tmp_mailto);
1597
1598         while (p) {
1599                 gchar *field, *value;
1600
1601                 field = p;
1602
1603                 p = strchr(p, '=');
1604                 if (!p) break;
1605                 *p = '\0';
1606                 p++;
1607
1608                 value = p;
1609
1610                 p = strchr(p, '&');
1611                 if (p) {
1612                         *p = '\0';
1613                         p++;
1614                 }
1615
1616                 if (*value == '\0') continue;
1617
1618                 if (cc && !*cc && !g_ascii_strcasecmp(field, "cc")) {
1619                         *cc = g_strdup(value);
1620                 } else if (bcc && !*bcc && !g_ascii_strcasecmp(field, "bcc")) {
1621                         *bcc = g_strdup(value);
1622                 } else if (subject && !*subject &&
1623                            !g_ascii_strcasecmp(field, "subject")) {
1624                         *subject = g_malloc(strlen(value) + 1);
1625                         decode_uri(*subject, value);
1626                 } else if (body && !*body && !g_ascii_strcasecmp(field, "body")) {
1627                         *body = g_malloc(strlen(value) + 1);
1628                         decode_uri(*body, value);
1629                 }
1630         }
1631
1632         return 0;
1633 }
1634
1635 /*
1636  * We need this wrapper around g_get_home_dir(), so that
1637  * we can fix some Windoze things here.  Should be done in glibc of course
1638  * but as long as we are not able to do our own extensions to glibc, we do
1639  * it here.
1640  */
1641 const gchar *get_home_dir(void)
1642 {
1643 #if HAVE_DOSISH_SYSTEM
1644     static gchar *home_dir;
1645
1646     if (!home_dir) {
1647         home_dir = read_w32_registry_string(NULL,
1648                                             "Software\\Sylpheed", "HomeDir" );
1649         if (!home_dir || !*home_dir) {
1650             if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1651                 const char *s = g_get_home_dir();
1652                 if (s && *s)
1653                     home_dir = g_strdup (s);
1654             }
1655             if (!home_dir || !*home_dir) 
1656                 home_dir = g_strdup ("c:\\sylpheed");
1657         }
1658         debug_print("initialized home_dir to `%s'\n", home_dir);
1659     }
1660     return home_dir;
1661 #else /* standard glib */
1662     return g_get_home_dir();
1663 #endif
1664 }
1665
1666 const gchar *get_rc_dir(void)
1667 {
1668         static gchar *rc_dir = NULL;
1669
1670         if (!rc_dir)
1671                 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1672                                      RC_DIR, NULL);
1673
1674         return rc_dir;
1675 }
1676
1677 const gchar *get_news_cache_dir(void)
1678 {
1679         static gchar *news_cache_dir = NULL;
1680
1681         if (!news_cache_dir)
1682                 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1683                                              NEWS_CACHE_DIR, NULL);
1684
1685         return news_cache_dir;
1686 }
1687
1688 const gchar *get_imap_cache_dir(void)
1689 {
1690         static gchar *imap_cache_dir = NULL;
1691
1692         if (!imap_cache_dir)
1693                 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1694                                              IMAP_CACHE_DIR, NULL);
1695
1696         return imap_cache_dir;
1697 }
1698
1699 const gchar *get_mbox_cache_dir(void)
1700 {
1701         static gchar *mbox_cache_dir = NULL;
1702
1703         if (!mbox_cache_dir)
1704                 mbox_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1705                                              MBOX_CACHE_DIR, NULL);
1706
1707         return mbox_cache_dir;
1708 }
1709
1710 const gchar *get_mime_tmp_dir(void)
1711 {
1712         static gchar *mime_tmp_dir = NULL;
1713
1714         if (!mime_tmp_dir)
1715                 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1716                                            MIME_TMP_DIR, NULL);
1717
1718         return mime_tmp_dir;
1719 }
1720
1721 const gchar *get_template_dir(void)
1722 {
1723         static gchar *template_dir = NULL;
1724
1725         if (!template_dir)
1726                 template_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1727                                            TEMPLATE_DIR, NULL);
1728
1729         return template_dir;
1730 }
1731
1732 const gchar *get_header_cache_dir(void)
1733 {
1734         static gchar *header_dir = NULL;
1735
1736         if (!header_dir)
1737                 header_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1738                                          HEADER_CACHE_DIR, NULL);
1739
1740         return header_dir;
1741 }
1742
1743 const gchar *get_tmp_dir(void)
1744 {
1745         static gchar *tmp_dir = NULL;
1746
1747         if (!tmp_dir)
1748                 tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1749                                       TMP_DIR, NULL);
1750
1751         return tmp_dir;
1752 }
1753
1754 gchar *get_tmp_file(void)
1755 {
1756         gchar *tmp_file;
1757         static guint32 id = 0;
1758
1759         tmp_file = g_strdup_printf("%s%ctmpfile.%08x",
1760                                    get_tmp_dir(), G_DIR_SEPARATOR, id++);
1761
1762         return tmp_file;
1763 }
1764
1765 const gchar *get_domain_name(void)
1766 {
1767         static gchar *domain_name = NULL;
1768
1769         if (!domain_name) {
1770                 struct hostent *hp;
1771                 struct utsname uts;
1772
1773                 if (uname(&uts) < 0) {
1774                         perror("gethostname");
1775                         domain_name = "unknown";
1776                 } else {
1777                         if ((hp = my_gethostbyname(uts.nodename)) == NULL) {
1778                                 perror("gethostbyname");
1779                                 domain_name = g_strdup(uts.nodename);
1780                         } else {
1781                                 domain_name = g_strdup(hp->h_name);
1782                         }
1783                 }
1784
1785                 debug_print("domain name = %s\n", domain_name);
1786         }
1787
1788         return domain_name;
1789 }
1790
1791 off_t get_file_size(const gchar *file)
1792 {
1793         struct stat s;
1794
1795         if (stat(file, &s) < 0) {
1796                 FILE_OP_ERROR(file, "stat");
1797                 return -1;
1798         }
1799
1800         return s.st_size;
1801 }
1802
1803 off_t get_file_size_as_crlf(const gchar *file)
1804 {
1805         FILE *fp;
1806         off_t size = 0;
1807         gchar buf[BUFFSIZE];
1808
1809         if ((fp = fopen(file, "rb")) == NULL) {
1810                 FILE_OP_ERROR(file, "fopen");
1811                 return -1;
1812         }
1813
1814         while (fgets(buf, sizeof(buf), fp) != NULL) {
1815                 strretchomp(buf);
1816                 size += strlen(buf) + 2;
1817         }
1818
1819         if (ferror(fp)) {
1820                 FILE_OP_ERROR(file, "fgets");
1821                 size = -1;
1822         }
1823
1824         fclose(fp);
1825
1826         return size;
1827 }
1828
1829 off_t get_left_file_size(FILE *fp)
1830 {
1831         glong pos;
1832         glong end;
1833         off_t size;
1834
1835         if ((pos = ftell(fp)) < 0) {
1836                 perror("ftell");
1837                 return -1;
1838         }
1839         if (fseek(fp, 0L, SEEK_END) < 0) {
1840                 perror("fseek");
1841                 return -1;
1842         }
1843         if ((end = ftell(fp)) < 0) {
1844                 perror("fseek");
1845                 return -1;
1846         }
1847         size = end - pos;
1848         if (fseek(fp, pos, SEEK_SET) < 0) {
1849                 perror("fseek");
1850                 return -1;
1851         }
1852
1853         return size;
1854 }
1855
1856 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1857 {
1858         struct stat s;
1859
1860         if (file == NULL)
1861                 return FALSE;
1862
1863         if (stat(file, &s) < 0) {
1864                 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1865                 return FALSE;
1866         }
1867
1868         if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1869                 return TRUE;
1870
1871         return FALSE;
1872 }
1873
1874 gboolean is_dir_exist(const gchar *dir)
1875 {
1876         struct stat s;
1877
1878         if (dir == NULL)
1879                 return FALSE;
1880
1881         if (stat(dir, &s) < 0) {
1882                 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1883                 return FALSE;
1884         }
1885
1886         if (S_ISDIR(s.st_mode))
1887                 return TRUE;
1888
1889         return FALSE;
1890 }
1891
1892 gboolean is_file_entry_exist(const gchar *file)
1893 {
1894         struct stat s;
1895
1896         if (file == NULL)
1897                 return FALSE;
1898
1899         if (stat(file, &s) < 0) {
1900                 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1901                 return FALSE;
1902         }
1903
1904         return TRUE;
1905 }
1906
1907 gboolean dirent_is_regular_file(struct dirent *d)
1908 {
1909         struct stat s;
1910
1911 #ifdef HAVE_DIRENT_D_TYPE
1912         if (d->d_type == DT_REG)
1913                 return TRUE;
1914         else if (d->d_type != DT_UNKNOWN)
1915                 return FALSE;
1916 #endif
1917
1918         return (stat(d->d_name, &s) == 0 && S_ISREG(s.st_mode));
1919 }
1920
1921 gboolean dirent_is_directory(struct dirent *d)
1922 {
1923         struct stat s;
1924
1925 #ifdef HAVE_DIRENT_D_TYPE
1926         if (d->d_type == DT_DIR)
1927                 return TRUE;
1928         else if (d->d_type != DT_UNKNOWN)
1929                 return FALSE;
1930 #endif
1931
1932         return (stat(d->d_name, &s) == 0 && S_ISDIR(s.st_mode));
1933 }
1934
1935 gint change_dir(const gchar *dir)
1936 {
1937         gchar *prevdir = NULL;
1938
1939         if (debug_mode)
1940                 prevdir = g_get_current_dir();
1941
1942         if (chdir(dir) < 0) {
1943                 FILE_OP_ERROR(dir, "chdir");
1944                 if (debug_mode) g_free(prevdir);
1945                 return -1;
1946         } else if (debug_mode) {
1947                 gchar *cwd;
1948
1949                 cwd = g_get_current_dir();
1950                 if (strcmp(prevdir, cwd) != 0)
1951                         g_print("current dir: %s\n", cwd);
1952                 g_free(cwd);
1953                 g_free(prevdir);
1954         }
1955
1956         return 0;
1957 }
1958
1959 gint make_dir(const gchar *dir)
1960 {
1961         if (mkdir(dir, S_IRWXU) < 0) {
1962                 FILE_OP_ERROR(dir, "mkdir");
1963                 return -1;
1964         }
1965         if (chmod(dir, S_IRWXU) < 0)
1966                 FILE_OP_ERROR(dir, "chmod");
1967
1968         return 0;
1969 }
1970
1971 gint make_dir_hier(const gchar *dir)
1972 {
1973         gchar *parent_dir;
1974         const gchar *p;
1975
1976         for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1977                 parent_dir = g_strndup(dir, p - dir);
1978                 if (*parent_dir != '\0') {
1979                         if (!is_dir_exist(parent_dir)) {
1980                                 if (make_dir(parent_dir) < 0) {
1981                                         g_free(parent_dir);
1982                                         return -1;
1983                                 }
1984                         }
1985                 }
1986                 g_free(parent_dir);
1987         }
1988
1989         if (!is_dir_exist(dir)) {
1990                 if (make_dir(dir) < 0)
1991                         return -1;
1992         }
1993
1994         return 0;
1995 }
1996
1997 gint remove_all_files(const gchar *dir)
1998 {
1999         DIR *dp;
2000         struct dirent *d;
2001         gchar *prev_dir;
2002
2003         prev_dir = g_get_current_dir();
2004
2005         if (chdir(dir) < 0) {
2006                 FILE_OP_ERROR(dir, "chdir");
2007                 g_free(prev_dir);
2008                 return -1;
2009         }
2010
2011         if ((dp = opendir(".")) == NULL) {
2012                 FILE_OP_ERROR(dir, "opendir");
2013                 g_free(prev_dir);
2014                 return -1;
2015         }
2016
2017         while ((d = readdir(dp)) != NULL) {
2018                 if (!strcmp(d->d_name, ".") ||
2019                     !strcmp(d->d_name, ".."))
2020                         continue;
2021
2022                 if (unlink(d->d_name) < 0)
2023                         FILE_OP_ERROR(d->d_name, "unlink");
2024         }
2025
2026         closedir(dp);
2027
2028         if (chdir(prev_dir) < 0) {
2029                 FILE_OP_ERROR(prev_dir, "chdir");
2030                 g_free(prev_dir);
2031                 return -1;
2032         }
2033
2034         g_free(prev_dir);
2035
2036         return 0;
2037 }
2038
2039 gint remove_numbered_files(const gchar *dir, guint first, guint last)
2040 {
2041         DIR *dp;
2042         struct dirent *d;
2043         gchar *prev_dir;
2044         gint fileno;
2045
2046         prev_dir = g_get_current_dir();
2047
2048         if (chdir(dir) < 0) {
2049                 FILE_OP_ERROR(dir, "chdir");
2050                 g_free(prev_dir);
2051                 return -1;
2052         }
2053
2054         if ((dp = opendir(".")) == NULL) {
2055                 FILE_OP_ERROR(dir, "opendir");
2056                 g_free(prev_dir);
2057                 return -1;
2058         }
2059
2060         while ((d = readdir(dp)) != NULL) {
2061                 fileno = to_number(d->d_name);
2062                 if (fileno >= 0 && first <= fileno && fileno <= last) {
2063                         if (is_dir_exist(d->d_name))
2064                                 continue;
2065                         if (unlink(d->d_name) < 0)
2066                                 FILE_OP_ERROR(d->d_name, "unlink");
2067                 }
2068         }
2069
2070         closedir(dp);
2071
2072         if (chdir(prev_dir) < 0) {
2073                 FILE_OP_ERROR(prev_dir, "chdir");
2074                 g_free(prev_dir);
2075                 return -1;
2076         }
2077
2078         g_free(prev_dir);
2079
2080         return 0;
2081 }
2082
2083 gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
2084 {
2085         DIR *dp;
2086         struct dirent *d;
2087         gchar *prev_dir;
2088         gint fileno;
2089
2090         prev_dir = g_get_current_dir();
2091
2092         if (chdir(dir) < 0) {
2093                 FILE_OP_ERROR(dir, "chdir");
2094                 g_free(prev_dir);
2095                 return -1;
2096         }
2097
2098         if ((dp = opendir(".")) == NULL) {
2099                 FILE_OP_ERROR(dir, "opendir");
2100                 g_free(prev_dir);
2101                 return -1;
2102         }
2103
2104         while ((d = readdir(dp)) != NULL) {
2105                 fileno = to_number(d->d_name);
2106                 if (fileno >= 0 && (g_slist_find(numberlist, GINT_TO_POINTER(fileno)) == NULL)) {
2107                         debug_print("removing unwanted file %d from %s\n", fileno, dir);
2108                         if (is_dir_exist(d->d_name))
2109                                 continue;
2110                         if (unlink(d->d_name) < 0)
2111                                 FILE_OP_ERROR(d->d_name, "unlink");
2112                 }
2113         }
2114
2115         closedir(dp);
2116
2117         if (chdir(prev_dir) < 0) {
2118                 FILE_OP_ERROR(prev_dir, "chdir");
2119                 g_free(prev_dir);
2120                 return -1;
2121         }
2122
2123         g_free(prev_dir);
2124
2125         return 0;
2126 }
2127
2128 gint remove_all_numbered_files(const gchar *dir)
2129 {
2130         return remove_numbered_files(dir, 0, UINT_MAX);
2131 }
2132
2133 gint remove_expired_files(const gchar *dir, guint hours)
2134 {
2135         DIR *dp;
2136         struct dirent *d;
2137         struct stat s;
2138         gchar *prev_dir;
2139         gint fileno;
2140         time_t mtime, now, expire_time;
2141
2142         prev_dir = g_get_current_dir();
2143
2144         if (chdir(dir) < 0) {
2145                 FILE_OP_ERROR(dir, "chdir");
2146                 g_free(prev_dir);
2147                 return -1;
2148         }
2149
2150         if ((dp = opendir(".")) == NULL) {
2151                 FILE_OP_ERROR(dir, "opendir");
2152                 g_free(prev_dir);
2153                 return -1;
2154         }
2155
2156         now = time(NULL);
2157         expire_time = hours * 60 * 60;
2158
2159         while ((d = readdir(dp)) != NULL) {
2160                 fileno = to_number(d->d_name);
2161                 if (fileno >= 0) {
2162                         if (stat(d->d_name, &s) < 0) {
2163                                 FILE_OP_ERROR(d->d_name, "stat");
2164                                 continue;
2165                         }
2166                         if (S_ISDIR(s.st_mode))
2167                                 continue;
2168                         mtime = MAX(s.st_mtime, s.st_atime);
2169                         if (now - mtime > expire_time) {
2170                                 if (unlink(d->d_name) < 0)
2171                                         FILE_OP_ERROR(d->d_name, "unlink");
2172                         }
2173                 }
2174         }
2175
2176         closedir(dp);
2177
2178         if (chdir(prev_dir) < 0) {
2179                 FILE_OP_ERROR(prev_dir, "chdir");
2180                 g_free(prev_dir);
2181                 return -1;
2182         }
2183
2184         g_free(prev_dir);
2185
2186         return 0;
2187 }
2188
2189 gint remove_dir_recursive(const gchar *dir)
2190 {
2191         struct stat s;
2192         DIR *dp;
2193         struct dirent *d;
2194         gchar *prev_dir;
2195
2196         /* g_print("dir = %s\n", dir); */
2197
2198         if (stat(dir, &s) < 0) {
2199                 FILE_OP_ERROR(dir, "stat");
2200                 if (ENOENT == errno) return 0;
2201                 return -1;
2202         }
2203
2204         if (!S_ISDIR(s.st_mode)) {
2205                 if (unlink(dir) < 0) {
2206                         FILE_OP_ERROR(dir, "unlink");
2207                         return -1;
2208                 }
2209
2210                 return 0;
2211         }
2212
2213         prev_dir = g_get_current_dir();
2214         /* g_print("prev_dir = %s\n", prev_dir); */
2215
2216         if (!path_cmp(prev_dir, dir)) {
2217                 g_free(prev_dir);
2218                 if (chdir("..") < 0) {
2219                         FILE_OP_ERROR(dir, "chdir");
2220                         return -1;
2221                 }
2222                 prev_dir = g_get_current_dir();
2223         }
2224
2225         if (chdir(dir) < 0) {
2226                 FILE_OP_ERROR(dir, "chdir");
2227                 g_free(prev_dir);
2228                 return -1;
2229         }
2230
2231         if ((dp = opendir(".")) == NULL) {
2232                 FILE_OP_ERROR(dir, "opendir");
2233                 chdir(prev_dir);
2234                 g_free(prev_dir);
2235                 return -1;
2236         }
2237
2238         /* remove all files in the directory */
2239         while ((d = readdir(dp)) != NULL) {
2240                 if (!strcmp(d->d_name, ".") ||
2241                     !strcmp(d->d_name, ".."))
2242                         continue;
2243
2244                 /* g_print("removing %s\n", d->d_name); */
2245
2246                 if (dirent_is_directory(d)) {
2247                         if (remove_dir_recursive(d->d_name) < 0) {
2248                                 g_warning("can't remove directory\n");
2249                                 return -1;
2250                         }
2251                 } else {
2252                         if (unlink(d->d_name) < 0)
2253                                 FILE_OP_ERROR(d->d_name, "unlink");
2254                 }
2255         }
2256
2257         closedir(dp);
2258
2259         if (chdir(prev_dir) < 0) {
2260                 FILE_OP_ERROR(prev_dir, "chdir");
2261                 g_free(prev_dir);
2262                 return -1;
2263         }
2264
2265         g_free(prev_dir);
2266
2267         if (rmdir(dir) < 0) {
2268                 FILE_OP_ERROR(dir, "rmdir");
2269                 return -1;
2270         }
2271
2272         return 0;
2273 }
2274
2275 #if 0
2276 /* this seems to be slower than the stdio version... */
2277 gint copy_file(const gchar *src, const gchar *dest)
2278 {
2279         gint src_fd, dest_fd;
2280         gint n_read;
2281         gint n_write;
2282         gchar buf[BUFSIZ];
2283         gchar *dest_bak = NULL;
2284
2285         if ((src_fd = open(src, O_RDONLY)) < 0) {
2286                 FILE_OP_ERROR(src, "open");
2287                 return -1;
2288         }
2289
2290         if (is_file_exist(dest)) {
2291                 dest_bak = g_strconcat(dest, ".bak", NULL);
2292                 if (rename(dest, dest_bak) < 0) {
2293                         FILE_OP_ERROR(dest, "rename");
2294                         close(src_fd);
2295                         g_free(dest_bak);
2296                         return -1;
2297                 }
2298         }
2299
2300         if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
2301                 FILE_OP_ERROR(dest, "open");
2302                 close(src_fd);
2303                 if (dest_bak) {
2304                         if (rename(dest_bak, dest) < 0)
2305                                 FILE_OP_ERROR(dest_bak, "rename");
2306                         g_free(dest_bak);
2307                 }
2308                 return -1;
2309         }
2310
2311         while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
2312                 gint len = n_read;
2313                 gchar *bufp = buf;
2314
2315                 while (len > 0) {
2316                         n_write = write(dest_fd, bufp, len);
2317                         if (n_write <= 0) {
2318                                 g_warning("writing to %s failed.\n", dest);
2319                                 close(dest_fd);
2320                                 close(src_fd);
2321                                 unlink(dest);
2322                                 if (dest_bak) {
2323                                         if (rename(dest_bak, dest) < 0)
2324                                                 FILE_OP_ERROR(dest_bak, "rename");
2325                                         g_free(dest_bak);
2326                                 }
2327                                 return -1;
2328                         }
2329                         len -= n_write;
2330                         bufp += n_write;
2331                 }
2332         }
2333
2334         close(src_fd);
2335         close(dest_fd);
2336
2337         if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
2338                 g_warning("File copy from %s to %s failed.\n", src, dest);
2339                 unlink(dest);
2340                 if (dest_bak) {
2341                         if (rename(dest_bak, dest) < 0)
2342                                 FILE_OP_ERROR(dest_bak, "rename");
2343                         g_free(dest_bak);
2344                 }
2345                 return -1;
2346         }
2347         g_free(dest_bak);
2348
2349         return 0;
2350 }
2351 #endif
2352
2353
2354 /*
2355  * Append src file body to the tail of dest file.
2356  * Now keep_backup has no effects.
2357  */
2358 gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
2359 {
2360         FILE *src_fp, *dest_fp;
2361         gint n_read;
2362         gchar buf[BUFSIZ];
2363
2364         gboolean err = FALSE;
2365
2366         if ((src_fp = fopen(src, "rb")) == NULL) {
2367                 FILE_OP_ERROR(src, "fopen");
2368                 return -1;
2369         }
2370         
2371         if ((dest_fp = fopen(dest, "ab")) == NULL) {
2372                 FILE_OP_ERROR(dest, "fopen");
2373                 fclose(src_fp);
2374                 return -1;
2375         }
2376
2377         if (change_file_mode_rw(dest_fp, dest) < 0) {
2378                 FILE_OP_ERROR(dest, "chmod");
2379                 g_warning("can't change file mode\n");
2380         }
2381
2382         while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
2383                 if (n_read < sizeof(buf) && ferror(src_fp))
2384                         break;
2385                 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
2386                         g_warning("writing to %s failed.\n", dest);
2387                         fclose(dest_fp);
2388                         fclose(src_fp);
2389                         unlink(dest);
2390                         return -1;
2391                 }
2392         }
2393
2394         if (ferror(src_fp)) {
2395                 FILE_OP_ERROR(src, "fread");
2396                 err = TRUE;
2397         }
2398         fclose(src_fp);
2399         if (fclose(dest_fp) == EOF) {
2400                 FILE_OP_ERROR(dest, "fclose");
2401                 err = TRUE;
2402         }
2403
2404         if (err) {
2405                 unlink(dest);
2406                 return -1;
2407         }
2408
2409         return 0;
2410 }
2411
2412 gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
2413 {
2414         FILE *src_fp, *dest_fp;
2415         gint n_read;
2416         gchar buf[BUFSIZ];
2417         gchar *dest_bak = NULL;
2418         gboolean err = FALSE;
2419
2420         if ((src_fp = fopen(src, "rb")) == NULL) {
2421                 FILE_OP_ERROR(src, "fopen");
2422                 return -1;
2423         }
2424         if (is_file_exist(dest)) {
2425                 dest_bak = g_strconcat(dest, ".bak", NULL);
2426                 if (rename(dest, dest_bak) < 0) {
2427                         FILE_OP_ERROR(dest, "rename");
2428                         fclose(src_fp);
2429                         g_free(dest_bak);
2430                         return -1;
2431                 }
2432         }
2433
2434         if ((dest_fp = fopen(dest, "wb")) == NULL) {
2435                 FILE_OP_ERROR(dest, "fopen");
2436                 fclose(src_fp);
2437                 if (dest_bak) {
2438                         if (rename(dest_bak, dest) < 0)
2439                                 FILE_OP_ERROR(dest_bak, "rename");
2440                         g_free(dest_bak);
2441                 }
2442                 return -1;
2443         }
2444
2445         if (change_file_mode_rw(dest_fp, dest) < 0) {
2446                 FILE_OP_ERROR(dest, "chmod");
2447                 g_warning("can't change file mode\n");
2448         }
2449
2450         while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
2451                 if (n_read < sizeof(buf) && ferror(src_fp))
2452                         break;
2453                 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
2454                         g_warning("writing to %s failed.\n", dest);
2455                         fclose(dest_fp);
2456                         fclose(src_fp);
2457                         unlink(dest);
2458                         if (dest_bak) {
2459                                 if (rename(dest_bak, dest) < 0)
2460                                         FILE_OP_ERROR(dest_bak, "rename");
2461                                 g_free(dest_bak);
2462                         }
2463                         return -1;
2464                 }
2465         }
2466
2467         if (ferror(src_fp)) {
2468                 FILE_OP_ERROR(src, "fread");
2469                 err = TRUE;
2470         }
2471         fclose(src_fp);
2472         if (fclose(dest_fp) == EOF) {
2473                 FILE_OP_ERROR(dest, "fclose");
2474                 err = TRUE;
2475         }
2476
2477         if (err) {
2478                 unlink(dest);
2479                 if (dest_bak) {
2480                         if (rename(dest_bak, dest) < 0)
2481                                 FILE_OP_ERROR(dest_bak, "rename");
2482                         g_free(dest_bak);
2483                 }
2484                 return -1;
2485         }
2486
2487         if (keep_backup == FALSE && dest_bak)
2488                 unlink(dest_bak);
2489
2490         g_free(dest_bak);
2491
2492         return 0;
2493 }
2494
2495 gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
2496 {
2497         if (overwrite == FALSE && is_file_exist(dest)) {
2498                 g_warning("move_file(): file %s already exists.", dest);
2499                 return -1;
2500         }
2501
2502         if (rename(src, dest) == 0) return 0;
2503
2504         if (EXDEV != errno) {
2505                 FILE_OP_ERROR(src, "rename");
2506                 return -1;
2507         }
2508
2509         if (copy_file(src, dest, FALSE) < 0) return -1;
2510
2511         unlink(src);
2512
2513         return 0;
2514 }
2515
2516 gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
2517 {
2518         gint n_read;
2519         gint bytes_left, to_read;
2520         gchar buf[BUFSIZ];
2521
2522         if (fseek(fp, offset, SEEK_SET) < 0) {
2523                 perror("fseek");
2524                 return -1;
2525         }
2526
2527         bytes_left = length;
2528         to_read = MIN(bytes_left, sizeof(buf));
2529
2530         while ((n_read = fread(buf, sizeof(gchar), to_read, fp)) > 0) {
2531                 if (n_read < to_read && ferror(fp))
2532                         break;
2533                 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
2534                         return -1;
2535                 }
2536                 bytes_left -= n_read;
2537                 if (bytes_left == 0)
2538                         break;
2539                 to_read = MIN(bytes_left, sizeof(buf));
2540         }
2541
2542         if (ferror(fp)) {
2543                 perror("fread");
2544                 return -1;
2545         }
2546
2547         return 0;
2548 }
2549
2550 gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
2551 {
2552         FILE *dest_fp;
2553         gboolean err = FALSE;
2554
2555         if ((dest_fp = fopen(dest, "wb")) == NULL) {
2556                 FILE_OP_ERROR(dest, "fopen");
2557                 return -1;
2558         }
2559
2560         if (change_file_mode_rw(dest_fp, dest) < 0) {
2561                 FILE_OP_ERROR(dest, "chmod");
2562                 g_warning("can't change file mode\n");
2563         }
2564
2565         if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
2566                 err = TRUE;
2567
2568         if (!err && fclose(dest_fp) == EOF) {
2569                 FILE_OP_ERROR(dest, "fclose");
2570                 err = TRUE;
2571         }
2572
2573         if (err) {
2574                 g_warning("writing to %s failed.\n", dest);
2575                 unlink(dest);
2576                 return -1;
2577         }
2578
2579         return 0;
2580 }
2581
2582 /* convert line endings into CRLF. If the last line doesn't end with
2583  * linebreak, add it.
2584  */
2585 gchar *canonicalize_str(const gchar *str)
2586 {
2587         const gchar *p;
2588         guint new_len = 0;
2589         gchar *out, *outp;
2590
2591         for (p = str; *p != '\0'; ++p) {
2592                 if (*p != '\r') {
2593                         ++new_len;
2594                         if (*p == '\n')
2595                                 ++new_len;
2596                 }
2597         }
2598         if (p == str || *(p - 1) != '\n')
2599                 new_len += 2;
2600
2601         out = outp = g_malloc(new_len + 1);
2602         for (p = str; *p != '\0'; ++p) {
2603                 if (*p != '\r') {
2604                         if (*p == '\n')
2605                                 *outp++ = '\r';
2606                         *outp++ = *p;
2607                 }
2608         }
2609         if (p == str || *(p - 1) != '\n') {
2610                 *outp++ = '\r';
2611                 *outp++ = '\n';
2612         }
2613         *outp = '\0';
2614
2615         return out;
2616 }
2617
2618 gint canonicalize_file(const gchar *src, const gchar *dest)
2619 {
2620         FILE *src_fp, *dest_fp;
2621         gchar buf[BUFFSIZE];
2622         gint len;
2623         gboolean err = FALSE;
2624         gboolean last_linebreak = FALSE;
2625
2626         if ((src_fp = fopen(src, "rb")) == NULL) {
2627                 FILE_OP_ERROR(src, "fopen");
2628                 return -1;
2629         }
2630
2631         if ((dest_fp = fopen(dest, "wb")) == NULL) {
2632                 FILE_OP_ERROR(dest, "fopen");
2633                 fclose(src_fp);
2634                 return -1;
2635         }
2636
2637         if (change_file_mode_rw(dest_fp, dest) < 0) {
2638                 FILE_OP_ERROR(dest, "chmod");
2639                 g_warning("can't change file mode\n");
2640         }
2641
2642         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
2643                 gint r = 0;
2644
2645                 len = strlen(buf);
2646                 if (len == 0) break;
2647                 last_linebreak = FALSE;
2648
2649                 if (buf[len - 1] != '\n') {
2650                         last_linebreak = TRUE;
2651                         r = fputs(buf, dest_fp);
2652                 } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
2653                         r = fputs(buf, dest_fp);
2654                 } else {
2655                         if (len > 1) {
2656                                 r = fwrite(buf, len - 1, 1, dest_fp);
2657                                 if (r != 1)
2658                                         r = EOF;
2659                         }
2660                         if (r != EOF)
2661                                 r = fputs("\r\n", dest_fp);
2662                 }
2663
2664                 if (r == EOF) {
2665                         g_warning("writing to %s failed.\n", dest);
2666                         fclose(dest_fp);
2667                         fclose(src_fp);
2668                         unlink(dest);
2669                         return -1;
2670                 }
2671         }
2672
2673         if (last_linebreak == TRUE) {
2674                 if (fputs("\r\n", dest_fp) == EOF)
2675                         err = TRUE;
2676         }
2677
2678         if (ferror(src_fp)) {
2679                 FILE_OP_ERROR(src, "fgets");
2680                 err = TRUE;
2681         }
2682         fclose(src_fp);
2683         if (fclose(dest_fp) == EOF) {
2684                 FILE_OP_ERROR(dest, "fclose");
2685                 err = TRUE;
2686         }
2687
2688         if (err) {
2689                 unlink(dest);
2690                 return -1;
2691         }
2692
2693         return 0;
2694 }
2695
2696 gint canonicalize_file_replace(const gchar *file)
2697 {
2698         gchar *tmp_file;
2699
2700         tmp_file = get_tmp_file();
2701
2702         if (canonicalize_file(file, tmp_file) < 0) {
2703                 g_free(tmp_file);
2704                 return -1;
2705         }
2706
2707         if (move_file(tmp_file, file, TRUE) < 0) {
2708                 g_warning("can't replace %s .\n", file);
2709                 unlink(tmp_file);
2710                 g_free(tmp_file);
2711                 return -1;
2712         }
2713
2714         g_free(tmp_file);
2715         return 0;
2716 }
2717
2718 gint uncanonicalize_file(const gchar *src, const gchar *dest)
2719 {
2720         FILE *src_fp, *dest_fp;
2721         gchar buf[BUFFSIZE];
2722         gboolean err = FALSE;
2723
2724         if ((src_fp = fopen(src, "rb")) == NULL) {
2725                 FILE_OP_ERROR(src, "fopen");
2726                 return -1;
2727         }
2728
2729         if ((dest_fp = fopen(dest, "wb")) == NULL) {
2730                 FILE_OP_ERROR(dest, "fopen");
2731                 fclose(src_fp);
2732                 return -1;
2733         }
2734
2735         if (change_file_mode_rw(dest_fp, dest) < 0) {
2736                 FILE_OP_ERROR(dest, "chmod");
2737                 g_warning("can't change file mode\n");
2738         }
2739
2740         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
2741                 strcrchomp(buf);
2742                 if (fputs(buf, dest_fp) == EOF) {
2743                         g_warning("writing to %s failed.\n", dest);
2744                         fclose(dest_fp);
2745                         fclose(src_fp);
2746                         unlink(dest);
2747                         return -1;
2748                 }
2749         }
2750
2751         if (ferror(src_fp)) {
2752                 FILE_OP_ERROR(src, "fgets");
2753                 err = TRUE;
2754         }
2755         fclose(src_fp);
2756         if (fclose(dest_fp) == EOF) {
2757                 FILE_OP_ERROR(dest, "fclose");
2758                 err = TRUE;
2759         }
2760
2761         if (err) {
2762                 unlink(dest);
2763                 return -1;
2764         }
2765
2766         return 0;
2767 }
2768
2769 gint uncanonicalize_file_replace(const gchar *file)
2770 {
2771         gchar *tmp_file;
2772
2773         tmp_file = get_tmp_file();
2774
2775         if (uncanonicalize_file(file, tmp_file) < 0) {
2776                 g_free(tmp_file);
2777                 return -1;
2778         }
2779
2780         if (move_file(tmp_file, file, TRUE) < 0) {
2781                 g_warning("can't replace %s .\n", file);
2782                 unlink(tmp_file);
2783                 g_free(tmp_file);
2784                 return -1;
2785         }
2786
2787         g_free(tmp_file);
2788         return 0;
2789 }
2790
2791 gchar *normalize_newlines(const gchar *str)
2792 {
2793         const gchar *p = str;
2794         gchar *out, *outp;
2795
2796         out = outp = g_malloc(strlen(str) + 1);
2797         for (p = str; *p != '\0'; ++p) {
2798                 if (*p == '\r') {
2799                         if (*(p + 1) != '\n')
2800                                 *outp++ = '\n';
2801                 } else
2802                         *outp++ = *p;
2803         }
2804
2805         *outp = '\0';
2806
2807         return out;
2808 }
2809
2810 gchar *get_outgoing_rfc2822_str(FILE *fp)
2811 {
2812         gchar buf[BUFFSIZE];
2813         GString *str;
2814         gchar *ret;
2815
2816         str = g_string_new(NULL);
2817
2818         /* output header part */
2819         while (fgets(buf, sizeof(buf), fp) != NULL) {
2820                 strretchomp(buf);
2821                 if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) {
2822                         gint next;
2823
2824                         for (;;) {
2825                                 next = fgetc(fp);
2826                                 if (next == EOF)
2827                                         break;
2828                                 else if (next != ' ' && next != '\t') {
2829                                         ungetc(next, fp);
2830                                         break;
2831                                 }
2832                                 if (fgets(buf, sizeof(buf), fp) == NULL)
2833                                         break;
2834                         }
2835                 } else {
2836                         g_string_append(str, buf);
2837                         g_string_append(str, "\r\n");
2838                         if (buf[0] == '\0')
2839                                 break;
2840                 }
2841         }
2842
2843         /* output body part */
2844         while (fgets(buf, sizeof(buf), fp) != NULL) {
2845                 strretchomp(buf);
2846                 if (buf[0] == '.')
2847                         g_string_append_c(str, '.');
2848                 g_string_append(str, buf);
2849                 g_string_append(str, "\r\n");
2850         }
2851
2852         ret = str->str;
2853         g_string_free(str, FALSE);
2854
2855         return ret;
2856 }
2857
2858 /*
2859  * Create a new boundary in a way that it is very unlikely that this
2860  * will occur in the following text.  It would be easy to ensure
2861  * uniqueness if everything is either quoted-printable or base64
2862  * encoded (note that conversion is allowed), but because MIME bodies
2863  * may be nested, it may happen that the same boundary has already
2864  * been used. We avoid scanning the message for conflicts and hope the
2865  * best.
2866  *
2867  *   boundary := 0*69<bchars> bcharsnospace
2868  *   bchars := bcharsnospace / " "
2869  *   bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
2870  *                    "+" / "_" / "," / "-" / "." /
2871  *                    "/" / ":" / "=" / "?"
2872  *
2873  * some special characters removed because of buggy MTAs
2874  */
2875
2876 gchar *generate_mime_boundary(const gchar *prefix)
2877 {
2878         static gchar tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2879                              "abcdefghijklmnopqrstuvwxyz"
2880                              "1234567890+_./=";
2881         gchar buf_uniq[17];
2882         gchar buf_date[64];
2883         gint i;
2884         gint pid;
2885
2886         pid = getpid();
2887
2888         /* We make the boundary depend on the pid, so that all running
2889          * processes generate different values even when they have been
2890          * started within the same second and srandom(time(NULL)) has been
2891          * used.  I can't see whether this is really an advantage but it
2892          * doesn't do any harm.
2893          */
2894         for (i = 0; i < sizeof(buf_uniq) - 1; i++)
2895                 buf_uniq[i] = tbl[(rand() ^ pid) % (sizeof(tbl) - 1)];
2896         buf_uniq[i] = '\0';
2897
2898         get_rfc822_date(buf_date, sizeof(buf_date));
2899         subst_char(buf_date, ' ', '_');
2900         subst_char(buf_date, ',', '_');
2901         subst_char(buf_date, ':', '_');
2902
2903         return g_strdup_printf("%s_%s_%s", prefix ? prefix : "Multipart",
2904                                buf_date, buf_uniq);
2905 }
2906
2907 gint change_file_mode_rw(FILE *fp, const gchar *file)
2908 {
2909 #if HAVE_FCHMOD
2910         return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
2911 #else
2912         return chmod(file, S_IRUSR|S_IWUSR);
2913 #endif
2914 }
2915
2916 FILE *my_tmpfile(void)
2917 {
2918 #if HAVE_MKSTEMP
2919         const gchar suffix[] = ".XXXXXX";
2920         const gchar *tmpdir;
2921         guint tmplen;
2922         const gchar *progname;
2923         guint proglen;
2924         gchar *fname;
2925         gint fd;
2926         FILE *fp;
2927
2928         tmpdir = get_tmp_dir();
2929         tmplen = strlen(tmpdir);
2930         progname = g_get_prgname();
2931         if (progname == NULL)
2932                 progname = "sylpheed-claws";
2933         proglen = strlen(progname);
2934         Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
2935                 return tmpfile());
2936
2937         memcpy(fname, tmpdir, tmplen);
2938         fname[tmplen] = G_DIR_SEPARATOR;
2939         memcpy(fname + tmplen + 1, progname, proglen);
2940         memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
2941
2942         fd = mkstemp(fname);
2943         if (fd < 0)
2944                 return tmpfile();
2945
2946         unlink(fname);
2947
2948         fp = fdopen(fd, "w+b");
2949         if (!fp)
2950                 close(fd);
2951         else
2952                 return fp;
2953 #endif /* HAVE_MKSTEMP */
2954
2955         return tmpfile();
2956 }
2957
2958 FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
2959 {
2960         int fd;
2961         
2962         *filename = g_strdup_printf("%s%csylpheed.XXXXXX", dir, G_DIR_SEPARATOR);
2963         fd = mkstemp(*filename);
2964
2965         return fdopen(fd, "w+");
2966 }
2967
2968 FILE *str_open_as_stream(const gchar *str)
2969 {
2970         FILE *fp;
2971         size_t len;
2972
2973         g_return_val_if_fail(str != NULL, NULL);
2974
2975         fp = my_tmpfile();
2976         if (!fp) {
2977                 FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
2978                 return NULL;
2979         }
2980
2981         len = strlen(str);
2982         if (len == 0) return fp;
2983
2984         if (fwrite(str, len, 1, fp) != 1) {
2985                 FILE_OP_ERROR("str_open_as_stream", "fwrite");
2986                 fclose(fp);
2987                 return NULL;
2988         }
2989
2990         rewind(fp);
2991         return fp;
2992 }
2993
2994 gint str_write_to_file(const gchar *str, const gchar *file)
2995 {
2996         FILE *fp;
2997         size_t len;
2998
2999         g_return_val_if_fail(str != NULL, -1);
3000         g_return_val_if_fail(file != NULL, -1);
3001
3002         if ((fp = fopen(file, "wb")) == NULL) {
3003                 FILE_OP_ERROR(file, "fopen");
3004                 return -1;
3005         }
3006
3007         len = strlen(str);
3008         if (len == 0) {
3009                 fclose(fp);
3010                 return 0;
3011         }
3012
3013         if (fwrite(str, len, 1, fp) != 1) {
3014                 FILE_OP_ERROR(file, "fwrite");
3015                 fclose(fp);
3016                 unlink(file);
3017                 return -1;
3018         }
3019
3020         if (fclose(fp) == EOF) {
3021                 FILE_OP_ERROR(file, "fclose");
3022                 unlink(file);
3023                 return -1;
3024         }
3025
3026         return 0;
3027 }
3028
3029 gchar *file_read_to_str(const gchar *file)
3030 {
3031         FILE *fp;
3032         gchar *str;
3033
3034         g_return_val_if_fail(file != NULL, NULL);
3035
3036         if ((fp = fopen(file, "rb")) == NULL) {
3037                 FILE_OP_ERROR(file, "fopen");
3038                 return NULL;
3039         }
3040
3041         str = file_read_stream_to_str(fp);
3042
3043         fclose(fp);
3044
3045         return str;
3046 }
3047
3048 gchar *file_read_stream_to_str(FILE *fp)
3049 {
3050         GByteArray *array;
3051         gchar buf[BUFSIZ];
3052         gint n_read;
3053         gchar *str;
3054
3055         g_return_val_if_fail(fp != NULL, NULL);
3056
3057         array = g_byte_array_new();
3058
3059         while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
3060                 if (n_read < sizeof(buf) && ferror(fp))
3061                         break;
3062                 g_byte_array_append(array, buf, n_read);
3063         }
3064
3065         if (ferror(fp)) {
3066                 FILE_OP_ERROR("file stream", "fread");
3067                 g_byte_array_free(array, TRUE);
3068                 return NULL;
3069         }
3070
3071         buf[0] = '\0';
3072         g_byte_array_append(array, buf, 1);
3073         str = (gchar *)array->data;
3074         g_byte_array_free(array, FALSE);
3075
3076         if (!g_utf8_validate(str, -1, NULL)) {
3077                 const gchar *src_codeset, *dest_codeset;
3078                 gchar *tmp = NULL;
3079                 src_codeset = conv_get_current_charset_str();
3080                 dest_codeset = CS_UTF_8;
3081                 tmp = conv_codeset_strdup(str, src_codeset, dest_codeset);
3082                 g_free(str);
3083                 str = tmp;
3084         }
3085
3086         return str;
3087 }
3088
3089 gint execute_async(gchar *const argv[])
3090 {
3091         pid_t pid;
3092         gint status;
3093
3094         if ((pid = fork()) < 0) {
3095                 perror("fork");
3096                 return -1;
3097         }
3098
3099         if (pid == 0) {                 /* child process */
3100                 pid_t gch_pid;
3101
3102                 if ((gch_pid = fork()) < 0) {
3103                         perror("fork");
3104                         _exit(1);
3105                 }
3106
3107                 if (gch_pid == 0) {     /* grandchild process */
3108                         execvp(argv[0], argv);
3109
3110                         perror("execvp");
3111                         _exit(1);
3112                 }
3113
3114                 _exit(0);
3115         }
3116
3117         waitpid(pid, &status, 0);
3118
3119         if (WIFEXITED(status))
3120                 return WEXITSTATUS(status);
3121         else
3122                 return -1;
3123 }
3124
3125 gint execute_sync(gchar *const argv[])
3126 {
3127         pid_t pid;
3128         gint status;
3129
3130         if ((pid = fork()) < 0) {
3131                 perror("fork");
3132                 return -1;
3133         }
3134
3135         if (pid == 0) {         /* child process */
3136                 execvp(argv[0], argv);
3137
3138                 perror("execvp");
3139                 _exit(1);
3140         }
3141
3142         waitpid(pid, &status, 0);
3143
3144         if (WIFEXITED(status))
3145                 return WEXITSTATUS(status);
3146         else
3147                 return -1;
3148 }
3149
3150 gint execute_command_line(const gchar *cmdline, gboolean async)
3151 {
3152         gchar **argv;
3153         gint ret;
3154
3155         debug_print("executing: %s\n", cmdline);
3156
3157         argv = strsplit_with_quote(cmdline, " ", 0);
3158
3159         if (async)
3160                 ret = execute_async(argv);
3161         else
3162                 ret = execute_sync(argv);
3163
3164         g_strfreev(argv);
3165
3166         return ret;
3167 }
3168
3169 gchar *get_command_output(const gchar *cmdline)
3170 {
3171         gchar buf[BUFFSIZE];
3172         FILE *fp;
3173         GString *str;
3174         gchar *ret;
3175
3176         g_return_val_if_fail(cmdline != NULL, NULL);
3177
3178         if ((fp = popen(cmdline, "r")) == NULL) {
3179                 FILE_OP_ERROR(cmdline, "popen");
3180                 return NULL;
3181         }
3182
3183         str = g_string_new("");
3184
3185         while (fgets(buf, sizeof(buf), fp) != NULL)
3186                 g_string_append(str, buf);
3187
3188         pclose(fp);
3189
3190         ret = str->str;
3191         g_string_free(str, FALSE);
3192
3193         if (!g_utf8_validate(ret, -1, NULL)) {
3194                 const gchar *src_codeset, *dest_codeset;
3195                 gchar *tmp = NULL;
3196                 src_codeset = conv_get_current_charset_str();
3197                 dest_codeset = CS_UTF_8;
3198                 tmp = conv_codeset_strdup(ret, src_codeset, dest_codeset);
3199                 g_free(ret);
3200                 ret = tmp;
3201         }
3202         
3203         return ret;
3204 }
3205
3206 static gint is_unchanged_uri_char(char c)
3207 {
3208         switch (c) {
3209                 case '(':
3210                 case ')':
3211                 case ',':
3212                         return 0;
3213                 default:
3214                         return 1;
3215         }
3216 }
3217
3218 void encode_uri(gchar *encoded_uri, gint bufsize, const gchar *uri)
3219 {
3220         int i;
3221         int k;
3222
3223         k = 0;
3224         for(i = 0; i < strlen(uri) ; i++) {
3225                 if (is_unchanged_uri_char(uri[i])) {
3226                         if (k + 2 >= bufsize)
3227                                 break;
3228                         encoded_uri[k++] = uri[i];
3229                 }
3230                 else {
3231                         char * hexa = "0123456789ABCDEF";
3232                         
3233                         if (k + 4 >= bufsize)
3234                                 break;
3235                         encoded_uri[k++] = '%';
3236                         encoded_uri[k++] = hexa[uri[i] / 16];
3237                         encoded_uri[k++] = hexa[uri[i] % 16];
3238                 }
3239         }
3240         encoded_uri[k] = 0;
3241 }
3242
3243 gint open_uri(const gchar *uri, const gchar *cmdline)
3244 {
3245         gchar buf[BUFFSIZE];
3246         gchar *p;
3247         gchar encoded_uri[BUFFSIZE];
3248         
3249         g_return_val_if_fail(uri != NULL, -1);
3250
3251         /* an option to choose whether to use encode_uri or not ? */
3252         encode_uri(encoded_uri, BUFFSIZE, uri);
3253         
3254         if (cmdline &&
3255             (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
3256             !strchr(p + 2, '%'))
3257                 g_snprintf(buf, sizeof(buf), cmdline, encoded_uri);
3258         else {
3259                 if (cmdline)
3260                         g_warning("Open URI command line is invalid "
3261                                   "(there must be only one '%%s'): %s",
3262                                   cmdline);
3263                 g_snprintf(buf, sizeof(buf), DEFAULT_BROWSER_CMD, encoded_uri);
3264         }
3265
3266         execute_command_line(buf, TRUE);
3267
3268         return 0;
3269 }
3270
3271 time_t remote_tzoffset_sec(const gchar *zone)
3272 {
3273         static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
3274         gchar zone3[4];
3275         gchar *p;
3276         gchar c;
3277         gint iustz;
3278         gint offset;
3279         time_t remoteoffset;
3280
3281         strncpy(zone3, zone, 3);
3282         zone3[3] = '\0';
3283         remoteoffset = 0;
3284
3285         if (sscanf(zone, "%c%d", &c, &offset) == 2 &&
3286             (c == '+' || c == '-')) {
3287                 remoteoffset = ((offset / 100) * 60 + (offset % 100)) * 60;
3288                 if (c == '-')
3289                         remoteoffset = -remoteoffset;
3290         } else if (!strncmp(zone, "UT" , 2) ||
3291                    !strncmp(zone, "GMT", 2)) {
3292                 remoteoffset = 0;
3293         } else if (strlen(zone3) == 3) {
3294                 for (p = ustzstr; *p != '\0'; p += 3) {
3295                         if (!g_ascii_strncasecmp(p, zone3, 3)) {
3296                                 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
3297                                 remoteoffset = iustz * 3600;
3298                                 break;
3299                         }
3300                 }
3301                 if (*p == '\0')
3302                         return -1;
3303         } else if (strlen(zone3) == 1) {
3304                 switch (zone[0]) {
3305                 case 'Z': remoteoffset =   0; break;
3306                 case 'A': remoteoffset =  -1; break;
3307                 case 'B': remoteoffset =  -2; break;
3308                 case 'C': remoteoffset =  -3; break;
3309                 case 'D': remoteoffset =  -4; break;
3310                 case 'E': remoteoffset =  -5; break;
3311                 case 'F': remoteoffset =  -6; break;
3312                 case 'G': remoteoffset =  -7; break;
3313                 case 'H': remoteoffset =  -8; break;
3314                 case 'I': remoteoffset =  -9; break;
3315                 case 'K': remoteoffset = -10; break; /* J is not used */
3316                 case 'L': remoteoffset = -11; break;
3317                 case 'M': remoteoffset = -12; break;
3318                 case 'N': remoteoffset =   1; break;
3319                 case 'O': remoteoffset =   2; break;
3320                 case 'P': remoteoffset =   3; break;
3321                 case 'Q': remoteoffset =   4; break;
3322                 case 'R': remoteoffset =   5; break;
3323                 case 'S': remoteoffset =   6; break;
3324                 case 'T': remoteoffset =   7; break;
3325                 case 'U': remoteoffset =   8; break;
3326                 case 'V': remoteoffset =   9; break;
3327                 case 'W': remoteoffset =  10; break;
3328                 case 'X': remoteoffset =  11; break;
3329                 case 'Y': remoteoffset =  12; break;
3330                 default:  remoteoffset =   0; break;
3331                 }
3332                 remoteoffset = remoteoffset * 3600;
3333         } else
3334                 return -1;
3335
3336         return remoteoffset;
3337 }
3338
3339 time_t tzoffset_sec(time_t *now)
3340 {
3341         struct tm gmt, *lt;
3342         gint off;
3343
3344         gmt = *gmtime(now);
3345         lt = localtime(now);
3346
3347         off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
3348
3349         if (lt->tm_year < gmt.tm_year)
3350                 off -= 24 * 60;
3351         else if (lt->tm_year > gmt.tm_year)
3352                 off += 24 * 60;
3353         else if (lt->tm_yday < gmt.tm_yday)
3354                 off -= 24 * 60;
3355         else if (lt->tm_yday > gmt.tm_yday)
3356                 off += 24 * 60;
3357
3358         if (off >= 24 * 60)             /* should be impossible */
3359                 off = 23 * 60 + 59;     /* if not, insert silly value */
3360         if (off <= -24 * 60)
3361                 off = -(23 * 60 + 59);
3362
3363         return off * 60;
3364 }
3365
3366 /* calculate timezone offset */
3367 gchar *tzoffset(time_t *now)
3368 {
3369         static gchar offset_string[6];
3370         struct tm gmt, *lt;
3371         gint off;
3372         gchar sign = '+';
3373
3374         gmt = *gmtime(now);
3375         lt = localtime(now);
3376
3377         off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
3378
3379         if (lt->tm_year < gmt.tm_year)
3380                 off -= 24 * 60;
3381         else if (lt->tm_year > gmt.tm_year)
3382                 off += 24 * 60;
3383         else if (lt->tm_yday < gmt.tm_yday)
3384                 off -= 24 * 60;
3385         else if (lt->tm_yday > gmt.tm_yday)
3386                 off += 24 * 60;
3387
3388         if (off < 0) {
3389                 sign = '-';
3390                 off = -off;
3391         }
3392
3393         if (off >= 24 * 60)             /* should be impossible */
3394                 off = 23 * 60 + 59;     /* if not, insert silly value */
3395
3396         sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
3397
3398         return offset_string;
3399 }
3400
3401 void get_rfc822_date(gchar *buf, gint len)
3402 {
3403         struct tm *lt;
3404         time_t t;
3405         gchar day[4], mon[4];
3406         gint dd, hh, mm, ss, yyyy;
3407
3408         t = time(NULL);
3409         lt = localtime(&t);
3410
3411         sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
3412                day, mon, &dd, &hh, &mm, &ss, &yyyy);
3413         g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
3414                    day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
3415 }
3416
3417 void debug_set_mode(gboolean mode)
3418 {
3419         debug_mode = mode;
3420 }
3421
3422 gboolean debug_get_mode(void)
3423 {
3424         return debug_mode;
3425 }
3426
3427 void debug_print_real(const gchar *format, ...)
3428 {
3429         va_list args;
3430         gchar buf[BUFFSIZE];
3431
3432         if (!debug_mode) return;
3433
3434         va_start(args, format);
3435         g_vsnprintf(buf, sizeof(buf), format, args);
3436         va_end(args);
3437
3438         fputs(buf, stdout);
3439 }
3440
3441 void * subject_table_lookup(GHashTable *subject_table, gchar * subject)
3442 {
3443         if (subject == NULL)
3444                 subject = "";
3445         else
3446                 subject += subject_get_prefix_length(subject);
3447
3448         return g_hash_table_lookup(subject_table, subject);
3449 }
3450
3451 void subject_table_insert(GHashTable *subject_table, gchar * subject,
3452                           void * data)
3453 {
3454         if (subject == NULL || *subject == 0)
3455                 return;
3456         subject += subject_get_prefix_length(subject);
3457         g_hash_table_insert(subject_table, subject, data);
3458 }
3459
3460 void subject_table_remove(GHashTable *subject_table, gchar * subject)
3461 {
3462         if (subject == NULL)
3463                 return;
3464
3465         subject += subject_get_prefix_length(subject);  
3466         g_hash_table_remove(subject_table, subject);
3467 }
3468
3469 /*!
3470  *\brief        Check if a string is prefixed with known (combinations) 
3471  *              of prefixes. The function assumes that each prefix 
3472  *              is terminated by zero or exactly _one_ space.
3473  *
3474  *\param        str String to check for a prefixes
3475  *
3476  *\return       int Number of chars in the prefix that should be skipped 
3477  *              for a "clean" subject line. If no prefix was found, 0
3478  *              is returned.
3479  */             
3480 int subject_get_prefix_length(const gchar *subject)
3481 {
3482         /*!< Array with allowable reply prefixes regexps. */
3483         static const gchar * const prefixes[] = {
3484                 "Re\\:",                        /* "Re:" */
3485                 "Re\\[[1-9][0-9]*\\]\\:",       /* "Re[XXX]:" (non-conforming news mail clients) */
3486                 "Antw\\:",                      /* "Antw:" (Dutch / German Outlook) */
3487                 "Aw\\:",                        /* "Aw:"   (German) */
3488                 "Antwort\\:",                   /* "Antwort:" (German Lotus Notes) */
3489                 "Res\\:",                       /* "Res:" (Brazilian Outlook) */
3490                 "Fw\\:",                        /* "Fw:" Forward */
3491                 "Enc\\:",                       /* "Enc:" Forward (Brazilian Outlook) */
3492                 "Odp\\:",                       /* "Odp:" Re (Polish Outlook) */
3493                 "Rif\\:"                        /* "Rif:" (Italian Outlook) */
3494                 /* add more */
3495         };
3496         const int PREFIXES = sizeof prefixes / sizeof prefixes[0];
3497         int n;
3498         regmatch_t pos;
3499         static regex_t regex;
3500         static gboolean init_;
3501
3502         if (!subject) return 0;
3503         if (!*subject) return 0;
3504
3505         if (!init_) {
3506                 GString *s = g_string_new("");
3507                 
3508                 for (n = 0; n < PREFIXES; n++)
3509                         /* Terminate each prefix regexpression by a
3510                          * "\ ?" (zero or ONE space), and OR them */
3511                         g_string_append_printf(s, "(%s\\ ?)%s",
3512                                           prefixes[n],
3513                                           n < PREFIXES - 1 ? 
3514                                           "|" : "");
3515                 
3516                 g_string_prepend(s, "(");
3517                 g_string_append(s, ")+");       /* match at least once */
3518                 g_string_prepend(s, "^\\ *");   /* from beginning of line */
3519                 
3520
3521                 /* We now have something like "^\ *((PREFIX1\ ?)|(PREFIX2\ ?))+" 
3522                  * TODO: Should this be       "^\ *(((PREFIX1)|(PREFIX2))\ ?)+" ??? */
3523                 if (regcomp(&regex, s->str, REG_EXTENDED | REG_ICASE)) { 
3524                         debug_print("Error compiling regexp %s\n", s->str);
3525                         g_string_free(s, TRUE);
3526                         return 0;
3527                 } else {
3528                         init_ = TRUE;
3529                         g_string_free(s, TRUE);
3530                 }
3531         }
3532         
3533         if (!regexec(&regex, subject, 1, &pos, 0) && pos.rm_so != -1)
3534                 return pos.rm_eo;
3535         else
3536                 return 0;
3537 }
3538
3539 guint g_stricase_hash(gconstpointer gptr)
3540 {
3541         guint hash_result = 0;
3542         const char *str;
3543
3544         for (str = gptr; str && *str; str++) {
3545                 if (isupper((guchar)*str)) hash_result += (*str + ' ');
3546                 else hash_result += *str;
3547         }
3548
3549         return hash_result;
3550 }
3551
3552 gint g_stricase_equal(gconstpointer gptr1, gconstpointer gptr2)
3553 {
3554         const char *str1 = gptr1;
3555         const char *str2 = gptr2;
3556
3557         return !g_utf8_collate(str1, str2);
3558 }
3559
3560 gint g_int_compare(gconstpointer a, gconstpointer b)
3561 {
3562         return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
3563 }
3564
3565 gchar *generate_msgid(gchar *buf, gint len)
3566 {
3567         struct tm *lt;
3568         time_t t;
3569         gchar *addr;
3570
3571         t = time(NULL);
3572         lt = localtime(&t);
3573
3574         addr = g_strconcat("@", get_domain_name(), NULL);
3575
3576         g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x%s",
3577                    lt->tm_year + 1900, lt->tm_mon + 1,
3578                    lt->tm_mday, lt->tm_hour,
3579                    lt->tm_min, lt->tm_sec,
3580                    (guint) rand(), addr);
3581
3582         g_free(addr);
3583         return buf;
3584 }
3585
3586 /*
3587    quote_cmd_argument()
3588    
3589    return a quoted string safely usable in argument of a command.
3590    
3591    code is extracted and adapted from etPan! project -- DINH V. Hoà.
3592 */
3593
3594 gint quote_cmd_argument(gchar * result, guint size,
3595                         const gchar * path)
3596 {
3597         const gchar * p;
3598         gchar * result_p;
3599         guint remaining;
3600
3601         result_p = result;
3602         remaining = size;
3603
3604         for(p = path ; * p != '\0' ; p ++) {
3605
3606                 if (isalnum((guchar)*p) || (* p == '/')) {
3607                         if (remaining > 0) {
3608                                 * result_p = * p;
3609                                 result_p ++; 
3610                                 remaining --;
3611                         }
3612                         else {
3613                                 result[size - 1] = '\0';
3614                                 return -1;
3615                         }
3616                 }
3617                 else { 
3618                         if (remaining >= 2) {
3619                                 * result_p = '\\';
3620                                 result_p ++; 
3621                                 * result_p = * p;
3622                                 result_p ++; 
3623                                 remaining -= 2;
3624                         }
3625                         else {
3626                                 result[size - 1] = '\0';
3627                                 return -1;
3628                         }
3629                 }
3630         }
3631         if (remaining > 0) {
3632                 * result_p = '\0';
3633         }
3634         else {
3635                 result[size - 1] = '\0';
3636                 return -1;
3637         }
3638   
3639         return 0;
3640 }
3641
3642 typedef struct 
3643 {
3644         GNode           *parent;
3645         GNodeMapFunc     func;
3646         gpointer         data;
3647 } GNodeMapData;
3648
3649 static void g_node_map_recursive(GNode *node, gpointer data)
3650 {
3651         GNodeMapData *mapdata = (GNodeMapData *) data;
3652         GNode *newnode;
3653         GNodeMapData newmapdata;
3654         gpointer newdata;
3655
3656         newdata = mapdata->func(node->data, mapdata->data);
3657         if (newdata != NULL) {
3658                 newnode = g_node_new(newdata);
3659                 g_node_append(mapdata->parent, newnode);
3660
3661                 newmapdata.parent = newnode;
3662                 newmapdata.func = mapdata->func;
3663                 newmapdata.data = mapdata->data;
3664
3665                 g_node_children_foreach(node, G_TRAVERSE_ALL, g_node_map_recursive, &newmapdata);
3666         }
3667 }
3668
3669 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data)
3670 {
3671         GNode *root;
3672         GNodeMapData mapdata;
3673
3674         g_return_val_if_fail(node != NULL, NULL);
3675         g_return_val_if_fail(func != NULL, NULL);
3676
3677         root = g_node_new(func(node->data, data));
3678
3679         mapdata.parent = root;
3680         mapdata.func = func;
3681         mapdata.data = data;
3682
3683         g_node_children_foreach(node, G_TRAVERSE_ALL, g_node_map_recursive, &mapdata);
3684
3685         return root;
3686 }
3687
3688 #define HEX_TO_INT(val, hex)                    \
3689 {                                               \
3690         gchar c = hex;                          \
3691                                                 \
3692         if ('0' <= c && c <= '9') {             \
3693                 val = c - '0';                  \
3694         } else if ('a' <= c && c <= 'f') {      \
3695                 val = c - 'a' + 10;             \
3696         } else if ('A' <= c && c <= 'F') {      \
3697                 val = c - 'A' + 10;             \
3698         } else {                                \
3699                 val = -1;                       \
3700         }                                       \
3701 }
3702
3703 gboolean get_hex_value(guchar *out, gchar c1, gchar c2)
3704 {
3705         gint hi, lo;
3706
3707         HEX_TO_INT(hi, c1);
3708         HEX_TO_INT(lo, c2);
3709
3710         if (hi == -1 || lo == -1)
3711                 return FALSE;
3712
3713         *out = (hi << 4) + lo;
3714         return TRUE;
3715 }
3716
3717 #define INT_TO_HEX(hex, val)            \
3718 {                                       \
3719         if ((val) < 10)                 \
3720                 hex = '0' + (val);      \
3721         else                            \
3722                 hex = 'A' + (val) - 10; \
3723 }
3724
3725 void get_hex_str(gchar *out, guchar ch)
3726 {
3727         gchar hex;
3728
3729         INT_TO_HEX(hex, ch >> 4);
3730         *out++ = hex;
3731         INT_TO_HEX(hex, ch & 0x0f);
3732         *out++ = hex;
3733 }
3734
3735 #undef REF_DEBUG
3736 #ifndef REF_DEBUG
3737 #define G_PRINT_REF 1 == 1 ? (void) 0 : (void)
3738 #else
3739 #define G_PRINT_REF g_print
3740 #endif
3741
3742 /*!
3743  *\brief        Register ref counted pointer. It is based on GBoxed, so should
3744  *              work with anything that uses the GType system. The semantics
3745  *              are similar to a C++ auto pointer, with the exception that
3746  *              C doesn't have automatic closure (calling destructors) when 
3747  *              exiting a block scope.
3748  *              Use the \ref G_TYPE_AUTO_POINTER macro instead of calling this
3749  *              function directly.
3750  *
3751  *\return       GType A GType type.
3752  */
3753 GType g_auto_pointer_register(void)
3754 {
3755         static GType auto_pointer_type;
3756         if (!auto_pointer_type)
3757                 auto_pointer_type =
3758                         g_boxed_type_register_static
3759                                 ("G_TYPE_AUTO_POINTER",
3760                                  (GBoxedCopyFunc) g_auto_pointer_copy,
3761                                  (GBoxedFreeFunc) g_auto_pointer_free);
3762         return auto_pointer_type;                                                    
3763 }
3764
3765 /*!
3766  *\brief        Structure with g_new() allocated pointer guarded by the
3767  *              auto pointer
3768  */
3769 typedef struct AutoPointerRef {
3770         void          (*free) (gpointer);
3771         gpointer        pointer;
3772         glong           cnt;
3773 } AutoPointerRef;
3774
3775 /*!
3776  *\brief        The auto pointer opaque structure that references the
3777  *              pointer guard block.
3778  */
3779 typedef struct AutoPointer {
3780         AutoPointerRef *ref;
3781         gpointer        ptr; /*!< access to protected pointer */
3782 } AutoPointer;
3783
3784 /*!
3785  *\brief        Creates an auto pointer for a g_new()ed pointer. Example:
3786  *
3787  *\code 
3788  *
3789  *              ... tell gtk_list_store it should use a G_TYPE_AUTO_POINTER
3790  *              ... when assigning, copying and freeing storage elements
3791  *
3792  *              gtk_list_store_new(N_S_COLUMNS, 
3793  *                                 G_TYPE_AUTO_POINTER,
3794  *                                 -1);
3795  *
3796  *
3797  *              Template *precious_data = g_new0(Template, 1);
3798  *              g_pointer protect = g_auto_pointer_new(precious_data);
3799  *
3800  *              gtk_list_store_set(container, &iter,
3801  *                                 S_DATA, protect,
3802  *                                 -1);
3803  *
3804  *              ... the gtk_list_store has copied the pointer and 
3805  *              ... incremented its reference count, we should free
3806  *              ... the auto pointer (in C++ a destructor would do
3807  *              ... this for us when leaving block scope)
3808  * 
3809  *              g_auto_pointer_free(protect);
3810  *
3811  *              ... gtk_list_store_set() now manages the data. When
3812  *              ... *explicitly* requesting a pointer from the list 
3813  *              ... store, don't forget you get a copy that should be 
3814  *              ... freed with g_auto_pointer_free() eventually.
3815  *
3816  *\endcode
3817  *
3818  *\param        pointer Pointer to be guarded.
3819  *
3820  *\return       GAuto * Pointer that should be used in containers with
3821  *              GType support.
3822  */
3823 GAuto *g_auto_pointer_new(gpointer p)
3824 {
3825         AutoPointerRef *ref = g_new0(AutoPointerRef, 1);
3826         AutoPointer    *ptr = g_new0(AutoPointer, 1);
3827
3828         ref->pointer = p;
3829         ref->free = g_free;
3830         ref->cnt = 1;
3831
3832         ptr->ref = ref;
3833         ptr->ptr = p;
3834
3835         G_PRINT_REF ("XXXX ALLOC(%lx)\n", p);
3836
3837         return ptr;
3838 }
3839
3840 /*!
3841  *\brief        Allocate an autopointer using the passed \a free function to
3842  *              free the guarded pointer
3843  */
3844 GAuto *g_auto_pointer_new_with_free(gpointer p, GFreeFunc free_)
3845 {       
3846         AutoPointer *aptr = g_auto_pointer_new(p);
3847
3848         aptr->ref->free = free_;
3849         return aptr; 
3850 }
3851
3852 gpointer g_auto_pointer_get_ptr(GAuto *auto_ptr)
3853 {
3854         return ((AutoPointer *) auto_ptr)->ptr; 
3855 }
3856
3857 /*!
3858  *\brief        Copies an auto pointer by. It's mostly not necessary
3859  *              to call this function directly, unless you copy/assign
3860  *              the guarded pointer.
3861  *
3862  *\param        auto_ptr Auto pointer returned by previous call to 
3863  *              g_auto_pointer_new_XXX()
3864  *
3865  *\return       gpointer An auto pointer
3866  */
3867 GAuto *g_auto_pointer_copy(GAuto *auto_ptr)
3868 {
3869         AutoPointer     *ptr = auto_ptr;
3870         AutoPointerRef  *ref = ptr->ref;
3871         AutoPointer     *newp = g_new0(AutoPointer, 1);
3872
3873         newp->ref = ref;
3874         newp->ptr = ref->pointer;
3875         ++(ref->cnt);
3876         
3877         G_PRINT_REF ("XXXX COPY(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
3878
3879         return newp;
3880 }
3881
3882 /*!
3883  *\brief        Free an auto pointer
3884  */
3885 void g_auto_pointer_free(GAuto *auto_ptr)
3886 {
3887         AutoPointer     *ptr = auto_ptr;
3888         AutoPointerRef  *ref = ptr->ref;
3889
3890         if (--(ref->cnt) == 0) {
3891                 G_PRINT_REF ("XXXX FREE(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
3892                 ref->free(ref->pointer);
3893                 g_free(ref);
3894         } else
3895                 G_PRINT_REF ("XXXX DEREF(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
3896         g_free(ptr);            
3897 }
3898