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 static void hash_free_value_mem_func(gpointer key, gpointer value,
90 void hash_free_value_mem(GHashTable *table)
92 g_hash_table_foreach(table, hash_free_value_mem_func, NULL);
95 void ptr_array_free_strings(GPtrArray *array)
100 g_return_if_fail(array != NULL);
102 for (i = 0; i < array->len; i++) {
103 str = g_ptr_array_index(array, i);
108 gint to_number(const gchar *nstr)
110 register const gchar *p;
112 if (*nstr == '\0') return -1;
114 for (p = nstr; *p != '\0'; p++)
115 if (!isdigit(*p)) return -1;
120 /* convert integer into string,
121 nstr must be not lower than 11 characters length */
122 gchar *itos_buf(gchar *nstr, gint n)
124 g_snprintf(nstr, 11, "%d", n);
128 /* convert integer into string */
131 static gchar nstr[11];
133 return itos_buf(nstr, n);
136 gchar *to_human_readable(off_t size)
138 static gchar str[10];
142 for (count = 0; count < 3; count++) {
143 if (size / div < 1024)
150 case 0: g_snprintf(str, sizeof(str), "%dB", (gint)size); break;
151 case 1: g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / div);
153 case 2: g_snprintf(str, sizeof(str), "%.2fMB", (gfloat)size / div);
156 g_snprintf(str, sizeof(str), "%.2fGB", (gfloat)size / div);
163 /* strcmp with NULL-checking */
164 gint strcmp2(const gchar *s1, const gchar *s2)
166 if (s1 == NULL || s2 == NULL)
169 return strcmp(s1, s2);
171 /* strstr with NULL-checking */
172 gint strstr2(const gchar *s1, const gchar *s2)
174 if (s1 == NULL || s2 == NULL)
177 if( strstr(s1, s2) !=NULL)
183 gint path_cmp(const gchar *s1, const gchar *s2)
187 if (s1 == NULL || s2 == NULL) return -1;
188 if (*s1 == '\0' || *s2 == '\0') return -1;
193 if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
194 if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
196 return strncmp(s1, s2, MAX(len1, len2));
199 /* remove trailing return code */
200 gchar *strretchomp(gchar *str)
204 if (!*str) return str;
206 for (s = str + strlen(str) - 1;
207 s >= str && (*s == '\n' || *s == '\r');
214 /* remove trailing character */
215 gchar *strtailchomp(gchar *str, gchar tail_char)
219 if (!*str) return str;
220 if (tail_char == '\0') return str;
222 for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
229 /* Similar to `strstr' but this function ignores the case of both strings. */
230 gchar *strcasestr(const gchar *haystack, const gchar *needle)
232 register size_t haystack_len, needle_len;
234 haystack_len = strlen(haystack);
235 needle_len = strlen(needle);
237 if (haystack_len < needle_len || needle_len == 0)
240 while (haystack_len >= needle_len) {
241 if (!strncasecmp(haystack, needle, needle_len))
242 return (gchar *)haystack;
252 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
253 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
267 /* don't do zero fill */
272 int iswalnum(wint_t wc)
274 return isalnum((int)wc);
279 int iswspace(wint_t wc)
281 return isspace((int)wc);
286 wint_t towlower(wint_t wc)
288 if (wc >= L'A' && wc <= L'Z')
289 return wc + L'a' - L'A';
296 size_t wcslen(const wchar_t *s)
308 /* Copy SRC to DEST. */
309 wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
317 } while (c != L'\0');
324 /* Copy no more than N wide-characters of SRC to DEST. */
325 wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
335 } while (c != L'\0');
346 /* Duplicate S, returning an identical malloc'd string. */
347 wchar_t *wcsdup(const wchar_t *s)
352 new_str = g_new(wchar_t, wcslen(s) + 1);
360 /* Duplicate no more than N wide-characters of S,
361 returning an identical malloc'd string. */
362 wchar_t *wcsndup(const wchar_t *s, size_t n)
367 new_str = g_new(wchar_t, n + 1);
368 wcsncpy(new_str, s, n);
369 new_str[n] = (wchar_t)0;
376 wchar_t *strdup_mbstowcs(const gchar *s)
381 new_str = g_new(wchar_t, strlen(s) + 1);
382 if (mbstowcs(new_str, s, strlen(s) + 1) < 0) {
386 new_str = g_realloc(new_str,
387 sizeof(wchar_t) * (wcslen(new_str) + 1));
394 gchar *strdup_wcstombs(const wchar_t *s)
400 len = wcslen(s) * MB_CUR_MAX + 1;
401 new_str = g_new(gchar, len);
402 if (wcstombs(new_str, s, len) < 0) {
406 new_str = g_realloc(new_str, strlen(new_str) + 1);
413 /* Compare S1 and S2, ignoring case. */
414 gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
420 c1 = towlower(*s1++);
421 c2 = towlower(*s2++);
424 else if (c1 == 0 && c2 == 0)
431 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
432 wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
434 register size_t haystack_len, needle_len;
436 haystack_len = wcslen(haystack);
437 needle_len = wcslen(needle);
439 if (haystack_len < needle_len || needle_len == 0)
442 while (haystack_len >= needle_len) {
443 if (!wcsncasecmp(haystack, needle, needle_len))
444 return (wchar_t *)haystack;
454 /* Examine if next block is non-ASCII string */
455 gboolean is_next_nonascii(const wchar_t *s)
459 /* skip head space */
460 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
462 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
470 /* Examine if next block is multi-byte string */
471 gboolean is_next_mbs(const wchar_t *s)
475 gchar tmp[MB_CUR_MAX];
477 /* skip head space */
478 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
480 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
481 mbl = wctomb(tmp, *wp);
489 wchar_t *find_wspace(const wchar_t *s)
493 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
495 for (; *wp != (wchar_t)0; wp++) {
497 return (wchar_t *)wp;
503 /* compare subjects */
504 gint subject_compare(const gchar *s1, const gchar *s2)
509 if (!s1 || !s2) return -1;
510 if (!*s1 || !*s2) return -1;
512 Xalloca(str1, strlen(s1) + 1, return -1);
513 Xalloca(str2, strlen(s2) + 1, return -1);
520 if (!*str1 || !*str2) return -1;
522 retval = strcmp(str1, str2);
525 g_print("\ns1 = %s\ns2 = %s\n"
526 "str1 = %s\nstr2 = %s\nmatched.\n",
532 void trim_subject(gchar *str)
536 eliminate_parenthesis(str, '[', ']');
537 eliminate_parenthesis(str, '(', ')');
540 while (!strncasecmp(str, "Re:", 3)) {
542 while (isspace(*srcp)) srcp++;
543 memmove(str, srcp, strlen(srcp) + 1);
547 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
549 register gchar *srcp, *destp;
554 while ((destp = strchr(destp, op))) {
560 else if (*srcp == cl)
566 while (isspace(*srcp)) srcp++;
567 memmove(destp, srcp, strlen(srcp) + 1);
571 void extract_parenthesis(gchar *str, gchar op, gchar cl)
573 register gchar *srcp, *destp;
578 while ((srcp = strchr(destp, op))) {
581 memmove(destp, srcp + 1, strlen(srcp));
586 else if (*destp == cl)
598 void extract_one_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
601 register gchar *srcp, *destp;
603 gboolean in_quote = FALSE;
607 if ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
608 memmove(destp, srcp + 1, strlen(srcp));
611 if (*destp == op && !in_quote)
613 else if (*destp == cl && !in_quote)
615 else if (*destp == quote_chr)
627 void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
630 register gchar *srcp, *destp;
632 gboolean in_quote = FALSE;
636 while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
639 memmove(destp, srcp + 1, strlen(srcp));
642 if (*destp == op && !in_quote)
644 else if (*destp == cl && !in_quote)
646 else if (*destp == quote_chr)
658 void eliminate_quote(gchar *str, gchar quote_chr)
660 register gchar *srcp, *destp;
664 while ((destp = strchr(destp, quote_chr))) {
665 if ((srcp = strchr(destp + 1, quote_chr))) {
667 while (isspace(*srcp)) srcp++;
668 memmove(destp, srcp, strlen(srcp) + 1);
676 void extract_quote(gchar *str, gchar quote_chr)
680 if ((str = strchr(str, quote_chr))) {
681 if ((p = strchr(str + 1, quote_chr))) {
683 memmove(str, str + 1, p - str);
688 void eliminate_address_comment(gchar *str)
690 register gchar *srcp, *destp;
695 while ((destp = strchr(destp, '"'))) {
696 if ((srcp = strchr(destp + 1, '"'))) {
701 while (isspace(*srcp)) srcp++;
702 memmove(destp, srcp, strlen(srcp) + 1);
712 while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
718 else if (*srcp == ')')
724 while (isspace(*srcp)) srcp++;
725 memmove(destp, srcp, strlen(srcp) + 1);
729 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
731 gboolean in_quote = FALSE;
734 if (*str == c && !in_quote)
736 if (*str == quote_chr)
744 gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
746 gboolean in_quote = FALSE;
749 p = str + strlen(str) - 1;
751 if (*p == c && !in_quote)
761 void extract_address(gchar *str)
763 eliminate_address_comment(str);
764 if (strchr_with_skip_quote(str, '"', '<'))
765 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
769 GSList *address_list_append(GSList *addr_list, const gchar *str)
774 if (!str) return addr_list;
776 Xstrdup_a(work, str, return addr_list);
778 eliminate_address_comment(work);
781 while (workp && *workp) {
784 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
790 if (strchr_with_skip_quote(workp, '"', '<'))
791 extract_parenthesis_with_skip_quote
792 (workp, '"', '<', '>');
796 addr_list = g_slist_append(addr_list, g_strdup(workp));
804 GSList *references_list_append(GSList *msgid_list, const gchar *str)
808 if (!str) return msgid_list;
811 while (strp && *strp) {
812 const gchar *start, *end;
815 if ((start = strchr(strp, '<')) != NULL) {
816 end = strchr(start + 1, '>');
821 msgid = g_strndup(start + 1, end - start - 1);
824 msgid_list = g_slist_append(msgid_list, msgid);
834 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
839 if (!str) return group_list;
841 Xstrdup_a(work, str, return group_list);
845 while (workp && *workp) {
848 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
856 group_list = g_slist_append(group_list,
865 void remove_return(gchar *str)
867 register gchar *p = str;
870 if (*p == '\n' || *p == '\r')
871 memmove(p, p + 1, strlen(p));
877 void remove_space(gchar *str)
879 register gchar *p = str;
884 while (isspace(*(p + spc)))
887 memmove(p, p + spc, strlen(p + spc) + 1);
893 void unfold_line(gchar *str)
895 register gchar *p = str;
899 if (*p == '\n' || *p == '\r') {
902 while (isspace(*(p + spc)))
905 memmove(p, p + spc, strlen(p + spc) + 1);
911 void subst_char(gchar *str, gchar orig, gchar subst)
913 register gchar *p = str;
922 gboolean is_header_line(const gchar *str)
924 if (str[0] == ':') return FALSE;
926 while (*str != '\0' && *str != ' ') {
935 gboolean is_ascii_str(const guchar *str)
937 while (*str != '\0') {
938 if (*str != '\t' && *str != ' ' &&
939 *str != '\r' && *str != '\n' &&
940 (*str < 32 || *str >= 127))
948 gint get_quote_level(const gchar *str)
950 const gchar *first_pos;
951 const gchar *last_pos;
952 const gchar *p = str;
953 gint quote_level = -1;
955 /* speed up line processing by only searching to the last '>' */
956 if ((first_pos = strchr(str, '>')) != NULL) {
957 /* skip a line if it contains a '<' before the initial '>' */
958 if (memchr(str, '<', first_pos - str) != NULL)
960 last_pos = strrchr(first_pos, '>');
964 while (p <= last_pos) {
965 while (p < last_pos) {
974 else if (*p != '-' && !isspace(*p) && p <= last_pos) {
975 /* any characters are allowed except '-' and space */
976 while (*p != '-' && *p != '>' && !isspace(*p) &&
991 GList *uri_list_extract_filenames(const gchar *uri_list)
993 GList *result = NULL;
1001 while (isspace(*p)) p++;
1002 if (!strncmp(p, "file:", 5)) {
1005 while (*q && *q != '\n' && *q != '\r') q++;
1009 while (q > p && isspace(*q)) q--;
1010 file = g_malloc(q - p + 2);
1011 strncpy(file, p, q - p + 1);
1012 file[q - p + 1] = '\0';
1013 result = g_list_append(result,file);
1017 p = strchr(p, '\n');
1024 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
1026 register guint haystack_len, needle_len;
1027 gboolean in_squote = FALSE, in_dquote = FALSE;
1029 haystack_len = strlen(haystack);
1030 needle_len = strlen(needle);
1032 if (haystack_len < needle_len || needle_len == 0)
1035 while (haystack_len >= needle_len) {
1036 if (!in_squote && !in_dquote &&
1037 !strncmp(haystack, needle, needle_len))
1038 return (gchar *)haystack;
1040 /* 'foo"bar"' -> foo"bar"
1041 "foo'bar'" -> foo'bar' */
1042 if (*haystack == '\'') {
1045 else if (!in_dquote)
1047 } else if (*haystack == '\"') {
1050 else if (!in_squote)
1061 /* this fuction was taken from gstrfuncs.c in glib. */
1062 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1065 GSList *string_list = NULL, *slist;
1066 gchar **str_array, *s;
1069 g_return_val_if_fail(str != NULL, NULL);
1070 g_return_val_if_fail(delim != NULL, NULL);
1073 max_tokens = G_MAXINT;
1075 s = strstr_with_skip_quote(str, delim);
1077 guint delimiter_len = strlen(delim);
1084 new_str = g_new(gchar, len + 1);
1085 strncpy(new_str, str, len);
1087 string_list = g_slist_prepend(string_list, new_str);
1089 str = s + delimiter_len;
1090 s = strstr_with_skip_quote(str, delim);
1091 } while (--max_tokens && s);
1096 string_list = g_slist_prepend(string_list, g_strdup(str));
1099 str_array = g_new(gchar*, n);
1103 str_array[i--] = NULL;
1104 for (slist = string_list; slist; slist = slist->next)
1105 str_array[i--] = slist->data;
1107 g_slist_free(string_list);
1113 * We need this wrapper around g_get_home_dir(), so that
1114 * we can fix some Windoze things here. Should be done in glibc of course
1115 * but as long as we are not able to do our own extensions to glibc, we do
1118 gchar *get_home_dir(void)
1120 #if HAVE_DOSISH_SYSTEM
1121 static gchar *home_dir;
1124 home_dir = read_w32_registry_string(NULL,
1125 "Software\\Sylpheed", "HomeDir" );
1126 if (!home_dir || !*home_dir) {
1127 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1128 const char *s = g_get_home_dir();
1130 home_dir = g_strdup (s);
1132 if (!home_dir || !*home_dir)
1133 home_dir = g_strdup ("c:\\sylpheed");
1135 debug_print("initialized home_dir to `%s'\n", home_dir);
1138 #else /* standard glib */
1139 return g_get_home_dir();
1143 gchar *get_rc_dir(void)
1145 static gchar *rc_dir = NULL;
1148 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1154 gchar *get_news_cache_dir(void)
1156 static gchar *news_cache_dir = NULL;
1158 if (!news_cache_dir)
1159 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1160 NEWS_CACHE_DIR, NULL);
1162 return news_cache_dir;
1165 gchar *get_imap_cache_dir(void)
1167 static gchar *imap_cache_dir = NULL;
1169 if (!imap_cache_dir)
1170 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1171 IMAP_CACHE_DIR, NULL);
1173 return imap_cache_dir;
1176 gchar *get_mbox_cache_dir(void)
1178 static gchar *mbox_cache_dir = NULL;
1180 if (!mbox_cache_dir)
1181 mbox_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1182 MBOX_CACHE_DIR, NULL);
1184 return mbox_cache_dir;
1187 gchar *get_mime_tmp_dir(void)
1189 static gchar *mime_tmp_dir = NULL;
1192 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1193 MIME_TMP_DIR, NULL);
1195 return mime_tmp_dir;
1198 gchar *get_tmp_file(void)
1200 static gchar *tmp_file = NULL;
1203 tmp_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1209 gchar *get_domain_name(void)
1211 static gchar *domain_name = NULL;
1214 gchar buf[BUFFSIZE] = "";
1216 if (gethostname(buf, sizeof(buf)) < 0) {
1217 perror("gethostname");
1218 strcpy(buf, "unknown");
1221 domain_name = g_strdup(buf);
1227 off_t get_file_size(const gchar *file)
1231 if (stat(file, &s) < 0) {
1232 FILE_OP_ERROR(file, "stat");
1239 off_t get_left_file_size(FILE *fp)
1245 if ((pos = ftell(fp)) < 0) {
1249 if (fseek(fp, 0L, SEEK_END) < 0) {
1253 if ((end = ftell(fp)) < 0) {
1258 if (fseek(fp, pos, SEEK_SET) < 0) {
1266 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1270 if (stat(file, &s) < 0) {
1271 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1275 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1281 gboolean is_dir_exist(const gchar *dir)
1285 if (stat(dir, &s) < 0) {
1286 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1290 if (S_ISDIR(s.st_mode))
1296 gint change_dir(const gchar *dir)
1298 gchar *prevdir = NULL;
1301 prevdir = g_get_current_dir();
1303 if (chdir(dir) < 0) {
1304 FILE_OP_ERROR(dir, "chdir");
1305 if (debug_mode) g_free(prevdir);
1307 } else if (debug_mode) {
1310 cwd = g_get_current_dir();
1311 if (strcmp(prevdir, cwd) != 0)
1312 g_print("current dir: %s\n", cwd);
1320 gint make_dir_hier(const gchar *dir)
1325 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1326 parent_dir = g_strndup(dir, p - dir);
1327 if (*parent_dir != '\0') {
1328 if (!is_dir_exist(parent_dir)) {
1329 if (mkdir(parent_dir, S_IRWXU) < 0) {
1330 FILE_OP_ERROR(parent_dir, "mkdir");
1334 if (chmod(parent_dir, S_IRWXU) < 0)
1335 FILE_OP_ERROR(parent_dir, "chmod");
1340 if (!is_dir_exist(dir)) {
1341 if (mkdir(dir, S_IRWXU) < 0) {
1342 FILE_OP_ERROR(dir, "mkdir");
1345 if (chmod(dir, S_IRWXU) < 0)
1346 FILE_OP_ERROR(dir, "chmod");
1352 gint remove_all_files(const gchar *dir)
1358 prev_dir = g_get_current_dir();
1360 if (chdir(dir) < 0) {
1361 FILE_OP_ERROR(dir, "chdir");
1365 if ((dp = opendir(".")) == NULL) {
1366 FILE_OP_ERROR(dir, "opendir");
1370 while ((d = readdir(dp)) != NULL) {
1371 if (!strcmp(d->d_name, ".") ||
1372 !strcmp(d->d_name, ".."))
1375 if (unlink(d->d_name) < 0)
1376 FILE_OP_ERROR(d->d_name, "unlink");
1381 if (chdir(prev_dir) < 0) {
1382 FILE_OP_ERROR(prev_dir, "chdir");
1392 gint remove_numbered_files(const gchar *dir, guint first, guint last)
1399 prev_dir = g_get_current_dir();
1401 if (chdir(dir) < 0) {
1402 FILE_OP_ERROR(dir, "chdir");
1406 if ((dp = opendir(".")) == NULL) {
1407 FILE_OP_ERROR(dir, "opendir");
1411 while ((d = readdir(dp)) != NULL) {
1412 fileno = to_number(d->d_name);
1413 if (fileno >= 0 && first <= fileno && fileno <= last) {
1414 if (unlink(d->d_name) < 0)
1415 FILE_OP_ERROR(d->d_name, "unlink");
1421 if (chdir(prev_dir) < 0) {
1422 FILE_OP_ERROR(prev_dir, "chdir");
1432 gint remove_all_numbered_files(const gchar *dir)
1434 return remove_numbered_files(dir, 0, UINT_MAX);
1437 gint remove_dir_recursive(const gchar *dir)
1444 /*g_print("dir = %s\n", dir);*/
1446 if (stat(dir, &s) < 0) {
1447 FILE_OP_ERROR(dir, "stat");
1448 if (ENOENT == errno) return 0;
1452 if (!S_ISDIR(s.st_mode)) {
1453 if (unlink(dir) < 0) {
1454 FILE_OP_ERROR(dir, "unlink");
1461 prev_dir = g_get_current_dir();
1462 /*g_print("prev_dir = %s\n", prev_dir);*/
1464 if (!path_cmp(prev_dir, dir)) {
1466 if (chdir("..") < 0) {
1467 FILE_OP_ERROR(dir, "chdir");
1470 prev_dir = g_get_current_dir();
1473 if (chdir(dir) < 0) {
1474 FILE_OP_ERROR(dir, "chdir");
1479 if ((dp = opendir(".")) == NULL) {
1480 FILE_OP_ERROR(dir, "opendir");
1486 /* remove all files in the directory */
1487 while ((d = readdir(dp)) != NULL) {
1488 if (!strcmp(d->d_name, ".") ||
1489 !strcmp(d->d_name, ".."))
1492 if (stat(d->d_name, &s) < 0) {
1493 FILE_OP_ERROR(d->d_name, "stat");
1497 /*g_print("removing %s\n", d->d_name);*/
1499 if (S_ISDIR(s.st_mode)) {
1500 if (remove_dir_recursive(d->d_name) < 0) {
1501 g_warning("can't remove directory\n");
1505 if (unlink(d->d_name) < 0)
1506 FILE_OP_ERROR(d->d_name, "unlink");
1512 if (chdir(prev_dir) < 0) {
1513 FILE_OP_ERROR(prev_dir, "chdir");
1520 if (rmdir(dir) < 0) {
1521 FILE_OP_ERROR(dir, "rmdir");
1529 /* this seems to be slower than the stdio version... */
1530 gint copy_file(const gchar *src, const gchar *dest)
1532 gint src_fd, dest_fd;
1536 gchar *dest_bak = NULL;
1538 if ((src_fd = open(src, O_RDONLY)) < 0) {
1539 FILE_OP_ERROR(src, "open");
1543 if (is_file_exist(dest)) {
1544 dest_bak = g_strconcat(dest, ".bak", NULL);
1545 if (rename(dest, dest_bak) < 0) {
1546 FILE_OP_ERROR(dest, "rename");
1553 if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
1554 FILE_OP_ERROR(dest, "open");
1557 if (rename(dest_bak, dest) < 0)
1558 FILE_OP_ERROR(dest_bak, "rename");
1564 while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
1569 n_write = write(dest_fd, bufp, len);
1571 g_warning(_("writing to %s failed.\n"), dest);
1576 if (rename(dest_bak, dest) < 0)
1577 FILE_OP_ERROR(dest_bak, "rename");
1590 if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
1591 g_warning(_("File copy from %s to %s failed.\n"), src, dest);
1594 if (rename(dest_bak, dest) < 0)
1595 FILE_OP_ERROR(dest_bak, "rename");
1606 gint copy_file(const gchar *src, const gchar *dest)
1608 FILE *src_fp, *dest_fp;
1611 gchar *dest_bak = NULL;
1612 gboolean err = FALSE;
1614 if ((src_fp = fopen(src, "r")) == NULL) {
1615 FILE_OP_ERROR(src, "fopen");
1618 if (is_file_exist(dest)) {
1619 dest_bak = g_strconcat(dest, ".bak", NULL);
1620 if (rename(dest, dest_bak) < 0) {
1621 FILE_OP_ERROR(dest, "rename");
1628 if ((dest_fp = fopen(dest, "w")) == NULL) {
1629 FILE_OP_ERROR(dest, "fopen");
1632 if (rename(dest_bak, dest) < 0)
1633 FILE_OP_ERROR(dest_bak, "rename");
1639 if (change_file_mode_rw(dest_fp, dest) < 0) {
1640 FILE_OP_ERROR(dest, "chmod");
1641 g_warning(_("can't change file mode\n"));
1644 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
1645 if (n_read < sizeof(buf) && ferror(src_fp))
1647 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
1648 g_warning(_("writing to %s failed.\n"), dest);
1653 if (rename(dest_bak, dest) < 0)
1654 FILE_OP_ERROR(dest_bak, "rename");
1661 if (ferror(src_fp)) {
1662 FILE_OP_ERROR(src, "fread");
1666 if (fclose(dest_fp) == EOF) {
1667 FILE_OP_ERROR(dest, "fclose");
1674 if (rename(dest_bak, dest) < 0)
1675 FILE_OP_ERROR(dest_bak, "rename");
1686 gint move_file(const gchar *src, const gchar *dest)
1688 if (is_file_exist(dest)) {
1689 g_warning(_("move_file(): file %s already exists."), dest);
1693 if (rename(src, dest) == 0) return 0;
1695 if (EXDEV != errno) {
1696 FILE_OP_ERROR(src, "rename");
1700 if (copy_file(src, dest) < 0) return -1;
1707 gint change_file_mode_rw(FILE *fp, const gchar *file)
1710 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
1712 return chmod(file, S_IRUSR|S_IWUSR);
1716 FILE *my_tmpfile(void)
1719 const gchar suffix[] = ".XXXXXX";
1720 const gchar *tmpdir;
1722 const gchar *progname;
1728 tmpdir = g_get_tmp_dir();
1729 tmplen = strlen(tmpdir);
1730 progname = g_get_prgname();
1731 proglen = strlen(progname);
1732 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
1735 memcpy(fname, tmpdir, tmplen);
1736 fname[tmplen] = G_DIR_SEPARATOR;
1737 memcpy(fname + tmplen + 1, progname, proglen);
1738 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
1740 fd = mkstemp(fname);
1746 fp = fdopen(fd, "w+b");
1751 #endif /* HAVE_MKSTEMP */
1756 gint execute_async(gchar *const argv[])
1760 if ((pid = fork()) < 0) {
1765 if (pid == 0) { /* child process */
1768 if ((gch_pid = fork()) < 0) {
1773 if (gch_pid == 0) { /* grandchild process */
1774 execvp(argv[0], argv);
1783 waitpid(pid, NULL, 0);
1788 gint execute_sync(gchar *const argv[])
1792 if ((pid = fork()) < 0) {
1797 if (pid == 0) { /* child process */
1798 execvp(argv[0], argv);
1804 waitpid(pid, NULL, 0);
1809 gint execute_command_line(const gchar *cmdline, gboolean async)
1815 argv = strsplit_with_quote(cmdline, " ", 0);
1817 for (i = 0; argv[i] != NULL; i++) {
1818 gchar *str = argv[i];
1820 if (str[0] == '\'' || str[0] == '\"') {
1824 if (str[len - 1] == str[0]) {
1825 str[len - 1] = '\0';
1826 memmove(str, str + 1, len - 1);
1832 ret = execute_async(argv);
1834 ret = execute_sync(argv);
1840 gint open_uri(const gchar *uri, const gchar *cmdline)
1842 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
1843 gchar buf[BUFFSIZE];
1846 g_return_val_if_fail(uri != NULL, -1);
1849 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1850 !strchr(p + 2, '%'))
1851 g_snprintf(buf, sizeof(buf), cmdline, uri);
1854 g_warning(_("Open URI command line is invalid: `%s'"),
1856 g_snprintf(buf, sizeof(buf), default_cmdline, uri);
1859 execute_command_line(buf, TRUE);
1864 time_t remote_tzoffset_sec(const gchar *zone)
1866 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
1872 time_t remoteoffset;
1874 strncpy(zone3, zone, 3);
1878 if (sscanf(zone, "%c%2d%2d", &c, &h, &m) == 3 &&
1879 (c == '+' || c == '-')) {
1880 remoteoffset = ((h * 60) + m) * 60;
1882 remoteoffset = -remoteoffset;
1883 } else if (!strncmp(zone, "UT" , 2) ||
1884 !strncmp(zone, "GMT", 2)) {
1886 } else if (strlen(zone3) == 3 &&
1887 (p = strstr(ustzstr, zone3)) != NULL &&
1888 (p - ustzstr) % 3 == 0) {
1889 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
1890 remoteoffset = iustz * 3600;
1891 } else if (strlen(zone3) == 1) {
1893 case 'Z': remoteoffset = 0; break;
1894 case 'A': remoteoffset = -1; break;
1895 case 'B': remoteoffset = -2; break;
1896 case 'C': remoteoffset = -3; break;
1897 case 'D': remoteoffset = -4; break;
1898 case 'E': remoteoffset = -5; break;
1899 case 'F': remoteoffset = -6; break;
1900 case 'G': remoteoffset = -7; break;
1901 case 'H': remoteoffset = -8; break;
1902 case 'I': remoteoffset = -9; break;
1903 case 'K': remoteoffset = -10; break; /* J is not used */
1904 case 'L': remoteoffset = -11; break;
1905 case 'M': remoteoffset = -12; break;
1906 case 'N': remoteoffset = 1; break;
1907 case 'O': remoteoffset = 2; break;
1908 case 'P': remoteoffset = 3; break;
1909 case 'Q': remoteoffset = 4; break;
1910 case 'R': remoteoffset = 5; break;
1911 case 'S': remoteoffset = 6; break;
1912 case 'T': remoteoffset = 7; break;
1913 case 'U': remoteoffset = 8; break;
1914 case 'V': remoteoffset = 9; break;
1915 case 'W': remoteoffset = 10; break;
1916 case 'X': remoteoffset = 11; break;
1917 case 'Y': remoteoffset = 12; break;
1918 default: remoteoffset = 0; break;
1920 remoteoffset = remoteoffset * 3600;
1923 return remoteoffset;
1926 time_t tzoffset_sec(time_t *now)
1932 lt = localtime(now);
1934 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1936 if (lt->tm_year < gmt.tm_year)
1938 else if (lt->tm_year > gmt.tm_year)
1940 else if (lt->tm_yday < gmt.tm_yday)
1942 else if (lt->tm_yday > gmt.tm_yday)
1945 if (off >= 24 * 60) /* should be impossible */
1946 off = 23 * 60 + 59; /* if not, insert silly value */
1947 if (off <= -24 * 60)
1948 off = -(23 * 60 + 59);
1957 /* calculate timezone offset */
1958 gchar *tzoffset(time_t *now)
1960 static gchar offset_string[6];
1966 lt = localtime(now);
1968 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1970 if (lt->tm_year < gmt.tm_year)
1972 else if (lt->tm_year > gmt.tm_year)
1974 else if (lt->tm_yday < gmt.tm_yday)
1976 else if (lt->tm_yday > gmt.tm_yday)
1984 if (off >= 24 * 60) /* should be impossible */
1985 off = 23 * 60 + 59; /* if not, insert silly value */
1987 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
1989 return offset_string;
1992 void get_rfc822_date(gchar *buf, gint len)
1996 gchar day[4], mon[4];
1997 gint dd, hh, mm, ss, yyyy;
2002 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
2003 day, mon, &dd, &hh, &mm, &ss, &yyyy);
2004 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
2005 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
2008 static FILE *log_fp = NULL;
2010 void set_log_file(const gchar *filename)
2013 log_fp = fopen(filename, "w");
2015 FILE_OP_ERROR(filename, "fopen");
2018 void close_log_file(void)
2026 static guint log_verbosity_count = 0;
2028 void log_verbosity_set(gboolean verbose)
2031 log_verbosity_count++;
2032 else if (log_verbosity_count > 0)
2033 log_verbosity_count--;
2036 void debug_print_real(const gchar *format, ...)
2039 gchar buf[BUFFSIZE];
2041 if (!debug_mode) return;
2043 va_start(args, format);
2044 g_vsnprintf(buf, sizeof(buf), format, args);
2050 void log_print(const gchar *format, ...)
2053 gchar buf[BUFFSIZE];
2058 va_start(args, format);
2059 g_vsnprintf(buf, sizeof(buf), format, args);
2063 strftime(timestr, 6, "%H:%M", localtime(&t));
2064 logbuf = g_strdup_printf("[%s] %s", timestr, buf);
2066 if (debug_mode) fputs(logbuf, stdout);
2067 log_window_append(logbuf, LOG_NORMAL);
2069 fputs(logbuf, log_fp);
2072 if (log_verbosity_count)
2073 statusbar_puts_all(buf);
2077 void log_message(const gchar *format, ...)
2080 gchar buf[BUFFSIZE];
2082 va_start(args, format);
2083 g_vsnprintf(buf, sizeof(buf), format, args);
2086 if (debug_mode) g_message("%s", buf);
2087 log_window_append(buf, LOG_MSG);
2089 fputs("message: ", log_fp);
2093 statusbar_puts_all(buf);
2096 void log_warning(const gchar *format, ...)
2099 gchar buf[BUFFSIZE];
2101 va_start(args, format);
2102 g_vsnprintf(buf, sizeof(buf), format, args);
2105 g_warning("%s", buf);
2106 log_window_append(buf, LOG_WARN);
2108 fputs("*** warning: ", log_fp);
2114 void log_error(const gchar *format, ...)
2117 gchar buf[BUFFSIZE];
2119 va_start(args, format);
2120 g_vsnprintf(buf, sizeof(buf), format, args);
2123 g_warning("%s", buf);
2124 log_window_append(buf, LOG_ERROR);
2126 fputs("*** error: ", log_fp);