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];
141 g_snprintf(str, sizeof(str), "%dB", (gint)size);
142 else if (size >> 10 < 1024)
143 g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / (1 << 10));
144 else if (size >> 20 < 1024)
145 g_snprintf(str, sizeof(str), "%.2fMB", (gfloat)size / (1 << 20));
147 g_snprintf(str, sizeof(str), "%.2fGB", (gfloat)size / (1 << 30));
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);
160 /* strstr with NULL-checking */
161 gint strstr2(const gchar *s1, const gchar *s2)
163 if (s1 == NULL || s2 == NULL)
166 if( strstr(s1, s2) !=NULL)
172 gint path_cmp(const gchar *s1, const gchar *s2)
176 if (s1 == NULL || s2 == NULL) return -1;
177 if (*s1 == '\0' || *s2 == '\0') return -1;
182 if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
183 if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
185 return strncmp(s1, s2, MAX(len1, len2));
188 /* remove trailing return code */
189 gchar *strretchomp(gchar *str)
193 if (!*str) return str;
195 for (s = str + strlen(str) - 1;
196 s >= str && (*s == '\n' || *s == '\r');
203 /* remove trailing character */
204 gchar *strtailchomp(gchar *str, gchar tail_char)
208 if (!*str) return str;
209 if (tail_char == '\0') return str;
211 for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
218 /* Similar to `strstr' but this function ignores the case of both strings. */
219 gchar *strcasestr(const gchar *haystack, const gchar *needle)
221 register size_t haystack_len, needle_len;
223 haystack_len = strlen(haystack);
224 needle_len = strlen(needle);
226 if (haystack_len < needle_len || needle_len == 0)
229 while (haystack_len >= needle_len) {
230 if (!strncasecmp(haystack, needle, needle_len))
231 return (gchar *)haystack;
241 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
242 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
256 /* don't do zero fill */
261 int iswalnum(wint_t wc)
263 return isalnum((int)wc);
268 int iswspace(wint_t wc)
270 return isspace((int)wc);
275 wint_t towlower(wint_t wc)
277 if (wc >= L'A' && wc <= L'Z')
278 return wc + L'a' - L'A';
285 size_t wcslen(const wchar_t *s)
297 /* Copy SRC to DEST. */
298 wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
306 } while (c != L'\0');
313 /* Copy no more than N wide-characters of SRC to DEST. */
314 wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
324 } while (c != L'\0');
335 /* Duplicate S, returning an identical malloc'd string. */
336 wchar_t *wcsdup(const wchar_t *s)
341 new_str = g_new(wchar_t, wcslen(s) + 1);
349 /* Duplicate no more than N wide-characters of S,
350 returning an identical malloc'd string. */
351 wchar_t *wcsndup(const wchar_t *s, size_t n)
356 new_str = g_new(wchar_t, n + 1);
357 wcsncpy(new_str, s, n);
358 new_str[n] = (wchar_t)0;
365 wchar_t *strdup_mbstowcs(const gchar *s)
370 new_str = g_new(wchar_t, strlen(s) + 1);
371 if (mbstowcs(new_str, s, strlen(s) + 1) < 0) {
375 new_str = g_realloc(new_str,
376 sizeof(wchar_t) * (wcslen(new_str) + 1));
383 gchar *strdup_wcstombs(const wchar_t *s)
389 len = wcslen(s) * MB_CUR_MAX + 1;
390 new_str = g_new(gchar, len);
391 if (wcstombs(new_str, s, len) < 0) {
395 new_str = g_realloc(new_str, strlen(new_str) + 1);
402 /* Compare S1 and S2, ignoring case. */
403 gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
409 c1 = towlower(*s1++);
410 c2 = towlower(*s2++);
413 else if (c1 == 0 && c2 == 0)
420 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
421 wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
423 register size_t haystack_len, needle_len;
425 haystack_len = wcslen(haystack);
426 needle_len = wcslen(needle);
428 if (haystack_len < needle_len || needle_len == 0)
431 while (haystack_len >= needle_len) {
432 if (!wcsncasecmp(haystack, needle, needle_len))
433 return (wchar_t *)haystack;
443 /* Examine if next block is non-ASCII string */
444 gboolean is_next_nonascii(const wchar_t *s)
448 /* skip head space */
449 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
451 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
459 /* Examine if next block is multi-byte string */
460 gboolean is_next_mbs(const wchar_t *s)
464 gchar tmp[MB_LEN_MAX];
466 /* skip head space */
467 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
469 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
470 mbl = wctomb(tmp, *wp);
478 wchar_t *find_wspace(const wchar_t *s)
482 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
484 for (; *wp != (wchar_t)0; wp++) {
486 return (wchar_t *)wp;
492 /* compare subjects */
493 gint subject_compare(const gchar *s1, const gchar *s2)
497 if (!s1 || !s2) return -1;
498 if (!*s1 || !*s2) return -1;
500 Xalloca(str1, strlen(s1) + 1, return -1);
501 Xalloca(str2, strlen(s2) + 1, return -1);
508 if (!*str1 || !*str2) return -1;
510 return strcmp(str1, str2);
513 void trim_subject(gchar *str)
517 eliminate_parenthesis(str, '[', ']');
518 eliminate_parenthesis(str, '(', ')');
521 while (!strncasecmp(str, "Re:", 3)) {
523 while (isspace(*srcp)) srcp++;
524 memmove(str, srcp, strlen(srcp) + 1);
528 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
530 register gchar *srcp, *destp;
535 while ((destp = strchr(destp, op))) {
541 else if (*srcp == cl)
547 while (isspace(*srcp)) srcp++;
548 memmove(destp, srcp, strlen(srcp) + 1);
552 void extract_parenthesis(gchar *str, gchar op, gchar cl)
554 register gchar *srcp, *destp;
559 while ((srcp = strchr(destp, op))) {
562 memmove(destp, srcp + 1, strlen(srcp));
567 else if (*destp == cl)
579 void extract_one_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
582 register gchar *srcp, *destp;
584 gboolean in_quote = FALSE;
588 if ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
589 memmove(destp, srcp + 1, strlen(srcp));
592 if (*destp == op && !in_quote)
594 else if (*destp == cl && !in_quote)
596 else if (*destp == quote_chr)
608 void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
611 register gchar *srcp, *destp;
613 gboolean in_quote = FALSE;
617 while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
620 memmove(destp, srcp + 1, strlen(srcp));
623 if (*destp == op && !in_quote)
625 else if (*destp == cl && !in_quote)
627 else if (*destp == quote_chr)
639 void eliminate_quote(gchar *str, gchar quote_chr)
641 register gchar *srcp, *destp;
645 while ((destp = strchr(destp, quote_chr))) {
646 if ((srcp = strchr(destp + 1, quote_chr))) {
648 while (isspace(*srcp)) srcp++;
649 memmove(destp, srcp, strlen(srcp) + 1);
657 void extract_quote(gchar *str, gchar quote_chr)
661 if ((str = strchr(str, quote_chr))) {
662 if ((p = strchr(str + 1, quote_chr))) {
664 memmove(str, str + 1, p - str);
669 void eliminate_address_comment(gchar *str)
671 register gchar *srcp, *destp;
676 while ((destp = strchr(destp, '"'))) {
677 if ((srcp = strchr(destp + 1, '"'))) {
682 while (isspace(*srcp)) srcp++;
683 memmove(destp, srcp, strlen(srcp) + 1);
693 while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
699 else if (*srcp == ')')
705 while (isspace(*srcp)) srcp++;
706 memmove(destp, srcp, strlen(srcp) + 1);
710 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
712 gboolean in_quote = FALSE;
715 if (*str == c && !in_quote)
717 if (*str == quote_chr)
725 gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
727 gboolean in_quote = FALSE;
730 p = str + strlen(str) - 1;
732 if (*p == c && !in_quote)
742 void extract_address(gchar *str)
744 eliminate_address_comment(str);
745 if (strchr_with_skip_quote(str, '"', '<'))
746 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
750 GSList *address_list_append(GSList *addr_list, const gchar *str)
755 if (!str) return addr_list;
757 Xstrdup_a(work, str, return addr_list);
759 eliminate_address_comment(work);
762 while (workp && *workp) {
765 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
771 if (strchr_with_skip_quote(workp, '"', '<'))
772 extract_parenthesis_with_skip_quote
773 (workp, '"', '<', '>');
777 addr_list = g_slist_append(addr_list, g_strdup(workp));
785 GSList *references_list_append(GSList *msgid_list, const gchar *str)
789 if (!str) return msgid_list;
792 while (strp && *strp) {
793 const gchar *start, *end;
796 if ((start = strchr(strp, '<')) != NULL) {
797 end = strchr(start + 1, '>');
802 msgid = g_strndup(start + 1, end - start - 1);
805 msgid_list = g_slist_append(msgid_list, msgid);
815 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
820 if (!str) return group_list;
822 Xstrdup_a(work, str, return group_list);
826 while (workp && *workp) {
829 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
837 group_list = g_slist_append(group_list,
846 void remove_return(gchar *str)
848 register gchar *p = str;
851 if (*p == '\n' || *p == '\r')
852 memmove(p, p + 1, strlen(p));
858 void remove_space(gchar *str)
860 register gchar *p = str;
865 while (isspace(*(p + spc)))
868 memmove(p, p + spc, strlen(p + spc) + 1);
874 void unfold_line(gchar *str)
876 register gchar *p = str;
880 if (*p == '\n' || *p == '\r') {
883 while (isspace(*(p + spc)))
886 memmove(p, p + spc, strlen(p + spc) + 1);
892 void subst_char(gchar *str, gchar orig, gchar subst)
894 register gchar *p = str;
903 gboolean is_header_line(const gchar *str)
905 if (str[0] == ':') return FALSE;
907 while (*str != '\0' && *str != ' ') {
916 gboolean is_ascii_str(const guchar *str)
918 while (*str != '\0') {
919 if (*str != '\t' && *str != ' ' &&
920 *str != '\r' && *str != '\n' &&
921 (*str < 32 || *str >= 127))
929 gint get_quote_level(const gchar *str)
931 const gchar *first_pos;
932 const gchar *last_pos;
933 const gchar *p = str;
934 gint quote_level = -1;
936 /* speed up line processing by only searching to the last '>' */
937 if ((first_pos = strchr(str, '>')) != NULL) {
938 /* skip a line if it contains a '<' before the initial '>' */
939 if (memchr(str, '<', first_pos - str) != NULL)
941 last_pos = strrchr(first_pos, '>');
945 while (p <= last_pos) {
946 while (p < last_pos) {
955 else if (*p != '-' && !isspace(*p) && p <= last_pos) {
956 /* any characters are allowed except '-' and space */
957 while (*p != '-' && *p != '>' && !isspace(*p) &&
972 GList *uri_list_extract_filenames(const gchar *uri_list)
974 GList *result = NULL;
982 while (isspace(*p)) p++;
983 if (!strncmp(p, "file:", 5)) {
986 while (*q && *q != '\n' && *q != '\r') q++;
990 while (q > p && isspace(*q)) q--;
991 file = g_malloc(q - p + 2);
992 strncpy(file, p, q - p + 1);
993 file[q - p + 1] = '\0';
994 result = g_list_append(result,file);
1005 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
1007 register guint haystack_len, needle_len;
1008 gboolean in_squote = FALSE, in_dquote = FALSE;
1010 haystack_len = strlen(haystack);
1011 needle_len = strlen(needle);
1013 if (haystack_len < needle_len || needle_len == 0)
1016 while (haystack_len >= needle_len) {
1017 if (!in_squote && !in_dquote &&
1018 !strncmp(haystack, needle, needle_len))
1019 return (gchar *)haystack;
1021 /* 'foo"bar"' -> foo"bar"
1022 "foo'bar'" -> foo'bar' */
1023 if (*haystack == '\'') {
1026 else if (!in_dquote)
1028 } else if (*haystack == '\"') {
1031 else if (!in_squote)
1042 /* this fuction was taken from gstrfuncs.c in glib. */
1043 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1046 GSList *string_list = NULL, *slist;
1047 gchar **str_array, *s;
1050 g_return_val_if_fail(str != NULL, NULL);
1051 g_return_val_if_fail(delim != NULL, NULL);
1054 max_tokens = G_MAXINT;
1056 s = strstr_with_skip_quote(str, delim);
1058 guint delimiter_len = strlen(delim);
1065 new_str = g_new(gchar, len + 1);
1066 strncpy(new_str, str, len);
1068 string_list = g_slist_prepend(string_list, new_str);
1070 str = s + delimiter_len;
1071 s = strstr_with_skip_quote(str, delim);
1072 } while (--max_tokens && s);
1077 string_list = g_slist_prepend(string_list, g_strdup(str));
1080 str_array = g_new(gchar*, n);
1084 str_array[i--] = NULL;
1085 for (slist = string_list; slist; slist = slist->next)
1086 str_array[i--] = slist->data;
1088 g_slist_free(string_list);
1094 * We need this wrapper around g_get_home_dir(), so that
1095 * we can fix some Windoze things here. Should be done in glibc of course
1096 * but as long as we are not able to do our own extensions to glibc, we do
1099 gchar *get_home_dir(void)
1101 #if HAVE_DOSISH_SYSTEM
1102 static gchar *home_dir;
1105 home_dir = read_w32_registry_string(NULL,
1106 "Software\\Sylpheed", "HomeDir" );
1107 if (!home_dir || !*home_dir) {
1108 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1109 const char *s = g_get_home_dir();
1111 home_dir = g_strdup (s);
1113 if (!home_dir || !*home_dir)
1114 home_dir = g_strdup ("c:\\sylpheed");
1116 debug_print("initialized home_dir to `%s'\n", home_dir);
1119 #else /* standard glib */
1120 return g_get_home_dir();
1124 gchar *get_rc_dir(void)
1126 static gchar *rc_dir = NULL;
1129 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1135 gchar *get_news_cache_dir(void)
1137 static gchar *news_cache_dir = NULL;
1139 if (!news_cache_dir)
1140 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1141 NEWS_CACHE_DIR, NULL);
1143 return news_cache_dir;
1146 gchar *get_imap_cache_dir(void)
1148 static gchar *imap_cache_dir = NULL;
1150 if (!imap_cache_dir)
1151 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1152 IMAP_CACHE_DIR, NULL);
1154 return imap_cache_dir;
1157 gchar *get_mbox_cache_dir(void)
1159 static gchar *mbox_cache_dir = NULL;
1161 if (!mbox_cache_dir)
1162 mbox_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1163 MBOX_CACHE_DIR, NULL);
1165 return mbox_cache_dir;
1168 gchar *get_mime_tmp_dir(void)
1170 static gchar *mime_tmp_dir = NULL;
1173 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1174 MIME_TMP_DIR, NULL);
1176 return mime_tmp_dir;
1179 gchar *get_tmp_file(void)
1181 static gchar *tmp_file = NULL;
1184 tmp_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1190 gchar *get_domain_name(void)
1192 static gchar *domain_name = NULL;
1195 gchar buf[BUFFSIZE] = "";
1197 if (gethostname(buf, sizeof(buf)) < 0) {
1198 perror("gethostname");
1199 strcpy(buf, "unknown");
1202 domain_name = g_strdup(buf);
1208 off_t get_file_size(const gchar *file)
1212 if (stat(file, &s) < 0) {
1213 FILE_OP_ERROR(file, "stat");
1220 off_t get_left_file_size(FILE *fp)
1226 if ((pos = ftell(fp)) < 0) {
1230 if (fseek(fp, 0L, SEEK_END) < 0) {
1234 if ((end = ftell(fp)) < 0) {
1239 if (fseek(fp, pos, SEEK_SET) < 0) {
1247 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1251 if (stat(file, &s) < 0) {
1252 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1256 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1262 gboolean is_dir_exist(const gchar *dir)
1266 if (stat(dir, &s) < 0) {
1267 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1271 if (S_ISDIR(s.st_mode))
1277 gint change_dir(const gchar *dir)
1279 gchar *prevdir = NULL;
1282 prevdir = g_get_current_dir();
1284 if (chdir(dir) < 0) {
1285 FILE_OP_ERROR(dir, "chdir");
1286 if (debug_mode) g_free(prevdir);
1288 } else if (debug_mode) {
1291 cwd = g_get_current_dir();
1292 if (strcmp(prevdir, cwd) != 0)
1293 g_print("current dir: %s\n", cwd);
1301 gint make_dir_hier(const gchar *dir)
1306 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1307 parent_dir = g_strndup(dir, p - dir);
1308 if (*parent_dir != '\0') {
1309 if (!is_dir_exist(parent_dir)) {
1310 if (mkdir(parent_dir, S_IRWXU) < 0) {
1311 FILE_OP_ERROR(parent_dir, "mkdir");
1315 if (chmod(parent_dir, S_IRWXU) < 0)
1316 FILE_OP_ERROR(parent_dir, "chmod");
1321 if (!is_dir_exist(dir)) {
1322 if (mkdir(dir, S_IRWXU) < 0) {
1323 FILE_OP_ERROR(dir, "mkdir");
1326 if (chmod(dir, S_IRWXU) < 0)
1327 FILE_OP_ERROR(dir, "chmod");
1333 gint remove_all_files(const gchar *dir)
1339 prev_dir = g_get_current_dir();
1341 if (chdir(dir) < 0) {
1342 FILE_OP_ERROR(dir, "chdir");
1346 if ((dp = opendir(".")) == NULL) {
1347 FILE_OP_ERROR(dir, "opendir");
1351 while ((d = readdir(dp)) != NULL) {
1352 if (!strcmp(d->d_name, ".") ||
1353 !strcmp(d->d_name, ".."))
1356 if (unlink(d->d_name) < 0)
1357 FILE_OP_ERROR(d->d_name, "unlink");
1362 if (chdir(prev_dir) < 0) {
1363 FILE_OP_ERROR(prev_dir, "chdir");
1373 gint remove_numbered_files(const gchar *dir, guint first, guint last)
1380 prev_dir = g_get_current_dir();
1382 if (chdir(dir) < 0) {
1383 FILE_OP_ERROR(dir, "chdir");
1387 if ((dp = opendir(".")) == NULL) {
1388 FILE_OP_ERROR(dir, "opendir");
1392 while ((d = readdir(dp)) != NULL) {
1393 fileno = to_number(d->d_name);
1394 if (fileno >= 0 && first <= fileno && fileno <= last) {
1395 if (unlink(d->d_name) < 0)
1396 FILE_OP_ERROR(d->d_name, "unlink");
1402 if (chdir(prev_dir) < 0) {
1403 FILE_OP_ERROR(prev_dir, "chdir");
1413 gint remove_all_numbered_files(const gchar *dir)
1415 return remove_numbered_files(dir, 0, UINT_MAX);
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_sync(gchar *const argv[])
1773 if ((pid = fork()) < 0) {
1778 if (pid == 0) { /* child process */
1779 execvp(argv[0], argv);
1785 waitpid(pid, NULL, 0);
1790 gint execute_command_line(const gchar *cmdline, gboolean async)
1796 argv = strsplit_with_quote(cmdline, " ", 0);
1798 for (i = 0; argv[i] != NULL; i++) {
1799 gchar *str = argv[i];
1801 if (str[0] == '\'' || str[0] == '\"') {
1805 if (str[len - 1] == str[0]) {
1806 str[len - 1] = '\0';
1807 memmove(str, str + 1, len - 1);
1813 ret = execute_async(argv);
1815 ret = execute_sync(argv);
1821 gint open_uri(const gchar *uri, const gchar *cmdline)
1823 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
1824 gchar buf[BUFFSIZE];
1827 g_return_val_if_fail(uri != NULL, -1);
1830 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1831 !strchr(p + 2, '%'))
1832 g_snprintf(buf, sizeof(buf), cmdline, uri);
1835 g_warning(_("Open URI command line is invalid: `%s'"),
1837 g_snprintf(buf, sizeof(buf), default_cmdline, uri);
1840 execute_command_line(buf, TRUE);
1845 time_t remote_tzoffset_sec(const gchar *zone)
1847 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
1853 time_t remoteoffset;
1855 strncpy(zone3, zone, 3);
1859 if (sscanf(zone, "%c%2d%2d", &c, &h, &m) == 3 &&
1860 (c == '+' || c == '-')) {
1861 remoteoffset = ((h * 60) + m) * 60;
1863 remoteoffset = -remoteoffset;
1864 } else if (!strncmp(zone, "UT" , 2) ||
1865 !strncmp(zone, "GMT", 2)) {
1867 } else if (strlen(zone3) == 3 &&
1868 (p = strstr(ustzstr, zone3)) != NULL &&
1869 (p - ustzstr) % 3 == 0) {
1870 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
1871 remoteoffset = iustz * 3600;
1872 } else if (strlen(zone3) == 1) {
1874 case 'Z': remoteoffset = 0; break;
1875 case 'A': remoteoffset = -1; break;
1876 case 'B': remoteoffset = -2; break;
1877 case 'C': remoteoffset = -3; break;
1878 case 'D': remoteoffset = -4; break;
1879 case 'E': remoteoffset = -5; break;
1880 case 'F': remoteoffset = -6; break;
1881 case 'G': remoteoffset = -7; break;
1882 case 'H': remoteoffset = -8; break;
1883 case 'I': remoteoffset = -9; break;
1884 case 'K': remoteoffset = -10; break; /* J is not used */
1885 case 'L': remoteoffset = -11; break;
1886 case 'M': remoteoffset = -12; break;
1887 case 'N': remoteoffset = 1; break;
1888 case 'O': remoteoffset = 2; break;
1889 case 'P': remoteoffset = 3; break;
1890 case 'Q': remoteoffset = 4; break;
1891 case 'R': remoteoffset = 5; break;
1892 case 'S': remoteoffset = 6; break;
1893 case 'T': remoteoffset = 7; break;
1894 case 'U': remoteoffset = 8; break;
1895 case 'V': remoteoffset = 9; break;
1896 case 'W': remoteoffset = 10; break;
1897 case 'X': remoteoffset = 11; break;
1898 case 'Y': remoteoffset = 12; break;
1899 default: remoteoffset = 0; break;
1901 remoteoffset = remoteoffset * 3600;
1904 return remoteoffset;
1907 time_t tzoffset_sec(time_t *now)
1913 lt = localtime(now);
1915 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1917 if (lt->tm_year < gmt.tm_year)
1919 else if (lt->tm_year > gmt.tm_year)
1921 else if (lt->tm_yday < gmt.tm_yday)
1923 else if (lt->tm_yday > gmt.tm_yday)
1926 if (off >= 24 * 60) /* should be impossible */
1927 off = 23 * 60 + 59; /* if not, insert silly value */
1928 if (off <= -24 * 60)
1929 off = -(23 * 60 + 59);
1938 /* calculate timezone offset */
1939 gchar *tzoffset(time_t *now)
1941 static gchar offset_string[6];
1947 lt = localtime(now);
1949 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
1951 if (lt->tm_year < gmt.tm_year)
1953 else if (lt->tm_year > gmt.tm_year)
1955 else if (lt->tm_yday < gmt.tm_yday)
1957 else if (lt->tm_yday > gmt.tm_yday)
1965 if (off >= 24 * 60) /* should be impossible */
1966 off = 23 * 60 + 59; /* if not, insert silly value */
1968 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
1970 return offset_string;
1973 void get_rfc822_date(gchar *buf, gint len)
1977 gchar day[4], mon[4];
1978 gint dd, hh, mm, ss, yyyy;
1983 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
1984 day, mon, &dd, &hh, &mm, &ss, &yyyy);
1985 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
1986 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
1989 static FILE *log_fp = NULL;
1991 void set_log_file(const gchar *filename)
1994 log_fp = fopen(filename, "w");
1996 FILE_OP_ERROR(filename, "fopen");
1999 void close_log_file(void)
2007 static guint log_verbosity_count = 0;
2009 void log_verbosity_set(gboolean verbose)
2012 log_verbosity_count++;
2013 else if (log_verbosity_count > 0)
2014 log_verbosity_count--;
2017 void debug_print_real(const gchar *format, ...)
2020 gchar buf[BUFFSIZE];
2022 if (!debug_mode) return;
2024 va_start(args, format);
2025 g_vsnprintf(buf, sizeof(buf), format, args);
2031 void log_print(const gchar *format, ...)
2034 gchar buf[BUFFSIZE];
2039 va_start(args, format);
2040 g_vsnprintf(buf, sizeof(buf), format, args);
2044 strftime(timestr, 6, "%H:%M", localtime(&t));
2045 logbuf = g_strdup_printf("[%s] %s", timestr, buf);
2047 if (debug_mode) fputs(logbuf, stdout);
2048 log_window_append(logbuf, LOG_NORMAL);
2050 fputs(logbuf, log_fp);
2053 if (log_verbosity_count)
2054 statusbar_puts_all(buf);
2058 void log_message(const gchar *format, ...)
2061 gchar buf[BUFFSIZE];
2063 va_start(args, format);
2064 g_vsnprintf(buf, sizeof(buf), format, args);
2067 if (debug_mode) g_message("%s", buf);
2068 log_window_append(buf, LOG_MSG);
2070 fputs("message: ", log_fp);
2074 statusbar_puts_all(buf);
2077 void log_warning(const gchar *format, ...)
2080 gchar buf[BUFFSIZE];
2082 va_start(args, format);
2083 g_vsnprintf(buf, sizeof(buf), format, args);
2086 g_warning("%s", buf);
2087 log_window_append(buf, LOG_WARN);
2089 fputs("*** warning: ", log_fp);
2095 void log_error(const gchar *format, ...)
2098 gchar buf[BUFFSIZE];
2100 va_start(args, format);
2101 g_vsnprintf(buf, sizeof(buf), format, args);
2104 g_warning("%s", buf);
2105 log_window_append(buf, LOG_ERROR);
2107 fputs("*** error: ", log_fp);