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 subst_char(gchar *str, gchar orig, gchar subst)
831 register gchar *p = str;
840 gboolean is_header_line(const gchar *str)
842 if (str[0] == ':') return FALSE;
844 while (*str != '\0' && *str != ' ') {
853 gboolean is_ascii_str(const guchar *str)
855 while (*str != '\0') {
856 if (*str != '\t' && *str != ' ' &&
857 *str != '\r' && *str != '\n' &&
858 (*str < 32 || *str >= 127))
866 gint get_quote_level(const gchar *str)
868 size_t firstquotepos;
869 size_t lastquotepos = -1;
870 const gchar *p = str;
872 gint quotelevel = -1;
875 /* speed up line processing by only searching to the last '>' */
876 if ((pos = strchr(str, '>')) != NULL) {
877 firstquotepos = pos - str;
878 lastquotepos = strrchr(str, '>') - str + 1;
880 /* skip a line if it contains a '<' before the initial '>' */
881 if (memchr(str, '<', pos - str) != NULL)
886 while (i < lastquotepos) {
887 while (i < lastquotepos) {
888 if (isspace(*p) || (*p == '\t')) {
894 if (i >= lastquotepos)
899 else if ((*p != '-') && !isspace(*p) && (i < lastquotepos)) {
900 /* any characters are allowed except '-' and space */
901 while ((*p != '-') && (*p != '>') && !isspace(*p) &&
902 (i < lastquotepos)) {
908 else if ((i >= lastquotepos) || isspace(*p))
919 GList *uri_list_extract_filenames(const gchar *uri_list)
921 GList *result = NULL;
929 while (isspace(*p)) p++;
930 if (!strncmp(p, "file:", 5)) {
933 while (*q && *q != '\n' && *q != '\r') q++;
937 while (q > p && isspace(*q)) q--;
938 file = g_malloc(q - p + 2);
939 strncpy(file, p, q - p + 1);
940 file[q - p + 1] = '\0';
941 result = g_list_append(result,file);
952 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
954 register guint haystack_len, needle_len;
955 gboolean in_squote = FALSE, in_dquote = FALSE;
957 haystack_len = strlen(haystack);
958 needle_len = strlen(needle);
960 if (haystack_len < needle_len || needle_len == 0)
963 while (haystack_len >= needle_len) {
964 if (!in_squote && !in_dquote &&
965 !strncmp(haystack, needle, needle_len))
966 return (gchar *)haystack;
968 /* 'foo"bar"' -> foo"bar"
969 "foo'bar'" -> foo'bar' */
970 if (*haystack == '\'') {
975 } else if (*haystack == '\"') {
989 /* this fuction was taken from gstrfuncs.c in glib. */
990 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
993 GSList *string_list = NULL, *slist;
994 gchar **str_array, *s;
997 g_return_val_if_fail(str != NULL, NULL);
998 g_return_val_if_fail(delim != NULL, NULL);
1001 max_tokens = G_MAXINT;
1003 s = strstr_with_skip_quote(str, delim);
1005 guint delimiter_len = strlen(delim);
1012 new_str = g_new(gchar, len + 1);
1013 strncpy(new_str, str, len);
1015 string_list = g_slist_prepend(string_list, new_str);
1017 str = s + delimiter_len;
1018 s = strstr_with_skip_quote(str, delim);
1019 } while (--max_tokens && s);
1024 string_list = g_slist_prepend(string_list, g_strdup(str));
1027 str_array = g_new(gchar*, n);
1031 str_array[i--] = NULL;
1032 for (slist = string_list; slist; slist = slist->next)
1033 str_array[i--] = slist->data;
1035 g_slist_free(string_list);
1041 * We need this wrapper around g_get_home_dir(), so that
1042 * we can fix some Windoze things here. Should be done in glibc of course
1043 * but as long as we are not able to do our own extensions to glibc, we do
1046 gchar *get_home_dir(void)
1048 #if HAVE_DOSISH_SYSTEM
1049 static gchar *home_dir;
1052 home_dir = read_w32_registry_string(NULL,
1053 "Software\\Sylpheed", "HomeDir" );
1054 if (!home_dir || !*home_dir) {
1055 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1056 const char *s = g_get_home_dir();
1058 home_dir = g_strdup (s);
1060 if (!home_dir || !*home_dir)
1061 home_dir = g_strdup ("c:\\sylpheed");
1063 debug_print("initialized home_dir to `%s'\n", home_dir);
1066 #else /* standard glib */
1067 return g_get_home_dir();
1071 gchar *get_rc_dir(void)
1073 static gchar *rc_dir = NULL;
1076 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1082 gchar *get_news_cache_dir(void)
1084 static gchar *news_cache_dir = NULL;
1086 if (!news_cache_dir)
1087 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1088 NEWS_CACHE_DIR, NULL);
1090 return news_cache_dir;
1093 gchar *get_imap_cache_dir(void)
1095 static gchar *imap_cache_dir = NULL;
1097 if (!imap_cache_dir)
1098 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1099 IMAP_CACHE_DIR, NULL);
1101 return imap_cache_dir;
1104 gchar *get_mime_tmp_dir(void)
1106 static gchar *mime_tmp_dir = NULL;
1109 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1110 MIME_TMP_DIR, NULL);
1112 return mime_tmp_dir;
1115 gchar *get_tmp_file(void)
1117 static gchar *tmp_file = NULL;
1120 tmp_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1126 gchar *get_domain_name(void)
1128 static gchar *domain_name = NULL;
1131 gchar buf[BUFFSIZE] = "";
1133 if (gethostname(buf, sizeof(buf)) < 0) {
1134 perror("gethostname");
1135 strcpy(buf, "unknown");
1138 domain_name = g_strdup(buf);
1144 off_t get_file_size(const gchar *file)
1148 if (stat(file, &s) < 0) {
1149 FILE_OP_ERROR(file, "stat");
1156 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1160 if (stat(file, &s) < 0) {
1161 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1165 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1171 gboolean is_dir_exist(const gchar *dir)
1175 if (stat(dir, &s) < 0) {
1176 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1180 if (S_ISDIR(s.st_mode))
1186 gint change_dir(const gchar *dir)
1188 gchar *prevdir = NULL;
1191 prevdir = g_get_current_dir();
1193 if (chdir(dir) < 0) {
1194 FILE_OP_ERROR(dir, "chdir");
1195 if (debug_mode) g_free(prevdir);
1197 } else if (debug_mode) {
1200 cwd = g_get_current_dir();
1201 if (strcmp(prevdir, cwd) != 0)
1202 g_print("current dir: %s\n", cwd);
1210 gint make_dir_hier(const gchar *dir)
1215 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1216 parent_dir = g_strndup(dir, p - dir);
1217 if (*parent_dir != '\0') {
1218 if (!is_dir_exist(parent_dir)) {
1219 if (mkdir(parent_dir, S_IRWXU) < 0) {
1220 FILE_OP_ERROR(parent_dir, "mkdir");
1224 if (chmod(parent_dir, S_IRWXU) < 0)
1225 FILE_OP_ERROR(parent_dir, "chmod");
1230 if (!is_dir_exist(dir)) {
1231 if (mkdir(dir, S_IRWXU) < 0) {
1232 FILE_OP_ERROR(dir, "mkdir");
1235 if (chmod(dir, S_IRWXU) < 0)
1236 FILE_OP_ERROR(dir, "chmod");
1242 gint remove_all_files(const gchar *dir)
1248 prev_dir = g_get_current_dir();
1250 if (chdir(dir) < 0) {
1251 FILE_OP_ERROR(dir, "chdir");
1255 if ((dp = opendir(".")) == NULL) {
1256 FILE_OP_ERROR(dir, "opendir");
1260 while ((d = readdir(dp)) != NULL) {
1261 if (!strcmp(d->d_name, ".") ||
1262 !strcmp(d->d_name, ".."))
1265 if (unlink(d->d_name) < 0)
1266 FILE_OP_ERROR(d->d_name, "unlink");
1271 if (chdir(prev_dir) < 0) {
1272 FILE_OP_ERROR(prev_dir, "chdir");
1282 gint remove_dir_recursive(const gchar *dir)
1289 //g_print("dir = %s\n", dir);
1291 if (stat(dir, &s) < 0) {
1292 FILE_OP_ERROR(dir, "stat");
1293 if (ENOENT == errno) return 0;
1297 if (!S_ISDIR(s.st_mode)) {
1298 if (unlink(dir) < 0) {
1299 FILE_OP_ERROR(dir, "unlink");
1306 prev_dir = g_get_current_dir();
1307 //g_print("prev_dir = %s\n", prev_dir);
1309 if (!path_cmp(prev_dir, dir)) {
1311 if (chdir("..") < 0) {
1312 FILE_OP_ERROR(dir, "chdir");
1315 prev_dir = g_get_current_dir();
1318 if (chdir(dir) < 0) {
1319 FILE_OP_ERROR(dir, "chdir");
1324 if ((dp = opendir(".")) == NULL) {
1325 FILE_OP_ERROR(dir, "opendir");
1331 /* remove all files in the directory */
1332 while ((d = readdir(dp)) != NULL) {
1333 if (!strcmp(d->d_name, ".") ||
1334 !strcmp(d->d_name, ".."))
1337 if (stat(d->d_name, &s) < 0) {
1338 FILE_OP_ERROR(d->d_name, "stat");
1342 //g_print("removing %s\n", d->d_name);
1344 if (S_ISDIR(s.st_mode)) {
1345 if (remove_dir_recursive(d->d_name) < 0) {
1346 g_warning("can't remove directory\n");
1350 if (unlink(d->d_name) < 0)
1351 FILE_OP_ERROR(d->d_name, "unlink");
1357 if (chdir(prev_dir) < 0) {
1358 FILE_OP_ERROR(prev_dir, "chdir");
1365 if (rmdir(dir) < 0) {
1366 FILE_OP_ERROR(dir, "rmdir");
1374 /* this seems to be slower than the stdio version... */
1375 gint copy_file(const gchar *src, const gchar *dest)
1377 gint src_fd, dest_fd;
1381 gchar *dest_bak = NULL;
1383 if ((src_fd = open(src, O_RDONLY)) < 0) {
1384 FILE_OP_ERROR(src, "open");
1388 if (is_file_exist(dest)) {
1389 dest_bak = g_strconcat(dest, ".bak", NULL);
1390 if (rename(dest, dest_bak) < 0) {
1391 FILE_OP_ERROR(dest, "rename");
1398 if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
1399 FILE_OP_ERROR(dest, "open");
1402 if (rename(dest_bak, dest) < 0)
1403 FILE_OP_ERROR(dest_bak, "rename");
1409 while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
1414 n_write = write(dest_fd, bufp, len);
1416 g_warning(_("writing to %s failed.\n"), dest);
1421 if (rename(dest_bak, dest) < 0)
1422 FILE_OP_ERROR(dest_bak, "rename");
1435 if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
1436 g_warning(_("File copy from %s to %s failed.\n"), src, dest);
1439 if (rename(dest_bak, dest) < 0)
1440 FILE_OP_ERROR(dest_bak, "rename");
1451 gint copy_file(const gchar *src, const gchar *dest)
1453 FILE *src_fp, *dest_fp;
1456 gchar *dest_bak = NULL;
1457 gboolean err = FALSE;
1459 if ((src_fp = fopen(src, "r")) == NULL) {
1460 FILE_OP_ERROR(src, "fopen");
1463 if (is_file_exist(dest)) {
1464 dest_bak = g_strconcat(dest, ".bak", NULL);
1465 if (rename(dest, dest_bak) < 0) {
1466 FILE_OP_ERROR(dest, "rename");
1473 if ((dest_fp = fopen(dest, "w")) == NULL) {
1474 FILE_OP_ERROR(dest, "fopen");
1477 if (rename(dest_bak, dest) < 0)
1478 FILE_OP_ERROR(dest_bak, "rename");
1484 if (change_file_mode_rw(dest_fp, dest) < 0) {
1485 FILE_OP_ERROR(dest, "chmod");
1486 g_warning(_("can't change file mode\n"));
1489 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
1490 if (n_read < sizeof(buf) && ferror(src_fp))
1492 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
1493 g_warning(_("writing to %s failed.\n"), dest);
1498 if (rename(dest_bak, dest) < 0)
1499 FILE_OP_ERROR(dest_bak, "rename");
1506 if (ferror(src_fp)) {
1507 FILE_OP_ERROR(src, "fread");
1511 if (fclose(dest_fp) == EOF) {
1512 FILE_OP_ERROR(dest, "fclose");
1519 if (rename(dest_bak, dest) < 0)
1520 FILE_OP_ERROR(dest_bak, "rename");
1531 gint change_file_mode_rw(FILE *fp, const gchar *file)
1534 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
1536 return chmod(file, S_IRUSR|S_IWUSR);
1540 FILE *my_tmpfile(void)
1543 const gchar suffix[] = ".XXXXXX";
1544 const gchar *tmpdir;
1546 const gchar *progname;
1552 tmpdir = g_get_tmp_dir();
1553 tmplen = strlen(tmpdir);
1554 progname = g_get_prgname();
1555 proglen = strlen(progname);
1556 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
1559 memcpy(fname, tmpdir, tmplen);
1560 fname[tmplen] = G_DIR_SEPARATOR;
1561 memcpy(fname + tmplen + 1, progname, proglen);
1562 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
1564 fd = mkstemp(fname);
1570 fp = fdopen(fd, "w+b");
1575 #endif /* HAVE_MKSTEMP */
1580 gint execute_async(gchar *const argv[])
1584 if ((pid = fork()) < 0) {
1589 if (pid == 0) { /* child process */
1592 if ((gch_pid = fork()) < 0) {
1597 if (gch_pid == 0) { /* grandchild process */
1598 execvp(argv[0], argv);
1607 waitpid(pid, NULL, 0);
1612 gint execute_command_line(const gchar *cmdline)
1618 argv = strsplit_with_quote(cmdline, " ", 0);
1620 for (i = 0; argv[i] != NULL; i++) {
1621 gchar *str = argv[i];
1623 if (str[0] == '\'' || str[0] == '\"') {
1627 if (str[len - 1] == str[0]) {
1628 str[len - 1] = '\0';
1629 memmove(str, str + 1, len - 1);
1634 ret = execute_async(argv);
1640 gint open_uri(const gchar *uri, const gchar *cmdline)
1642 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
1643 gchar buf[BUFFSIZE];
1646 g_return_val_if_fail(uri != NULL, -1);
1649 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1650 !strchr(p + 2, '%'))
1651 g_snprintf(buf, sizeof(buf), cmdline, uri);
1654 g_warning(_("Open URI command line is invalid: `%s'"),
1656 g_snprintf(buf, sizeof(buf), default_cmdline, uri);
1659 execute_command_line(buf);
1664 time_t remote_tzoffset_sec(const gchar *zone)
1666 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
1672 time_t remoteoffset;
1674 strncpy(zone3, zone, 3);
1678 if (sscanf(zone, "%c%2d%2d", &c, &h, &m) == 3 &&
1679 (c == '+' || c == '-')) {
1680 remoteoffset = ((h * 60) + m) * 60;
1682 remoteoffset = -remoteoffset;
1683 } else if (!strncmp(zone, "UT" , 2) ||
1684 !strncmp(zone, "GMT", 2)) {
1686 } else if (strlen(zone3) == 3 &&
1687 (p = strstr(ustzstr, zone3)) != NULL &&
1688 (p - ustzstr) % 3 == 0) {
1689 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
1690 remoteoffset = iustz * 3600;
1691 } else if (strlen(zone3) == 1) {
1693 case 'Z': remoteoffset = 0; break;
1694 case 'A': remoteoffset = -1; break;
1695 case 'B': remoteoffset = -2; break;
1696 case 'C': remoteoffset = -3; break;
1697 case 'D': remoteoffset = -4; break;
1698 case 'E': remoteoffset = -5; break;
1699 case 'F': remoteoffset = -6; break;
1700 case 'G': remoteoffset = -7; break;
1701 case 'H': remoteoffset = -8; break;
1702 case 'I': remoteoffset = -9; break;
1703 case 'K': remoteoffset = -10; break; /* J is not used */
1704 case 'L': remoteoffset = -11; break;
1705 case 'M': remoteoffset = -12; break;
1706 case 'N': remoteoffset = 1; break;
1707 case 'O': remoteoffset = 2; break;
1708 case 'P': remoteoffset = 3; break;
1709 case 'Q': remoteoffset = 4; break;
1710 case 'R': remoteoffset = 5; break;
1711 case 'S': remoteoffset = 6; break;
1712 case 'T': remoteoffset = 7; break;
1713 case 'U': remoteoffset = 8; break;
1714 case 'V': remoteoffset = 9; break;
1715 case 'W': remoteoffset = 10; break;
1716 case 'X': remoteoffset = 11; break;
1717 case 'Y': remoteoffset = 12; break;
1718 default: remoteoffset = 0; break;
1720 remoteoffset = remoteoffset * 3600;
1723 return remoteoffset;
1726 time_t tzoffset_sec(time_t *now)
1732 lt = localtime(now);
1734 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1736 if (lt->tm_year < gmt.tm_year)
1738 else if (lt->tm_year > gmt.tm_year)
1740 else if (lt->tm_yday < gmt.tm_yday)
1742 else if (lt->tm_yday > gmt.tm_yday)
1745 if (off >= 24 * 60) /* should be impossible */
1746 off = 23 * 60 + 59; /* if not, insert silly value */
1747 if (off <= -24 * 60)
1748 off = -(23 * 60 + 59);
1757 /* calculate timezone offset */
1758 gchar *tzoffset(time_t *now)
1760 static gchar offset_string[6];
1766 lt = localtime(now);
1768 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1770 if (lt->tm_year < gmt.tm_year)
1772 else if (lt->tm_year > gmt.tm_year)
1774 else if (lt->tm_yday < gmt.tm_yday)
1776 else if (lt->tm_yday > gmt.tm_yday)
1784 if (off >= 24 * 60) /* should be impossible */
1785 off = 23 * 60 + 59; /* if not, insert silly value */
1787 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
1789 return offset_string;
1792 void get_rfc822_date(gchar *buf, gint len)
1796 gchar day[4], mon[4];
1797 gint dd, hh, mm, ss, yyyy;
1802 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
1803 day, mon, &dd, &hh, &mm, &ss, &yyyy);
1804 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
1805 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
1808 void debug_print(const gchar *format, ...)
1811 gchar buf[BUFFSIZE];
1813 if (!debug_mode) return;
1815 va_start(args, format);
1816 g_vsnprintf(buf, sizeof(buf), format, args);
1822 void log_print(const gchar *format, ...)
1825 gchar buf[BUFFSIZE];
1827 va_start(args, format);
1828 g_vsnprintf(buf, sizeof(buf), format, args);
1831 if (debug_mode) fputs(buf, stdout);
1832 log_window_append(buf, LOG_NORMAL);
1833 statusbar_puts_all(buf);
1836 void log_message(const gchar *format, ...)
1839 gchar buf[BUFFSIZE];
1841 va_start(args, format);
1842 g_vsnprintf(buf, sizeof(buf), format, args);
1845 if (debug_mode) g_message("%s", buf);
1846 log_window_append(buf, LOG_MSG);
1849 void log_warning(const gchar *format, ...)
1852 gchar buf[BUFFSIZE];
1854 va_start(args, format);
1855 g_vsnprintf(buf, sizeof(buf), format, args);
1858 g_warning("%s", buf);
1859 log_window_append(buf, LOG_WARN);
1862 void log_error(const gchar *format, ...)
1865 gchar buf[BUFFSIZE];
1867 va_start(args, format);
1868 g_vsnprintf(buf, sizeof(buf), format, args);
1871 g_warning("%s", buf);
1872 log_window_append(buf, LOG_ERROR);