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 void ptr_array_free_strings(GPtrArray *array)
89 g_return_if_fail(array != NULL);
91 for (i = 0; i < array->len; i++) {
92 str = g_ptr_array_index(array, i);
97 gint to_number(const gchar *nstr)
99 register const gchar *p;
101 if (*nstr == '\0') return -1;
103 for (p = nstr; *p != '\0'; p++)
104 if (!isdigit(*p)) return -1;
109 /* convert integer into string
110 nstr must be a 11 characters table
112 gchar *itos_buf(gchar nstr[], gint n)
114 g_snprintf(nstr, 11, "%d", n);
118 /* convert integer into string
119 use an internal static buffer */
122 static gchar nstr[11];
124 return itos_buf(nstr, n);
127 gchar *to_human_readable(off_t size)
133 for (count = 0; count < 3; count++) {
134 if (size / div < 1024)
141 case 0: g_snprintf(str, sizeof(str), "%dB", (gint)size); break;
142 case 1: g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / div);
144 case 2: g_snprintf(str, sizeof(str), "%.1fMB", (gfloat)size / div);
147 g_snprintf(str, sizeof(str), "%.1fGB", (gfloat)size / div);
154 /* strcmp with NULL-checking */
155 gint strcmp2(const gchar *s1, const gchar *s2)
157 if (s1 == NULL || s2 == NULL)
160 return strcmp(s1, s2);
164 gint path_cmp(const gchar *s1, const gchar *s2)
168 if (s1 == NULL || s2 == NULL) return -1;
169 if (*s1 == '\0' || *s2 == '\0') return -1;
174 if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
175 if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
177 return strncmp(s1, s2, MAX(len1, len2));
180 /* remove trailing return code */
181 gchar *strretchomp(gchar *str)
185 if (!*str) return str;
187 for (s = str + strlen(str) - 1;
188 s >= str && (*s == '\n' || *s == '\r');
195 /* Similar to `strstr' but this function ignores the case of both strings. */
196 gchar *strcasestr(const gchar *haystack, const gchar *needle)
198 register size_t haystack_len, needle_len;
200 haystack_len = strlen(haystack);
201 needle_len = strlen(needle);
203 if (haystack_len < needle_len || needle_len == 0)
206 while (haystack_len >= needle_len) {
207 if (!strncasecmp(haystack, needle, needle_len))
208 return (gchar *)haystack;
218 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
219 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
233 /* don't do zero fill */
238 int iswalnum(wint_t wc)
240 return isalnum((int)wc);
245 int iswspace(wint_t wc)
247 return isspace((int)wc);
252 wint_t towlower(wint_t wc)
254 if (wc >= L'A' && wc <= L'Z')
255 return wc + L'a' - L'A';
262 size_t wcslen(const wchar_t *s)
274 /* Copy SRC to DEST. */
275 wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
283 } while (c != L'\0');
290 /* Copy no more than N wide-characters of SRC to DEST. */
291 wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
301 } while (c != L'\0');
312 /* Duplicate S, returning an identical malloc'd string. */
313 wchar_t *wcsdup(const wchar_t *s)
318 new_str = g_new(wchar_t, wcslen(s) + 1);
326 /* Duplicate no more than N wide-characters of S,
327 returning an identical malloc'd string. */
328 wchar_t *wcsndup(const wchar_t *s, size_t n)
333 new_str = g_new(wchar_t, n + 1);
334 wcsncpy(new_str, s, n);
335 new_str[n] = (wchar_t)0;
342 wchar_t *strdup_mbstowcs(const gchar *s)
347 new_str = g_new(wchar_t, strlen(s) + 1);
348 if (mbstowcs(new_str, s, strlen(s) + 1) < 0) {
352 new_str = g_realloc(new_str,
353 sizeof(wchar_t) * (wcslen(new_str) + 1));
360 gchar *strdup_wcstombs(const wchar_t *s)
366 len = wcslen(s) * MB_CUR_MAX + 1;
367 new_str = g_new(gchar, len);
368 if (wcstombs(new_str, s, len) < 0) {
372 new_str = g_realloc(new_str, strlen(new_str) + 1);
379 /* Compare S1 and S2, ignoring case. */
380 gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
386 c1 = towlower(*s1++);
387 c2 = towlower(*s2++);
390 else if (c1 == 0 && c2 == 0)
397 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
398 wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
400 register size_t haystack_len, needle_len;
402 haystack_len = wcslen(haystack);
403 needle_len = wcslen(needle);
405 if (haystack_len < needle_len || needle_len == 0)
408 while (haystack_len >= needle_len) {
409 if (!wcsncasecmp(haystack, needle, needle_len))
410 return (wchar_t *)haystack;
420 /* Examine if next block is non-ASCII string */
421 gboolean is_next_nonascii(const wchar_t *s)
425 /* skip head space */
426 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
428 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
436 /* Examine if next block is multi-byte string */
437 gboolean is_next_mbs(const wchar_t *s)
441 gchar tmp[MB_CUR_MAX];
443 /* skip head space */
444 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
446 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
447 mbl = wctomb(tmp, *wp);
455 wchar_t *find_wspace(const wchar_t *s)
459 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
461 for (; *wp != (wchar_t)0; wp++) {
463 return (wchar_t *)wp;
469 /* compare subjects */
470 gint subject_compare(const gchar *s1, const gchar *s2)
475 if (!s1 || !s2) return -1;
476 if (!*s1 || !*s2) return -1;
478 Xalloca(str1, strlen(s1) + 1, return -1);
479 Xalloca(str2, strlen(s2) + 1, return -1);
486 if (!*str1 || !*str2) return -1;
488 retval = strcmp(str1, str2);
490 // g_print("\ns1 = %s\ns2 = %s\n"
491 // "str1 = %s\nstr2 = %s\nmatched.\n",
492 // s1, s2, str1, str2);
497 void trim_subject(gchar *str)
501 eliminate_parenthesis(str, '[', ']');
502 eliminate_parenthesis(str, '(', ')');
505 while (!strncasecmp(str, "Re:", 3)) {
507 while (isspace(*srcp)) srcp++;
508 memmove(str, srcp, strlen(srcp) + 1);
512 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
514 register gchar *srcp, *destp;
519 while ((destp = strchr(destp, op))) {
525 else if (*srcp == cl)
531 while (isspace(*srcp)) srcp++;
532 memmove(destp, srcp, strlen(srcp) + 1);
536 void extract_parenthesis(gchar *str, gchar op, gchar cl)
538 register gchar *srcp, *destp;
543 while ((srcp = strchr(destp, op))) {
546 memmove(destp, srcp + 1, strlen(srcp));
551 else if (*destp == cl)
563 void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
566 register gchar *srcp, *destp;
568 gboolean in_quote = FALSE;
572 while ((srcp = strchr_with_skip_quote(destp, '"', op))) {
575 memmove(destp, srcp + 1, strlen(srcp));
578 if (*destp == op && !in_quote)
580 else if (*destp == cl && !in_quote)
582 else if (*destp == quote_chr)
594 void eliminate_quote(gchar *str, gchar quote_chr)
596 register gchar *srcp, *destp;
600 while ((destp = strchr(destp, quote_chr))) {
601 if ((srcp = strchr(destp + 1, quote_chr))) {
603 while (isspace(*srcp)) srcp++;
604 memmove(destp, srcp, strlen(srcp) + 1);
612 void extract_quote(gchar *str, gchar quote_chr)
616 if ((str = strchr(str, quote_chr))) {
617 if ((p = strchr(str + 1, quote_chr))) {
619 memmove(str, str + 1, p - str);
624 void eliminate_address_comment(gchar *str)
626 register gchar *srcp, *destp;
631 while ((destp = strchr(destp, '"'))) {
632 if ((srcp = strchr(destp + 1, '"'))) {
637 while (isspace(*srcp)) srcp++;
638 memmove(destp, srcp, strlen(srcp) + 1);
648 while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
654 else if (*srcp == ')')
660 while (isspace(*srcp)) srcp++;
661 memmove(destp, srcp, strlen(srcp) + 1);
665 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
667 gboolean in_quote = FALSE;
670 if (*str == c && !in_quote)
672 if (*str == quote_chr)
680 gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
682 gboolean in_quote = FALSE;
685 p = str + strlen(str) - 1;
687 if (*p == c && !in_quote)
697 void extract_address(gchar *str)
699 eliminate_address_comment(str);
700 if (strchr_with_skip_quote(str, '"', '<'))
701 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
705 GSList *address_list_append(GSList *addr_list, const gchar *str)
710 if (!str) return addr_list;
712 Xstrdup_a(work, str, return addr_list);
714 eliminate_address_comment(work);
717 while (workp && *workp) {
720 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
726 if (strchr_with_skip_quote(workp, '"', '<'))
727 extract_parenthesis_with_skip_quote
728 (workp, '"', '<', '>');
732 addr_list = g_slist_append(addr_list, g_strdup(workp));
740 GSList *references_list_append(GSList *msgid_list, const gchar *str)
744 if (!str) return msgid_list;
747 while (strp && *strp) {
748 const gchar *start, *end;
751 if ((start = strchr(strp, '<')) != NULL) {
752 end = strchr(start + 1, '>');
757 msgid = g_strndup(start + 1, end - start - 1);
760 msgid_list = g_slist_append(msgid_list, msgid);
770 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
775 if (!str) return group_list;
777 Xstrdup_a(work, str, return group_list);
781 while (workp && *workp) {
784 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
792 group_list = g_slist_append(group_list,
801 void remove_return(gchar *str)
803 register gchar *p = str;
806 if (*p == '\n' || *p == '\r')
807 memmove(p, p + 1, strlen(p));
813 void remove_space(gchar *str)
815 register gchar *p = str;
820 while (isspace(*(p + spc)))
823 memmove(p, p + spc, strlen(p + spc) + 1);
829 void unfold_line(gchar *str)
831 register gchar *p = str;
835 if (*p == '\n' || *p == '\r') {
838 while (isspace(*(p + spc)))
841 memmove(p, p + spc, strlen(p + spc) + 1);
847 void subst_char(gchar *str, gchar orig, gchar subst)
849 register gchar *p = str;
858 gboolean is_header_line(const gchar *str)
860 if (str[0] == ':') return FALSE;
862 while (*str != '\0' && *str != ' ') {
871 gboolean is_ascii_str(const guchar *str)
873 while (*str != '\0') {
874 if (*str != '\t' && *str != ' ' &&
875 *str != '\r' && *str != '\n' &&
876 (*str < 32 || *str >= 127))
884 gint get_quote_level(const gchar *str)
886 size_t firstquotepos;
887 size_t lastquotepos = -1;
888 const gchar *p = str;
890 gint quotelevel = -1;
893 /* speed up line processing by only searching to the last '>' */
894 if ((pos = strchr(str, '>')) != NULL) {
895 firstquotepos = pos - str;
896 lastquotepos = strrchr(str, '>') - str + 1;
898 /* skip a line if it contains a '<' before the initial '>' */
899 if (memchr(str, '<', pos - str) != NULL)
904 while (i < lastquotepos) {
905 while (i < lastquotepos) {
906 if (isspace(*p) || (*p == '\t')) {
912 if (i >= lastquotepos)
917 else if ((*p != '-') && !isspace(*p) && (i < lastquotepos)) {
918 /* any characters are allowed except '-' and space */
919 while ((*p != '-') && (*p != '>') && !isspace(*p) &&
920 (i < lastquotepos)) {
926 else if ((i >= lastquotepos) || isspace(*p))
937 GList *uri_list_extract_filenames(const gchar *uri_list)
939 GList *result = NULL;
947 while (isspace(*p)) p++;
948 if (!strncmp(p, "file:", 5)) {
951 while (*q && *q != '\n' && *q != '\r') q++;
955 while (q > p && isspace(*q)) q--;
956 file = g_malloc(q - p + 2);
957 strncpy(file, p, q - p + 1);
958 file[q - p + 1] = '\0';
959 result = g_list_append(result,file);
970 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
972 register guint haystack_len, needle_len;
973 gboolean in_squote = FALSE, in_dquote = FALSE;
975 haystack_len = strlen(haystack);
976 needle_len = strlen(needle);
978 if (haystack_len < needle_len || needle_len == 0)
981 while (haystack_len >= needle_len) {
982 if (!in_squote && !in_dquote &&
983 !strncmp(haystack, needle, needle_len))
984 return (gchar *)haystack;
986 /* 'foo"bar"' -> foo"bar"
987 "foo'bar'" -> foo'bar' */
988 if (*haystack == '\'') {
993 } else if (*haystack == '\"') {
1007 /* this fuction was taken from gstrfuncs.c in glib. */
1008 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1011 GSList *string_list = NULL, *slist;
1012 gchar **str_array, *s;
1015 g_return_val_if_fail(str != NULL, NULL);
1016 g_return_val_if_fail(delim != NULL, NULL);
1019 max_tokens = G_MAXINT;
1021 s = strstr_with_skip_quote(str, delim);
1023 guint delimiter_len = strlen(delim);
1030 new_str = g_new(gchar, len + 1);
1031 strncpy(new_str, str, len);
1033 string_list = g_slist_prepend(string_list, new_str);
1035 str = s + delimiter_len;
1036 s = strstr_with_skip_quote(str, delim);
1037 } while (--max_tokens && s);
1042 string_list = g_slist_prepend(string_list, g_strdup(str));
1045 str_array = g_new(gchar*, n);
1049 str_array[i--] = NULL;
1050 for (slist = string_list; slist; slist = slist->next)
1051 str_array[i--] = slist->data;
1053 g_slist_free(string_list);
1059 * We need this wrapper around g_get_home_dir(), so that
1060 * we can fix some Windoze things here. Should be done in glibc of course
1061 * but as long as we are not able to do our own extensions to glibc, we do
1064 gchar *get_home_dir(void)
1066 #if HAVE_DOSISH_SYSTEM
1067 static gchar *home_dir;
1070 home_dir = read_w32_registry_string(NULL,
1071 "Software\\Sylpheed", "HomeDir" );
1072 if (!home_dir || !*home_dir) {
1073 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1074 const char *s = g_get_home_dir();
1076 home_dir = g_strdup (s);
1078 if (!home_dir || !*home_dir)
1079 home_dir = g_strdup ("c:\\sylpheed");
1081 debug_print("initialized home_dir to `%s'\n", home_dir);
1084 #else /* standard glib */
1085 return g_get_home_dir();
1089 gchar *get_rc_dir(void)
1091 static gchar *rc_dir = NULL;
1094 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1100 gchar *get_news_cache_dir(void)
1102 static gchar *news_cache_dir = NULL;
1104 if (!news_cache_dir)
1105 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1106 NEWS_CACHE_DIR, NULL);
1108 return news_cache_dir;
1111 gchar *get_imap_cache_dir(void)
1113 static gchar *imap_cache_dir = NULL;
1115 if (!imap_cache_dir)
1116 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1117 IMAP_CACHE_DIR, NULL);
1119 return imap_cache_dir;
1122 gchar *get_mime_tmp_dir(void)
1124 static gchar *mime_tmp_dir = NULL;
1127 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1128 MIME_TMP_DIR, NULL);
1130 return mime_tmp_dir;
1133 gchar *get_tmp_file(void)
1135 static gchar *tmp_file = NULL;
1138 tmp_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1144 gchar *get_domain_name(void)
1146 static gchar *domain_name = NULL;
1149 gchar buf[BUFFSIZE] = "";
1151 if (gethostname(buf, sizeof(buf)) < 0) {
1152 perror("gethostname");
1153 strcpy(buf, "unknown");
1156 domain_name = g_strdup(buf);
1162 off_t get_file_size(const gchar *file)
1166 if (stat(file, &s) < 0) {
1167 FILE_OP_ERROR(file, "stat");
1174 off_t get_left_file_size(FILE *fp)
1180 if ((pos = ftell(fp)) < 0) {
1184 if (fseek(fp, 0L, SEEK_END) < 0) {
1188 if ((end = ftell(fp)) < 0) {
1193 if (fseek(fp, pos, SEEK_SET) < 0) {
1201 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1205 if (stat(file, &s) < 0) {
1206 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1210 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1216 gboolean is_dir_exist(const gchar *dir)
1220 if (stat(dir, &s) < 0) {
1221 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1225 if (S_ISDIR(s.st_mode))
1231 gint change_dir(const gchar *dir)
1233 gchar *prevdir = NULL;
1236 prevdir = g_get_current_dir();
1238 if (chdir(dir) < 0) {
1239 FILE_OP_ERROR(dir, "chdir");
1240 if (debug_mode) g_free(prevdir);
1242 } else if (debug_mode) {
1245 cwd = g_get_current_dir();
1246 if (strcmp(prevdir, cwd) != 0)
1247 g_print("current dir: %s\n", cwd);
1255 gint make_dir_hier(const gchar *dir)
1260 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1261 parent_dir = g_strndup(dir, p - dir);
1262 if (*parent_dir != '\0') {
1263 if (!is_dir_exist(parent_dir)) {
1264 if (mkdir(parent_dir, S_IRWXU) < 0) {
1265 FILE_OP_ERROR(parent_dir, "mkdir");
1269 if (chmod(parent_dir, S_IRWXU) < 0)
1270 FILE_OP_ERROR(parent_dir, "chmod");
1275 if (!is_dir_exist(dir)) {
1276 if (mkdir(dir, S_IRWXU) < 0) {
1277 FILE_OP_ERROR(dir, "mkdir");
1280 if (chmod(dir, S_IRWXU) < 0)
1281 FILE_OP_ERROR(dir, "chmod");
1287 gint remove_all_files(const gchar *dir)
1293 prev_dir = g_get_current_dir();
1295 if (chdir(dir) < 0) {
1296 FILE_OP_ERROR(dir, "chdir");
1300 if ((dp = opendir(".")) == NULL) {
1301 FILE_OP_ERROR(dir, "opendir");
1305 while ((d = readdir(dp)) != NULL) {
1306 if (!strcmp(d->d_name, ".") ||
1307 !strcmp(d->d_name, ".."))
1310 if (unlink(d->d_name) < 0)
1311 FILE_OP_ERROR(d->d_name, "unlink");
1316 if (chdir(prev_dir) < 0) {
1317 FILE_OP_ERROR(prev_dir, "chdir");
1327 gint remove_dir_recursive(const gchar *dir)
1334 //g_print("dir = %s\n", dir);
1336 if (stat(dir, &s) < 0) {
1337 FILE_OP_ERROR(dir, "stat");
1338 if (ENOENT == errno) return 0;
1342 if (!S_ISDIR(s.st_mode)) {
1343 if (unlink(dir) < 0) {
1344 FILE_OP_ERROR(dir, "unlink");
1351 prev_dir = g_get_current_dir();
1352 //g_print("prev_dir = %s\n", prev_dir);
1354 if (!path_cmp(prev_dir, dir)) {
1356 if (chdir("..") < 0) {
1357 FILE_OP_ERROR(dir, "chdir");
1360 prev_dir = g_get_current_dir();
1363 if (chdir(dir) < 0) {
1364 FILE_OP_ERROR(dir, "chdir");
1369 if ((dp = opendir(".")) == NULL) {
1370 FILE_OP_ERROR(dir, "opendir");
1376 /* remove all files in the directory */
1377 while ((d = readdir(dp)) != NULL) {
1378 if (!strcmp(d->d_name, ".") ||
1379 !strcmp(d->d_name, ".."))
1382 if (stat(d->d_name, &s) < 0) {
1383 FILE_OP_ERROR(d->d_name, "stat");
1387 //g_print("removing %s\n", d->d_name);
1389 if (S_ISDIR(s.st_mode)) {
1390 if (remove_dir_recursive(d->d_name) < 0) {
1391 g_warning("can't remove directory\n");
1395 if (unlink(d->d_name) < 0)
1396 FILE_OP_ERROR(d->d_name, "unlink");
1402 if (chdir(prev_dir) < 0) {
1403 FILE_OP_ERROR(prev_dir, "chdir");
1410 if (rmdir(dir) < 0) {
1411 FILE_OP_ERROR(dir, "rmdir");
1419 /* this seems to be slower than the stdio version... */
1420 gint copy_file(const gchar *src, const gchar *dest)
1422 gint src_fd, dest_fd;
1426 gchar *dest_bak = NULL;
1428 if ((src_fd = open(src, O_RDONLY)) < 0) {
1429 FILE_OP_ERROR(src, "open");
1433 if (is_file_exist(dest)) {
1434 dest_bak = g_strconcat(dest, ".bak", NULL);
1435 if (rename(dest, dest_bak) < 0) {
1436 FILE_OP_ERROR(dest, "rename");
1443 if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
1444 FILE_OP_ERROR(dest, "open");
1447 if (rename(dest_bak, dest) < 0)
1448 FILE_OP_ERROR(dest_bak, "rename");
1454 while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
1459 n_write = write(dest_fd, bufp, len);
1461 g_warning(_("writing to %s failed.\n"), dest);
1466 if (rename(dest_bak, dest) < 0)
1467 FILE_OP_ERROR(dest_bak, "rename");
1480 if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
1481 g_warning(_("File copy from %s to %s failed.\n"), src, dest);
1484 if (rename(dest_bak, dest) < 0)
1485 FILE_OP_ERROR(dest_bak, "rename");
1496 gint copy_file(const gchar *src, const gchar *dest)
1498 FILE *src_fp, *dest_fp;
1501 gchar *dest_bak = NULL;
1502 gboolean err = FALSE;
1504 if ((src_fp = fopen(src, "r")) == NULL) {
1505 FILE_OP_ERROR(src, "fopen");
1508 if (is_file_exist(dest)) {
1509 dest_bak = g_strconcat(dest, ".bak", NULL);
1510 if (rename(dest, dest_bak) < 0) {
1511 FILE_OP_ERROR(dest, "rename");
1518 if ((dest_fp = fopen(dest, "w")) == NULL) {
1519 FILE_OP_ERROR(dest, "fopen");
1522 if (rename(dest_bak, dest) < 0)
1523 FILE_OP_ERROR(dest_bak, "rename");
1529 if (change_file_mode_rw(dest_fp, dest) < 0) {
1530 FILE_OP_ERROR(dest, "chmod");
1531 g_warning(_("can't change file mode\n"));
1534 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
1535 if (n_read < sizeof(buf) && ferror(src_fp))
1537 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
1538 g_warning(_("writing to %s failed.\n"), dest);
1543 if (rename(dest_bak, dest) < 0)
1544 FILE_OP_ERROR(dest_bak, "rename");
1551 if (ferror(src_fp)) {
1552 FILE_OP_ERROR(src, "fread");
1556 if (fclose(dest_fp) == EOF) {
1557 FILE_OP_ERROR(dest, "fclose");
1564 if (rename(dest_bak, dest) < 0)
1565 FILE_OP_ERROR(dest_bak, "rename");
1576 gint change_file_mode_rw(FILE *fp, const gchar *file)
1579 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
1581 return chmod(file, S_IRUSR|S_IWUSR);
1585 FILE *my_tmpfile(void)
1588 const gchar suffix[] = ".XXXXXX";
1589 const gchar *tmpdir;
1591 const gchar *progname;
1597 tmpdir = g_get_tmp_dir();
1598 tmplen = strlen(tmpdir);
1599 progname = g_get_prgname();
1600 proglen = strlen(progname);
1601 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
1604 memcpy(fname, tmpdir, tmplen);
1605 fname[tmplen] = G_DIR_SEPARATOR;
1606 memcpy(fname + tmplen + 1, progname, proglen);
1607 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
1609 fd = mkstemp(fname);
1615 fp = fdopen(fd, "w+b");
1620 #endif /* HAVE_MKSTEMP */
1625 gint execute_async(gchar *const argv[])
1629 if ((pid = fork()) < 0) {
1634 if (pid == 0) { /* child process */
1637 if ((gch_pid = fork()) < 0) {
1642 if (gch_pid == 0) { /* grandchild process */
1643 execvp(argv[0], argv);
1652 waitpid(pid, NULL, 0);
1657 gint execute_command_line(const gchar *cmdline)
1663 argv = strsplit_with_quote(cmdline, " ", 0);
1665 for (i = 0; argv[i] != NULL; i++) {
1666 gchar *str = argv[i];
1668 if (str[0] == '\'' || str[0] == '\"') {
1672 if (str[len - 1] == str[0]) {
1673 str[len - 1] = '\0';
1674 memmove(str, str + 1, len - 1);
1679 ret = execute_async(argv);
1685 gint open_uri(const gchar *uri, const gchar *cmdline)
1687 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
1688 gchar buf[BUFFSIZE];
1691 g_return_val_if_fail(uri != NULL, -1);
1694 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1695 !strchr(p + 2, '%'))
1696 g_snprintf(buf, sizeof(buf), cmdline, uri);
1699 g_warning(_("Open URI command line is invalid: `%s'"),
1701 g_snprintf(buf, sizeof(buf), default_cmdline, uri);
1704 execute_command_line(buf);
1709 time_t remote_tzoffset_sec(const gchar *zone)
1711 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
1717 time_t remoteoffset;
1719 strncpy(zone3, zone, 3);
1723 if (sscanf(zone, "%c%2d%2d", &c, &h, &m) == 3 &&
1724 (c == '+' || c == '-')) {
1725 remoteoffset = ((h * 60) + m) * 60;
1727 remoteoffset = -remoteoffset;
1728 } else if (!strncmp(zone, "UT" , 2) ||
1729 !strncmp(zone, "GMT", 2)) {
1731 } else if (strlen(zone3) == 3 &&
1732 (p = strstr(ustzstr, zone3)) != NULL &&
1733 (p - ustzstr) % 3 == 0) {
1734 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
1735 remoteoffset = iustz * 3600;
1736 } else if (strlen(zone3) == 1) {
1738 case 'Z': remoteoffset = 0; break;
1739 case 'A': remoteoffset = -1; break;
1740 case 'B': remoteoffset = -2; break;
1741 case 'C': remoteoffset = -3; break;
1742 case 'D': remoteoffset = -4; break;
1743 case 'E': remoteoffset = -5; break;
1744 case 'F': remoteoffset = -6; break;
1745 case 'G': remoteoffset = -7; break;
1746 case 'H': remoteoffset = -8; break;
1747 case 'I': remoteoffset = -9; break;
1748 case 'K': remoteoffset = -10; break; /* J is not used */
1749 case 'L': remoteoffset = -11; break;
1750 case 'M': remoteoffset = -12; break;
1751 case 'N': remoteoffset = 1; break;
1752 case 'O': remoteoffset = 2; break;
1753 case 'P': remoteoffset = 3; break;
1754 case 'Q': remoteoffset = 4; break;
1755 case 'R': remoteoffset = 5; break;
1756 case 'S': remoteoffset = 6; break;
1757 case 'T': remoteoffset = 7; break;
1758 case 'U': remoteoffset = 8; break;
1759 case 'V': remoteoffset = 9; break;
1760 case 'W': remoteoffset = 10; break;
1761 case 'X': remoteoffset = 11; break;
1762 case 'Y': remoteoffset = 12; break;
1763 default: remoteoffset = 0; break;
1765 remoteoffset = remoteoffset * 3600;
1768 return remoteoffset;
1771 time_t tzoffset_sec(time_t *now)
1777 lt = localtime(now);
1779 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1781 if (lt->tm_year < gmt.tm_year)
1783 else if (lt->tm_year > gmt.tm_year)
1785 else if (lt->tm_yday < gmt.tm_yday)
1787 else if (lt->tm_yday > gmt.tm_yday)
1790 if (off >= 24 * 60) /* should be impossible */
1791 off = 23 * 60 + 59; /* if not, insert silly value */
1792 if (off <= -24 * 60)
1793 off = -(23 * 60 + 59);
1802 /* calculate timezone offset */
1803 gchar *tzoffset(time_t *now)
1805 static gchar offset_string[6];
1811 lt = localtime(now);
1813 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1815 if (lt->tm_year < gmt.tm_year)
1817 else if (lt->tm_year > gmt.tm_year)
1819 else if (lt->tm_yday < gmt.tm_yday)
1821 else if (lt->tm_yday > gmt.tm_yday)
1829 if (off >= 24 * 60) /* should be impossible */
1830 off = 23 * 60 + 59; /* if not, insert silly value */
1832 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
1834 return offset_string;
1837 void get_rfc822_date(gchar *buf, gint len)
1841 gchar day[4], mon[4];
1842 gint dd, hh, mm, ss, yyyy;
1847 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
1848 day, mon, &dd, &hh, &mm, &ss, &yyyy);
1849 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
1850 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
1853 void debug_print(const gchar *format, ...)
1856 gchar buf[BUFFSIZE];
1858 if (!debug_mode) return;
1860 va_start(args, format);
1861 g_vsnprintf(buf, sizeof(buf), format, args);
1867 void log_print(const gchar *format, ...)
1870 gchar buf[BUFFSIZE];
1872 va_start(args, format);
1873 g_vsnprintf(buf, sizeof(buf), format, args);
1876 if (debug_mode) fputs(buf, stdout);
1877 log_window_append(buf, LOG_NORMAL);
1878 statusbar_puts_all(buf);
1881 void log_message(const gchar *format, ...)
1884 gchar buf[BUFFSIZE];
1886 va_start(args, format);
1887 g_vsnprintf(buf, sizeof(buf), format, args);
1890 if (debug_mode) g_message("%s", buf);
1891 log_window_append(buf, LOG_MSG);
1894 void log_warning(const gchar *format, ...)
1897 gchar buf[BUFFSIZE];
1899 va_start(args, format);
1900 g_vsnprintf(buf, sizeof(buf), format, args);
1903 g_warning("%s", buf);
1904 log_window_append(buf, LOG_WARN);
1907 void log_error(const gchar *format, ...)
1910 gchar buf[BUFFSIZE];
1912 va_start(args, format);
1913 g_vsnprintf(buf, sizeof(buf), format, args);
1916 g_warning("%s", buf);
1917 log_window_append(buf, LOG_ERROR);