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);
1031 gchar *get_rc_dir(void)
1033 static gchar *rc_dir = NULL;
1036 rc_dir = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S,
1042 gchar *get_news_cache_dir(void)
1044 static gchar *news_cache_dir = NULL;
1046 if (!news_cache_dir)
1047 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1048 NEWS_CACHE_DIR, NULL);
1050 return news_cache_dir;
1053 gchar *get_imap_cache_dir(void)
1055 static gchar *imap_cache_dir = NULL;
1057 if (!imap_cache_dir)
1058 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1059 IMAP_CACHE_DIR, NULL);
1061 return imap_cache_dir;
1064 gchar *get_mime_tmp_dir(void)
1066 static gchar *mime_tmp_dir = NULL;
1069 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1070 MIME_TMP_DIR, NULL);
1072 return mime_tmp_dir;
1075 gchar *get_tmp_file(void)
1077 static gchar *tmp_file = NULL;
1080 tmp_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1086 gchar *get_domain_name(void)
1088 static gchar *domain_name = NULL;
1091 gchar buf[BUFFSIZE] = "";
1093 if (gethostname(buf, sizeof(buf)) < 0) {
1094 perror("gethostname");
1095 strcpy(buf, "unknown");
1098 domain_name = g_strdup(buf);
1104 off_t get_file_size(const gchar *file)
1108 if (stat(file, &s) < 0) {
1109 FILE_OP_ERROR(file, "stat");
1116 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1120 if (stat(file, &s) < 0) {
1121 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1125 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1131 gboolean is_dir_exist(const gchar *dir)
1135 if (stat(dir, &s) < 0) {
1136 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1140 if (S_ISDIR(s.st_mode))
1146 gint change_dir(const gchar *dir)
1148 gchar *prevdir = NULL;
1151 prevdir = g_get_current_dir();
1153 if (chdir(dir) < 0) {
1154 FILE_OP_ERROR(dir, "chdir");
1155 if (debug_mode) g_free(prevdir);
1157 } else if (debug_mode) {
1160 cwd = g_get_current_dir();
1161 if (strcmp(prevdir, cwd) != 0)
1162 g_print("current dir: %s\n", cwd);
1170 gint make_dir_hier(const gchar *dir)
1175 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1176 parent_dir = g_strndup(dir, p - dir);
1177 if (*parent_dir != '\0') {
1178 if (!is_dir_exist(parent_dir)) {
1179 if (mkdir(parent_dir, S_IRWXU) < 0) {
1180 FILE_OP_ERROR(parent_dir, "mkdir");
1184 if (chmod(parent_dir, S_IRWXU) < 0)
1185 FILE_OP_ERROR(parent_dir, "chmod");
1190 if (!is_dir_exist(dir)) {
1191 if (mkdir(dir, S_IRWXU) < 0) {
1192 FILE_OP_ERROR(dir, "mkdir");
1195 if (chmod(dir, S_IRWXU) < 0)
1196 FILE_OP_ERROR(dir, "chmod");
1202 gint remove_all_files(const gchar *dir)
1208 prev_dir = g_get_current_dir();
1210 if (chdir(dir) < 0) {
1211 FILE_OP_ERROR(dir, "chdir");
1215 if ((dp = opendir(".")) == NULL) {
1216 FILE_OP_ERROR(dir, "opendir");
1220 while ((d = readdir(dp)) != NULL) {
1221 if (!strcmp(d->d_name, ".") ||
1222 !strcmp(d->d_name, ".."))
1225 if (unlink(d->d_name) < 0)
1226 FILE_OP_ERROR(d->d_name, "unlink");
1231 if (chdir(prev_dir) < 0) {
1232 FILE_OP_ERROR(prev_dir, "chdir");
1242 gint remove_dir_recursive(const gchar *dir)
1249 //g_print("dir = %s\n", dir);
1251 if (stat(dir, &s) < 0) {
1252 FILE_OP_ERROR(dir, "stat");
1253 if (ENOENT == errno) return 0;
1257 if (!S_ISDIR(s.st_mode)) {
1258 if (unlink(dir) < 0) {
1259 FILE_OP_ERROR(dir, "unlink");
1266 prev_dir = g_get_current_dir();
1267 //g_print("prev_dir = %s\n", prev_dir);
1269 if (!path_cmp(prev_dir, dir)) {
1271 if (chdir("..") < 0) {
1272 FILE_OP_ERROR(dir, "chdir");
1275 prev_dir = g_get_current_dir();
1278 if (chdir(dir) < 0) {
1279 FILE_OP_ERROR(dir, "chdir");
1284 if ((dp = opendir(".")) == NULL) {
1285 FILE_OP_ERROR(dir, "opendir");
1291 /* remove all files in the directory */
1292 while ((d = readdir(dp)) != NULL) {
1293 if (!strcmp(d->d_name, ".") ||
1294 !strcmp(d->d_name, ".."))
1297 if (stat(d->d_name, &s) < 0) {
1298 FILE_OP_ERROR(d->d_name, "stat");
1302 //g_print("removing %s\n", d->d_name);
1304 if (S_ISDIR(s.st_mode)) {
1305 if (remove_dir_recursive(d->d_name) < 0) {
1306 g_warning("can't remove directory\n");
1310 if (unlink(d->d_name) < 0)
1311 FILE_OP_ERROR(d->d_name, "unlink");
1317 if (chdir(prev_dir) < 0) {
1318 FILE_OP_ERROR(prev_dir, "chdir");
1325 if (rmdir(dir) < 0) {
1326 FILE_OP_ERROR(dir, "rmdir");
1334 /* this seems to be slower than the stdio version... */
1335 gint copy_file(const gchar *src, const gchar *dest)
1337 gint src_fd, dest_fd;
1341 gchar *dest_bak = NULL;
1343 if ((src_fd = open(src, O_RDONLY)) < 0) {
1344 FILE_OP_ERROR(src, "open");
1348 if (is_file_exist(dest)) {
1349 dest_bak = g_strconcat(dest, ".bak", NULL);
1350 if (rename(dest, dest_bak) < 0) {
1351 FILE_OP_ERROR(dest, "rename");
1358 if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
1359 FILE_OP_ERROR(dest, "open");
1362 if (rename(dest_bak, dest) < 0)
1363 FILE_OP_ERROR(dest_bak, "rename");
1369 while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
1374 n_write = write(dest_fd, bufp, len);
1376 g_warning(_("writing to %s failed.\n"), dest);
1381 if (rename(dest_bak, dest) < 0)
1382 FILE_OP_ERROR(dest_bak, "rename");
1395 if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
1396 g_warning(_("File copy from %s to %s failed.\n"), src, dest);
1399 if (rename(dest_bak, dest) < 0)
1400 FILE_OP_ERROR(dest_bak, "rename");
1411 gint copy_file(const gchar *src, const gchar *dest)
1413 FILE *src_fp, *dest_fp;
1416 gchar *dest_bak = NULL;
1417 gboolean err = FALSE;
1419 if ((src_fp = fopen(src, "r")) == NULL) {
1420 FILE_OP_ERROR(src, "fopen");
1423 if (is_file_exist(dest)) {
1424 dest_bak = g_strconcat(dest, ".bak", NULL);
1425 if (rename(dest, dest_bak) < 0) {
1426 FILE_OP_ERROR(dest, "rename");
1433 if ((dest_fp = fopen(dest, "w")) == NULL) {
1434 FILE_OP_ERROR(dest, "fopen");
1437 if (rename(dest_bak, dest) < 0)
1438 FILE_OP_ERROR(dest_bak, "rename");
1444 if (change_file_mode_rw(dest_fp, dest) < 0) {
1445 FILE_OP_ERROR(dest, "chmod");
1446 g_warning(_("can't change file mode\n"));
1449 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
1450 if (n_read < sizeof(buf) && ferror(src_fp))
1452 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
1453 g_warning(_("writing to %s failed.\n"), dest);
1458 if (rename(dest_bak, dest) < 0)
1459 FILE_OP_ERROR(dest_bak, "rename");
1466 if (ferror(src_fp)) {
1467 FILE_OP_ERROR(src, "fread");
1471 if (fclose(dest_fp) == EOF) {
1472 FILE_OP_ERROR(dest, "fclose");
1479 if (rename(dest_bak, dest) < 0)
1480 FILE_OP_ERROR(dest_bak, "rename");
1491 gint change_file_mode_rw(FILE *fp, const gchar *file)
1494 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
1496 return chmod(file, S_IRUSR|S_IWUSR);
1500 FILE *my_tmpfile(void)
1503 const gchar suffix[] = ".XXXXXX";
1504 const gchar *tmpdir;
1506 const gchar *progname;
1512 tmpdir = g_get_tmp_dir();
1513 tmplen = strlen(tmpdir);
1514 progname = g_get_prgname();
1515 proglen = strlen(progname);
1516 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
1519 memcpy(fname, tmpdir, tmplen);
1520 fname[tmplen] = G_DIR_SEPARATOR;
1521 memcpy(fname + tmplen + 1, progname, proglen);
1522 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
1524 fd = mkstemp(fname);
1530 fp = fdopen(fd, "w+b");
1535 #endif /* HAVE_MKSTEMP */
1540 gint execute_async(gchar *const argv[])
1544 if ((pid = fork()) < 0) {
1549 if (pid == 0) { /* child process */
1552 if ((gch_pid = fork()) < 0) {
1557 if (gch_pid == 0) { /* grandchild process */
1558 execvp(argv[0], argv);
1567 waitpid(pid, NULL, 0);
1572 gint execute_command_line(const gchar *cmdline)
1578 argv = strsplit_with_quote(cmdline, " ", 0);
1580 for (i = 0; argv[i] != NULL; i++) {
1581 gchar *str = argv[i];
1583 if (str[0] == '\'' || str[0] == '\"') {
1587 if (str[len - 1] == str[0]) {
1588 str[len - 1] = '\0';
1589 memmove(str, str + 1, len - 1);
1594 ret = execute_async(argv);
1600 gint open_uri(const gchar *uri, const gchar *cmdline)
1602 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
1603 gchar buf[BUFFSIZE];
1606 g_return_val_if_fail(uri != NULL, -1);
1609 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1610 !strchr(p + 2, '%'))
1611 g_snprintf(buf, sizeof(buf), cmdline, uri);
1614 g_warning(_("Open URI command line is invalid: `%s'"),
1616 g_snprintf(buf, sizeof(buf), default_cmdline, uri);
1619 execute_command_line(buf);
1624 time_t remote_tzoffset_sec(const gchar *zone)
1626 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
1632 time_t remoteoffset;
1634 strncpy(zone3, zone, 3);
1638 if (sscanf(zone, "%c%2d%2d", &c, &h, &m) == 3 &&
1639 (c == '+' || c == '-')) {
1640 remoteoffset = ((h * 60) + m) * 60;
1642 remoteoffset = -remoteoffset;
1643 } else if (!strncmp(zone, "UT" , 2) ||
1644 !strncmp(zone, "GMT", 2)) {
1646 } else if (strlen(zone3) == 3 &&
1647 (p = strstr(ustzstr, zone3)) != NULL &&
1648 (p - ustzstr) % 3 == 0) {
1649 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
1650 remoteoffset = iustz * 3600;
1651 } else if (strlen(zone3) == 1) {
1653 case 'Z': remoteoffset = 0; break;
1654 case 'A': remoteoffset = -1; break;
1655 case 'B': remoteoffset = -2; break;
1656 case 'C': remoteoffset = -3; break;
1657 case 'D': remoteoffset = -4; break;
1658 case 'E': remoteoffset = -5; break;
1659 case 'F': remoteoffset = -6; break;
1660 case 'G': remoteoffset = -7; break;
1661 case 'H': remoteoffset = -8; break;
1662 case 'I': remoteoffset = -9; break;
1663 case 'K': remoteoffset = -10; break; /* J is not used */
1664 case 'L': remoteoffset = -11; break;
1665 case 'M': remoteoffset = -12; break;
1666 case 'N': remoteoffset = 1; break;
1667 case 'O': remoteoffset = 2; break;
1668 case 'P': remoteoffset = 3; break;
1669 case 'Q': remoteoffset = 4; break;
1670 case 'R': remoteoffset = 5; break;
1671 case 'S': remoteoffset = 6; break;
1672 case 'T': remoteoffset = 7; break;
1673 case 'U': remoteoffset = 8; break;
1674 case 'V': remoteoffset = 9; break;
1675 case 'W': remoteoffset = 10; break;
1676 case 'X': remoteoffset = 11; break;
1677 case 'Y': remoteoffset = 12; break;
1678 default: remoteoffset = 0; break;
1680 remoteoffset = remoteoffset * 3600;
1683 return remoteoffset;
1686 time_t tzoffset_sec(time_t *now)
1692 lt = localtime(now);
1694 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1696 if (lt->tm_year < gmt.tm_year)
1698 else if (lt->tm_year > gmt.tm_year)
1700 else if (lt->tm_yday < gmt.tm_yday)
1702 else if (lt->tm_yday > gmt.tm_yday)
1705 if (off >= 24 * 60) /* should be impossible */
1706 off = 23 * 60 + 59; /* if not, insert silly value */
1707 if (off <= -24 * 60)
1708 off = -(23 * 60 + 59);
1717 /* calculate timezone offset */
1718 gchar *tzoffset(time_t *now)
1720 static gchar offset_string[6];
1726 lt = localtime(now);
1728 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1730 if (lt->tm_year < gmt.tm_year)
1732 else if (lt->tm_year > gmt.tm_year)
1734 else if (lt->tm_yday < gmt.tm_yday)
1736 else if (lt->tm_yday > gmt.tm_yday)
1744 if (off >= 24 * 60) /* should be impossible */
1745 off = 23 * 60 + 59; /* if not, insert silly value */
1747 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
1749 return offset_string;
1752 void get_rfc822_date(gchar *buf, gint len)
1756 gchar day[4], mon[4];
1757 gint dd, hh, mm, ss, yyyy;
1762 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
1763 day, mon, &dd, &hh, &mm, &ss, &yyyy);
1764 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
1765 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
1768 void debug_print(const gchar *format, ...)
1771 gchar buf[BUFFSIZE];
1773 if (!debug_mode) return;
1775 va_start(args, format);
1776 g_vsnprintf(buf, sizeof(buf), format, args);
1782 void log_print(const gchar *format, ...)
1785 gchar buf[BUFFSIZE];
1787 va_start(args, format);
1788 g_vsnprintf(buf, sizeof(buf), format, args);
1791 if (debug_mode) fputs(buf, stdout);
1792 log_window_append(buf, LOG_NORMAL);
1793 statusbar_puts_all(buf);
1796 void log_message(const gchar *format, ...)
1799 gchar buf[BUFFSIZE];
1801 va_start(args, format);
1802 g_vsnprintf(buf, sizeof(buf), format, args);
1805 if (debug_mode) g_message("%s", buf);
1806 log_window_append(buf, LOG_MSG);
1809 void log_warning(const gchar *format, ...)
1812 gchar buf[BUFFSIZE];
1814 va_start(args, format);
1815 g_vsnprintf(buf, sizeof(buf), format, args);
1818 g_warning("%s", buf);
1819 log_window_append(buf, LOG_WARN);
1822 void log_error(const gchar *format, ...)
1825 gchar buf[BUFFSIZE];
1827 va_start(args, format);
1828 g_vsnprintf(buf, sizeof(buf), format, args);
1831 g_warning("%s", buf);
1832 log_window_append(buf, LOG_ERROR);