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 not lower than 11 characters length */
111 gchar *itos_buf(gchar *nstr, gint n)
113 g_snprintf(nstr, 11, "%d", n);
117 /* convert integer into string */
120 static gchar nstr[11];
122 return itos_buf(nstr, n);
125 gchar *to_human_readable(off_t size)
131 for (count = 0; count < 3; count++) {
132 if (size / div < 1024)
139 case 0: g_snprintf(str, sizeof(str), "%dB", (gint)size); break;
140 case 1: g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / div);
142 case 2: g_snprintf(str, sizeof(str), "%.1fMB", (gfloat)size / div);
145 g_snprintf(str, sizeof(str), "%.1fGB", (gfloat)size / div);
152 /* strcmp with NULL-checking */
153 gint strcmp2(const gchar *s1, const gchar *s2)
155 if (s1 == NULL || s2 == NULL)
158 return strcmp(s1, s2);
162 gint path_cmp(const gchar *s1, const gchar *s2)
166 if (s1 == NULL || s2 == NULL) return -1;
167 if (*s1 == '\0' || *s2 == '\0') return -1;
172 if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
173 if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
175 return strncmp(s1, s2, MAX(len1, len2));
178 /* remove trailing return code */
179 gchar *strretchomp(gchar *str)
183 if (!*str) return str;
185 for (s = str + strlen(str) - 1;
186 s >= str && (*s == '\n' || *s == '\r');
193 /* remove trailing character */
194 gchar *strtailchomp(gchar *str, gchar tail_char)
198 if (!*str) return str;
199 if (tail_char == '\0') return str;
201 for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
208 /* Similar to `strstr' but this function ignores the case of both strings. */
209 gchar *strcasestr(const gchar *haystack, const gchar *needle)
211 register size_t haystack_len, needle_len;
213 haystack_len = strlen(haystack);
214 needle_len = strlen(needle);
216 if (haystack_len < needle_len || needle_len == 0)
219 while (haystack_len >= needle_len) {
220 if (!strncasecmp(haystack, needle, needle_len))
221 return (gchar *)haystack;
231 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
232 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
246 /* don't do zero fill */
251 int iswalnum(wint_t wc)
253 return isalnum((int)wc);
258 int iswspace(wint_t wc)
260 return isspace((int)wc);
265 wint_t towlower(wint_t wc)
267 if (wc >= L'A' && wc <= L'Z')
268 return wc + L'a' - L'A';
275 size_t wcslen(const wchar_t *s)
287 /* Copy SRC to DEST. */
288 wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
296 } while (c != L'\0');
303 /* Copy no more than N wide-characters of SRC to DEST. */
304 wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
314 } while (c != L'\0');
325 /* Duplicate S, returning an identical malloc'd string. */
326 wchar_t *wcsdup(const wchar_t *s)
331 new_str = g_new(wchar_t, wcslen(s) + 1);
339 /* Duplicate no more than N wide-characters of S,
340 returning an identical malloc'd string. */
341 wchar_t *wcsndup(const wchar_t *s, size_t n)
346 new_str = g_new(wchar_t, n + 1);
347 wcsncpy(new_str, s, n);
348 new_str[n] = (wchar_t)0;
355 wchar_t *strdup_mbstowcs(const gchar *s)
360 new_str = g_new(wchar_t, strlen(s) + 1);
361 if (mbstowcs(new_str, s, strlen(s) + 1) < 0) {
365 new_str = g_realloc(new_str,
366 sizeof(wchar_t) * (wcslen(new_str) + 1));
373 gchar *strdup_wcstombs(const wchar_t *s)
379 len = wcslen(s) * MB_CUR_MAX + 1;
380 new_str = g_new(gchar, len);
381 if (wcstombs(new_str, s, len) < 0) {
385 new_str = g_realloc(new_str, strlen(new_str) + 1);
392 /* Compare S1 and S2, ignoring case. */
393 gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
399 c1 = towlower(*s1++);
400 c2 = towlower(*s2++);
403 else if (c1 == 0 && c2 == 0)
410 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
411 wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
413 register size_t haystack_len, needle_len;
415 haystack_len = wcslen(haystack);
416 needle_len = wcslen(needle);
418 if (haystack_len < needle_len || needle_len == 0)
421 while (haystack_len >= needle_len) {
422 if (!wcsncasecmp(haystack, needle, needle_len))
423 return (wchar_t *)haystack;
433 /* Examine if next block is non-ASCII string */
434 gboolean is_next_nonascii(const wchar_t *s)
438 /* skip head space */
439 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
441 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
449 /* Examine if next block is multi-byte string */
450 gboolean is_next_mbs(const wchar_t *s)
454 gchar tmp[MB_CUR_MAX];
456 /* skip head space */
457 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
459 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
460 mbl = wctomb(tmp, *wp);
468 wchar_t *find_wspace(const wchar_t *s)
472 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
474 for (; *wp != (wchar_t)0; wp++) {
476 return (wchar_t *)wp;
482 /* compare subjects */
483 gint subject_compare(const gchar *s1, const gchar *s2)
488 if (!s1 || !s2) return -1;
489 if (!*s1 || !*s2) return -1;
491 Xalloca(str1, strlen(s1) + 1, return -1);
492 Xalloca(str2, strlen(s2) + 1, return -1);
499 if (!*str1 || !*str2) return -1;
501 retval = strcmp(str1, str2);
503 // g_print("\ns1 = %s\ns2 = %s\n"
504 // "str1 = %s\nstr2 = %s\nmatched.\n",
505 // s1, s2, str1, str2);
510 void trim_subject(gchar *str)
514 eliminate_parenthesis(str, '[', ']');
515 eliminate_parenthesis(str, '(', ')');
518 while (!strncasecmp(str, "Re:", 3)) {
520 while (isspace(*srcp)) srcp++;
521 memmove(str, srcp, strlen(srcp) + 1);
525 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
527 register gchar *srcp, *destp;
532 while ((destp = strchr(destp, op))) {
538 else if (*srcp == cl)
544 while (isspace(*srcp)) srcp++;
545 memmove(destp, srcp, strlen(srcp) + 1);
549 void extract_parenthesis(gchar *str, gchar op, gchar cl)
551 register gchar *srcp, *destp;
556 while ((srcp = strchr(destp, op))) {
559 memmove(destp, srcp + 1, strlen(srcp));
564 else if (*destp == cl)
576 void extract_one_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
579 register gchar *srcp, *destp;
581 gboolean in_quote = FALSE;
585 if ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
586 memmove(destp, srcp + 1, strlen(srcp));
589 if (*destp == op && !in_quote)
591 else if (*destp == cl && !in_quote)
593 else if (*destp == quote_chr)
605 void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
608 register gchar *srcp, *destp;
610 gboolean in_quote = FALSE;
614 while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
617 memmove(destp, srcp + 1, strlen(srcp));
620 if (*destp == op && !in_quote)
622 else if (*destp == cl && !in_quote)
624 else if (*destp == quote_chr)
636 void eliminate_quote(gchar *str, gchar quote_chr)
638 register gchar *srcp, *destp;
642 while ((destp = strchr(destp, quote_chr))) {
643 if ((srcp = strchr(destp + 1, quote_chr))) {
645 while (isspace(*srcp)) srcp++;
646 memmove(destp, srcp, strlen(srcp) + 1);
654 void extract_quote(gchar *str, gchar quote_chr)
658 if ((str = strchr(str, quote_chr))) {
659 if ((p = strchr(str + 1, quote_chr))) {
661 memmove(str, str + 1, p - str);
666 void eliminate_address_comment(gchar *str)
668 register gchar *srcp, *destp;
673 while ((destp = strchr(destp, '"'))) {
674 if ((srcp = strchr(destp + 1, '"'))) {
679 while (isspace(*srcp)) srcp++;
680 memmove(destp, srcp, strlen(srcp) + 1);
690 while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
696 else if (*srcp == ')')
702 while (isspace(*srcp)) srcp++;
703 memmove(destp, srcp, strlen(srcp) + 1);
707 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
709 gboolean in_quote = FALSE;
712 if (*str == c && !in_quote)
714 if (*str == quote_chr)
722 gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
724 gboolean in_quote = FALSE;
727 p = str + strlen(str) - 1;
729 if (*p == c && !in_quote)
739 void extract_address(gchar *str)
741 eliminate_address_comment(str);
742 if (strchr_with_skip_quote(str, '"', '<'))
743 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
747 GSList *address_list_append(GSList *addr_list, const gchar *str)
752 if (!str) return addr_list;
754 Xstrdup_a(work, str, return addr_list);
756 eliminate_address_comment(work);
759 while (workp && *workp) {
762 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
768 if (strchr_with_skip_quote(workp, '"', '<'))
769 extract_parenthesis_with_skip_quote
770 (workp, '"', '<', '>');
774 addr_list = g_slist_append(addr_list, g_strdup(workp));
782 GSList *references_list_append(GSList *msgid_list, const gchar *str)
786 if (!str) return msgid_list;
789 while (strp && *strp) {
790 const gchar *start, *end;
793 if ((start = strchr(strp, '<')) != NULL) {
794 end = strchr(start + 1, '>');
799 msgid = g_strndup(start + 1, end - start - 1);
802 msgid_list = g_slist_append(msgid_list, msgid);
812 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
817 if (!str) return group_list;
819 Xstrdup_a(work, str, return group_list);
823 while (workp && *workp) {
826 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
834 group_list = g_slist_append(group_list,
843 void remove_return(gchar *str)
845 register gchar *p = str;
848 if (*p == '\n' || *p == '\r')
849 memmove(p, p + 1, strlen(p));
855 void remove_space(gchar *str)
857 register gchar *p = str;
862 while (isspace(*(p + spc)))
865 memmove(p, p + spc, strlen(p + spc) + 1);
871 void unfold_line(gchar *str)
873 register gchar *p = str;
877 if (*p == '\n' || *p == '\r') {
880 while (isspace(*(p + spc)))
883 memmove(p, p + spc, strlen(p + spc) + 1);
889 void subst_char(gchar *str, gchar orig, gchar subst)
891 register gchar *p = str;
900 gboolean is_header_line(const gchar *str)
902 if (str[0] == ':') return FALSE;
904 while (*str != '\0' && *str != ' ') {
913 gboolean is_ascii_str(const guchar *str)
915 while (*str != '\0') {
916 if (*str != '\t' && *str != ' ' &&
917 *str != '\r' && *str != '\n' &&
918 (*str < 32 || *str >= 127))
926 gint get_quote_level(const gchar *str)
928 size_t firstquotepos;
929 size_t lastquotepos = -1;
930 const gchar *p = str;
932 gint quotelevel = -1;
935 /* speed up line processing by only searching to the last '>' */
936 if ((pos = strchr(str, '>')) != NULL) {
937 firstquotepos = pos - str;
938 lastquotepos = strrchr(str, '>') - str + 1;
940 /* skip a line if it contains a '<' before the initial '>' */
941 if (memchr(str, '<', pos - str) != NULL)
946 while (i < lastquotepos) {
947 while (i < lastquotepos) {
948 if (isspace(*p) || (*p == '\t')) {
954 if (i >= lastquotepos)
959 else if ((*p != '-') && !isspace(*p) && (i < lastquotepos)) {
960 /* any characters are allowed except '-' and space */
961 while ((*p != '-') && (*p != '>') && !isspace(*p) &&
962 (i < lastquotepos)) {
968 else if ((i >= lastquotepos) || isspace(*p))
979 GList *uri_list_extract_filenames(const gchar *uri_list)
981 GList *result = NULL;
989 while (isspace(*p)) p++;
990 if (!strncmp(p, "file:", 5)) {
993 while (*q && *q != '\n' && *q != '\r') q++;
997 while (q > p && isspace(*q)) q--;
998 file = g_malloc(q - p + 2);
999 strncpy(file, p, q - p + 1);
1000 file[q - p + 1] = '\0';
1001 result = g_list_append(result,file);
1005 p = strchr(p, '\n');
1012 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
1014 register guint haystack_len, needle_len;
1015 gboolean in_squote = FALSE, in_dquote = FALSE;
1017 haystack_len = strlen(haystack);
1018 needle_len = strlen(needle);
1020 if (haystack_len < needle_len || needle_len == 0)
1023 while (haystack_len >= needle_len) {
1024 if (!in_squote && !in_dquote &&
1025 !strncmp(haystack, needle, needle_len))
1026 return (gchar *)haystack;
1028 /* 'foo"bar"' -> foo"bar"
1029 "foo'bar'" -> foo'bar' */
1030 if (*haystack == '\'') {
1033 else if (!in_dquote)
1035 } else if (*haystack == '\"') {
1038 else if (!in_squote)
1049 /* this fuction was taken from gstrfuncs.c in glib. */
1050 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1053 GSList *string_list = NULL, *slist;
1054 gchar **str_array, *s;
1057 g_return_val_if_fail(str != NULL, NULL);
1058 g_return_val_if_fail(delim != NULL, NULL);
1061 max_tokens = G_MAXINT;
1063 s = strstr_with_skip_quote(str, delim);
1065 guint delimiter_len = strlen(delim);
1072 new_str = g_new(gchar, len + 1);
1073 strncpy(new_str, str, len);
1075 string_list = g_slist_prepend(string_list, new_str);
1077 str = s + delimiter_len;
1078 s = strstr_with_skip_quote(str, delim);
1079 } while (--max_tokens && s);
1084 string_list = g_slist_prepend(string_list, g_strdup(str));
1087 str_array = g_new(gchar*, n);
1091 str_array[i--] = NULL;
1092 for (slist = string_list; slist; slist = slist->next)
1093 str_array[i--] = slist->data;
1095 g_slist_free(string_list);
1101 * We need this wrapper around g_get_home_dir(), so that
1102 * we can fix some Windoze things here. Should be done in glibc of course
1103 * but as long as we are not able to do our own extensions to glibc, we do
1106 gchar *get_home_dir(void)
1108 #if HAVE_DOSISH_SYSTEM
1109 static gchar *home_dir;
1112 home_dir = read_w32_registry_string(NULL,
1113 "Software\\Sylpheed", "HomeDir" );
1114 if (!home_dir || !*home_dir) {
1115 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1116 const char *s = g_get_home_dir();
1118 home_dir = g_strdup (s);
1120 if (!home_dir || !*home_dir)
1121 home_dir = g_strdup ("c:\\sylpheed");
1123 debug_print("initialized home_dir to `%s'\n", home_dir);
1126 #else /* standard glib */
1127 return g_get_home_dir();
1131 gchar *get_rc_dir(void)
1133 static gchar *rc_dir = NULL;
1136 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1142 gchar *get_news_cache_dir(void)
1144 static gchar *news_cache_dir = NULL;
1146 if (!news_cache_dir)
1147 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1148 NEWS_CACHE_DIR, NULL);
1150 return news_cache_dir;
1153 gchar *get_imap_cache_dir(void)
1155 static gchar *imap_cache_dir = NULL;
1157 if (!imap_cache_dir)
1158 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1159 IMAP_CACHE_DIR, NULL);
1161 return imap_cache_dir;
1164 gchar *get_mbox_cache_dir(void)
1166 static gchar *mbox_cache_dir = NULL;
1168 if (!mbox_cache_dir)
1169 mbox_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1170 MBOX_CACHE_DIR, NULL);
1172 return mbox_cache_dir;
1175 gchar *get_mime_tmp_dir(void)
1177 static gchar *mime_tmp_dir = NULL;
1180 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1181 MIME_TMP_DIR, NULL);
1183 return mime_tmp_dir;
1186 gchar *get_tmp_file(void)
1188 static gchar *tmp_file = NULL;
1191 tmp_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1197 gchar *get_domain_name(void)
1199 static gchar *domain_name = NULL;
1202 gchar buf[BUFFSIZE] = "";
1204 if (gethostname(buf, sizeof(buf)) < 0) {
1205 perror("gethostname");
1206 strcpy(buf, "unknown");
1209 domain_name = g_strdup(buf);
1215 off_t get_file_size(const gchar *file)
1219 if (stat(file, &s) < 0) {
1220 FILE_OP_ERROR(file, "stat");
1227 off_t get_left_file_size(FILE *fp)
1233 if ((pos = ftell(fp)) < 0) {
1237 if (fseek(fp, 0L, SEEK_END) < 0) {
1241 if ((end = ftell(fp)) < 0) {
1246 if (fseek(fp, pos, SEEK_SET) < 0) {
1254 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1258 if (stat(file, &s) < 0) {
1259 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1263 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1269 gboolean is_dir_exist(const gchar *dir)
1273 if (stat(dir, &s) < 0) {
1274 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1278 if (S_ISDIR(s.st_mode))
1284 gint change_dir(const gchar *dir)
1286 gchar *prevdir = NULL;
1289 prevdir = g_get_current_dir();
1291 if (chdir(dir) < 0) {
1292 FILE_OP_ERROR(dir, "chdir");
1293 if (debug_mode) g_free(prevdir);
1295 } else if (debug_mode) {
1298 cwd = g_get_current_dir();
1299 if (strcmp(prevdir, cwd) != 0)
1300 g_print("current dir: %s\n", cwd);
1308 gint make_dir_hier(const gchar *dir)
1313 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1314 parent_dir = g_strndup(dir, p - dir);
1315 if (*parent_dir != '\0') {
1316 if (!is_dir_exist(parent_dir)) {
1317 if (mkdir(parent_dir, S_IRWXU) < 0) {
1318 FILE_OP_ERROR(parent_dir, "mkdir");
1322 if (chmod(parent_dir, S_IRWXU) < 0)
1323 FILE_OP_ERROR(parent_dir, "chmod");
1328 if (!is_dir_exist(dir)) {
1329 if (mkdir(dir, S_IRWXU) < 0) {
1330 FILE_OP_ERROR(dir, "mkdir");
1333 if (chmod(dir, S_IRWXU) < 0)
1334 FILE_OP_ERROR(dir, "chmod");
1340 gint remove_all_files(const gchar *dir)
1346 prev_dir = g_get_current_dir();
1348 if (chdir(dir) < 0) {
1349 FILE_OP_ERROR(dir, "chdir");
1353 if ((dp = opendir(".")) == NULL) {
1354 FILE_OP_ERROR(dir, "opendir");
1358 while ((d = readdir(dp)) != NULL) {
1359 if (!strcmp(d->d_name, ".") ||
1360 !strcmp(d->d_name, ".."))
1363 if (unlink(d->d_name) < 0)
1364 FILE_OP_ERROR(d->d_name, "unlink");
1369 if (chdir(prev_dir) < 0) {
1370 FILE_OP_ERROR(prev_dir, "chdir");
1380 gint remove_all_numbered_files(const gchar *dir)
1386 prev_dir = g_get_current_dir();
1388 if (chdir(dir) < 0) {
1389 FILE_OP_ERROR(dir, "chdir");
1393 if ((dp = opendir(".")) == NULL) {
1394 FILE_OP_ERROR(dir, "opendir");
1398 while ((d = readdir(dp)) != NULL) {
1399 if (to_number(d->d_name) < 0) continue;
1401 if (unlink(d->d_name) < 0)
1402 FILE_OP_ERROR(d->d_name, "unlink");
1407 if (chdir(prev_dir) < 0) {
1408 FILE_OP_ERROR(prev_dir, "chdir");
1418 gint remove_dir_recursive(const gchar *dir)
1425 //g_print("dir = %s\n", dir);
1427 if (stat(dir, &s) < 0) {
1428 FILE_OP_ERROR(dir, "stat");
1429 if (ENOENT == errno) return 0;
1433 if (!S_ISDIR(s.st_mode)) {
1434 if (unlink(dir) < 0) {
1435 FILE_OP_ERROR(dir, "unlink");
1442 prev_dir = g_get_current_dir();
1443 //g_print("prev_dir = %s\n", prev_dir);
1445 if (!path_cmp(prev_dir, dir)) {
1447 if (chdir("..") < 0) {
1448 FILE_OP_ERROR(dir, "chdir");
1451 prev_dir = g_get_current_dir();
1454 if (chdir(dir) < 0) {
1455 FILE_OP_ERROR(dir, "chdir");
1460 if ((dp = opendir(".")) == NULL) {
1461 FILE_OP_ERROR(dir, "opendir");
1467 /* remove all files in the directory */
1468 while ((d = readdir(dp)) != NULL) {
1469 if (!strcmp(d->d_name, ".") ||
1470 !strcmp(d->d_name, ".."))
1473 if (stat(d->d_name, &s) < 0) {
1474 FILE_OP_ERROR(d->d_name, "stat");
1478 //g_print("removing %s\n", d->d_name);
1480 if (S_ISDIR(s.st_mode)) {
1481 if (remove_dir_recursive(d->d_name) < 0) {
1482 g_warning("can't remove directory\n");
1486 if (unlink(d->d_name) < 0)
1487 FILE_OP_ERROR(d->d_name, "unlink");
1493 if (chdir(prev_dir) < 0) {
1494 FILE_OP_ERROR(prev_dir, "chdir");
1501 if (rmdir(dir) < 0) {
1502 FILE_OP_ERROR(dir, "rmdir");
1510 /* this seems to be slower than the stdio version... */
1511 gint copy_file(const gchar *src, const gchar *dest)
1513 gint src_fd, dest_fd;
1517 gchar *dest_bak = NULL;
1519 if ((src_fd = open(src, O_RDONLY)) < 0) {
1520 FILE_OP_ERROR(src, "open");
1524 if (is_file_exist(dest)) {
1525 dest_bak = g_strconcat(dest, ".bak", NULL);
1526 if (rename(dest, dest_bak) < 0) {
1527 FILE_OP_ERROR(dest, "rename");
1534 if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
1535 FILE_OP_ERROR(dest, "open");
1538 if (rename(dest_bak, dest) < 0)
1539 FILE_OP_ERROR(dest_bak, "rename");
1545 while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
1550 n_write = write(dest_fd, bufp, len);
1552 g_warning(_("writing to %s failed.\n"), dest);
1557 if (rename(dest_bak, dest) < 0)
1558 FILE_OP_ERROR(dest_bak, "rename");
1571 if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
1572 g_warning(_("File copy from %s to %s failed.\n"), src, dest);
1575 if (rename(dest_bak, dest) < 0)
1576 FILE_OP_ERROR(dest_bak, "rename");
1587 gint copy_file(const gchar *src, const gchar *dest)
1589 FILE *src_fp, *dest_fp;
1592 gchar *dest_bak = NULL;
1593 gboolean err = FALSE;
1595 if ((src_fp = fopen(src, "r")) == NULL) {
1596 FILE_OP_ERROR(src, "fopen");
1599 if (is_file_exist(dest)) {
1600 dest_bak = g_strconcat(dest, ".bak", NULL);
1601 if (rename(dest, dest_bak) < 0) {
1602 FILE_OP_ERROR(dest, "rename");
1609 if ((dest_fp = fopen(dest, "w")) == NULL) {
1610 FILE_OP_ERROR(dest, "fopen");
1613 if (rename(dest_bak, dest) < 0)
1614 FILE_OP_ERROR(dest_bak, "rename");
1620 if (change_file_mode_rw(dest_fp, dest) < 0) {
1621 FILE_OP_ERROR(dest, "chmod");
1622 g_warning(_("can't change file mode\n"));
1625 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
1626 if (n_read < sizeof(buf) && ferror(src_fp))
1628 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
1629 g_warning(_("writing to %s failed.\n"), dest);
1634 if (rename(dest_bak, dest) < 0)
1635 FILE_OP_ERROR(dest_bak, "rename");
1642 if (ferror(src_fp)) {
1643 FILE_OP_ERROR(src, "fread");
1647 if (fclose(dest_fp) == EOF) {
1648 FILE_OP_ERROR(dest, "fclose");
1655 if (rename(dest_bak, dest) < 0)
1656 FILE_OP_ERROR(dest_bak, "rename");
1667 gint move_file(const gchar *src, const gchar *dest)
1669 if (is_file_exist(dest)) {
1670 g_warning(_("move_file(): file %s already exists."), dest);
1674 if (rename(src, dest) == 0) return 0;
1676 if (EXDEV != errno) {
1677 FILE_OP_ERROR(src, "rename");
1681 if (copy_file(src, dest) < 0) return -1;
1688 gint change_file_mode_rw(FILE *fp, const gchar *file)
1691 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
1693 return chmod(file, S_IRUSR|S_IWUSR);
1697 FILE *my_tmpfile(void)
1700 const gchar suffix[] = ".XXXXXX";
1701 const gchar *tmpdir;
1703 const gchar *progname;
1709 tmpdir = g_get_tmp_dir();
1710 tmplen = strlen(tmpdir);
1711 progname = g_get_prgname();
1712 proglen = strlen(progname);
1713 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
1716 memcpy(fname, tmpdir, tmplen);
1717 fname[tmplen] = G_DIR_SEPARATOR;
1718 memcpy(fname + tmplen + 1, progname, proglen);
1719 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
1721 fd = mkstemp(fname);
1727 fp = fdopen(fd, "w+b");
1732 #endif /* HAVE_MKSTEMP */
1737 gint execute_async(gchar *const argv[])
1741 if ((pid = fork()) < 0) {
1746 if (pid == 0) { /* child process */
1749 if ((gch_pid = fork()) < 0) {
1754 if (gch_pid == 0) { /* grandchild process */
1755 execvp(argv[0], argv);
1764 waitpid(pid, NULL, 0);
1769 gint execute_command_line(const gchar *cmdline)
1775 argv = strsplit_with_quote(cmdline, " ", 0);
1777 for (i = 0; argv[i] != NULL; i++) {
1778 gchar *str = argv[i];
1780 if (str[0] == '\'' || str[0] == '\"') {
1784 if (str[len - 1] == str[0]) {
1785 str[len - 1] = '\0';
1786 memmove(str, str + 1, len - 1);
1791 ret = execute_async(argv);
1797 gint open_uri(const gchar *uri, const gchar *cmdline)
1799 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
1800 gchar buf[BUFFSIZE];
1803 g_return_val_if_fail(uri != NULL, -1);
1806 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1807 !strchr(p + 2, '%'))
1808 g_snprintf(buf, sizeof(buf), cmdline, uri);
1811 g_warning(_("Open URI command line is invalid: `%s'"),
1813 g_snprintf(buf, sizeof(buf), default_cmdline, uri);
1816 execute_command_line(buf);
1821 time_t remote_tzoffset_sec(const gchar *zone)
1823 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
1829 time_t remoteoffset;
1831 strncpy(zone3, zone, 3);
1835 if (sscanf(zone, "%c%2d%2d", &c, &h, &m) == 3 &&
1836 (c == '+' || c == '-')) {
1837 remoteoffset = ((h * 60) + m) * 60;
1839 remoteoffset = -remoteoffset;
1840 } else if (!strncmp(zone, "UT" , 2) ||
1841 !strncmp(zone, "GMT", 2)) {
1843 } else if (strlen(zone3) == 3 &&
1844 (p = strstr(ustzstr, zone3)) != NULL &&
1845 (p - ustzstr) % 3 == 0) {
1846 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
1847 remoteoffset = iustz * 3600;
1848 } else if (strlen(zone3) == 1) {
1850 case 'Z': remoteoffset = 0; break;
1851 case 'A': remoteoffset = -1; break;
1852 case 'B': remoteoffset = -2; break;
1853 case 'C': remoteoffset = -3; break;
1854 case 'D': remoteoffset = -4; break;
1855 case 'E': remoteoffset = -5; break;
1856 case 'F': remoteoffset = -6; break;
1857 case 'G': remoteoffset = -7; break;
1858 case 'H': remoteoffset = -8; break;
1859 case 'I': remoteoffset = -9; break;
1860 case 'K': remoteoffset = -10; break; /* J is not used */
1861 case 'L': remoteoffset = -11; break;
1862 case 'M': remoteoffset = -12; break;
1863 case 'N': remoteoffset = 1; break;
1864 case 'O': remoteoffset = 2; break;
1865 case 'P': remoteoffset = 3; break;
1866 case 'Q': remoteoffset = 4; break;
1867 case 'R': remoteoffset = 5; break;
1868 case 'S': remoteoffset = 6; break;
1869 case 'T': remoteoffset = 7; break;
1870 case 'U': remoteoffset = 8; break;
1871 case 'V': remoteoffset = 9; break;
1872 case 'W': remoteoffset = 10; break;
1873 case 'X': remoteoffset = 11; break;
1874 case 'Y': remoteoffset = 12; break;
1875 default: remoteoffset = 0; break;
1877 remoteoffset = remoteoffset * 3600;
1880 return remoteoffset;
1883 time_t tzoffset_sec(time_t *now)
1889 lt = localtime(now);
1891 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1893 if (lt->tm_year < gmt.tm_year)
1895 else if (lt->tm_year > gmt.tm_year)
1897 else if (lt->tm_yday < gmt.tm_yday)
1899 else if (lt->tm_yday > gmt.tm_yday)
1902 if (off >= 24 * 60) /* should be impossible */
1903 off = 23 * 60 + 59; /* if not, insert silly value */
1904 if (off <= -24 * 60)
1905 off = -(23 * 60 + 59);
1914 /* calculate timezone offset */
1915 gchar *tzoffset(time_t *now)
1917 static gchar offset_string[6];
1923 lt = localtime(now);
1925 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1927 if (lt->tm_year < gmt.tm_year)
1929 else if (lt->tm_year > gmt.tm_year)
1931 else if (lt->tm_yday < gmt.tm_yday)
1933 else if (lt->tm_yday > gmt.tm_yday)
1941 if (off >= 24 * 60) /* should be impossible */
1942 off = 23 * 60 + 59; /* if not, insert silly value */
1944 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
1946 return offset_string;
1949 void get_rfc822_date(gchar *buf, gint len)
1953 gchar day[4], mon[4];
1954 gint dd, hh, mm, ss, yyyy;
1959 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
1960 day, mon, &dd, &hh, &mm, &ss, &yyyy);
1961 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
1962 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
1965 void debug_print(const gchar *format, ...)
1968 gchar buf[BUFFSIZE];
1970 if (!debug_mode) return;
1972 va_start(args, format);
1973 g_vsnprintf(buf, sizeof(buf), format, args);
1979 void log_print(const gchar *format, ...)
1982 gchar buf[BUFFSIZE];
1984 va_start(args, format);
1985 g_vsnprintf(buf, sizeof(buf), format, args);
1988 if (debug_mode) fputs(buf, stdout);
1989 log_window_append(buf, LOG_NORMAL);
1990 statusbar_puts_all(buf);
1993 void log_message(const gchar *format, ...)
1996 gchar buf[BUFFSIZE];
1998 va_start(args, format);
1999 g_vsnprintf(buf, sizeof(buf), format, args);
2002 if (debug_mode) g_message("%s", buf);
2003 log_window_append(buf, LOG_MSG);
2006 void log_warning(const gchar *format, ...)
2009 gchar buf[BUFFSIZE];
2011 va_start(args, format);
2012 g_vsnprintf(buf, sizeof(buf), format, args);
2015 g_warning("%s", buf);
2016 log_window_append(buf, LOG_WARN);
2019 void log_error(const gchar *format, ...)
2022 gchar buf[BUFFSIZE];
2024 va_start(args, format);
2025 g_vsnprintf(buf, sizeof(buf), format, args);
2028 g_warning("%s", buf);
2029 log_window_append(buf, LOG_ERROR);