2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
40 #include <sys/types.h>
47 #include "statusbar.h"
48 #include "logwindow.h"
52 extern gboolean debug_mode
;
54 static void hash_free_strings_func(gpointer key
, gpointer value
, gpointer data
);
56 void list_free_strings(GList
*list
)
58 list
= g_list_first(list
);
60 while (list
!= NULL
) {
66 void slist_free_strings(GSList
*list
)
68 while (list
!= NULL
) {
74 static void hash_free_strings_func(gpointer key
, gpointer value
, gpointer data
)
79 void hash_free_strings(GHashTable
*table
)
81 g_hash_table_foreach(table
, hash_free_strings_func
, NULL
);
84 static void hash_free_value_mem_func(gpointer key
, gpointer value
,
90 void hash_free_value_mem(GHashTable
*table
)
92 g_hash_table_foreach(table
, hash_free_value_mem_func
, NULL
);
95 void ptr_array_free_strings(GPtrArray
*array
)
100 g_return_if_fail(array
!= NULL
);
102 for (i
= 0; i
< array
->len
; i
++) {
103 str
= g_ptr_array_index(array
, i
);
108 gint
to_number(const gchar
*nstr
)
110 register const gchar
*p
;
112 if (*nstr
== '\0') return -1;
114 for (p
= nstr
; *p
!= '\0'; p
++)
115 if (!isdigit(*p
)) return -1;
120 /* convert integer into string,
121 nstr must be not lower than 11 characters length */
122 gchar
*itos_buf(gchar
*nstr
, gint n
)
124 g_snprintf(nstr
, 11, "%d", n
);
128 /* convert integer into string */
131 static gchar nstr
[11];
133 return itos_buf(nstr
, n
);
136 gchar
*to_human_readable(off_t size
)
138 static gchar str
[10];
142 for (count
= 0; count
< 3; count
++) {
143 if (size
/ div
< 1024)
150 case 0: g_snprintf(str
, sizeof(str
), "%dB", (gint
)size
); break;
151 case 1: g_snprintf(str
, sizeof(str
), "%.1fKB", (gfloat
)size
/ div
);
153 case 2: g_snprintf(str
, sizeof(str
), "%.2fMB", (gfloat
)size
/ div
);
156 g_snprintf(str
, sizeof(str
), "%.2fGB", (gfloat
)size
/ div
);
163 /* strcmp with NULL-checking */
164 gint
strcmp2(const gchar
*s1
, const gchar
*s2
)
166 if (s1
== NULL
|| s2
== NULL
)
169 return strcmp(s1
, s2
);
173 gint
path_cmp(const gchar
*s1
, const gchar
*s2
)
177 if (s1
== NULL
|| s2
== NULL
) return -1;
178 if (*s1
== '\0' || *s2
== '\0') return -1;
183 if (s1
[len1
- 1] == G_DIR_SEPARATOR
) len1
--;
184 if (s2
[len2
- 1] == G_DIR_SEPARATOR
) len2
--;
186 return strncmp(s1
, s2
, MAX(len1
, len2
));
189 /* remove trailing return code */
190 gchar
*strretchomp(gchar
*str
)
194 if (!*str
) return str
;
196 for (s
= str
+ strlen(str
) - 1;
197 s
>= str
&& (*s
== '\n' || *s
== '\r');
204 /* remove trailing character */
205 gchar
*strtailchomp(gchar
*str
, gchar tail_char
)
209 if (!*str
) return str
;
210 if (tail_char
== '\0') return str
;
212 for (s
= str
+ strlen(str
) - 1; s
>= str
&& *s
== tail_char
; s
--)
219 /* Similar to `strstr' but this function ignores the case of both strings. */
220 gchar
*strcasestr(const gchar
*haystack
, const gchar
*needle
)
222 register size_t haystack_len
, needle_len
;
224 haystack_len
= strlen(haystack
);
225 needle_len
= strlen(needle
);
227 if (haystack_len
< needle_len
|| needle_len
== 0)
230 while (haystack_len
>= needle_len
) {
231 if (!strncasecmp(haystack
, needle
, needle_len
))
232 return (gchar
*)haystack
;
242 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
243 gchar
*strncpy2(gchar
*dest
, const gchar
*src
, size_t n
)
257 /* don't do zero fill */
262 int iswalnum(wint_t wc
)
264 return isalnum((int)wc
);
269 int iswspace(wint_t wc
)
271 return isspace((int)wc
);
276 wint_t towlower(wint_t wc
)
278 if (wc
>= L
'A' && wc
<= L
'Z')
279 return wc
+ L
'a' - L
'A';
286 size_t wcslen(const wchar_t *s
)
298 /* Copy SRC to DEST. */
299 wchar_t *wcscpy(wchar_t *dest
, const wchar_t *src
)
307 } while (c
!= L
'\0');
314 /* Copy no more than N wide-characters of SRC to DEST. */
315 wchar_t *wcsncpy (wchar_t *dest
, const wchar_t *src
, size_t n
)
325 } while (c
!= L
'\0');
336 /* Duplicate S, returning an identical malloc'd string. */
337 wchar_t *wcsdup(const wchar_t *s
)
342 new_str
= g_new(wchar_t, wcslen(s
) + 1);
350 /* Duplicate no more than N wide-characters of S,
351 returning an identical malloc'd string. */
352 wchar_t *wcsndup(const wchar_t *s
, size_t n
)
357 new_str
= g_new(wchar_t, n
+ 1);
358 wcsncpy(new_str
, s
, n
);
359 new_str
[n
] = (wchar_t)0;
366 wchar_t *strdup_mbstowcs(const gchar
*s
)
371 new_str
= g_new(wchar_t, strlen(s
) + 1);
372 if (mbstowcs(new_str
, s
, strlen(s
) + 1) < 0) {
376 new_str
= g_realloc(new_str
,
377 sizeof(wchar_t) * (wcslen(new_str
) + 1));
384 gchar
*strdup_wcstombs(const wchar_t *s
)
390 len
= wcslen(s
) * MB_CUR_MAX
+ 1;
391 new_str
= g_new(gchar
, len
);
392 if (wcstombs(new_str
, s
, len
) < 0) {
396 new_str
= g_realloc(new_str
, strlen(new_str
) + 1);
403 /* Compare S1 and S2, ignoring case. */
404 gint
wcsncasecmp(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
410 c1
= towlower(*s1
++);
411 c2
= towlower(*s2
++);
414 else if (c1
== 0 && c2
== 0)
421 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
422 wchar_t *wcscasestr(const wchar_t *haystack
, const wchar_t *needle
)
424 register size_t haystack_len
, needle_len
;
426 haystack_len
= wcslen(haystack
);
427 needle_len
= wcslen(needle
);
429 if (haystack_len
< needle_len
|| needle_len
== 0)
432 while (haystack_len
>= needle_len
) {
433 if (!wcsncasecmp(haystack
, needle
, needle_len
))
434 return (wchar_t *)haystack
;
444 /* Examine if next block is non-ASCII string */
445 gboolean
is_next_nonascii(const wchar_t *s
)
449 /* skip head space */
450 for (wp
= s
; *wp
!= (wchar_t)0 && iswspace(*wp
); wp
++)
452 for (; *wp
!= (wchar_t)0 && !iswspace(*wp
); wp
++) {
460 /* Examine if next block is multi-byte string */
461 gboolean
is_next_mbs(const wchar_t *s
)
465 gchar tmp
[MB_CUR_MAX
];
467 /* skip head space */
468 for (wp
= s
; *wp
!= (wchar_t)0 && iswspace(*wp
); wp
++)
470 for (; *wp
!= (wchar_t)0 && !iswspace(*wp
); wp
++) {
471 mbl
= wctomb(tmp
, *wp
);
479 wchar_t *find_wspace(const wchar_t *s
)
483 for (wp
= s
; *wp
!= (wchar_t)0 && iswspace(*wp
); wp
++)
485 for (; *wp
!= (wchar_t)0; wp
++) {
487 return (wchar_t *)wp
;
493 /* compare subjects */
494 gint
subject_compare(const gchar
*s1
, const gchar
*s2
)
499 if (!s1
|| !s2
) return -1;
500 if (!*s1
|| !*s2
) return -1;
502 Xalloca(str1
, strlen(s1
) + 1, return -1);
503 Xalloca(str2
, strlen(s2
) + 1, return -1);
510 if (!*str1
|| !*str2
) return -1;
512 retval
= strcmp(str1
, str2
);
515 g_print("\ns1 = %s\ns2 = %s\n"
516 "str1 = %s\nstr2 = %s\nmatched.\n",
522 void trim_subject(gchar
*str
)
526 eliminate_parenthesis(str
, '[', ']');
527 eliminate_parenthesis(str
, '(', ')');
530 while (!strncasecmp(str
, "Re:", 3)) {
532 while (isspace(*srcp
)) srcp
++;
533 memmove(str
, srcp
, strlen(srcp
) + 1);
537 void eliminate_parenthesis(gchar
*str
, gchar op
, gchar cl
)
539 register gchar
*srcp
, *destp
;
544 while ((destp
= strchr(destp
, op
))) {
550 else if (*srcp
== cl
)
556 while (isspace(*srcp
)) srcp
++;
557 memmove(destp
, srcp
, strlen(srcp
) + 1);
561 void extract_parenthesis(gchar
*str
, gchar op
, gchar cl
)
563 register gchar
*srcp
, *destp
;
568 while ((srcp
= strchr(destp
, op
))) {
571 memmove(destp
, srcp
+ 1, strlen(srcp
));
576 else if (*destp
== cl
)
588 void extract_one_parenthesis_with_skip_quote(gchar
*str
, gchar quote_chr
,
591 register gchar
*srcp
, *destp
;
593 gboolean in_quote
= FALSE
;
597 if ((srcp
= strchr_with_skip_quote(destp
, quote_chr
, op
))) {
598 memmove(destp
, srcp
+ 1, strlen(srcp
));
601 if (*destp
== op
&& !in_quote
)
603 else if (*destp
== cl
&& !in_quote
)
605 else if (*destp
== quote_chr
)
617 void extract_parenthesis_with_skip_quote(gchar
*str
, gchar quote_chr
,
620 register gchar
*srcp
, *destp
;
622 gboolean in_quote
= FALSE
;
626 while ((srcp
= strchr_with_skip_quote(destp
, quote_chr
, op
))) {
629 memmove(destp
, srcp
+ 1, strlen(srcp
));
632 if (*destp
== op
&& !in_quote
)
634 else if (*destp
== cl
&& !in_quote
)
636 else if (*destp
== quote_chr
)
648 void eliminate_quote(gchar
*str
, gchar quote_chr
)
650 register gchar
*srcp
, *destp
;
654 while ((destp
= strchr(destp
, quote_chr
))) {
655 if ((srcp
= strchr(destp
+ 1, quote_chr
))) {
657 while (isspace(*srcp
)) srcp
++;
658 memmove(destp
, srcp
, strlen(srcp
) + 1);
666 void extract_quote(gchar
*str
, gchar quote_chr
)
670 if ((str
= strchr(str
, quote_chr
))) {
671 if ((p
= strchr(str
+ 1, quote_chr
))) {
673 memmove(str
, str
+ 1, p
- str
);
678 void eliminate_address_comment(gchar
*str
)
680 register gchar
*srcp
, *destp
;
685 while ((destp
= strchr(destp
, '"'))) {
686 if ((srcp
= strchr(destp
+ 1, '"'))) {
691 while (isspace(*srcp
)) srcp
++;
692 memmove(destp
, srcp
, strlen(srcp
) + 1);
702 while ((destp
= strchr_with_skip_quote(destp
, '"', '('))) {
708 else if (*srcp
== ')')
714 while (isspace(*srcp
)) srcp
++;
715 memmove(destp
, srcp
, strlen(srcp
) + 1);
719 gchar
*strchr_with_skip_quote(const gchar
*str
, gint quote_chr
, gint c
)
721 gboolean in_quote
= FALSE
;
724 if (*str
== c
&& !in_quote
)
726 if (*str
== quote_chr
)
734 gchar
*strrchr_with_skip_quote(const gchar
*str
, gint quote_chr
, gint c
)
736 gboolean in_quote
= FALSE
;
739 p
= str
+ strlen(str
) - 1;
741 if (*p
== c
&& !in_quote
)
751 void extract_address(gchar
*str
)
753 eliminate_address_comment(str
);
754 if (strchr_with_skip_quote(str
, '"', '<'))
755 extract_parenthesis_with_skip_quote(str
, '"', '<', '>');
759 GSList
*address_list_append(GSList
*addr_list
, const gchar
*str
)
764 if (!str
) return addr_list
;
766 Xstrdup_a(work
, str
, return addr_list
);
768 eliminate_address_comment(work
);
771 while (workp
&& *workp
) {
774 if ((p
= strchr_with_skip_quote(workp
, '"', ','))) {
780 if (strchr_with_skip_quote(workp
, '"', '<'))
781 extract_parenthesis_with_skip_quote
782 (workp
, '"', '<', '>');
786 addr_list
= g_slist_append(addr_list
, g_strdup(workp
));
794 GSList
*references_list_append(GSList
*msgid_list
, const gchar
*str
)
798 if (!str
) return msgid_list
;
801 while (strp
&& *strp
) {
802 const gchar
*start
, *end
;
805 if ((start
= strchr(strp
, '<')) != NULL
) {
806 end
= strchr(start
+ 1, '>');
811 msgid
= g_strndup(start
+ 1, end
- start
- 1);
814 msgid_list
= g_slist_append(msgid_list
, msgid
);
824 GSList
*newsgroup_list_append(GSList
*group_list
, const gchar
*str
)
829 if (!str
) return group_list
;
831 Xstrdup_a(work
, str
, return group_list
);
835 while (workp
&& *workp
) {
838 if ((p
= strchr_with_skip_quote(workp
, '"', ','))) {
846 group_list
= g_slist_append(group_list
,
855 void remove_return(gchar
*str
)
857 register gchar
*p
= str
;
860 if (*p
== '\n' || *p
== '\r')
861 memmove(p
, p
+ 1, strlen(p
));
867 void remove_space(gchar
*str
)
869 register gchar
*p
= str
;
874 while (isspace(*(p
+ spc
)))
877 memmove(p
, p
+ spc
, strlen(p
+ spc
) + 1);
883 void unfold_line(gchar
*str
)
885 register gchar
*p
= str
;
889 if (*p
== '\n' || *p
== '\r') {
892 while (isspace(*(p
+ spc
)))
895 memmove(p
, p
+ spc
, strlen(p
+ spc
) + 1);
901 void subst_char(gchar
*str
, gchar orig
, gchar subst
)
903 register gchar
*p
= str
;
912 gboolean
is_header_line(const gchar
*str
)
914 if (str
[0] == ':') return FALSE
;
916 while (*str
!= '\0' && *str
!= ' ') {
925 gboolean
is_ascii_str(const guchar
*str
)
927 while (*str
!= '\0') {
928 if (*str
!= '\t' && *str
!= ' ' &&
929 *str
!= '\r' && *str
!= '\n' &&
930 (*str
< 32 || *str
>= 127))
938 gint
get_quote_level(const gchar
*str
)
940 size_t firstquotepos
;
941 size_t lastquotepos
= -1;
942 const gchar
*p
= str
;
944 gint quotelevel
= -1;
947 /* speed up line processing by only searching to the last '>' */
948 if ((pos
= strchr(str
, '>')) != NULL
) {
949 firstquotepos
= pos
- str
;
950 lastquotepos
= strrchr(str
, '>') - str
+ 1;
952 /* skip a line if it contains a '<' before the initial '>' */
953 if (memchr(str
, '<', pos
- str
) != NULL
)
958 while (i
< lastquotepos
) {
959 while (i
< lastquotepos
) {
960 if (isspace(*p
) || (*p
== '\t')) {
966 if (i
>= lastquotepos
)
971 else if ((*p
!= '-') && !isspace(*p
) && (i
< lastquotepos
)) {
972 /* any characters are allowed except '-' and space */
973 while ((*p
!= '-') && (*p
!= '>') && !isspace(*p
) &&
974 (i
< lastquotepos
)) {
980 else if ((i
>= lastquotepos
) || isspace(*p
))
991 GList
*uri_list_extract_filenames(const gchar
*uri_list
)
993 GList
*result
= NULL
;
1001 while (isspace(*p
)) p
++;
1002 if (!strncmp(p
, "file:", 5)) {
1005 while (*q
&& *q
!= '\n' && *q
!= '\r') q
++;
1009 while (q
> p
&& isspace(*q
)) q
--;
1010 file
= g_malloc(q
- p
+ 2);
1011 strncpy(file
, p
, q
- p
+ 1);
1012 file
[q
- p
+ 1] = '\0';
1013 result
= g_list_append(result
,file
);
1017 p
= strchr(p
, '\n');
1024 gchar
*strstr_with_skip_quote(const gchar
*haystack
, const gchar
*needle
)
1026 register guint haystack_len
, needle_len
;
1027 gboolean in_squote
= FALSE
, in_dquote
= FALSE
;
1029 haystack_len
= strlen(haystack
);
1030 needle_len
= strlen(needle
);
1032 if (haystack_len
< needle_len
|| needle_len
== 0)
1035 while (haystack_len
>= needle_len
) {
1036 if (!in_squote
&& !in_dquote
&&
1037 !strncmp(haystack
, needle
, needle_len
))
1038 return (gchar
*)haystack
;
1040 /* 'foo"bar"' -> foo"bar"
1041 "foo'bar'" -> foo'bar' */
1042 if (*haystack
== '\'') {
1045 else if (!in_dquote
)
1047 } else if (*haystack
== '\"') {
1050 else if (!in_squote
)
1061 /* this fuction was taken from gstrfuncs.c in glib. */
1062 gchar
**strsplit_with_quote(const gchar
*str
, const gchar
*delim
,
1065 GSList
*string_list
= NULL
, *slist
;
1066 gchar
**str_array
, *s
;
1069 g_return_val_if_fail(str
!= NULL
, NULL
);
1070 g_return_val_if_fail(delim
!= NULL
, NULL
);
1073 max_tokens
= G_MAXINT
;
1075 s
= strstr_with_skip_quote(str
, delim
);
1077 guint delimiter_len
= strlen(delim
);
1084 new_str
= g_new(gchar
, len
+ 1);
1085 strncpy(new_str
, str
, len
);
1087 string_list
= g_slist_prepend(string_list
, new_str
);
1089 str
= s
+ delimiter_len
;
1090 s
= strstr_with_skip_quote(str
, delim
);
1091 } while (--max_tokens
&& s
);
1096 string_list
= g_slist_prepend(string_list
, g_strdup(str
));
1099 str_array
= g_new(gchar
*, n
);
1103 str_array
[i
--] = NULL
;
1104 for (slist
= string_list
; slist
; slist
= slist
->next
)
1105 str_array
[i
--] = slist
->data
;
1107 g_slist_free(string_list
);
1113 * We need this wrapper around g_get_home_dir(), so that
1114 * we can fix some Windoze things here. Should be done in glibc of course
1115 * but as long as we are not able to do our own extensions to glibc, we do
1118 gchar
*get_home_dir(void)
1120 #if HAVE_DOSISH_SYSTEM
1121 static gchar
*home_dir
;
1124 home_dir
= read_w32_registry_string(NULL
,
1125 "Software\\Sylpheed", "HomeDir" );
1126 if (!home_dir
|| !*home_dir
) {
1127 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1128 const char *s
= g_get_home_dir();
1130 home_dir
= g_strdup (s
);
1132 if (!home_dir
|| !*home_dir
)
1133 home_dir
= g_strdup ("c:\\sylpheed");
1135 debug_print("initialized home_dir to `%s'\n", home_dir
);
1138 #else /* standard glib */
1139 return g_get_home_dir();
1143 gchar
*get_rc_dir(void)
1145 static gchar
*rc_dir
= NULL
;
1148 rc_dir
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
1154 gchar
*get_news_cache_dir(void)
1156 static gchar
*news_cache_dir
= NULL
;
1158 if (!news_cache_dir
)
1159 news_cache_dir
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
1160 NEWS_CACHE_DIR
, NULL
);
1162 return news_cache_dir
;
1165 gchar
*get_imap_cache_dir(void)
1167 static gchar
*imap_cache_dir
= NULL
;
1169 if (!imap_cache_dir
)
1170 imap_cache_dir
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
1171 IMAP_CACHE_DIR
, NULL
);
1173 return imap_cache_dir
;
1176 gchar
*get_mbox_cache_dir(void)
1178 static gchar
*mbox_cache_dir
= NULL
;
1180 if (!mbox_cache_dir
)
1181 mbox_cache_dir
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
1182 MBOX_CACHE_DIR
, NULL
);
1184 return mbox_cache_dir
;
1187 gchar
*get_mime_tmp_dir(void)
1189 static gchar
*mime_tmp_dir
= NULL
;
1192 mime_tmp_dir
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
1193 MIME_TMP_DIR
, NULL
);
1195 return mime_tmp_dir
;
1198 gchar
*get_tmp_file(void)
1200 static gchar
*tmp_file
= NULL
;
1203 tmp_file
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
1209 gchar
*get_domain_name(void)
1211 static gchar
*domain_name
= NULL
;
1214 gchar buf
[BUFFSIZE
] = "";
1216 if (gethostname(buf
, sizeof(buf
)) < 0) {
1217 perror("gethostname");
1218 strcpy(buf
, "unknown");
1221 domain_name
= g_strdup(buf
);
1227 off_t
get_file_size(const gchar
*file
)
1231 if (stat(file
, &s
) < 0) {
1232 FILE_OP_ERROR(file
, "stat");
1239 off_t
get_left_file_size(FILE *fp
)
1245 if ((pos
= ftell(fp
)) < 0) {
1249 if (fseek(fp
, 0L, SEEK_END
) < 0) {
1253 if ((end
= ftell(fp
)) < 0) {
1258 if (fseek(fp
, pos
, SEEK_SET
) < 0) {
1266 gboolean
file_exist(const gchar
*file
, gboolean allow_fifo
)
1270 if (stat(file
, &s
) < 0) {
1271 if (ENOENT
!= errno
) FILE_OP_ERROR(file
, "stat");
1275 if (S_ISREG(s
.st_mode
) || (allow_fifo
&& S_ISFIFO(s
.st_mode
)))
1281 gboolean
is_dir_exist(const gchar
*dir
)
1285 if (stat(dir
, &s
) < 0) {
1286 if (ENOENT
!= errno
) FILE_OP_ERROR(dir
, "stat");
1290 if (S_ISDIR(s
.st_mode
))
1296 gint
change_dir(const gchar
*dir
)
1298 gchar
*prevdir
= NULL
;
1301 prevdir
= g_get_current_dir();
1303 if (chdir(dir
) < 0) {
1304 FILE_OP_ERROR(dir
, "chdir");
1305 if (debug_mode
) g_free(prevdir
);
1307 } else if (debug_mode
) {
1310 cwd
= g_get_current_dir();
1311 if (strcmp(prevdir
, cwd
) != 0)
1312 g_print("current dir: %s\n", cwd
);
1320 gint
make_dir_hier(const gchar
*dir
)
1325 for (p
= dir
; (p
= strchr(p
, G_DIR_SEPARATOR
)) != NULL
; p
++) {
1326 parent_dir
= g_strndup(dir
, p
- dir
);
1327 if (*parent_dir
!= '\0') {
1328 if (!is_dir_exist(parent_dir
)) {
1329 if (mkdir(parent_dir
, S_IRWXU
) < 0) {
1330 FILE_OP_ERROR(parent_dir
, "mkdir");
1334 if (chmod(parent_dir
, S_IRWXU
) < 0)
1335 FILE_OP_ERROR(parent_dir
, "chmod");
1340 if (!is_dir_exist(dir
)) {
1341 if (mkdir(dir
, S_IRWXU
) < 0) {
1342 FILE_OP_ERROR(dir
, "mkdir");
1345 if (chmod(dir
, S_IRWXU
) < 0)
1346 FILE_OP_ERROR(dir
, "chmod");
1352 gint
remove_all_files(const gchar
*dir
)
1358 prev_dir
= g_get_current_dir();
1360 if (chdir(dir
) < 0) {
1361 FILE_OP_ERROR(dir
, "chdir");
1365 if ((dp
= opendir(".")) == NULL
) {
1366 FILE_OP_ERROR(dir
, "opendir");
1370 while ((d
= readdir(dp
)) != NULL
) {
1371 if (!strcmp(d
->d_name
, ".") ||
1372 !strcmp(d
->d_name
, ".."))
1375 if (unlink(d
->d_name
) < 0)
1376 FILE_OP_ERROR(d
->d_name
, "unlink");
1381 if (chdir(prev_dir
) < 0) {
1382 FILE_OP_ERROR(prev_dir
, "chdir");
1392 gint
remove_numbered_files(const gchar
*dir
, guint first
, guint last
)
1399 prev_dir
= g_get_current_dir();
1401 if (chdir(dir
) < 0) {
1402 FILE_OP_ERROR(dir
, "chdir");
1406 if ((dp
= opendir(".")) == NULL
) {
1407 FILE_OP_ERROR(dir
, "opendir");
1411 while ((d
= readdir(dp
)) != NULL
) {
1412 fileno
= to_number(d
->d_name
);
1413 if (fileno
>= 0 && first
<= fileno
&& fileno
<= last
) {
1414 if (unlink(d
->d_name
) < 0)
1415 FILE_OP_ERROR(d
->d_name
, "unlink");
1421 if (chdir(prev_dir
) < 0) {
1422 FILE_OP_ERROR(prev_dir
, "chdir");
1432 gint
remove_all_numbered_files(const gchar
*dir
)
1434 return remove_numbered_files(dir
, 0, UINT_MAX
);
1437 gint
remove_dir_recursive(const gchar
*dir
)
1444 /*g_print("dir = %s\n", dir);*/
1446 if (stat(dir
, &s
) < 0) {
1447 FILE_OP_ERROR(dir
, "stat");
1448 if (ENOENT
== errno
) return 0;
1452 if (!S_ISDIR(s
.st_mode
)) {
1453 if (unlink(dir
) < 0) {
1454 FILE_OP_ERROR(dir
, "unlink");
1461 prev_dir
= g_get_current_dir();
1462 /*g_print("prev_dir = %s\n", prev_dir);*/
1464 if (!path_cmp(prev_dir
, dir
)) {
1466 if (chdir("..") < 0) {
1467 FILE_OP_ERROR(dir
, "chdir");
1470 prev_dir
= g_get_current_dir();
1473 if (chdir(dir
) < 0) {
1474 FILE_OP_ERROR(dir
, "chdir");
1479 if ((dp
= opendir(".")) == NULL
) {
1480 FILE_OP_ERROR(dir
, "opendir");
1486 /* remove all files in the directory */
1487 while ((d
= readdir(dp
)) != NULL
) {
1488 if (!strcmp(d
->d_name
, ".") ||
1489 !strcmp(d
->d_name
, ".."))
1492 if (stat(d
->d_name
, &s
) < 0) {
1493 FILE_OP_ERROR(d
->d_name
, "stat");
1497 /*g_print("removing %s\n", d->d_name);*/
1499 if (S_ISDIR(s
.st_mode
)) {
1500 if (remove_dir_recursive(d
->d_name
) < 0) {
1501 g_warning("can't remove directory\n");
1505 if (unlink(d
->d_name
) < 0)
1506 FILE_OP_ERROR(d
->d_name
, "unlink");
1512 if (chdir(prev_dir
) < 0) {
1513 FILE_OP_ERROR(prev_dir
, "chdir");
1520 if (rmdir(dir
) < 0) {
1521 FILE_OP_ERROR(dir
, "rmdir");
1529 /* this seems to be slower than the stdio version... */
1530 gint
copy_file(const gchar
*src
, const gchar
*dest
)
1532 gint src_fd
, dest_fd
;
1536 gchar
*dest_bak
= NULL
;
1538 if ((src_fd
= open(src
, O_RDONLY
)) < 0) {
1539 FILE_OP_ERROR(src
, "open");
1543 if (is_file_exist(dest
)) {
1544 dest_bak
= g_strconcat(dest
, ".bak", NULL
);
1545 if (rename(dest
, dest_bak
) < 0) {
1546 FILE_OP_ERROR(dest
, "rename");
1553 if ((dest_fd
= open(dest
, O_RDWR
|O_CREAT
, S_IRUSR
|S_IWUSR
)) < 0) {
1554 FILE_OP_ERROR(dest
, "open");
1557 if (rename(dest_bak
, dest
) < 0)
1558 FILE_OP_ERROR(dest_bak
, "rename");
1564 while ((n_read
= read(src_fd
, buf
, sizeof(buf
))) > 0) {
1569 n_write
= write(dest_fd
, bufp
, len
);
1571 g_warning(_("writing to %s failed.\n"), dest
);
1576 if (rename(dest_bak
, dest
) < 0)
1577 FILE_OP_ERROR(dest_bak
, "rename");
1590 if (n_read
< 0 || get_file_size(src
) != get_file_size(dest
)) {
1591 g_warning(_("File copy from %s to %s failed.\n"), src
, dest
);
1594 if (rename(dest_bak
, dest
) < 0)
1595 FILE_OP_ERROR(dest_bak
, "rename");
1606 gint
copy_file(const gchar
*src
, const gchar
*dest
)
1608 FILE *src_fp
, *dest_fp
;
1611 gchar
*dest_bak
= NULL
;
1612 gboolean err
= FALSE
;
1614 if ((src_fp
= fopen(src
, "r")) == NULL
) {
1615 FILE_OP_ERROR(src
, "fopen");
1618 if (is_file_exist(dest
)) {
1619 dest_bak
= g_strconcat(dest
, ".bak", NULL
);
1620 if (rename(dest
, dest_bak
) < 0) {
1621 FILE_OP_ERROR(dest
, "rename");
1628 if ((dest_fp
= fopen(dest
, "w")) == NULL
) {
1629 FILE_OP_ERROR(dest
, "fopen");
1632 if (rename(dest_bak
, dest
) < 0)
1633 FILE_OP_ERROR(dest_bak
, "rename");
1639 if (change_file_mode_rw(dest_fp
, dest
) < 0) {
1640 FILE_OP_ERROR(dest
, "chmod");
1641 g_warning(_("can't change file mode\n"));
1644 while ((n_read
= fread(buf
, sizeof(gchar
), sizeof(buf
), src_fp
)) > 0) {
1645 if (n_read
< sizeof(buf
) && ferror(src_fp
))
1647 if (fwrite(buf
, n_read
, 1, dest_fp
) < 1) {
1648 g_warning(_("writing to %s failed.\n"), dest
);
1653 if (rename(dest_bak
, dest
) < 0)
1654 FILE_OP_ERROR(dest_bak
, "rename");
1661 if (ferror(src_fp
)) {
1662 FILE_OP_ERROR(src
, "fread");
1666 if (fclose(dest_fp
) == EOF
) {
1667 FILE_OP_ERROR(dest
, "fclose");
1674 if (rename(dest_bak
, dest
) < 0)
1675 FILE_OP_ERROR(dest_bak
, "rename");
1686 gint
move_file(const gchar
*src
, const gchar
*dest
)
1688 if (is_file_exist(dest
)) {
1689 g_warning(_("move_file(): file %s already exists."), dest
);
1693 if (rename(src
, dest
) == 0) return 0;
1695 if (EXDEV
!= errno
) {
1696 FILE_OP_ERROR(src
, "rename");
1700 if (copy_file(src
, dest
) < 0) return -1;
1707 gint
change_file_mode_rw(FILE *fp
, const gchar
*file
)
1710 return fchmod(fileno(fp
), S_IRUSR
|S_IWUSR
);
1712 return chmod(file
, S_IRUSR
|S_IWUSR
);
1716 FILE *my_tmpfile(void)
1719 const gchar suffix
[] = ".XXXXXX";
1720 const gchar
*tmpdir
;
1722 const gchar
*progname
;
1728 tmpdir
= g_get_tmp_dir();
1729 tmplen
= strlen(tmpdir
);
1730 progname
= g_get_prgname();
1731 proglen
= strlen(progname
);
1732 Xalloca(fname
, tmplen
+ 1 + proglen
+ sizeof(suffix
),
1735 memcpy(fname
, tmpdir
, tmplen
);
1736 fname
[tmplen
] = G_DIR_SEPARATOR
;
1737 memcpy(fname
+ tmplen
+ 1, progname
, proglen
);
1738 memcpy(fname
+ tmplen
+ 1 + proglen
, suffix
, sizeof(suffix
));
1740 fd
= mkstemp(fname
);
1746 fp
= fdopen(fd
, "w+b");
1751 #endif /* HAVE_MKSTEMP */
1756 gint
execute_async(gchar
*const argv
[])
1760 if ((pid
= fork()) < 0) {
1765 if (pid
== 0) { /* child process */
1768 if ((gch_pid
= fork()) < 0) {
1773 if (gch_pid
== 0) { /* grandchild process */
1774 execvp(argv
[0], argv
);
1783 waitpid(pid
, NULL
, 0);
1788 gint
execute_command_line(const gchar
*cmdline
)
1794 argv
= strsplit_with_quote(cmdline
, " ", 0);
1796 for (i
= 0; argv
[i
] != NULL
; i
++) {
1797 gchar
*str
= argv
[i
];
1799 if (str
[0] == '\'' || str
[0] == '\"') {
1803 if (str
[len
- 1] == str
[0]) {
1804 str
[len
- 1] = '\0';
1805 memmove(str
, str
+ 1, len
- 1);
1810 ret
= execute_async(argv
);
1816 gint
open_uri(const gchar
*uri
, const gchar
*cmdline
)
1818 static gchar
*default_cmdline
= "netscape -remote openURL(%s,raise)";
1819 gchar buf
[BUFFSIZE
];
1822 g_return_val_if_fail(uri
!= NULL
, -1);
1825 (p
= strchr(cmdline
, '%')) && *(p
+ 1) == 's' &&
1826 !strchr(p
+ 2, '%'))
1827 g_snprintf(buf
, sizeof(buf
), cmdline
, uri
);
1830 g_warning(_("Open URI command line is invalid: `%s'"),
1832 g_snprintf(buf
, sizeof(buf
), default_cmdline
, uri
);
1835 execute_command_line(buf
);
1840 time_t remote_tzoffset_sec(const gchar
*zone
)
1842 static gchar ustzstr
[] = "PSTPDTMSTMDTCSTCDTESTEDT";
1848 time_t remoteoffset
;
1850 strncpy(zone3
, zone
, 3);
1854 if (sscanf(zone
, "%c%2d%2d", &c
, &h
, &m
) == 3 &&
1855 (c
== '+' || c
== '-')) {
1856 remoteoffset
= ((h
* 60) + m
) * 60;
1858 remoteoffset
= -remoteoffset
;
1859 } else if (!strncmp(zone
, "UT" , 2) ||
1860 !strncmp(zone
, "GMT", 2)) {
1862 } else if (strlen(zone3
) == 3 &&
1863 (p
= strstr(ustzstr
, zone3
)) != NULL
&&
1864 (p
- ustzstr
) % 3 == 0) {
1865 iustz
= ((gint
)(p
- ustzstr
) / 3 + 1) / 2 - 8;
1866 remoteoffset
= iustz
* 3600;
1867 } else if (strlen(zone3
) == 1) {
1869 case 'Z': remoteoffset
= 0; break;
1870 case 'A': remoteoffset
= -1; break;
1871 case 'B': remoteoffset
= -2; break;
1872 case 'C': remoteoffset
= -3; break;
1873 case 'D': remoteoffset
= -4; break;
1874 case 'E': remoteoffset
= -5; break;
1875 case 'F': remoteoffset
= -6; break;
1876 case 'G': remoteoffset
= -7; break;
1877 case 'H': remoteoffset
= -8; break;
1878 case 'I': remoteoffset
= -9; break;
1879 case 'K': remoteoffset
= -10; break; /* J is not used */
1880 case 'L': remoteoffset
= -11; break;
1881 case 'M': remoteoffset
= -12; break;
1882 case 'N': remoteoffset
= 1; break;
1883 case 'O': remoteoffset
= 2; break;
1884 case 'P': remoteoffset
= 3; break;
1885 case 'Q': remoteoffset
= 4; break;
1886 case 'R': remoteoffset
= 5; break;
1887 case 'S': remoteoffset
= 6; break;
1888 case 'T': remoteoffset
= 7; break;
1889 case 'U': remoteoffset
= 8; break;
1890 case 'V': remoteoffset
= 9; break;
1891 case 'W': remoteoffset
= 10; break;
1892 case 'X': remoteoffset
= 11; break;
1893 case 'Y': remoteoffset
= 12; break;
1894 default: remoteoffset
= 0; break;
1896 remoteoffset
= remoteoffset
* 3600;
1899 return remoteoffset
;
1902 time_t tzoffset_sec(time_t *now
)
1908 lt
= localtime(now
);
1910 off
= (lt
->tm_hour
- gmt
.tm_hour
) * 60 + lt
->tm_min
- gmt
.tm_min
;
1912 if (lt
->tm_year
< gmt
.tm_year
)
1914 else if (lt
->tm_year
> gmt
.tm_year
)
1916 else if (lt
->tm_yday
< gmt
.tm_yday
)
1918 else if (lt
->tm_yday
> gmt
.tm_yday
)
1921 if (off
>= 24 * 60) /* should be impossible */
1922 off
= 23 * 60 + 59; /* if not, insert silly value */
1923 if (off
<= -24 * 60)
1924 off
= -(23 * 60 + 59);
1933 /* calculate timezone offset */
1934 gchar
*tzoffset(time_t *now
)
1936 static gchar offset_string
[6];
1942 lt
= localtime(now
);
1944 off
= (lt
->tm_hour
- gmt
.tm_hour
) * 60 + lt
->tm_min
- gmt
.tm_min
;
1946 if (lt
->tm_year
< gmt
.tm_year
)
1948 else if (lt
->tm_year
> gmt
.tm_year
)
1950 else if (lt
->tm_yday
< gmt
.tm_yday
)
1952 else if (lt
->tm_yday
> gmt
.tm_yday
)
1960 if (off
>= 24 * 60) /* should be impossible */
1961 off
= 23 * 60 + 59; /* if not, insert silly value */
1963 sprintf(offset_string
, "%c%02d%02d", sign
, off
/ 60, off
% 60);
1965 return offset_string
;
1968 void get_rfc822_date(gchar
*buf
, gint len
)
1972 gchar day
[4], mon
[4];
1973 gint dd
, hh
, mm
, ss
, yyyy
;
1978 sscanf(asctime(lt
), "%3s %3s %d %d:%d:%d %d\n",
1979 day
, mon
, &dd
, &hh
, &mm
, &ss
, &yyyy
);
1980 g_snprintf(buf
, len
, "%s, %d %s %d %02d:%02d:%02d %s",
1981 day
, dd
, mon
, yyyy
, hh
, mm
, ss
, tzoffset(&t
));
1984 static FILE *log_fp
= NULL
;
1986 void set_log_file(const gchar
*filename
)
1989 log_fp
= fopen(filename
, "w");
1991 FILE_OP_ERROR(filename
, "fopen");
1994 void close_log_file(void)
2002 void debug_print(const gchar
*format
, ...)
2005 gchar buf
[BUFFSIZE
];
2007 if (!debug_mode
) return;
2009 va_start(args
, format
);
2010 g_vsnprintf(buf
, sizeof(buf
), format
, args
);
2016 void log_print(const gchar
*format
, ...)
2019 gchar buf
[BUFFSIZE
];
2024 va_start(args
, format
);
2025 g_vsnprintf(buf
, sizeof(buf
), format
, args
);
2029 strftime(timestr
, 6, "%H:%M", localtime(&t
));
2030 logbuf
= g_strdup_printf("[%s] %s", timestr
, buf
);
2032 if (debug_mode
) fputs(logbuf
, stdout
);
2033 log_window_append(logbuf
, LOG_NORMAL
);
2035 fputs(logbuf
, log_fp
);
2038 statusbar_puts_all(buf
);
2043 void log_message(const gchar
*format
, ...)
2046 gchar buf
[BUFFSIZE
];
2048 va_start(args
, format
);
2049 g_vsnprintf(buf
, sizeof(buf
), format
, args
);
2052 if (debug_mode
) g_message("%s", buf
);
2053 log_window_append(buf
, LOG_MSG
);
2055 fputs("message: ", log_fp
);
2061 void log_warning(const gchar
*format
, ...)
2064 gchar buf
[BUFFSIZE
];
2066 va_start(args
, format
);
2067 g_vsnprintf(buf
, sizeof(buf
), format
, args
);
2070 g_warning("%s", buf
);
2071 log_window_append(buf
, LOG_WARN
);
2073 fputs("*** warning: ", log_fp
);
2079 void log_error(const gchar
*format
, ...)
2082 gchar buf
[BUFFSIZE
];
2084 va_start(args
, format
);
2085 g_vsnprintf(buf
, sizeof(buf
), format
, args
);
2088 g_warning("%s", buf
);
2089 log_window_append(buf
, LOG_ERROR
);
2091 fputs("*** error: ", log_fp
);