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];
141 g_snprintf(str, sizeof(str), "%dB", (gint)size);
142 else if (size >> 10 < 1024)
143 g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / (1 << 10));
144 else if (size >> 20 < 1024)
145 g_snprintf(str, sizeof(str), "%.2fMB", (gfloat)size / (1 << 20));
147 g_snprintf(str, sizeof(str), "%.2fGB", (gfloat)size / (1 << 30));
152 /* strcmp with NULL-checking */
153 gint strcmp2(const gchar *s1, const gchar *s2)
155 if (s1 == NULL || s2 == NULL)
158 return strcmp(s1, s2);
160 /* strstr with NULL-checking */
161 gchar *strstr2(const gchar *s1, const gchar *s2)
163 if (s1 == NULL || s2 == NULL)
166 return strstr(s1, s2);
169 gint path_cmp(const gchar *s1, const gchar *s2)
173 if (s1 == NULL || s2 == NULL) return -1;
174 if (*s1 == '\0' || *s2 == '\0') return -1;
179 if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
180 if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
182 return strncmp(s1, s2, MAX(len1, len2));
185 /* remove trailing return code */
186 gchar *strretchomp(gchar *str)
190 if (!*str) return str;
192 for (s = str + strlen(str) - 1;
193 s >= str && (*s == '\n' || *s == '\r');
200 /* remove trailing character */
201 gchar *strtailchomp(gchar *str, gchar tail_char)
205 if (!*str) return str;
206 if (tail_char == '\0') return str;
208 for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
215 /* Similar to `strstr' but this function ignores the case of both strings. */
216 gchar *strcasestr(const gchar *haystack, const gchar *needle)
218 register size_t haystack_len, needle_len;
220 haystack_len = strlen(haystack);
221 needle_len = strlen(needle);
223 if (haystack_len < needle_len || needle_len == 0)
226 while (haystack_len >= needle_len) {
227 if (!strncasecmp(haystack, needle, needle_len))
228 return (gchar *)haystack;
238 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
239 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
253 /* don't do zero fill */
258 int iswalnum(wint_t wc)
260 return isalnum((int)wc);
265 int iswspace(wint_t wc)
267 return isspace((int)wc);
272 wint_t towlower(wint_t wc)
274 if (wc >= L'A' && wc <= L'Z')
275 return wc + L'a' - L'A';
282 size_t wcslen(const wchar_t *s)
294 /* Copy SRC to DEST. */
295 wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
303 } while (c != L'\0');
310 /* Copy no more than N wide-characters of SRC to DEST. */
311 wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
321 } while (c != L'\0');
332 /* Duplicate S, returning an identical malloc'd string. */
333 wchar_t *wcsdup(const wchar_t *s)
338 new_str = g_new(wchar_t, wcslen(s) + 1);
346 /* Duplicate no more than N wide-characters of S,
347 returning an identical malloc'd string. */
348 wchar_t *wcsndup(const wchar_t *s, size_t n)
353 new_str = g_new(wchar_t, n + 1);
354 wcsncpy(new_str, s, n);
355 new_str[n] = (wchar_t)0;
362 wchar_t *strdup_mbstowcs(const gchar *s)
367 new_str = g_new(wchar_t, strlen(s) + 1);
368 if (mbstowcs(new_str, s, strlen(s) + 1) < 0) {
372 new_str = g_realloc(new_str,
373 sizeof(wchar_t) * (wcslen(new_str) + 1));
380 gchar *strdup_wcstombs(const wchar_t *s)
386 len = wcslen(s) * MB_CUR_MAX + 1;
387 new_str = g_new(gchar, len);
388 if (wcstombs(new_str, s, len) < 0) {
392 new_str = g_realloc(new_str, strlen(new_str) + 1);
399 /* Compare S1 and S2, ignoring case. */
400 gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
406 c1 = towlower(*s1++);
407 c2 = towlower(*s2++);
410 else if (c1 == 0 && c2 == 0)
417 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
418 wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
420 register size_t haystack_len, needle_len;
422 haystack_len = wcslen(haystack);
423 needle_len = wcslen(needle);
425 if (haystack_len < needle_len || needle_len == 0)
428 while (haystack_len >= needle_len) {
429 if (!wcsncasecmp(haystack, needle, needle_len))
430 return (wchar_t *)haystack;
440 /* Examine if next block is non-ASCII string */
441 gboolean is_next_nonascii(const wchar_t *s)
445 /* skip head space */
446 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
448 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
456 /* Examine if next block is multi-byte string */
457 gboolean is_next_mbs(const wchar_t *s)
461 gchar tmp[MB_LEN_MAX];
463 /* skip head space */
464 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
466 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
467 mbl = wctomb(tmp, *wp);
475 wchar_t *find_wspace(const wchar_t *s)
479 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
481 for (; *wp != (wchar_t)0; wp++) {
483 return (wchar_t *)wp;
489 /* compare subjects */
490 gint subject_compare(const gchar *s1, const gchar *s2)
494 if (!s1 || !s2) return -1;
495 if (!*s1 || !*s2) return -1;
497 Xalloca(str1, strlen(s1) + 1, return -1);
498 Xalloca(str2, strlen(s2) + 1, return -1);
505 if (!*str1 || !*str2) return -1;
507 return strcmp(str1, str2);
510 void trim_subject(gchar *str)
514 eliminate_parenthesis(str, '[', ']');
515 eliminate_parenthesis(str, '(', ')');
518 while (!strncasecmp(str, "Re:", 3)) {
520 while (isspace(*srcp)) srcp++;
521 memmove(str, srcp, strlen(srcp) + 1);
525 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
527 register gchar *srcp, *destp;
532 while ((destp = strchr(destp, op))) {
538 else if (*srcp == cl)
544 while (isspace(*srcp)) srcp++;
545 memmove(destp, srcp, strlen(srcp) + 1);
549 void extract_parenthesis(gchar *str, gchar op, gchar cl)
551 register gchar *srcp, *destp;
556 while ((srcp = strchr(destp, op))) {
559 memmove(destp, srcp + 1, strlen(srcp));
564 else if (*destp == cl)
576 void extract_one_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
579 register gchar *srcp, *destp;
581 gboolean in_quote = FALSE;
585 if ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
586 memmove(destp, srcp + 1, strlen(srcp));
589 if (*destp == op && !in_quote)
591 else if (*destp == cl && !in_quote)
593 else if (*destp == quote_chr)
605 void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
608 register gchar *srcp, *destp;
610 gboolean in_quote = FALSE;
614 while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
617 memmove(destp, srcp + 1, strlen(srcp));
620 if (*destp == op && !in_quote)
622 else if (*destp == cl && !in_quote)
624 else if (*destp == quote_chr)
636 void eliminate_quote(gchar *str, gchar quote_chr)
638 register gchar *srcp, *destp;
642 while ((destp = strchr(destp, quote_chr))) {
643 if ((srcp = strchr(destp + 1, quote_chr))) {
645 while (isspace(*srcp)) srcp++;
646 memmove(destp, srcp, strlen(srcp) + 1);
654 void extract_quote(gchar *str, gchar quote_chr)
658 if ((str = strchr(str, quote_chr))) {
659 if ((p = strchr(str + 1, quote_chr))) {
661 memmove(str, str + 1, p - str);
666 void eliminate_address_comment(gchar *str)
668 register gchar *srcp, *destp;
673 while ((destp = strchr(destp, '"'))) {
674 if ((srcp = strchr(destp + 1, '"'))) {
679 while (isspace(*srcp)) srcp++;
680 memmove(destp, srcp, strlen(srcp) + 1);
690 while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
696 else if (*srcp == ')')
702 while (isspace(*srcp)) srcp++;
703 memmove(destp, srcp, strlen(srcp) + 1);
707 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
709 gboolean in_quote = FALSE;
712 if (*str == c && !in_quote)
714 if (*str == quote_chr)
722 gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
724 gboolean in_quote = FALSE;
727 p = str + strlen(str) - 1;
729 if (*p == c && !in_quote)
739 void extract_address(gchar *str)
741 eliminate_address_comment(str);
742 if (strchr_with_skip_quote(str, '"', '<'))
743 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
747 GSList *address_list_append(GSList *addr_list, const gchar *str)
752 if (!str) return addr_list;
754 Xstrdup_a(work, str, return addr_list);
756 eliminate_address_comment(work);
759 while (workp && *workp) {
762 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
768 if (strchr_with_skip_quote(workp, '"', '<'))
769 extract_parenthesis_with_skip_quote
770 (workp, '"', '<', '>');
774 addr_list = g_slist_append(addr_list, g_strdup(workp));
782 GSList *references_list_append(GSList *msgid_list, const gchar *str)
786 if (!str) return msgid_list;
789 while (strp && *strp) {
790 const gchar *start, *end;
793 if ((start = strchr(strp, '<')) != NULL) {
794 end = strchr(start + 1, '>');
799 msgid = g_strndup(start + 1, end - start - 1);
802 msgid_list = g_slist_append(msgid_list, msgid);
812 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
817 if (!str) return group_list;
819 Xstrdup_a(work, str, return group_list);
823 while (workp && *workp) {
826 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
834 group_list = g_slist_append(group_list,
843 void remove_return(gchar *str)
845 register gchar *p = str;
848 if (*p == '\n' || *p == '\r')
849 memmove(p, p + 1, strlen(p));
855 void remove_space(gchar *str)
857 register gchar *p = str;
862 while (isspace(*(p + spc)))
865 memmove(p, p + spc, strlen(p + spc) + 1);
871 void unfold_line(gchar *str)
873 register gchar *p = str;
877 if (*p == '\n' || *p == '\r') {
880 while (isspace(*(p + spc)))
883 memmove(p, p + spc, strlen(p + spc) + 1);
889 void subst_char(gchar *str, gchar orig, gchar subst)
891 register gchar *p = str;
900 gboolean is_header_line(const gchar *str)
902 if (str[0] == ':') return FALSE;
904 while (*str != '\0' && *str != ' ') {
913 gboolean is_ascii_str(const guchar *str)
915 while (*str != '\0') {
916 if (*str != '\t' && *str != ' ' &&
917 *str != '\r' && *str != '\n' &&
918 (*str < 32 || *str >= 127))
926 gint get_quote_level(const gchar *str)
928 const gchar *first_pos;
929 const gchar *last_pos;
930 const gchar *p = str;
931 gint quote_level = -1;
933 /* speed up line processing by only searching to the last '>' */
934 if ((first_pos = strchr(str, '>')) != NULL) {
935 /* skip a line if it contains a '<' before the initial '>' */
936 if (memchr(str, '<', first_pos - str) != NULL)
938 last_pos = strrchr(first_pos, '>');
942 while (p <= last_pos) {
943 while (p < last_pos) {
952 else if (*p != '-' && !isspace(*p) && p <= last_pos) {
953 /* any characters are allowed except '-' and space */
954 while (*p != '-' && *p != '>' && !isspace(*p) &&
969 GList *uri_list_extract_filenames(const gchar *uri_list)
971 GList *result = NULL;
979 while (isspace(*p)) p++;
980 if (!strncmp(p, "file:", 5)) {
983 while (*q && *q != '\n' && *q != '\r') q++;
987 while (q > p && isspace(*q)) q--;
988 file = g_malloc(q - p + 2);
989 strncpy(file, p, q - p + 1);
990 file[q - p + 1] = '\0';
991 result = g_list_append(result,file);
1002 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
1004 register guint haystack_len, needle_len;
1005 gboolean in_squote = FALSE, in_dquote = FALSE;
1007 haystack_len = strlen(haystack);
1008 needle_len = strlen(needle);
1010 if (haystack_len < needle_len || needle_len == 0)
1013 while (haystack_len >= needle_len) {
1014 if (!in_squote && !in_dquote &&
1015 !strncmp(haystack, needle, needle_len))
1016 return (gchar *)haystack;
1018 /* 'foo"bar"' -> foo"bar"
1019 "foo'bar'" -> foo'bar' */
1020 if (*haystack == '\'') {
1023 else if (!in_dquote)
1025 } else if (*haystack == '\"') {
1028 else if (!in_squote)
1039 /* this fuction was taken from gstrfuncs.c in glib. */
1040 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1043 GSList *string_list = NULL, *slist;
1044 gchar **str_array, *s;
1047 g_return_val_if_fail(str != NULL, NULL);
1048 g_return_val_if_fail(delim != NULL, NULL);
1051 max_tokens = G_MAXINT;
1053 s = strstr_with_skip_quote(str, delim);
1055 guint delimiter_len = strlen(delim);
1062 new_str = g_new(gchar, len + 1);
1063 strncpy(new_str, str, len);
1065 string_list = g_slist_prepend(string_list, new_str);
1067 str = s + delimiter_len;
1068 s = strstr_with_skip_quote(str, delim);
1069 } while (--max_tokens && s);
1074 string_list = g_slist_prepend(string_list, g_strdup(str));
1077 str_array = g_new(gchar*, n);
1081 str_array[i--] = NULL;
1082 for (slist = string_list; slist; slist = slist->next)
1083 str_array[i--] = slist->data;
1085 g_slist_free(string_list);
1091 * We need this wrapper around g_get_home_dir(), so that
1092 * we can fix some Windoze things here. Should be done in glibc of course
1093 * but as long as we are not able to do our own extensions to glibc, we do
1096 gchar *get_home_dir(void)
1098 #if HAVE_DOSISH_SYSTEM
1099 static gchar *home_dir;
1102 home_dir = read_w32_registry_string(NULL,
1103 "Software\\Sylpheed", "HomeDir" );
1104 if (!home_dir || !*home_dir) {
1105 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1106 const char *s = g_get_home_dir();
1108 home_dir = g_strdup (s);
1110 if (!home_dir || !*home_dir)
1111 home_dir = g_strdup ("c:\\sylpheed");
1113 debug_print("initialized home_dir to `%s'\n", home_dir);
1116 #else /* standard glib */
1117 return g_get_home_dir();
1121 gchar *get_rc_dir(void)
1123 static gchar *rc_dir = NULL;
1126 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1132 gchar *get_news_cache_dir(void)
1134 static gchar *news_cache_dir = NULL;
1136 if (!news_cache_dir)
1137 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1138 NEWS_CACHE_DIR, NULL);
1140 return news_cache_dir;
1143 gchar *get_imap_cache_dir(void)
1145 static gchar *imap_cache_dir = NULL;
1147 if (!imap_cache_dir)
1148 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1149 IMAP_CACHE_DIR, NULL);
1151 return imap_cache_dir;
1154 gchar *get_mbox_cache_dir(void)
1156 static gchar *mbox_cache_dir = NULL;
1158 if (!mbox_cache_dir)
1159 mbox_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1160 MBOX_CACHE_DIR, NULL);
1162 return mbox_cache_dir;
1165 gchar *get_mime_tmp_dir(void)
1167 static gchar *mime_tmp_dir = NULL;
1170 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1171 MIME_TMP_DIR, NULL);
1173 return mime_tmp_dir;
1176 gchar *get_tmp_file(void)
1178 static gchar *tmp_file = NULL;
1181 tmp_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1187 gchar *get_domain_name(void)
1189 static gchar *domain_name = NULL;
1192 gchar buf[BUFFSIZE] = "";
1194 if (gethostname(buf, sizeof(buf)) < 0) {
1195 perror("gethostname");
1196 strcpy(buf, "unknown");
1199 domain_name = g_strdup(buf);
1205 off_t get_file_size(const gchar *file)
1209 if (stat(file, &s) < 0) {
1210 FILE_OP_ERROR(file, "stat");
1217 off_t get_left_file_size(FILE *fp)
1223 if ((pos = ftell(fp)) < 0) {
1227 if (fseek(fp, 0L, SEEK_END) < 0) {
1231 if ((end = ftell(fp)) < 0) {
1236 if (fseek(fp, pos, SEEK_SET) < 0) {
1244 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1248 if (stat(file, &s) < 0) {
1249 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1253 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1259 gboolean is_dir_exist(const gchar *dir)
1263 if (stat(dir, &s) < 0) {
1264 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1268 if (S_ISDIR(s.st_mode))
1274 gint change_dir(const gchar *dir)
1276 gchar *prevdir = NULL;
1279 prevdir = g_get_current_dir();
1281 if (chdir(dir) < 0) {
1282 FILE_OP_ERROR(dir, "chdir");
1283 if (debug_mode) g_free(prevdir);
1285 } else if (debug_mode) {
1288 cwd = g_get_current_dir();
1289 if (strcmp(prevdir, cwd) != 0)
1290 g_print("current dir: %s\n", cwd);
1298 gint make_dir_hier(const gchar *dir)
1303 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1304 parent_dir = g_strndup(dir, p - dir);
1305 if (*parent_dir != '\0') {
1306 if (!is_dir_exist(parent_dir)) {
1307 if (mkdir(parent_dir, S_IRWXU) < 0) {
1308 FILE_OP_ERROR(parent_dir, "mkdir");
1312 if (chmod(parent_dir, S_IRWXU) < 0)
1313 FILE_OP_ERROR(parent_dir, "chmod");
1318 if (!is_dir_exist(dir)) {
1319 if (mkdir(dir, S_IRWXU) < 0) {
1320 FILE_OP_ERROR(dir, "mkdir");
1323 if (chmod(dir, S_IRWXU) < 0)
1324 FILE_OP_ERROR(dir, "chmod");
1330 gint remove_all_files(const gchar *dir)
1336 prev_dir = g_get_current_dir();
1338 if (chdir(dir) < 0) {
1339 FILE_OP_ERROR(dir, "chdir");
1343 if ((dp = opendir(".")) == NULL) {
1344 FILE_OP_ERROR(dir, "opendir");
1348 while ((d = readdir(dp)) != NULL) {
1349 if (!strcmp(d->d_name, ".") ||
1350 !strcmp(d->d_name, ".."))
1353 if (unlink(d->d_name) < 0)
1354 FILE_OP_ERROR(d->d_name, "unlink");
1359 if (chdir(prev_dir) < 0) {
1360 FILE_OP_ERROR(prev_dir, "chdir");
1370 gint remove_numbered_files(const gchar *dir, guint first, guint last)
1377 prev_dir = g_get_current_dir();
1379 if (chdir(dir) < 0) {
1380 FILE_OP_ERROR(dir, "chdir");
1384 if ((dp = opendir(".")) == NULL) {
1385 FILE_OP_ERROR(dir, "opendir");
1389 while ((d = readdir(dp)) != NULL) {
1390 fileno = to_number(d->d_name);
1391 if (fileno >= 0 && first <= fileno && fileno <= last) {
1392 if (unlink(d->d_name) < 0)
1393 FILE_OP_ERROR(d->d_name, "unlink");
1399 if (chdir(prev_dir) < 0) {
1400 FILE_OP_ERROR(prev_dir, "chdir");
1410 gint remove_all_numbered_files(const gchar *dir)
1412 return remove_numbered_files(dir, 0, UINT_MAX);
1415 gint remove_dir_recursive(const gchar *dir)
1422 /* g_print("dir = %s\n", dir); */
1424 if (stat(dir, &s) < 0) {
1425 FILE_OP_ERROR(dir, "stat");
1426 if (ENOENT == errno) return 0;
1430 if (!S_ISDIR(s.st_mode)) {
1431 if (unlink(dir) < 0) {
1432 FILE_OP_ERROR(dir, "unlink");
1439 prev_dir = g_get_current_dir();
1440 /* g_print("prev_dir = %s\n", prev_dir); */
1442 if (!path_cmp(prev_dir, dir)) {
1444 if (chdir("..") < 0) {
1445 FILE_OP_ERROR(dir, "chdir");
1448 prev_dir = g_get_current_dir();
1451 if (chdir(dir) < 0) {
1452 FILE_OP_ERROR(dir, "chdir");
1457 if ((dp = opendir(".")) == NULL) {
1458 FILE_OP_ERROR(dir, "opendir");
1464 /* remove all files in the directory */
1465 while ((d = readdir(dp)) != NULL) {
1466 if (!strcmp(d->d_name, ".") ||
1467 !strcmp(d->d_name, ".."))
1470 if (stat(d->d_name, &s) < 0) {
1471 FILE_OP_ERROR(d->d_name, "stat");
1475 /* g_print("removing %s\n", d->d_name); */
1477 if (S_ISDIR(s.st_mode)) {
1478 if (remove_dir_recursive(d->d_name) < 0) {
1479 g_warning("can't remove directory\n");
1483 if (unlink(d->d_name) < 0)
1484 FILE_OP_ERROR(d->d_name, "unlink");
1490 if (chdir(prev_dir) < 0) {
1491 FILE_OP_ERROR(prev_dir, "chdir");
1498 if (rmdir(dir) < 0) {
1499 FILE_OP_ERROR(dir, "rmdir");
1507 /* this seems to be slower than the stdio version... */
1508 gint copy_file(const gchar *src, const gchar *dest)
1510 gint src_fd, dest_fd;
1514 gchar *dest_bak = NULL;
1516 if ((src_fd = open(src, O_RDONLY)) < 0) {
1517 FILE_OP_ERROR(src, "open");
1521 if (is_file_exist(dest)) {
1522 dest_bak = g_strconcat(dest, ".bak", NULL);
1523 if (rename(dest, dest_bak) < 0) {
1524 FILE_OP_ERROR(dest, "rename");
1531 if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
1532 FILE_OP_ERROR(dest, "open");
1535 if (rename(dest_bak, dest) < 0)
1536 FILE_OP_ERROR(dest_bak, "rename");
1542 while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
1547 n_write = write(dest_fd, bufp, len);
1549 g_warning(_("writing to %s failed.\n"), dest);
1554 if (rename(dest_bak, dest) < 0)
1555 FILE_OP_ERROR(dest_bak, "rename");
1568 if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
1569 g_warning(_("File copy from %s to %s failed.\n"), src, dest);
1572 if (rename(dest_bak, dest) < 0)
1573 FILE_OP_ERROR(dest_bak, "rename");
1584 gint copy_file(const gchar *src, const gchar *dest)
1586 FILE *src_fp, *dest_fp;
1589 gchar *dest_bak = NULL;
1590 gboolean err = FALSE;
1592 if ((src_fp = fopen(src, "r")) == NULL) {
1593 FILE_OP_ERROR(src, "fopen");
1596 if (is_file_exist(dest)) {
1597 dest_bak = g_strconcat(dest, ".bak", NULL);
1598 if (rename(dest, dest_bak) < 0) {
1599 FILE_OP_ERROR(dest, "rename");
1606 if ((dest_fp = fopen(dest, "w")) == NULL) {
1607 FILE_OP_ERROR(dest, "fopen");
1610 if (rename(dest_bak, dest) < 0)
1611 FILE_OP_ERROR(dest_bak, "rename");
1617 if (change_file_mode_rw(dest_fp, dest) < 0) {
1618 FILE_OP_ERROR(dest, "chmod");
1619 g_warning(_("can't change file mode\n"));
1622 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
1623 if (n_read < sizeof(buf) && ferror(src_fp))
1625 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
1626 g_warning(_("writing to %s failed.\n"), dest);
1631 if (rename(dest_bak, dest) < 0)
1632 FILE_OP_ERROR(dest_bak, "rename");
1639 if (ferror(src_fp)) {
1640 FILE_OP_ERROR(src, "fread");
1644 if (fclose(dest_fp) == EOF) {
1645 FILE_OP_ERROR(dest, "fclose");
1652 if (rename(dest_bak, dest) < 0)
1653 FILE_OP_ERROR(dest_bak, "rename");
1664 gint move_file(const gchar *src, const gchar *dest)
1666 if (is_file_exist(dest)) {
1667 g_warning(_("move_file(): file %s already exists."), dest);
1671 if (rename(src, dest) == 0) return 0;
1673 if (EXDEV != errno) {
1674 FILE_OP_ERROR(src, "rename");
1678 if (copy_file(src, dest) < 0) return -1;
1685 gint change_file_mode_rw(FILE *fp, const gchar *file)
1688 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
1690 return chmod(file, S_IRUSR|S_IWUSR);
1694 FILE *my_tmpfile(void)
1697 const gchar suffix[] = ".XXXXXX";
1698 const gchar *tmpdir;
1700 const gchar *progname;
1706 tmpdir = g_get_tmp_dir();
1707 tmplen = strlen(tmpdir);
1708 progname = g_get_prgname();
1709 proglen = strlen(progname);
1710 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
1713 memcpy(fname, tmpdir, tmplen);
1714 fname[tmplen] = G_DIR_SEPARATOR;
1715 memcpy(fname + tmplen + 1, progname, proglen);
1716 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
1718 fd = mkstemp(fname);
1724 fp = fdopen(fd, "w+b");
1729 #endif /* HAVE_MKSTEMP */
1734 gint execute_async(gchar *const argv[])
1738 if ((pid = fork()) < 0) {
1743 if (pid == 0) { /* child process */
1746 if ((gch_pid = fork()) < 0) {
1751 if (gch_pid == 0) { /* grandchild process */
1752 execvp(argv[0], argv);
1761 waitpid(pid, NULL, 0);
1766 gint execute_sync(gchar *const argv[])
1770 if ((pid = fork()) < 0) {
1775 if (pid == 0) { /* child process */
1776 execvp(argv[0], argv);
1782 waitpid(pid, NULL, 0);
1787 gint execute_command_line(const gchar *cmdline, gboolean async)
1793 argv = strsplit_with_quote(cmdline, " ", 0);
1795 for (i = 0; argv[i] != NULL; i++) {
1796 gchar *str = argv[i];
1798 if (str[0] == '\'' || str[0] == '\"') {
1802 if (str[len - 1] == str[0]) {
1803 str[len - 1] = '\0';
1804 memmove(str, str + 1, len - 1);
1810 ret = execute_async(argv);
1812 ret = execute_sync(argv);
1818 static gint is_unchanged_uri_char(char c)
1830 void encode_uri(gchar * encoded_uri, gint bufsize, const gchar * uri)
1836 for(i = 0; i < strlen(uri) ; i++) {
1837 if (is_unchanged_uri_char(uri[i])) {
1838 if (k + 2 >= bufsize)
1840 encoded_uri[k++] = uri[i];
1843 char * hexa = "0123456789ABCDEF";
1845 if (k + 4 >= bufsize)
1847 encoded_uri[k++] = '%';
1848 encoded_uri[k++] = hexa[uri[i] / 16];
1849 encoded_uri[k++] = hexa[uri[i] % 16];
1855 /* Converts two-digit hexadecimal to decimal. Used for unescaping escaped
1858 static gint axtoi(const gchar *hexstr)
1860 gint Hi, Lo, Result;
1863 if ('0' <= Hi && Hi <= '9') {
1866 if ('a' <= Hi && Hi <= 'f') {
1869 if ('A' <= Hi && Hi <= 'F') {
1874 if ('0' <= Lo && Lo <='9') {
1877 if ('a' <= Lo && Lo <= 'f') {
1880 if ('A' <= Lo && Lo <= 'F') {
1883 Result = Lo + (16 * Hi);
1888 /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
1889 * plusses, and escape characters are used)
1892 void decode_uri(gchar * decoded_uri, const gchar * encoded_uri)
1894 const gchar * encoded;
1897 // strcpy(decoded_uri, encoded_uri);
1898 // subst_char(decoded_uri, '+', ' ');
1900 encoded = encoded_uri;
1901 decoded = decoded_uri;
1904 if (* encoded == '%') {
1906 if (isxdigit(encoded[0])
1907 && isxdigit(encoded[1])) {
1908 * decoded = (gchar) axtoi(encoded);
1913 else if (* encoded == '+') {
1919 * decoded = * encoded;
1929 gint open_uri(const gchar *uri, const gchar *cmdline)
1931 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
1932 gchar buf[BUFFSIZE];
1934 gchar encoded_uri[BUFFSIZE];
1936 g_return_val_if_fail(uri != NULL, -1);
1938 /* an option to choose whether to use encode_uri or not ? */
1939 encode_uri(encoded_uri, BUFFSIZE, uri);
1942 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1943 !strchr(p + 2, '%'))
1944 g_snprintf(buf, sizeof(buf), cmdline, encoded_uri);
1947 g_warning(_("Open URI command line is invalid: `%s'"),
1949 g_snprintf(buf, sizeof(buf), default_cmdline, encoded_uri);
1952 execute_command_line(buf, TRUE);
1957 time_t remote_tzoffset_sec(const gchar *zone)
1959 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
1965 time_t remoteoffset;
1967 strncpy(zone3, zone, 3);
1971 if (sscanf(zone, "%c%2d%2d", &c, &h, &m) == 3 &&
1972 (c == '+' || c == '-')) {
1973 remoteoffset = ((h * 60) + m) * 60;
1975 remoteoffset = -remoteoffset;
1976 } else if (!strncmp(zone, "UT" , 2) ||
1977 !strncmp(zone, "GMT", 2)) {
1979 } else if (strlen(zone3) == 3 &&
1980 (p = strstr(ustzstr, zone3)) != NULL &&
1981 (p - ustzstr) % 3 == 0) {
1982 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
1983 remoteoffset = iustz * 3600;
1984 } else if (strlen(zone3) == 1) {
1986 case 'Z': remoteoffset = 0; break;
1987 case 'A': remoteoffset = -1; break;
1988 case 'B': remoteoffset = -2; break;
1989 case 'C': remoteoffset = -3; break;
1990 case 'D': remoteoffset = -4; break;
1991 case 'E': remoteoffset = -5; break;
1992 case 'F': remoteoffset = -6; break;
1993 case 'G': remoteoffset = -7; break;
1994 case 'H': remoteoffset = -8; break;
1995 case 'I': remoteoffset = -9; break;
1996 case 'K': remoteoffset = -10; break; /* J is not used */
1997 case 'L': remoteoffset = -11; break;
1998 case 'M': remoteoffset = -12; break;
1999 case 'N': remoteoffset = 1; break;
2000 case 'O': remoteoffset = 2; break;
2001 case 'P': remoteoffset = 3; break;
2002 case 'Q': remoteoffset = 4; break;
2003 case 'R': remoteoffset = 5; break;
2004 case 'S': remoteoffset = 6; break;
2005 case 'T': remoteoffset = 7; break;
2006 case 'U': remoteoffset = 8; break;
2007 case 'V': remoteoffset = 9; break;
2008 case 'W': remoteoffset = 10; break;
2009 case 'X': remoteoffset = 11; break;
2010 case 'Y': remoteoffset = 12; break;
2011 default: remoteoffset = 0; break;
2013 remoteoffset = remoteoffset * 3600;
2016 return remoteoffset;
2019 time_t tzoffset_sec(time_t *now)
2025 lt = localtime(now);
2027 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
2029 if (lt->tm_year < gmt.tm_year)
2031 else if (lt->tm_year > gmt.tm_year)
2033 else if (lt->tm_yday < gmt.tm_yday)
2035 else if (lt->tm_yday > gmt.tm_yday)
2038 if (off >= 24 * 60) /* should be impossible */
2039 off = 23 * 60 + 59; /* if not, insert silly value */
2040 if (off <= -24 * 60)
2041 off = -(23 * 60 + 59);
2050 /* calculate timezone offset */
2051 gchar *tzoffset(time_t *now)
2053 static gchar offset_string[6];
2059 lt = localtime(now);
2061 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
2063 if (lt->tm_year < gmt.tm_year)
2065 else if (lt->tm_year > gmt.tm_year)
2067 else if (lt->tm_yday < gmt.tm_yday)
2069 else if (lt->tm_yday > gmt.tm_yday)
2077 if (off >= 24 * 60) /* should be impossible */
2078 off = 23 * 60 + 59; /* if not, insert silly value */
2080 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
2082 return offset_string;
2085 void get_rfc822_date(gchar *buf, gint len)
2089 gchar day[4], mon[4];
2090 gint dd, hh, mm, ss, yyyy;
2095 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
2096 day, mon, &dd, &hh, &mm, &ss, &yyyy);
2097 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
2098 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
2101 static FILE *log_fp = NULL;
2103 void set_log_file(const gchar *filename)
2106 log_fp = fopen(filename, "w");
2108 FILE_OP_ERROR(filename, "fopen");
2111 void close_log_file(void)
2119 static guint log_verbosity_count = 0;
2121 void log_verbosity_set(gboolean verbose)
2124 log_verbosity_count++;
2125 else if (log_verbosity_count > 0)
2126 log_verbosity_count--;
2129 void debug_print_real(const gchar *format, ...)
2132 gchar buf[BUFFSIZE];
2134 if (!debug_mode) return;
2136 va_start(args, format);
2137 g_vsnprintf(buf, sizeof(buf), format, args);
2143 void log_print(const gchar *format, ...)
2146 gchar buf[BUFFSIZE];
2151 va_start(args, format);
2152 g_vsnprintf(buf, sizeof(buf), format, args);
2156 strftime(timestr, 6, "%H:%M", localtime(&t));
2157 logbuf = g_strdup_printf("[%s] %s", timestr, buf);
2159 if (debug_mode) fputs(logbuf, stdout);
2160 log_window_append(logbuf, LOG_NORMAL);
2162 fputs(logbuf, log_fp);
2165 if (log_verbosity_count)
2166 statusbar_puts_all(buf);
2170 void log_message(const gchar *format, ...)
2173 gchar buf[BUFFSIZE];
2175 va_start(args, format);
2176 g_vsnprintf(buf, sizeof(buf), format, args);
2179 if (debug_mode) g_message("%s", buf);
2180 log_window_append(buf, LOG_MSG);
2182 fputs("message: ", log_fp);
2186 statusbar_puts_all(buf);
2189 void log_warning(const gchar *format, ...)
2192 gchar buf[BUFFSIZE];
2194 va_start(args, format);
2195 g_vsnprintf(buf, sizeof(buf), format, args);
2198 g_warning("%s", buf);
2199 log_window_append(buf, LOG_WARN);
2201 fputs("*** warning: ", log_fp);
2207 void log_error(const gchar *format, ...)
2210 gchar buf[BUFFSIZE];
2212 va_start(args, format);
2213 g_vsnprintf(buf, sizeof(buf), format, args);
2216 g_warning("%s", buf);
2217 log_window_append(buf, LOG_ERROR);
2219 fputs("*** error: ", log_fp);