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 */
112 static gchar nstr[11];
114 g_snprintf(nstr, 11, "%d", n);
118 gchar *to_human_readable(off_t size)
124 for (count = 0; count < 3; count++) {
125 if (size / div < 1024)
132 case 0: g_snprintf(str, sizeof(str), "%dB", (gint)size); break;
133 case 1: g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / div);
135 case 2: g_snprintf(str, sizeof(str), "%.1fMB", (gfloat)size / div);
138 g_snprintf(str, sizeof(str), "%.1fGB", (gfloat)size / div);
145 /* strcmp with NULL-checking */
146 gint strcmp2(const gchar *s1, const gchar *s2)
148 if (s1 == NULL || s2 == NULL)
151 return strcmp(s1, s2);
155 gint path_cmp(const gchar *s1, const gchar *s2)
159 if (s1 == NULL || s2 == NULL) return -1;
160 if (*s1 == '\0' || *s2 == '\0') return -1;
165 if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
166 if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
168 return strncmp(s1, s2, MAX(len1, len2));
171 /* remove trailing return code */
172 gchar *strretchomp(gchar *str)
176 if (!*str) return str;
178 for (s = str + strlen(str) - 1;
179 s >= str && (*s == '\n' || *s == '\r');
186 /* Similar to `strstr' but this function ignores the case of both strings. */
187 gchar *strcasestr(const gchar *haystack, const gchar *needle)
189 register size_t haystack_len, needle_len;
191 haystack_len = strlen(haystack);
192 needle_len = strlen(needle);
194 if (haystack_len < needle_len || needle_len == 0)
197 while (haystack_len >= needle_len) {
198 if (!strncasecmp(haystack, needle, needle_len))
199 return (gchar *)haystack;
209 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
210 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
224 /* don't do zero fill */
229 int iswalnum(wint_t wc)
231 return isalnum((int)wc);
236 int iswspace(wint_t wc)
238 return isspace((int)wc);
243 wint_t towlower(wint_t wc)
245 if (wc >= L'A' && wc <= L'Z')
246 return wc + L'a' - L'A';
253 size_t wcslen(const wchar_t *s)
265 /* Copy SRC to DEST. */
266 wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
274 } while (c != L'\0');
281 /* Copy no more than N wide-characters of SRC to DEST. */
282 wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
292 } while (c != L'\0');
303 /* Duplicate S, returning an identical malloc'd string. */
304 wchar_t *wcsdup(const wchar_t *s)
309 new_str = g_new(wchar_t, wcslen(s) + 1);
317 /* Duplicate no more than N wide-characters of S,
318 returning an identical malloc'd string. */
319 wchar_t *wcsndup(const wchar_t *s, size_t n)
324 new_str = g_new(wchar_t, n + 1);
325 wcsncpy(new_str, s, n);
326 new_str[n] = (wchar_t)0;
333 wchar_t *strdup_mbstowcs(const gchar *s)
338 new_str = g_new(wchar_t, strlen(s) + 1);
339 if (mbstowcs(new_str, s, strlen(s) + 1) < 0) {
343 new_str = g_realloc(new_str,
344 sizeof(wchar_t) * (wcslen(new_str) + 1));
351 gchar *strdup_wcstombs(const wchar_t *s)
357 len = wcslen(s) * MB_CUR_MAX + 1;
358 new_str = g_new(gchar, len);
359 if (wcstombs(new_str, s, len) < 0) {
363 new_str = g_realloc(new_str, strlen(new_str) + 1);
370 /* Compare S1 and S2, ignoring case. */
371 gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
377 c1 = towlower(*s1++);
378 c2 = towlower(*s2++);
381 else if (c1 == 0 && c2 == 0)
388 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
389 wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
391 register size_t haystack_len, needle_len;
393 haystack_len = wcslen(haystack);
394 needle_len = wcslen(needle);
396 if (haystack_len < needle_len || needle_len == 0)
399 while (haystack_len >= needle_len) {
400 if (!wcsncasecmp(haystack, needle, needle_len))
401 return (wchar_t *)haystack;
411 /* Examine if next block is non-ASCII string */
412 gboolean is_next_nonascii(const wchar_t *s)
416 /* skip head space */
417 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
419 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
427 /* Examine if next block is multi-byte string */
428 gboolean is_next_mbs(const wchar_t *s)
432 gchar tmp[MB_CUR_MAX];
434 /* skip head space */
435 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
437 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
438 mbl = wctomb(tmp, *wp);
446 wchar_t *find_wspace(const wchar_t *s)
450 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
452 for (; *wp != (wchar_t)0; wp++) {
454 return (wchar_t *)wp;
460 /* compare subjects */
461 gint subject_compare(const gchar *s1, const gchar *s2)
466 if (!s1 || !s2) return -1;
467 if (!*s1 || !*s2) return -1;
469 Xalloca(str1, strlen(s1) + 1, return -1);
470 Xalloca(str2, strlen(s2) + 1, return -1);
477 if (!*str1 || !*str2) return -1;
479 retval = strcmp(str1, str2);
481 // g_print("\ns1 = %s\ns2 = %s\n"
482 // "str1 = %s\nstr2 = %s\nmatched.\n",
483 // s1, s2, str1, str2);
488 void trim_subject(gchar *str)
492 eliminate_parenthesis(str, '[', ']');
493 eliminate_parenthesis(str, '(', ')');
496 while (!strncasecmp(str, "Re:", 3)) {
498 while (isspace(*srcp)) srcp++;
499 memmove(str, srcp, strlen(srcp) + 1);
503 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
505 register gchar *srcp, *destp;
510 while ((destp = strchr(destp, op))) {
516 else if (*srcp == cl)
522 while (isspace(*srcp)) srcp++;
523 memmove(destp, srcp, strlen(srcp) + 1);
527 void extract_parenthesis(gchar *str, gchar op, gchar cl)
529 register gchar *srcp, *destp;
534 while ((srcp = strchr(destp, op))) {
537 memmove(destp, srcp + 1, strlen(srcp));
542 else if (*destp == cl)
554 void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
557 register gchar *srcp, *destp;
559 gboolean in_quote = FALSE;
563 while ((srcp = strchr_with_skip_quote(destp, '"', op))) {
566 memmove(destp, srcp + 1, strlen(srcp));
569 if (*destp == op && !in_quote)
571 else if (*destp == cl && !in_quote)
573 else if (*destp == quote_chr)
585 void eliminate_quote(gchar *str, gchar quote_chr)
587 register gchar *srcp, *destp;
591 while ((destp = strchr(destp, quote_chr))) {
592 if ((srcp = strchr(destp + 1, quote_chr))) {
594 while (isspace(*srcp)) srcp++;
595 memmove(destp, srcp, strlen(srcp) + 1);
603 void extract_quote(gchar *str, gchar quote_chr)
607 if ((str = strchr(str, quote_chr))) {
608 if ((p = strchr(str + 1, quote_chr))) {
610 memmove(str, str + 1, p - str);
615 void eliminate_address_comment(gchar *str)
617 register gchar *srcp, *destp;
622 while ((destp = strchr(destp, '"'))) {
623 if ((srcp = strchr(destp + 1, '"'))) {
628 while (isspace(*srcp)) srcp++;
629 memmove(destp, srcp, strlen(srcp) + 1);
639 while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
645 else if (*srcp == ')')
651 while (isspace(*srcp)) srcp++;
652 memmove(destp, srcp, strlen(srcp) + 1);
656 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
658 gboolean in_quote = FALSE;
661 if (*str == c && !in_quote)
663 if (*str == quote_chr)
671 gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
673 gboolean in_quote = FALSE;
676 p = str + strlen(str) - 1;
678 if (*p == c && !in_quote)
688 void extract_address(gchar *str)
690 eliminate_address_comment(str);
691 if (strchr_with_skip_quote(str, '"', '<'))
692 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
696 GSList *address_list_append(GSList *addr_list, const gchar *str)
701 if (!str) return addr_list;
703 Xstrdup_a(work, str, return addr_list);
705 eliminate_address_comment(work);
708 while (workp && *workp) {
711 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
717 if (strchr_with_skip_quote(workp, '"', '<'))
718 extract_parenthesis_with_skip_quote
719 (workp, '"', '<', '>');
723 addr_list = g_slist_append(addr_list, g_strdup(workp));
731 GSList *references_list_append(GSList *msgid_list, const gchar *str)
735 if (!str) return msgid_list;
738 while (strp && *strp) {
739 const gchar *start, *end;
742 if ((start = strchr(strp, '<')) != NULL) {
743 end = strchr(start + 1, '>');
748 msgid = g_strndup(start + 1, end - start - 1);
751 msgid_list = g_slist_append(msgid_list, msgid);
761 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
766 if (!str) return group_list;
768 Xstrdup_a(work, str, return group_list);
772 while (workp && *workp) {
775 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
783 group_list = g_slist_append(group_list,
792 void remove_return(gchar *str)
794 register gchar *p = str;
797 if (*p == '\n' || *p == '\r')
798 memmove(p, p + 1, strlen(p));
804 void remove_space(gchar *str)
806 register gchar *p = str;
811 while (isspace(*(p + spc)))
814 memmove(p, p + spc, strlen(p + spc) + 1);
820 void subst_char(gchar *str, gchar orig, gchar subst)
822 register gchar *p = str;
831 gboolean is_header_line(const gchar *str)
833 if (str[0] == ':') return FALSE;
835 while (*str != '\0' && *str != ' ') {
844 gboolean is_ascii_str(const guchar *str)
846 while (*str != '\0') {
847 if (*str != '\t' && *str != ' ' &&
848 *str != '\r' && *str != '\n' &&
849 (*str < 32 || *str >= 127))
857 gint get_quote_level(const gchar *str)
859 size_t firstquotepos;
860 size_t lastquotepos = -1;
861 const gchar *p = str;
863 gint quotelevel = -1;
866 /* speed up line processing by only searching to the last '>' */
867 if ((pos = strchr(str, '>')) != NULL) {
868 firstquotepos = pos - str;
869 lastquotepos = strrchr(str, '>') - str + 1;
871 /* skip a line if it contains a '<' before the initial '>' */
872 if (memchr(str, '<', pos - str) != NULL)
877 while (i < lastquotepos) {
878 while (i < lastquotepos) {
879 if (isspace(*p) || (*p == '\t')) {
885 if (i >= lastquotepos)
890 else if ((*p != '-') && !isspace(*p) && (i < lastquotepos)) {
891 /* any characters are allowed except '-' and space */
892 while ((*p != '-') && (*p != '>') && !isspace(*p) &&
893 (i < lastquotepos)) {
899 else if ((i >= lastquotepos) || isspace(*p))
910 GList *uri_list_extract_filenames(const gchar *uri_list)
912 GList *result = NULL;
920 while (isspace(*p)) p++;
921 if (!strncmp(p, "file:", 5)) {
924 while (*q && *q != '\n' && *q != '\r') q++;
928 while (q > p && isspace(*q)) q--;
929 file = g_malloc(q - p + 2);
930 strncpy(file, p, q - p + 1);
931 file[q - p + 1] = '\0';
932 result = g_list_append(result,file);
943 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
945 register guint haystack_len, needle_len;
946 gboolean in_squote = FALSE, in_dquote = FALSE;
948 haystack_len = strlen(haystack);
949 needle_len = strlen(needle);
951 if (haystack_len < needle_len || needle_len == 0)
954 while (haystack_len >= needle_len) {
955 if (!in_squote && !in_dquote &&
956 !strncmp(haystack, needle, needle_len))
957 return (gchar *)haystack;
959 /* 'foo"bar"' -> foo"bar"
960 "foo'bar'" -> foo'bar' */
961 if (*haystack == '\'') {
966 } else if (*haystack == '\"') {
980 /* this fuction was taken from gstrfuncs.c in glib. */
981 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
984 GSList *string_list = NULL, *slist;
985 gchar **str_array, *s;
988 g_return_val_if_fail(str != NULL, NULL);
989 g_return_val_if_fail(delim != NULL, NULL);
992 max_tokens = G_MAXINT;
994 s = strstr_with_skip_quote(str, delim);
996 guint delimiter_len = strlen(delim);
1003 new_str = g_new(gchar, len + 1);
1004 strncpy(new_str, str, len);
1006 string_list = g_slist_prepend(string_list, new_str);
1008 str = s + delimiter_len;
1009 s = strstr_with_skip_quote(str, delim);
1010 } while (--max_tokens && s);
1015 string_list = g_slist_prepend(string_list, g_strdup(str));
1018 str_array = g_new(gchar*, n);
1022 str_array[i--] = NULL;
1023 for (slist = string_list; slist; slist = slist->next)
1024 str_array[i--] = slist->data;
1026 g_slist_free(string_list);
1032 * We need this wrapper around g_get_home_dir(), so that
1033 * we can fix some Windoze things here. Should be done in glibc of course
1034 * but as long as we are not able to do our own extensions to glibc, we do
1037 gchar *get_home_dir(void)
1039 #if HAVE_DOSISH_SYSTEM
1040 static gchar *home_dir;
1043 home_dir = read_w32_registry_string(NULL,
1044 "Software\\Sylpheed", "HomeDir" );
1045 if (!home_dir || !*home_dir) {
1046 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1047 const char *s = g_get_home_dir();
1049 home_dir = g_strdup (s);
1051 if (!home_dir || !*home_dir)
1052 home_dir = g_strdup ("c:\\sylpheed");
1054 debug_print("initialized home_dir to `%s'\n", home_dir);
1057 #else /* standard glib */
1058 return g_get_home_dir();
1062 gchar *get_rc_dir(void)
1064 static gchar *rc_dir = NULL;
1067 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1073 gchar *get_news_cache_dir(void)
1075 static gchar *news_cache_dir = NULL;
1077 if (!news_cache_dir)
1078 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1079 NEWS_CACHE_DIR, NULL);
1081 return news_cache_dir;
1084 gchar *get_imap_cache_dir(void)
1086 static gchar *imap_cache_dir = NULL;
1088 if (!imap_cache_dir)
1089 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1090 IMAP_CACHE_DIR, NULL);
1092 return imap_cache_dir;
1095 gchar *get_mime_tmp_dir(void)
1097 static gchar *mime_tmp_dir = NULL;
1100 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1101 MIME_TMP_DIR, NULL);
1103 return mime_tmp_dir;
1106 gchar *get_tmp_file(void)
1108 static gchar *tmp_file = NULL;
1111 tmp_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1117 gchar *get_domain_name(void)
1119 static gchar *domain_name = NULL;
1122 gchar buf[BUFFSIZE] = "";
1124 if (gethostname(buf, sizeof(buf)) < 0) {
1125 perror("gethostname");
1126 strcpy(buf, "unknown");
1129 domain_name = g_strdup(buf);
1135 off_t get_file_size(const gchar *file)
1139 if (stat(file, &s) < 0) {
1140 FILE_OP_ERROR(file, "stat");
1147 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1151 if (stat(file, &s) < 0) {
1152 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1156 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1162 gboolean is_dir_exist(const gchar *dir)
1166 if (stat(dir, &s) < 0) {
1167 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1171 if (S_ISDIR(s.st_mode))
1177 gint change_dir(const gchar *dir)
1179 gchar *prevdir = NULL;
1182 prevdir = g_get_current_dir();
1184 if (chdir(dir) < 0) {
1185 FILE_OP_ERROR(dir, "chdir");
1186 if (debug_mode) g_free(prevdir);
1188 } else if (debug_mode) {
1191 cwd = g_get_current_dir();
1192 if (strcmp(prevdir, cwd) != 0)
1193 g_print("current dir: %s\n", cwd);
1201 gint make_dir_hier(const gchar *dir)
1206 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1207 parent_dir = g_strndup(dir, p - dir);
1208 if (*parent_dir != '\0') {
1209 if (!is_dir_exist(parent_dir)) {
1210 if (mkdir(parent_dir, S_IRWXU) < 0) {
1211 FILE_OP_ERROR(parent_dir, "mkdir");
1215 if (chmod(parent_dir, S_IRWXU) < 0)
1216 FILE_OP_ERROR(parent_dir, "chmod");
1221 if (!is_dir_exist(dir)) {
1222 if (mkdir(dir, S_IRWXU) < 0) {
1223 FILE_OP_ERROR(dir, "mkdir");
1226 if (chmod(dir, S_IRWXU) < 0)
1227 FILE_OP_ERROR(dir, "chmod");
1233 gint remove_all_files(const gchar *dir)
1239 prev_dir = g_get_current_dir();
1241 if (chdir(dir) < 0) {
1242 FILE_OP_ERROR(dir, "chdir");
1246 if ((dp = opendir(".")) == NULL) {
1247 FILE_OP_ERROR(dir, "opendir");
1251 while ((d = readdir(dp)) != NULL) {
1252 if (!strcmp(d->d_name, ".") ||
1253 !strcmp(d->d_name, ".."))
1256 if (unlink(d->d_name) < 0)
1257 FILE_OP_ERROR(d->d_name, "unlink");
1262 if (chdir(prev_dir) < 0) {
1263 FILE_OP_ERROR(prev_dir, "chdir");
1273 gint remove_dir_recursive(const gchar *dir)
1280 //g_print("dir = %s\n", dir);
1282 if (stat(dir, &s) < 0) {
1283 FILE_OP_ERROR(dir, "stat");
1284 if (ENOENT == errno) return 0;
1288 if (!S_ISDIR(s.st_mode)) {
1289 if (unlink(dir) < 0) {
1290 FILE_OP_ERROR(dir, "unlink");
1297 prev_dir = g_get_current_dir();
1298 //g_print("prev_dir = %s\n", prev_dir);
1300 if (!path_cmp(prev_dir, dir)) {
1302 if (chdir("..") < 0) {
1303 FILE_OP_ERROR(dir, "chdir");
1306 prev_dir = g_get_current_dir();
1309 if (chdir(dir) < 0) {
1310 FILE_OP_ERROR(dir, "chdir");
1315 if ((dp = opendir(".")) == NULL) {
1316 FILE_OP_ERROR(dir, "opendir");
1322 /* remove all files in the directory */
1323 while ((d = readdir(dp)) != NULL) {
1324 if (!strcmp(d->d_name, ".") ||
1325 !strcmp(d->d_name, ".."))
1328 if (stat(d->d_name, &s) < 0) {
1329 FILE_OP_ERROR(d->d_name, "stat");
1333 //g_print("removing %s\n", d->d_name);
1335 if (S_ISDIR(s.st_mode)) {
1336 if (remove_dir_recursive(d->d_name) < 0) {
1337 g_warning("can't remove directory\n");
1341 if (unlink(d->d_name) < 0)
1342 FILE_OP_ERROR(d->d_name, "unlink");
1348 if (chdir(prev_dir) < 0) {
1349 FILE_OP_ERROR(prev_dir, "chdir");
1356 if (rmdir(dir) < 0) {
1357 FILE_OP_ERROR(dir, "rmdir");
1365 /* this seems to be slower than the stdio version... */
1366 gint copy_file(const gchar *src, const gchar *dest)
1368 gint src_fd, dest_fd;
1372 gchar *dest_bak = NULL;
1374 if ((src_fd = open(src, O_RDONLY)) < 0) {
1375 FILE_OP_ERROR(src, "open");
1379 if (is_file_exist(dest)) {
1380 dest_bak = g_strconcat(dest, ".bak", NULL);
1381 if (rename(dest, dest_bak) < 0) {
1382 FILE_OP_ERROR(dest, "rename");
1389 if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
1390 FILE_OP_ERROR(dest, "open");
1393 if (rename(dest_bak, dest) < 0)
1394 FILE_OP_ERROR(dest_bak, "rename");
1400 while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
1405 n_write = write(dest_fd, bufp, len);
1407 g_warning(_("writing to %s failed.\n"), dest);
1412 if (rename(dest_bak, dest) < 0)
1413 FILE_OP_ERROR(dest_bak, "rename");
1426 if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
1427 g_warning(_("File copy from %s to %s failed.\n"), src, dest);
1430 if (rename(dest_bak, dest) < 0)
1431 FILE_OP_ERROR(dest_bak, "rename");
1442 gint copy_file(const gchar *src, const gchar *dest)
1444 FILE *src_fp, *dest_fp;
1447 gchar *dest_bak = NULL;
1448 gboolean err = FALSE;
1450 if ((src_fp = fopen(src, "r")) == NULL) {
1451 FILE_OP_ERROR(src, "fopen");
1454 if (is_file_exist(dest)) {
1455 dest_bak = g_strconcat(dest, ".bak", NULL);
1456 if (rename(dest, dest_bak) < 0) {
1457 FILE_OP_ERROR(dest, "rename");
1464 if ((dest_fp = fopen(dest, "w")) == NULL) {
1465 FILE_OP_ERROR(dest, "fopen");
1468 if (rename(dest_bak, dest) < 0)
1469 FILE_OP_ERROR(dest_bak, "rename");
1475 if (change_file_mode_rw(dest_fp, dest) < 0) {
1476 FILE_OP_ERROR(dest, "chmod");
1477 g_warning(_("can't change file mode\n"));
1480 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
1481 if (n_read < sizeof(buf) && ferror(src_fp))
1483 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
1484 g_warning(_("writing to %s failed.\n"), dest);
1489 if (rename(dest_bak, dest) < 0)
1490 FILE_OP_ERROR(dest_bak, "rename");
1497 if (ferror(src_fp)) {
1498 FILE_OP_ERROR(src, "fread");
1502 if (fclose(dest_fp) == EOF) {
1503 FILE_OP_ERROR(dest, "fclose");
1510 if (rename(dest_bak, dest) < 0)
1511 FILE_OP_ERROR(dest_bak, "rename");
1522 gint change_file_mode_rw(FILE *fp, const gchar *file)
1525 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
1527 return chmod(file, S_IRUSR|S_IWUSR);
1531 FILE *my_tmpfile(void)
1534 const gchar suffix[] = ".XXXXXX";
1535 const gchar *tmpdir;
1537 const gchar *progname;
1543 tmpdir = g_get_tmp_dir();
1544 tmplen = strlen(tmpdir);
1545 progname = g_get_prgname();
1546 proglen = strlen(progname);
1547 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
1550 memcpy(fname, tmpdir, tmplen);
1551 fname[tmplen] = G_DIR_SEPARATOR;
1552 memcpy(fname + tmplen + 1, progname, proglen);
1553 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
1555 fd = mkstemp(fname);
1561 fp = fdopen(fd, "w+b");
1566 #endif /* HAVE_MKSTEMP */
1571 gint execute_async(gchar *const argv[])
1575 if ((pid = fork()) < 0) {
1580 if (pid == 0) { /* child process */
1583 if ((gch_pid = fork()) < 0) {
1588 if (gch_pid == 0) { /* grandchild process */
1589 execvp(argv[0], argv);
1598 waitpid(pid, NULL, 0);
1603 gint execute_command_line(const gchar *cmdline)
1609 argv = strsplit_with_quote(cmdline, " ", 0);
1611 for (i = 0; argv[i] != NULL; i++) {
1612 gchar *str = argv[i];
1614 if (str[0] == '\'' || str[0] == '\"') {
1618 if (str[len - 1] == str[0]) {
1619 str[len - 1] = '\0';
1620 memmove(str, str + 1, len - 1);
1625 ret = execute_async(argv);
1631 gint open_uri(const gchar *uri, const gchar *cmdline)
1633 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
1634 gchar buf[BUFFSIZE];
1637 g_return_val_if_fail(uri != NULL, -1);
1640 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1641 !strchr(p + 2, '%'))
1642 g_snprintf(buf, sizeof(buf), cmdline, uri);
1645 g_warning(_("Open URI command line is invalid: `%s'"),
1647 g_snprintf(buf, sizeof(buf), default_cmdline, uri);
1650 execute_command_line(buf);
1655 time_t remote_tzoffset_sec(const gchar *zone)
1657 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
1663 time_t remoteoffset;
1665 strncpy(zone3, zone, 3);
1669 if (sscanf(zone, "%c%2d%2d", &c, &h, &m) == 3 &&
1670 (c == '+' || c == '-')) {
1671 remoteoffset = ((h * 60) + m) * 60;
1673 remoteoffset = -remoteoffset;
1674 } else if (!strncmp(zone, "UT" , 2) ||
1675 !strncmp(zone, "GMT", 2)) {
1677 } else if (strlen(zone3) == 3 &&
1678 (p = strstr(ustzstr, zone3)) != NULL &&
1679 (p - ustzstr) % 3 == 0) {
1680 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
1681 remoteoffset = iustz * 3600;
1682 } else if (strlen(zone3) == 1) {
1684 case 'Z': remoteoffset = 0; break;
1685 case 'A': remoteoffset = -1; break;
1686 case 'B': remoteoffset = -2; break;
1687 case 'C': remoteoffset = -3; break;
1688 case 'D': remoteoffset = -4; break;
1689 case 'E': remoteoffset = -5; break;
1690 case 'F': remoteoffset = -6; break;
1691 case 'G': remoteoffset = -7; break;
1692 case 'H': remoteoffset = -8; break;
1693 case 'I': remoteoffset = -9; break;
1694 case 'K': remoteoffset = -10; break; /* J is not used */
1695 case 'L': remoteoffset = -11; break;
1696 case 'M': remoteoffset = -12; break;
1697 case 'N': remoteoffset = 1; break;
1698 case 'O': remoteoffset = 2; break;
1699 case 'P': remoteoffset = 3; break;
1700 case 'Q': remoteoffset = 4; break;
1701 case 'R': remoteoffset = 5; break;
1702 case 'S': remoteoffset = 6; break;
1703 case 'T': remoteoffset = 7; break;
1704 case 'U': remoteoffset = 8; break;
1705 case 'V': remoteoffset = 9; break;
1706 case 'W': remoteoffset = 10; break;
1707 case 'X': remoteoffset = 11; break;
1708 case 'Y': remoteoffset = 12; break;
1709 default: remoteoffset = 0; break;
1711 remoteoffset = remoteoffset * 3600;
1714 return remoteoffset;
1717 time_t tzoffset_sec(time_t *now)
1723 lt = localtime(now);
1725 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1727 if (lt->tm_year < gmt.tm_year)
1729 else if (lt->tm_year > gmt.tm_year)
1731 else if (lt->tm_yday < gmt.tm_yday)
1733 else if (lt->tm_yday > gmt.tm_yday)
1736 if (off >= 24 * 60) /* should be impossible */
1737 off = 23 * 60 + 59; /* if not, insert silly value */
1738 if (off <= -24 * 60)
1739 off = -(23 * 60 + 59);
1748 /* calculate timezone offset */
1749 gchar *tzoffset(time_t *now)
1751 static gchar offset_string[6];
1757 lt = localtime(now);
1759 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1761 if (lt->tm_year < gmt.tm_year)
1763 else if (lt->tm_year > gmt.tm_year)
1765 else if (lt->tm_yday < gmt.tm_yday)
1767 else if (lt->tm_yday > gmt.tm_yday)
1775 if (off >= 24 * 60) /* should be impossible */
1776 off = 23 * 60 + 59; /* if not, insert silly value */
1778 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
1780 return offset_string;
1783 void get_rfc822_date(gchar *buf, gint len)
1787 gchar day[4], mon[4];
1788 gint dd, hh, mm, ss, yyyy;
1793 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
1794 day, mon, &dd, &hh, &mm, &ss, &yyyy);
1795 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
1796 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
1799 void debug_print(const gchar *format, ...)
1802 gchar buf[BUFFSIZE];
1804 if (!debug_mode) return;
1806 va_start(args, format);
1807 g_vsnprintf(buf, sizeof(buf), format, args);
1813 void log_print(const gchar *format, ...)
1816 gchar buf[BUFFSIZE];
1818 va_start(args, format);
1819 g_vsnprintf(buf, sizeof(buf), format, args);
1822 if (debug_mode) fputs(buf, stdout);
1823 log_window_append(buf, LOG_NORMAL);
1824 statusbar_puts_all(buf);
1827 void log_message(const gchar *format, ...)
1830 gchar buf[BUFFSIZE];
1832 va_start(args, format);
1833 g_vsnprintf(buf, sizeof(buf), format, args);
1836 if (debug_mode) g_message("%s", buf);
1837 log_window_append(buf, LOG_MSG);
1840 void log_warning(const gchar *format, ...)
1843 gchar buf[BUFFSIZE];
1845 va_start(args, format);
1846 g_vsnprintf(buf, sizeof(buf), format, args);
1849 g_warning("%s", buf);
1850 log_window_append(buf, LOG_WARN);
1853 void log_error(const gchar *format, ...)
1856 gchar buf[BUFFSIZE];
1858 va_start(args, format);
1859 g_vsnprintf(buf, sizeof(buf), format, args);
1862 g_warning("%s", buf);
1863 log_window_append(buf, LOG_ERROR);