root / libsylph / utils.c @ 2259
History | View | Annotate | Download (84 kB)
| 1 | /*
|
|---|---|
| 2 | * LibSylph -- E-Mail client library |
| 3 | * Copyright (C) 1999-2007 Hiroyuki Yamamoto |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library 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 GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H
|
| 21 | # include "config.h" |
| 22 | #endif
|
| 23 | |
| 24 | #include "defs.h" |
| 25 | |
| 26 | #include <glib.h> |
| 27 | #include <glib/gi18n.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <ctype.h> |
| 31 | #include <errno.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <unistd.h> |
| 36 | #include <stdarg.h> |
| 37 | #include <sys/types.h> |
| 38 | #if HAVE_SYS_WAIT_H
|
| 39 | # include <sys/wait.h> |
| 40 | #endif
|
| 41 | #include <dirent.h> |
| 42 | #include <time.h> |
| 43 | |
| 44 | #ifdef G_OS_WIN32
|
| 45 | #ifndef WINVER
|
| 46 | # define WINVER 0x0500 |
| 47 | #endif
|
| 48 | # include <windows.h> |
| 49 | # include <wchar.h> |
| 50 | # include <direct.h> |
| 51 | # include <io.h> |
| 52 | # include <shlobj.h> |
| 53 | #endif
|
| 54 | |
| 55 | #include "utils.h" |
| 56 | #include "socket.h" |
| 57 | |
| 58 | #define BUFFSIZE 8192 |
| 59 | |
| 60 | static gboolean debug_mode = FALSE;
|
| 61 | |
| 62 | |
| 63 | #if !GLIB_CHECK_VERSION(2, 7, 0) && !defined(G_OS_UNIX) |
| 64 | gint g_chdir(const gchar *path)
|
| 65 | {
|
| 66 | #ifdef G_OS_WIN32
|
| 67 | if (G_WIN32_HAVE_WIDECHAR_API()) {
|
| 68 | wchar_t *wpath; |
| 69 | gint retval; |
| 70 | gint save_errno; |
| 71 | |
| 72 | wpath = g_utf8_to_utf16(path, -1, NULL, NULL, NULL); |
| 73 | if (wpath == NULL) { |
| 74 | errno = EINVAL; |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | retval = _wchdir(wpath); |
| 79 | save_errno = errno; |
| 80 | |
| 81 | g_free(wpath); |
| 82 | |
| 83 | errno = save_errno; |
| 84 | return retval;
|
| 85 | } else {
|
| 86 | gchar *cp_path; |
| 87 | gint retval; |
| 88 | gint save_errno; |
| 89 | |
| 90 | cp_path = g_locale_from_utf8(path, -1, NULL, NULL, NULL); |
| 91 | if (cp_path == NULL) { |
| 92 | errno = EINVAL; |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | retval = chdir(cp_path); |
| 97 | save_errno = errno; |
| 98 | |
| 99 | g_free(cp_path); |
| 100 | |
| 101 | errno = save_errno; |
| 102 | return retval;
|
| 103 | } |
| 104 | #else
|
| 105 | return chdir(path);
|
| 106 | #endif
|
| 107 | } |
| 108 | |
| 109 | gint g_chmod(const gchar *path, gint mode)
|
| 110 | {
|
| 111 | #ifdef G_OS_WIN32
|
| 112 | if (G_WIN32_HAVE_WIDECHAR_API()) {
|
| 113 | wchar_t *wpath; |
| 114 | gint retval; |
| 115 | gint save_errno; |
| 116 | |
| 117 | wpath = g_utf8_to_utf16(path, -1, NULL, NULL, NULL); |
| 118 | if (wpath == NULL) { |
| 119 | errno = EINVAL; |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | retval = _wchmod(wpath, mode); |
| 124 | save_errno = errno; |
| 125 | |
| 126 | g_free(wpath); |
| 127 | |
| 128 | errno = save_errno; |
| 129 | return retval;
|
| 130 | } else {
|
| 131 | gchar *cp_path; |
| 132 | gint retval; |
| 133 | gint save_errno; |
| 134 | |
| 135 | cp_path = g_locale_from_utf8(path, -1, NULL, NULL, NULL); |
| 136 | if (cp_path == NULL) { |
| 137 | errno = EINVAL; |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | retval = chmod(cp_path, mode); |
| 142 | save_errno = errno; |
| 143 | |
| 144 | g_free(cp_path); |
| 145 | |
| 146 | errno = save_errno; |
| 147 | return retval;
|
| 148 | } |
| 149 | #else
|
| 150 | return chmod(path, mode);
|
| 151 | #endif
|
| 152 | } |
| 153 | #endif /* GLIB_CHECK_VERSION && G_OS_UNIX */ |
| 154 | |
| 155 | #ifndef G_OS_UNIX
|
| 156 | gint syl_link(const gchar *src, const gchar *dest) |
| 157 | {
|
| 158 | #ifdef G_OS_WIN32
|
| 159 | wchar_t *wsrc; |
| 160 | wchar_t *wdest; |
| 161 | gint retval; |
| 162 | gint save_errno; |
| 163 | |
| 164 | wsrc = g_utf8_to_utf16(src, -1, NULL, NULL, NULL); |
| 165 | if (wsrc == NULL) { |
| 166 | errno = EINVAL; |
| 167 | return -1; |
| 168 | } |
| 169 | wdest = g_utf8_to_utf16(dest, -1, NULL, NULL, NULL); |
| 170 | if (wdest == NULL) { |
| 171 | g_free(wsrc); |
| 172 | errno = EINVAL; |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | errno = 0;
|
| 177 | if (CreateHardLinkW(wdest, wsrc, NULL)) { |
| 178 | retval = 0;
|
| 179 | /* debug_print("hard link created: %s -> %s\n", src, dest); */
|
| 180 | } else {
|
| 181 | retval = -1;
|
| 182 | switch (GetLastError()) {
|
| 183 | case ERROR_FILE_NOT_FOUND:
|
| 184 | case ERROR_PATH_NOT_FOUND:
|
| 185 | errno = ENOENT; break;
|
| 186 | case ERROR_ACCESS_DENIED:
|
| 187 | case ERROR_LOCK_VIOLATION:
|
| 188 | case ERROR_SHARING_VIOLATION:
|
| 189 | errno = EACCES; break;
|
| 190 | case ERROR_NOT_SAME_DEVICE:
|
| 191 | errno = EXDEV; break;
|
| 192 | case ERROR_FILE_EXISTS:
|
| 193 | case ERROR_ALREADY_EXISTS:
|
| 194 | errno = EEXIST; break;
|
| 195 | case ERROR_TOO_MANY_LINKS:
|
| 196 | errno = EMLINK; break;
|
| 197 | default:
|
| 198 | errno = EIO; break;
|
| 199 | } |
| 200 | } |
| 201 | save_errno = errno; |
| 202 | |
| 203 | g_free(wdest); |
| 204 | g_free(wsrc); |
| 205 | |
| 206 | errno = save_errno; |
| 207 | return retval;
|
| 208 | #else
|
| 209 | return link(src, dest);
|
| 210 | #endif
|
| 211 | } |
| 212 | #endif /* !G_OS_UNIX */ |
| 213 | |
| 214 | void list_free_strings(GList *list)
|
| 215 | {
|
| 216 | list = g_list_first(list); |
| 217 | |
| 218 | while (list != NULL) { |
| 219 | g_free(list->data); |
| 220 | list = list->next; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void slist_free_strings(GSList *list)
|
| 225 | {
|
| 226 | while (list != NULL) { |
| 227 | g_free(list->data); |
| 228 | list = list->next; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static void hash_free_strings_func(gpointer key, gpointer value, gpointer data) |
| 233 | {
|
| 234 | g_free(key); |
| 235 | } |
| 236 | |
| 237 | void hash_free_strings(GHashTable *table)
|
| 238 | {
|
| 239 | g_hash_table_foreach(table, hash_free_strings_func, NULL);
|
| 240 | } |
| 241 | |
| 242 | static void hash_free_value_mem_func(gpointer key, gpointer value, |
| 243 | gpointer data) |
| 244 | {
|
| 245 | g_free(value); |
| 246 | } |
| 247 | |
| 248 | void hash_free_value_mem(GHashTable *table)
|
| 249 | {
|
| 250 | g_hash_table_foreach(table, hash_free_value_mem_func, NULL);
|
| 251 | } |
| 252 | |
| 253 | gint str_case_equal(gconstpointer v, gconstpointer v2) |
| 254 | {
|
| 255 | return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0; |
| 256 | } |
| 257 | |
| 258 | guint str_case_hash(gconstpointer key) |
| 259 | {
|
| 260 | const gchar *p = key;
|
| 261 | guint h = *p; |
| 262 | |
| 263 | if (h) {
|
| 264 | h = g_ascii_tolower(h); |
| 265 | for (p += 1; *p != '\0'; p++) |
| 266 | h = (h << 5) - h + g_ascii_tolower(*p);
|
| 267 | } |
| 268 | |
| 269 | return h;
|
| 270 | } |
| 271 | |
| 272 | void ptr_array_free_strings(GPtrArray *array)
|
| 273 | {
|
| 274 | gint i; |
| 275 | gchar *str; |
| 276 | |
| 277 | g_return_if_fail(array != NULL);
|
| 278 | |
| 279 | for (i = 0; i < array->len; i++) { |
| 280 | str = g_ptr_array_index(array, i); |
| 281 | g_free(str); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | gboolean str_find(const gchar *haystack, const gchar *needle) |
| 286 | {
|
| 287 | return strstr(haystack, needle) != NULL ? TRUE : FALSE; |
| 288 | } |
| 289 | |
| 290 | gboolean str_case_find(const gchar *haystack, const gchar *needle) |
| 291 | {
|
| 292 | return strcasestr(haystack, needle) != NULL ? TRUE : FALSE; |
| 293 | } |
| 294 | |
| 295 | gboolean str_find_equal(const gchar *haystack, const gchar *needle) |
| 296 | {
|
| 297 | return strcmp(haystack, needle) == 0; |
| 298 | } |
| 299 | |
| 300 | gboolean str_case_find_equal(const gchar *haystack, const gchar *needle) |
| 301 | {
|
| 302 | return g_ascii_strcasecmp(haystack, needle) == 0; |
| 303 | } |
| 304 | |
| 305 | gint to_number(const gchar *nstr)
|
| 306 | {
|
| 307 | register const gchar *p; |
| 308 | |
| 309 | if (*nstr == '\0') return -1; |
| 310 | |
| 311 | for (p = nstr; *p != '\0'; p++) |
| 312 | if (!g_ascii_isdigit(*p)) return -1; |
| 313 | |
| 314 | return atoi(nstr);
|
| 315 | } |
| 316 | |
| 317 | /* convert integer into string,
|
| 318 | nstr must be not lower than 11 characters length */ |
| 319 | gchar *itos_buf(gchar *nstr, gint n) |
| 320 | {
|
| 321 | g_snprintf(nstr, 11, "%d", n); |
| 322 | return nstr;
|
| 323 | } |
| 324 | |
| 325 | /* convert integer into string */
|
| 326 | gchar *itos(gint n) |
| 327 | {
|
| 328 | static gchar nstr[11]; |
| 329 | |
| 330 | return itos_buf(nstr, n);
|
| 331 | } |
| 332 | |
| 333 | gchar *to_human_readable(gint64 size) |
| 334 | {
|
| 335 | static gchar str[16]; |
| 336 | |
| 337 | if (size < 1024) |
| 338 | g_snprintf(str, sizeof(str), "%dB", (gint)size); |
| 339 | else if ((size >> 10) < 1024) |
| 340 | g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / (1 << 10)); |
| 341 | else if ((size >> 20) < 1024) |
| 342 | g_snprintf(str, sizeof(str), "%.2fMB", (gfloat)size / (1 << 20)); |
| 343 | else
|
| 344 | g_snprintf(str, sizeof(str), "%.2fGB", (gfloat)size / (1 << 30)); |
| 345 | |
| 346 | return str;
|
| 347 | } |
| 348 | |
| 349 | /* strcmp with NULL-checking */
|
| 350 | gint strcmp2(const gchar *s1, const gchar *s2) |
| 351 | {
|
| 352 | if (s1 == NULL || s2 == NULL) |
| 353 | return -1; |
| 354 | else
|
| 355 | return strcmp(s1, s2);
|
| 356 | } |
| 357 | |
| 358 | /* compare paths */
|
| 359 | gint path_cmp(const gchar *s1, const gchar *s2) |
| 360 | {
|
| 361 | gint len1, len2; |
| 362 | #ifdef G_OS_WIN32
|
| 363 | gchar *s1_, *s2_; |
| 364 | #endif
|
| 365 | |
| 366 | if (s1 == NULL || s2 == NULL) return -1; |
| 367 | if (*s1 == '\0' || *s2 == '\0') return -1; |
| 368 | |
| 369 | len1 = strlen(s1); |
| 370 | len2 = strlen(s2); |
| 371 | |
| 372 | #ifdef G_OS_WIN32
|
| 373 | Xstrdup_a(s1_, s1, return -1); |
| 374 | Xstrdup_a(s2_, s2, return -1); |
| 375 | subst_char(s1_, '/', G_DIR_SEPARATOR);
|
| 376 | subst_char(s2_, '/', G_DIR_SEPARATOR);
|
| 377 | if (s1_[len1 - 1] == G_DIR_SEPARATOR) len1--; |
| 378 | if (s2_[len2 - 1] == G_DIR_SEPARATOR) len2--; |
| 379 | |
| 380 | return strncmp(s1_, s2_, MAX(len1, len2));
|
| 381 | #else
|
| 382 | if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--; |
| 383 | if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--; |
| 384 | |
| 385 | return strncmp(s1, s2, MAX(len1, len2));
|
| 386 | #endif
|
| 387 | } |
| 388 | |
| 389 | /* remove trailing return code */
|
| 390 | gchar *strretchomp(gchar *str) |
| 391 | {
|
| 392 | register gchar *s;
|
| 393 | |
| 394 | if (!*str) return str; |
| 395 | |
| 396 | for (s = str + strlen(str) - 1; |
| 397 | s >= str && (*s == '\n' || *s == '\r'); |
| 398 | s--) |
| 399 | *s = '\0';
|
| 400 | |
| 401 | return str;
|
| 402 | } |
| 403 | |
| 404 | /* remove trailing character */
|
| 405 | gchar *strtailchomp(gchar *str, gchar tail_char) |
| 406 | {
|
| 407 | register gchar *s;
|
| 408 | |
| 409 | if (!*str) return str; |
| 410 | if (tail_char == '\0') return str; |
| 411 | |
| 412 | for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--) |
| 413 | *s = '\0';
|
| 414 | |
| 415 | return str;
|
| 416 | } |
| 417 | |
| 418 | /* remove CR (carriage return) */
|
| 419 | gchar *strcrchomp(gchar *str) |
| 420 | {
|
| 421 | register gchar *s;
|
| 422 | |
| 423 | if (!*str) return str; |
| 424 | |
| 425 | s = str + strlen(str) - 1;
|
| 426 | if (*s == '\n' && s > str && *(s - 1) == '\r') { |
| 427 | *(s - 1) = '\n'; |
| 428 | *s = '\0';
|
| 429 | } |
| 430 | |
| 431 | return str;
|
| 432 | } |
| 433 | |
| 434 | /* Similar to `strstr' but this function ignores the case of both strings. */
|
| 435 | gchar *strcasestr(const gchar *haystack, const gchar *needle) |
| 436 | {
|
| 437 | register size_t haystack_len, needle_len;
|
| 438 | |
| 439 | haystack_len = strlen(haystack); |
| 440 | needle_len = strlen(needle); |
| 441 | |
| 442 | if (haystack_len < needle_len || needle_len == 0) |
| 443 | return NULL; |
| 444 | |
| 445 | while (haystack_len >= needle_len) {
|
| 446 | if (!g_ascii_strncasecmp(haystack, needle, needle_len))
|
| 447 | return (gchar *)haystack;
|
| 448 | else {
|
| 449 | haystack++; |
| 450 | haystack_len--; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | return NULL; |
| 455 | } |
| 456 | |
| 457 | gpointer my_memmem(gconstpointer haystack, size_t haystacklen, |
| 458 | gconstpointer needle, size_t needlelen) |
| 459 | {
|
| 460 | const gchar *haystack_ = (const gchar *)haystack; |
| 461 | const gchar *needle_ = (const gchar *)needle; |
| 462 | const gchar *haystack_cur = (const gchar *)haystack; |
| 463 | size_t haystack_left = haystacklen; |
| 464 | |
| 465 | if (needlelen == 1) |
| 466 | return memchr(haystack_, *needle_, haystacklen);
|
| 467 | |
| 468 | while ((haystack_cur = memchr(haystack_cur, *needle_, haystack_left))
|
| 469 | != NULL) {
|
| 470 | if (haystacklen - (haystack_cur - haystack_) < needlelen)
|
| 471 | break;
|
| 472 | if (memcmp(haystack_cur + 1, needle_ + 1, needlelen - 1) == 0) |
| 473 | return (gpointer)haystack_cur;
|
| 474 | else {
|
| 475 | haystack_cur++; |
| 476 | haystack_left = haystacklen - (haystack_cur - haystack_); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return NULL; |
| 481 | } |
| 482 | |
| 483 | /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
|
| 484 | gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
|
| 485 | {
|
| 486 | register const gchar *s = src; |
| 487 | register gchar *d = dest;
|
| 488 | |
| 489 | while (--n && *s)
|
| 490 | *d++ = *s++; |
| 491 | *d = '\0';
|
| 492 | |
| 493 | return dest;
|
| 494 | } |
| 495 | |
| 496 | /* Similar to g_str_has_suffix() but case-insensitive */
|
| 497 | gboolean str_has_suffix_case(const gchar *str, const gchar *suffix) |
| 498 | {
|
| 499 | size_t len, s_len; |
| 500 | |
| 501 | if (!str || !suffix)
|
| 502 | return FALSE;
|
| 503 | |
| 504 | len = strlen(str); |
| 505 | s_len = strlen(suffix); |
| 506 | |
| 507 | if (s_len > len)
|
| 508 | return FALSE;
|
| 509 | |
| 510 | return (g_ascii_strcasecmp(str + (len - s_len), suffix) == 0); |
| 511 | } |
| 512 | |
| 513 | gint str_find_format_times(const gchar *haystack, gchar ch)
|
| 514 | {
|
| 515 | gint n = 0;
|
| 516 | const gchar *p = haystack;
|
| 517 | |
| 518 | while ((p = strchr(p, '%')) != NULL) { |
| 519 | ++p; |
| 520 | if (*p == '%') { |
| 521 | ++p; |
| 522 | } else if (*p == ch) { |
| 523 | ++p; |
| 524 | ++n; |
| 525 | } else
|
| 526 | return -1; |
| 527 | } |
| 528 | |
| 529 | return n;
|
| 530 | } |
| 531 | |
| 532 | /* Examine if next block is non-ASCII string */
|
| 533 | gboolean is_next_nonascii(const gchar *s)
|
| 534 | {
|
| 535 | const gchar *p;
|
| 536 | gboolean in_quote = FALSE; |
| 537 | |
| 538 | /* skip head space */
|
| 539 | for (p = s; *p != '\0' && g_ascii_isspace(*p); p++) |
| 540 | ; |
| 541 | while (*p != '\0') { |
| 542 | if (!in_quote && g_ascii_isspace(*p))
|
| 543 | break;
|
| 544 | if (*p == '"') |
| 545 | in_quote ^= TRUE; |
| 546 | else if (*(guchar *)p > 127 || *(guchar *)p < 32) |
| 547 | return TRUE;
|
| 548 | ++p; |
| 549 | } |
| 550 | |
| 551 | return FALSE;
|
| 552 | } |
| 553 | |
| 554 | gint get_next_word_len(const gchar *s)
|
| 555 | {
|
| 556 | const gchar *p = s;
|
| 557 | gboolean in_quote = FALSE; |
| 558 | |
| 559 | while (*p != '\0') { |
| 560 | if (!in_quote && g_ascii_isspace(*p))
|
| 561 | break;
|
| 562 | if (*p == '"') |
| 563 | in_quote ^= TRUE; |
| 564 | ++p; |
| 565 | } |
| 566 | |
| 567 | return p - s;
|
| 568 | } |
| 569 | |
| 570 | /* compare subjects */
|
| 571 | gint subject_compare(const gchar *s1, const gchar *s2) |
| 572 | {
|
| 573 | gchar *str1, *str2; |
| 574 | |
| 575 | if (!s1 || !s2) return -1; |
| 576 | if (!*s1 || !*s2) return -1; |
| 577 | |
| 578 | Xstrdup_a(str1, s1, return -1); |
| 579 | Xstrdup_a(str2, s2, return -1); |
| 580 | |
| 581 | trim_subject_for_compare(str1); |
| 582 | trim_subject_for_compare(str2); |
| 583 | |
| 584 | if (!*str1 || !*str2) return -1; |
| 585 | |
| 586 | return strcmp(str1, str2);
|
| 587 | } |
| 588 | |
| 589 | gint subject_compare_for_sort(const gchar *s1, const gchar *s2) |
| 590 | {
|
| 591 | gchar *str1, *str2; |
| 592 | |
| 593 | if (!s1 || !s2) return -1; |
| 594 | |
| 595 | Xstrdup_a(str1, s1, return -1); |
| 596 | Xstrdup_a(str2, s2, return -1); |
| 597 | |
| 598 | trim_subject_for_sort(str1); |
| 599 | trim_subject_for_sort(str2); |
| 600 | |
| 601 | return g_ascii_strcasecmp(str1, str2);
|
| 602 | } |
| 603 | |
| 604 | void trim_subject_for_compare(gchar *str)
|
| 605 | {
|
| 606 | gchar *srcp; |
| 607 | |
| 608 | eliminate_parenthesis(str, '[', ']'); |
| 609 | eliminate_parenthesis(str, '(', ')'); |
| 610 | g_strstrip(str); |
| 611 | |
| 612 | while (!g_ascii_strncasecmp(str, "Re:", 3)) { |
| 613 | srcp = str + 3;
|
| 614 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 615 | memmove(str, srcp, strlen(srcp) + 1);
|
| 616 | } |
| 617 | } |
| 618 | |
| 619 | void trim_subject_for_sort(gchar *str)
|
| 620 | {
|
| 621 | gchar *srcp; |
| 622 | |
| 623 | g_strstrip(str); |
| 624 | |
| 625 | while (!g_ascii_strncasecmp(str, "Re:", 3)) { |
| 626 | srcp = str + 3;
|
| 627 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 628 | memmove(str, srcp, strlen(srcp) + 1);
|
| 629 | } |
| 630 | } |
| 631 | |
| 632 | void trim_subject(gchar *str)
|
| 633 | {
|
| 634 | register gchar *srcp, *destp;
|
| 635 | gchar op, cl; |
| 636 | gint in_brace; |
| 637 | |
| 638 | destp = str; |
| 639 | while (!g_ascii_strncasecmp(destp, "Re:", 3)) { |
| 640 | destp += 3;
|
| 641 | while (g_ascii_isspace(*destp)) destp++;
|
| 642 | } |
| 643 | |
| 644 | if (*destp == '[') { |
| 645 | op = '[';
|
| 646 | cl = ']';
|
| 647 | } else if (*destp == '(') { |
| 648 | op = '(';
|
| 649 | cl = ')';
|
| 650 | } else
|
| 651 | return;
|
| 652 | |
| 653 | srcp = destp + 1;
|
| 654 | in_brace = 1;
|
| 655 | while (*srcp) {
|
| 656 | if (*srcp == op)
|
| 657 | in_brace++; |
| 658 | else if (*srcp == cl) |
| 659 | in_brace--; |
| 660 | srcp++; |
| 661 | if (in_brace == 0) |
| 662 | break;
|
| 663 | } |
| 664 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 665 | memmove(destp, srcp, strlen(srcp) + 1);
|
| 666 | } |
| 667 | |
| 668 | void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
|
| 669 | {
|
| 670 | register gchar *srcp, *destp;
|
| 671 | gint in_brace; |
| 672 | |
| 673 | srcp = destp = str; |
| 674 | |
| 675 | while ((destp = strchr(destp, op))) {
|
| 676 | in_brace = 1;
|
| 677 | srcp = destp + 1;
|
| 678 | while (*srcp) {
|
| 679 | if (*srcp == op)
|
| 680 | in_brace++; |
| 681 | else if (*srcp == cl) |
| 682 | in_brace--; |
| 683 | srcp++; |
| 684 | if (in_brace == 0) |
| 685 | break;
|
| 686 | } |
| 687 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 688 | memmove(destp, srcp, strlen(srcp) + 1);
|
| 689 | } |
| 690 | } |
| 691 | |
| 692 | void extract_parenthesis(gchar *str, gchar op, gchar cl)
|
| 693 | {
|
| 694 | register gchar *srcp, *destp;
|
| 695 | gint in_brace; |
| 696 | |
| 697 | srcp = destp = str; |
| 698 | |
| 699 | while ((srcp = strchr(destp, op))) {
|
| 700 | if (destp > str)
|
| 701 | *destp++ = ' ';
|
| 702 | memmove(destp, srcp + 1, strlen(srcp));
|
| 703 | in_brace = 1;
|
| 704 | while(*destp) {
|
| 705 | if (*destp == op)
|
| 706 | in_brace++; |
| 707 | else if (*destp == cl) |
| 708 | in_brace--; |
| 709 | |
| 710 | if (in_brace == 0) |
| 711 | break;
|
| 712 | |
| 713 | destp++; |
| 714 | } |
| 715 | } |
| 716 | *destp = '\0';
|
| 717 | } |
| 718 | |
| 719 | void extract_parenthesis_with_escape(gchar *str, gchar op, gchar cl)
|
| 720 | {
|
| 721 | register gchar *srcp, *destp;
|
| 722 | gint in_brace; |
| 723 | |
| 724 | srcp = destp = str; |
| 725 | |
| 726 | while ((srcp = strchr(srcp, op))) {
|
| 727 | if (destp > str)
|
| 728 | *destp++ = ' ';
|
| 729 | ++srcp; |
| 730 | in_brace = 1;
|
| 731 | while (*srcp) {
|
| 732 | if (*srcp == op)
|
| 733 | in_brace++; |
| 734 | else if (*srcp == cl) |
| 735 | in_brace--; |
| 736 | |
| 737 | if (in_brace == 0) |
| 738 | break;
|
| 739 | |
| 740 | if (*srcp == '\\' && *(srcp + 1) != '\0') |
| 741 | ++srcp; |
| 742 | |
| 743 | *destp++ = *srcp++; |
| 744 | } |
| 745 | } |
| 746 | *destp = '\0';
|
| 747 | } |
| 748 | |
| 749 | void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
|
| 750 | gchar op, gchar cl) |
| 751 | {
|
| 752 | register gchar *srcp, *destp;
|
| 753 | gint in_brace; |
| 754 | gboolean in_quote = FALSE; |
| 755 | |
| 756 | srcp = destp = str; |
| 757 | |
| 758 | while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
|
| 759 | if (destp > str)
|
| 760 | *destp++ = ' ';
|
| 761 | memmove(destp, srcp + 1, strlen(srcp));
|
| 762 | in_brace = 1;
|
| 763 | while(*destp) {
|
| 764 | if (*destp == op && !in_quote)
|
| 765 | in_brace++; |
| 766 | else if (*destp == cl && !in_quote) |
| 767 | in_brace--; |
| 768 | else if (*destp == quote_chr) |
| 769 | in_quote ^= TRUE; |
| 770 | |
| 771 | if (in_brace == 0) |
| 772 | break;
|
| 773 | |
| 774 | destp++; |
| 775 | } |
| 776 | } |
| 777 | *destp = '\0';
|
| 778 | } |
| 779 | |
| 780 | void eliminate_quote(gchar *str, gchar quote_chr)
|
| 781 | {
|
| 782 | register gchar *srcp, *destp;
|
| 783 | |
| 784 | srcp = destp = str; |
| 785 | |
| 786 | while ((destp = strchr(destp, quote_chr))) {
|
| 787 | if ((srcp = strchr(destp + 1, quote_chr))) { |
| 788 | srcp++; |
| 789 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 790 | memmove(destp, srcp, strlen(srcp) + 1);
|
| 791 | } else {
|
| 792 | *destp = '\0';
|
| 793 | break;
|
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | void extract_quote(gchar *str, gchar quote_chr)
|
| 799 | {
|
| 800 | register gchar *p;
|
| 801 | |
| 802 | if ((str = strchr(str, quote_chr))) {
|
| 803 | if ((p = strchr(str + 1, quote_chr))) { |
| 804 | *p = '\0';
|
| 805 | memmove(str, str + 1, p - str);
|
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | void extract_quote_with_escape(gchar *str, gchar quote_chr)
|
| 811 | {
|
| 812 | register gchar *sp, *dp;
|
| 813 | |
| 814 | if ((sp = strchr(str, quote_chr))) {
|
| 815 | dp = sp; |
| 816 | ++sp; |
| 817 | while (*sp) {
|
| 818 | if (*sp == quote_chr)
|
| 819 | break;
|
| 820 | else if (*sp == '\\' && *(sp + 1) != '\0') |
| 821 | ++sp; |
| 822 | |
| 823 | *dp++ = *sp++; |
| 824 | } |
| 825 | *dp = '\0';
|
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void eliminate_address_comment(gchar *str)
|
| 830 | {
|
| 831 | register gchar *srcp, *destp;
|
| 832 | gint in_brace; |
| 833 | |
| 834 | srcp = destp = str; |
| 835 | |
| 836 | while ((destp = strchr(destp, '"'))) { |
| 837 | if ((srcp = strchr(destp + 1, '"'))) { |
| 838 | srcp++; |
| 839 | if (*srcp == '@') { |
| 840 | destp = srcp + 1;
|
| 841 | } else {
|
| 842 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 843 | memmove(destp, srcp, strlen(srcp) + 1);
|
| 844 | } |
| 845 | } else {
|
| 846 | *destp = '\0';
|
| 847 | break;
|
| 848 | } |
| 849 | } |
| 850 | |
| 851 | srcp = destp = str; |
| 852 | |
| 853 | while ((destp = strchr_with_skip_quote(destp, '"', '('))) { |
| 854 | in_brace = 1;
|
| 855 | srcp = destp + 1;
|
| 856 | while (*srcp) {
|
| 857 | if (*srcp == '(') |
| 858 | in_brace++; |
| 859 | else if (*srcp == ')') |
| 860 | in_brace--; |
| 861 | srcp++; |
| 862 | if (in_brace == 0) |
| 863 | break;
|
| 864 | } |
| 865 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 866 | memmove(destp, srcp, strlen(srcp) + 1);
|
| 867 | } |
| 868 | } |
| 869 | |
| 870 | gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
|
| 871 | {
|
| 872 | gboolean in_quote = FALSE; |
| 873 | |
| 874 | while (*str) {
|
| 875 | if (*str == c && !in_quote)
|
| 876 | return (gchar *)str;
|
| 877 | if (*str == quote_chr)
|
| 878 | in_quote ^= TRUE; |
| 879 | str++; |
| 880 | } |
| 881 | |
| 882 | return NULL; |
| 883 | } |
| 884 | |
| 885 | gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
|
| 886 | {
|
| 887 | gboolean in_quote = FALSE; |
| 888 | const gchar *p;
|
| 889 | |
| 890 | p = str + strlen(str) - 1;
|
| 891 | while (p >= str) {
|
| 892 | if (*p == c && !in_quote)
|
| 893 | return (gchar *)p;
|
| 894 | if (*p == quote_chr)
|
| 895 | in_quote ^= TRUE; |
| 896 | p--; |
| 897 | } |
| 898 | |
| 899 | return NULL; |
| 900 | } |
| 901 | |
| 902 | void extract_address(gchar *str)
|
| 903 | {
|
| 904 | eliminate_address_comment(str); |
| 905 | if (strchr_with_skip_quote(str, '"', '<')) |
| 906 | extract_parenthesis_with_skip_quote(str, '"', '<', '>'); |
| 907 | g_strstrip(str); |
| 908 | } |
| 909 | |
| 910 | void extract_list_id_str(gchar *str)
|
| 911 | {
|
| 912 | if (strchr_with_skip_quote(str, '"', '<')) |
| 913 | extract_parenthesis_with_skip_quote(str, '"', '<', '>'); |
| 914 | g_strstrip(str); |
| 915 | } |
| 916 | |
| 917 | gchar *normalize_address_field(const gchar *str)
|
| 918 | {
|
| 919 | GString *new_str; |
| 920 | GSList *addr_list, *cur; |
| 921 | gchar *addr, *p, *q, *r; |
| 922 | gchar *ret_str; |
| 923 | |
| 924 | addr_list = address_list_append_orig(NULL, str);
|
| 925 | |
| 926 | new_str = g_string_new(NULL);
|
| 927 | |
| 928 | for (cur = addr_list; cur != NULL; cur = cur->next) { |
| 929 | p = addr = (gchar *)cur->data; |
| 930 | q = strchr_with_skip_quote(p, '"', '<'); |
| 931 | if (q && q > p) {
|
| 932 | r = q - 1;
|
| 933 | while (r > p && g_ascii_isspace(*r))
|
| 934 | --r; |
| 935 | g_string_append_len(new_str, p, r - p + 1);
|
| 936 | g_string_append_c(new_str, ' ');
|
| 937 | p = q; |
| 938 | } |
| 939 | if (*p == '<') { |
| 940 | q = strchr(p, '>');
|
| 941 | if (q) {
|
| 942 | r = q + 1;
|
| 943 | if (*r) {
|
| 944 | while (g_ascii_isspace(*r))
|
| 945 | ++r; |
| 946 | g_string_append(new_str, r); |
| 947 | if (new_str->len > 0 && |
| 948 | !g_ascii_isspace |
| 949 | (new_str->str[new_str->len - 1]))
|
| 950 | g_string_append_c(new_str, ' ');
|
| 951 | } |
| 952 | g_string_append_len(new_str, p, q - p + 1);
|
| 953 | } else {
|
| 954 | g_string_append(new_str, p); |
| 955 | g_string_append_c(new_str, '>');
|
| 956 | } |
| 957 | } else
|
| 958 | g_string_append(new_str, p); |
| 959 | |
| 960 | if (cur->next)
|
| 961 | g_string_append(new_str, ", ");
|
| 962 | } |
| 963 | |
| 964 | slist_free_strings(addr_list); |
| 965 | ret_str = new_str->str; |
| 966 | g_string_free(new_str, FALSE); |
| 967 | |
| 968 | return ret_str;
|
| 969 | } |
| 970 | |
| 971 | gboolean address_equal(const gchar *addr1, const gchar *addr2) |
| 972 | {
|
| 973 | gchar *addr1_, *addr2_; |
| 974 | |
| 975 | if (!addr1 || !addr2)
|
| 976 | return FALSE;
|
| 977 | |
| 978 | Xstrdup_a(addr1_, addr1, return FALSE);
|
| 979 | Xstrdup_a(addr2_, addr2, return FALSE);
|
| 980 | |
| 981 | extract_address(addr1_); |
| 982 | extract_address(addr2_); |
| 983 | |
| 984 | return strcmp(addr1_, addr2_) == 0; |
| 985 | } |
| 986 | |
| 987 | GSList *address_list_append_orig(GSList *addr_list, const gchar *str)
|
| 988 | {
|
| 989 | const gchar *p = str, *q;
|
| 990 | gchar *addr; |
| 991 | |
| 992 | if (!str) return addr_list; |
| 993 | |
| 994 | while (*p) {
|
| 995 | if (*p == ',' || g_ascii_isspace(*p)) { |
| 996 | ++p; |
| 997 | } else if ((q = strchr_with_skip_quote(p, '"', ','))) { |
| 998 | addr = g_strndup(p, q - p); |
| 999 | g_strstrip(addr); |
| 1000 | addr_list = g_slist_append(addr_list, addr); |
| 1001 | p = q + 1;
|
| 1002 | } else {
|
| 1003 | addr = g_strdup(p); |
| 1004 | g_strstrip(addr); |
| 1005 | addr_list = g_slist_append(addr_list, addr); |
| 1006 | break;
|
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | return addr_list;
|
| 1011 | } |
| 1012 | |
| 1013 | GSList *address_list_append(GSList *addr_list, const gchar *str)
|
| 1014 | {
|
| 1015 | gchar *work; |
| 1016 | gchar *workp; |
| 1017 | |
| 1018 | if (!str) return addr_list; |
| 1019 | |
| 1020 | Xstrdup_a(work, str, return addr_list);
|
| 1021 | |
| 1022 | eliminate_address_comment(work); |
| 1023 | workp = work; |
| 1024 | |
| 1025 | while (workp && *workp) {
|
| 1026 | gchar *p, *next; |
| 1027 | |
| 1028 | if ((p = strchr_with_skip_quote(workp, '"', ','))) { |
| 1029 | *p = '\0';
|
| 1030 | next = p + 1;
|
| 1031 | } else
|
| 1032 | next = NULL;
|
| 1033 | |
| 1034 | if (strchr_with_skip_quote(workp, '"', '<')) |
| 1035 | extract_parenthesis_with_skip_quote |
| 1036 | (workp, '"', '<', '>'); |
| 1037 | |
| 1038 | g_strstrip(workp); |
| 1039 | if (*workp)
|
| 1040 | addr_list = g_slist_append(addr_list, g_strdup(workp)); |
| 1041 | |
| 1042 | workp = next; |
| 1043 | } |
| 1044 | |
| 1045 | return addr_list;
|
| 1046 | } |
| 1047 | |
| 1048 | GSList *references_list_prepend(GSList *msgid_list, const gchar *str)
|
| 1049 | {
|
| 1050 | const gchar *strp;
|
| 1051 | |
| 1052 | if (!str) return msgid_list; |
| 1053 | strp = str; |
| 1054 | |
| 1055 | while (strp && *strp) {
|
| 1056 | const gchar *start, *end;
|
| 1057 | gchar *msgid; |
| 1058 | |
| 1059 | if ((start = strchr(strp, '<')) != NULL) { |
| 1060 | end = strchr(start + 1, '>'); |
| 1061 | if (!end) break; |
| 1062 | } else
|
| 1063 | break;
|
| 1064 | |
| 1065 | msgid = g_strndup(start + 1, end - start - 1); |
| 1066 | g_strstrip(msgid); |
| 1067 | if (*msgid)
|
| 1068 | msgid_list = g_slist_prepend(msgid_list, msgid); |
| 1069 | else
|
| 1070 | g_free(msgid); |
| 1071 | |
| 1072 | strp = end + 1;
|
| 1073 | } |
| 1074 | |
| 1075 | return msgid_list;
|
| 1076 | } |
| 1077 | |
| 1078 | GSList *references_list_append(GSList *msgid_list, const gchar *str)
|
| 1079 | {
|
| 1080 | GSList *list; |
| 1081 | |
| 1082 | list = references_list_prepend(NULL, str);
|
| 1083 | list = g_slist_reverse(list); |
| 1084 | msgid_list = g_slist_concat(msgid_list, list); |
| 1085 | |
| 1086 | return msgid_list;
|
| 1087 | } |
| 1088 | |
| 1089 | GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
|
| 1090 | {
|
| 1091 | gchar *work; |
| 1092 | gchar *workp; |
| 1093 | |
| 1094 | if (!str) return group_list; |
| 1095 | |
| 1096 | Xstrdup_a(work, str, return group_list);
|
| 1097 | |
| 1098 | workp = work; |
| 1099 | |
| 1100 | while (workp && *workp) {
|
| 1101 | gchar *p, *next; |
| 1102 | |
| 1103 | if ((p = strchr_with_skip_quote(workp, '"', ','))) { |
| 1104 | *p = '\0';
|
| 1105 | next = p + 1;
|
| 1106 | } else
|
| 1107 | next = NULL;
|
| 1108 | |
| 1109 | g_strstrip(workp); |
| 1110 | if (*workp)
|
| 1111 | group_list = g_slist_append(group_list, |
| 1112 | g_strdup(workp)); |
| 1113 | |
| 1114 | workp = next; |
| 1115 | } |
| 1116 | |
| 1117 | return group_list;
|
| 1118 | } |
| 1119 | |
| 1120 | GList *add_history(GList *list, const gchar *str)
|
| 1121 | {
|
| 1122 | GList *old; |
| 1123 | |
| 1124 | g_return_val_if_fail(str != NULL, list);
|
| 1125 | |
| 1126 | old = g_list_find_custom(list, (gpointer)str, (GCompareFunc)strcmp2); |
| 1127 | if (old) {
|
| 1128 | g_free(old->data); |
| 1129 | list = g_list_remove(list, old->data); |
| 1130 | } else if (g_list_length(list) >= MAX_HISTORY_SIZE) { |
| 1131 | GList *last; |
| 1132 | |
| 1133 | last = g_list_last(list); |
| 1134 | if (last) {
|
| 1135 | g_free(last->data); |
| 1136 | list = g_list_remove(list, last->data); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | list = g_list_prepend(list, g_strdup(str)); |
| 1141 | |
| 1142 | return list;
|
| 1143 | } |
| 1144 | |
| 1145 | void remove_return(gchar *str)
|
| 1146 | {
|
| 1147 | register gchar *p = str;
|
| 1148 | |
| 1149 | while (*p) {
|
| 1150 | if (*p == '\n' || *p == '\r') |
| 1151 | memmove(p, p + 1, strlen(p));
|
| 1152 | else
|
| 1153 | p++; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | void remove_space(gchar *str)
|
| 1158 | {
|
| 1159 | register gchar *p = str;
|
| 1160 | register gint spc;
|
| 1161 | |
| 1162 | while (*p) {
|
| 1163 | spc = 0;
|
| 1164 | while (g_ascii_isspace(*(p + spc)))
|
| 1165 | spc++; |
| 1166 | if (spc)
|
| 1167 | memmove(p, p + spc, strlen(p + spc) + 1);
|
| 1168 | else
|
| 1169 | p++; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | void unfold_line(gchar *str)
|
| 1174 | {
|
| 1175 | register gchar *p = str;
|
| 1176 | register gint spc;
|
| 1177 | |
| 1178 | while (*p) {
|
| 1179 | if (*p == '\n' || *p == '\r') { |
| 1180 | *p++ = ' ';
|
| 1181 | spc = 0;
|
| 1182 | while (g_ascii_isspace(*(p + spc)))
|
| 1183 | spc++; |
| 1184 | if (spc)
|
| 1185 | memmove(p, p + spc, strlen(p + spc) + 1);
|
| 1186 | } else
|
| 1187 | p++; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | void subst_char(gchar *str, gchar orig, gchar subst)
|
| 1192 | {
|
| 1193 | register gchar *p = str;
|
| 1194 | |
| 1195 | while (*p) {
|
| 1196 | if (*p == orig)
|
| 1197 | *p = subst; |
| 1198 | p++; |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | void subst_chars(gchar *str, gchar *orig, gchar subst)
|
| 1203 | {
|
| 1204 | register gchar *p = str;
|
| 1205 | |
| 1206 | while (*p) {
|
| 1207 | if (strchr(orig, *p) != NULL) |
| 1208 | *p = subst; |
| 1209 | ++p; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | void subst_null(gchar *str, gint len, gchar subst)
|
| 1214 | {
|
| 1215 | register gchar *p = str;
|
| 1216 | |
| 1217 | while (len--) {
|
| 1218 | if (*p == '\0') |
| 1219 | *p = subst; |
| 1220 | ++p; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | void subst_control(gchar *str, gchar subst)
|
| 1225 | {
|
| 1226 | register gchar *p = str;
|
| 1227 | |
| 1228 | while (*p) {
|
| 1229 | if (g_ascii_iscntrl(*p))
|
| 1230 | *p = subst; |
| 1231 | ++p; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | void subst_for_filename(gchar *str)
|
| 1236 | {
|
| 1237 | subst_chars(str, " \t\r\n\"'\\/:;*?<>|", '_'); |
| 1238 | } |
| 1239 | |
| 1240 | gchar *get_alt_filename(const gchar *filename, gint count)
|
| 1241 | {
|
| 1242 | const gchar *ext;
|
| 1243 | gchar *alt_filename; |
| 1244 | |
| 1245 | ext = strrchr(filename, '.');
|
| 1246 | |
| 1247 | if (ext) {
|
| 1248 | gchar *base; |
| 1249 | |
| 1250 | base = g_strndup(filename, ext - filename); |
| 1251 | alt_filename = g_strdup_printf("%s-%d%s", base, count, ext);
|
| 1252 | g_free(base); |
| 1253 | } else
|
| 1254 | alt_filename = g_strdup_printf("%s-%d", filename, count);
|
| 1255 | |
| 1256 | return alt_filename;
|
| 1257 | } |
| 1258 | |
| 1259 | gboolean is_header_line(const gchar *str)
|
| 1260 | {
|
| 1261 | if (str[0] == ':') return FALSE; |
| 1262 | |
| 1263 | while (*str != '\0' && *str != ' ') { |
| 1264 | if (*str == ':') |
| 1265 | return TRUE;
|
| 1266 | str++; |
| 1267 | } |
| 1268 | |
| 1269 | return FALSE;
|
| 1270 | } |
| 1271 | |
| 1272 | gboolean is_ascii_str(const gchar *str)
|
| 1273 | {
|
| 1274 | const guchar *p = (const guchar *)str; |
| 1275 | |
| 1276 | while (*p != '\0') { |
| 1277 | if (*p != '\t' && *p != ' ' && |
| 1278 | *p != '\r' && *p != '\n' && |
| 1279 | (*p < 32 || *p >= 127)) |
| 1280 | return FALSE;
|
| 1281 | p++; |
| 1282 | } |
| 1283 | |
| 1284 | return TRUE;
|
| 1285 | } |
| 1286 | |
| 1287 | gint get_quote_level(const gchar *str)
|
| 1288 | {
|
| 1289 | const gchar *first_pos;
|
| 1290 | const gchar *last_pos;
|
| 1291 | const gchar *p = str;
|
| 1292 | gint quote_level = -1;
|
| 1293 | |
| 1294 | /* speed up line processing by only searching to the last '>' */
|
| 1295 | if ((first_pos = strchr(str, '>')) != NULL) { |
| 1296 | /* skip a line if it contains a '<' before the initial '>' */
|
| 1297 | if (memchr(str, '<', first_pos - str) != NULL) |
| 1298 | return -1; |
| 1299 | last_pos = strrchr(first_pos, '>');
|
| 1300 | } else
|
| 1301 | return -1; |
| 1302 | |
| 1303 | while (p <= last_pos) {
|
| 1304 | while (p < last_pos) {
|
| 1305 | if (g_ascii_isspace(*p))
|
| 1306 | p++; |
| 1307 | else
|
| 1308 | break;
|
| 1309 | } |
| 1310 | |
| 1311 | if (*p == '>') |
| 1312 | quote_level++; |
| 1313 | else if (*p != '-' && !g_ascii_isspace(*p) && p <= last_pos) { |
| 1314 | /* any characters are allowed except '-' and space */
|
| 1315 | while (*p != '-' && *p != '>' && !g_ascii_isspace(*p) && |
| 1316 | p < last_pos) |
| 1317 | p++; |
| 1318 | if (*p == '>') |
| 1319 | quote_level++; |
| 1320 | else
|
| 1321 | break;
|
| 1322 | } |
| 1323 | |
| 1324 | p++; |
| 1325 | } |
| 1326 | |
| 1327 | return quote_level;
|
| 1328 | } |
| 1329 | |
| 1330 | gint check_line_length(const gchar *str, gint max_chars, gint *line)
|
| 1331 | {
|
| 1332 | const gchar *p = str, *q;
|
| 1333 | gint cur_line = 0, len;
|
| 1334 | |
| 1335 | while ((q = strchr(p, '\n')) != NULL) { |
| 1336 | len = q - p + 1;
|
| 1337 | if (len > max_chars) {
|
| 1338 | if (line)
|
| 1339 | *line = cur_line; |
| 1340 | return -1; |
| 1341 | } |
| 1342 | p = q + 1;
|
| 1343 | ++cur_line; |
| 1344 | } |
| 1345 | |
| 1346 | len = strlen(p); |
| 1347 | if (len > max_chars) {
|
| 1348 | if (line)
|
| 1349 | *line = cur_line; |
| 1350 | return -1; |
| 1351 | } |
| 1352 | |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle) |
| 1357 | {
|
| 1358 | register guint haystack_len, needle_len;
|
| 1359 | gboolean in_squote = FALSE, in_dquote = FALSE; |
| 1360 | |
| 1361 | haystack_len = strlen(haystack); |
| 1362 | needle_len = strlen(needle); |
| 1363 | |
| 1364 | if (haystack_len < needle_len || needle_len == 0) |
| 1365 | return NULL; |
| 1366 | |
| 1367 | while (haystack_len >= needle_len) {
|
| 1368 | if (!in_squote && !in_dquote &&
|
| 1369 | !strncmp(haystack, needle, needle_len)) |
| 1370 | return (gchar *)haystack;
|
| 1371 | |
| 1372 | /* 'foo"bar"' -> foo"bar"
|
| 1373 | "foo'bar'" -> foo'bar' */ |
| 1374 | if (*haystack == '\'') { |
| 1375 | if (in_squote)
|
| 1376 | in_squote = FALSE; |
| 1377 | else if (!in_dquote) |
| 1378 | in_squote = TRUE; |
| 1379 | } else if (*haystack == '\"') { |
| 1380 | if (in_dquote)
|
| 1381 | in_dquote = FALSE; |
| 1382 | else if (!in_squote) |
| 1383 | in_dquote = TRUE; |
| 1384 | } |
| 1385 | |
| 1386 | haystack++; |
| 1387 | haystack_len--; |
| 1388 | } |
| 1389 | |
| 1390 | return NULL; |
| 1391 | } |
| 1392 | |
| 1393 | gchar *strchr_parenthesis_close(const gchar *str, gchar op, gchar cl)
|
| 1394 | {
|
| 1395 | const gchar *p;
|
| 1396 | gchar quote_chr = '"';
|
| 1397 | gint in_brace; |
| 1398 | gboolean in_quote = FALSE; |
| 1399 | |
| 1400 | p = str; |
| 1401 | |
| 1402 | if ((p = strchr_with_skip_quote(p, quote_chr, op))) {
|
| 1403 | p++; |
| 1404 | in_brace = 1;
|
| 1405 | while (*p) {
|
| 1406 | if (*p == op && !in_quote)
|
| 1407 | in_brace++; |
| 1408 | else if (*p == cl && !in_quote) |
| 1409 | in_brace--; |
| 1410 | else if (*p == quote_chr) |
| 1411 | in_quote ^= TRUE; |
| 1412 | |
| 1413 | if (in_brace == 0) |
| 1414 | return (gchar *)p;
|
| 1415 | |
| 1416 | p++; |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | return NULL; |
| 1421 | } |
| 1422 | |
| 1423 | gchar **strsplit_parenthesis(const gchar *str, gchar op, gchar cl,
|
| 1424 | gint max_tokens) |
| 1425 | {
|
| 1426 | GSList *string_list = NULL, *slist;
|
| 1427 | gchar **str_array; |
| 1428 | const gchar *s_op, *s_cl;
|
| 1429 | guint i, n = 1;
|
| 1430 | |
| 1431 | g_return_val_if_fail(str != NULL, NULL); |
| 1432 | |
| 1433 | if (max_tokens < 1) |
| 1434 | max_tokens = G_MAXINT; |
| 1435 | |
| 1436 | s_op = strchr_with_skip_quote(str, '"', op);
|
| 1437 | if (!s_op) return NULL; |
| 1438 | str = s_op; |
| 1439 | s_cl = strchr_parenthesis_close(str, op, cl); |
| 1440 | if (s_cl) {
|
| 1441 | do {
|
| 1442 | guint len; |
| 1443 | gchar *new_string; |
| 1444 | |
| 1445 | str++; |
| 1446 | len = s_cl - str; |
| 1447 | new_string = g_new(gchar, len + 1);
|
| 1448 | strncpy(new_string, str, len); |
| 1449 | new_string[len] = 0;
|
| 1450 | string_list = g_slist_prepend(string_list, new_string); |
| 1451 | n++; |
| 1452 | str = s_cl + 1;
|
| 1453 | |
| 1454 | while (*str && g_ascii_isspace(*str)) str++;
|
| 1455 | if (*str != op) {
|
| 1456 | string_list = g_slist_prepend(string_list, |
| 1457 | g_strdup(""));
|
| 1458 | n++; |
| 1459 | s_op = strchr_with_skip_quote(str, '"', op);
|
| 1460 | if (!--max_tokens || !s_op) break; |
| 1461 | str = s_op; |
| 1462 | } else
|
| 1463 | s_op = str; |
| 1464 | s_cl = strchr_parenthesis_close(str, op, cl); |
| 1465 | } while (--max_tokens && s_cl);
|
| 1466 | } |
| 1467 | |
| 1468 | str_array = g_new(gchar*, n); |
| 1469 | |
| 1470 | i = n - 1;
|
| 1471 | str_array[i--] = NULL;
|
| 1472 | for (slist = string_list; slist; slist = slist->next)
|
| 1473 | str_array[i--] = slist->data; |
| 1474 | |
| 1475 | g_slist_free(string_list); |
| 1476 | |
| 1477 | return str_array;
|
| 1478 | } |
| 1479 | |
| 1480 | gchar **strsplit_with_quote(const gchar *str, const gchar *delim, |
| 1481 | gint max_tokens) |
| 1482 | {
|
| 1483 | GSList *string_list = NULL, *slist;
|
| 1484 | gchar **str_array, *s, *new_str; |
| 1485 | guint i, n = 1, len;
|
| 1486 | |
| 1487 | g_return_val_if_fail(str != NULL, NULL); |
| 1488 | g_return_val_if_fail(delim != NULL, NULL); |
| 1489 | |
| 1490 | if (max_tokens < 1) |
| 1491 | max_tokens = G_MAXINT; |
| 1492 | |
| 1493 | s = strstr_with_skip_quote(str, delim); |
| 1494 | if (s) {
|
| 1495 | guint delimiter_len = strlen(delim); |
| 1496 | |
| 1497 | do {
|
| 1498 | len = s - str; |
| 1499 | new_str = g_strndup(str, len); |
| 1500 | |
| 1501 | if (new_str[0] == '\'' || new_str[0] == '\"') { |
| 1502 | if (new_str[len - 1] == new_str[0]) { |
| 1503 | new_str[len - 1] = '\0'; |
| 1504 | memmove(new_str, new_str + 1, len - 1); |
| 1505 | } |
| 1506 | } |
| 1507 | string_list = g_slist_prepend(string_list, new_str); |
| 1508 | n++; |
| 1509 | str = s + delimiter_len; |
| 1510 | s = strstr_with_skip_quote(str, delim); |
| 1511 | } while (--max_tokens && s);
|
| 1512 | } |
| 1513 | |
| 1514 | if (*str) {
|
| 1515 | new_str = g_strdup(str); |
| 1516 | if (new_str[0] == '\'' || new_str[0] == '\"') { |
| 1517 | len = strlen(str); |
| 1518 | if (new_str[len - 1] == new_str[0]) { |
| 1519 | new_str[len - 1] = '\0'; |
| 1520 | memmove(new_str, new_str + 1, len - 1); |
| 1521 | } |
| 1522 | } |
| 1523 | string_list = g_slist_prepend(string_list, new_str); |
| 1524 | n++; |
| 1525 | } |
| 1526 | |
| 1527 | str_array = g_new(gchar*, n); |
| 1528 | |
| 1529 | i = n - 1;
|
| 1530 | str_array[i--] = NULL;
|
| 1531 | for (slist = string_list; slist; slist = slist->next)
|
| 1532 | str_array[i--] = slist->data; |
| 1533 | |
| 1534 | g_slist_free(string_list); |
| 1535 | |
| 1536 | return str_array;
|
| 1537 | } |
| 1538 | |
| 1539 | gchar **strsplit_csv(const gchar *str, gchar delim, gint max_tokens)
|
| 1540 | {
|
| 1541 | GSList *string_list = NULL, *slist;
|
| 1542 | gchar **str_array, *s, *new_str; |
| 1543 | gchar *tmp, *tmpp, *p; |
| 1544 | guint i, n = 1, len;
|
| 1545 | |
| 1546 | g_return_val_if_fail(str != NULL, NULL); |
| 1547 | |
| 1548 | if (max_tokens < 1) |
| 1549 | max_tokens = G_MAXINT; |
| 1550 | |
| 1551 | s = strchr_with_skip_quote(str, '"', delim);
|
| 1552 | if (s) {
|
| 1553 | do {
|
| 1554 | len = s - str; |
| 1555 | tmpp = tmp = g_strndup(str, len); |
| 1556 | |
| 1557 | if (tmp[0] == '"' && tmp[len - 1] == tmp[0]) { |
| 1558 | tmp[len - 1] = '\0'; |
| 1559 | ++tmpp; |
| 1560 | p = new_str = g_malloc(len - 1);
|
| 1561 | while (*tmpp) {
|
| 1562 | if (*tmpp == '"' && *(tmpp + 1) == '"') |
| 1563 | ++tmpp; |
| 1564 | *p++ = *tmpp++; |
| 1565 | } |
| 1566 | *p = '\0';
|
| 1567 | g_free(tmp); |
| 1568 | } else
|
| 1569 | new_str = tmp; |
| 1570 | |
| 1571 | string_list = g_slist_prepend(string_list, new_str); |
| 1572 | n++; |
| 1573 | str = s + 1;
|
| 1574 | s = strchr_with_skip_quote(str, '"', delim);
|
| 1575 | } while (--max_tokens && s);
|
| 1576 | } |
| 1577 | |
| 1578 | if (*str) {
|
| 1579 | len = strlen(str); |
| 1580 | tmpp = tmp = g_strdup(str); |
| 1581 | |
| 1582 | if (tmp[0] == '"' && tmp[len - 1] == tmp[0]) { |
| 1583 | tmp[len - 1] = '\0'; |
| 1584 | ++tmpp; |
| 1585 | p = new_str = g_malloc(len - 1);
|
| 1586 | while (*tmpp) {
|
| 1587 | if (*tmpp == '"' && *(tmpp + 1) == '"') |
| 1588 | ++tmpp; |
| 1589 | *p++ = *tmpp++; |
| 1590 | } |
| 1591 | *p = '\0';
|
| 1592 | g_free(tmp); |
| 1593 | } else
|
| 1594 | new_str = tmp; |
| 1595 | |
| 1596 | string_list = g_slist_prepend(string_list, new_str); |
| 1597 | n++; |
| 1598 | } |
| 1599 | |
| 1600 | str_array = g_new(gchar*, n); |
| 1601 | |
| 1602 | i = n - 1;
|
| 1603 | str_array[i--] = NULL;
|
| 1604 | for (slist = string_list; slist; slist = slist->next)
|
| 1605 | str_array[i--] = slist->data; |
| 1606 | |
| 1607 | g_slist_free(string_list); |
| 1608 | |
| 1609 | return str_array;
|
| 1610 | } |
| 1611 | |
| 1612 | gchar *get_abbrev_newsgroup_name(const gchar *group, gint len)
|
| 1613 | {
|
| 1614 | gchar *abbrev_group; |
| 1615 | gchar *ap; |
| 1616 | const gchar *p = group;
|
| 1617 | const gchar *last;
|
| 1618 | |
| 1619 | last = group + strlen(group); |
| 1620 | abbrev_group = ap = g_malloc(strlen(group) + 1);
|
| 1621 | |
| 1622 | while (*p) {
|
| 1623 | while (*p == '.') |
| 1624 | *ap++ = *p++; |
| 1625 | if ((ap - abbrev_group) + (last - p) > len && strchr(p, '.')) { |
| 1626 | *ap++ = *p++; |
| 1627 | while (*p != '.') p++; |
| 1628 | } else {
|
| 1629 | strcpy(ap, p); |
| 1630 | return abbrev_group;
|
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | *ap = '\0';
|
| 1635 | return abbrev_group;
|
| 1636 | } |
| 1637 | |
| 1638 | gchar *trim_string(const gchar *str, gint len)
|
| 1639 | {
|
| 1640 | const gchar *p = str;
|
| 1641 | gint mb_len; |
| 1642 | gchar *new_str; |
| 1643 | gint new_len = 0;
|
| 1644 | |
| 1645 | if (!str) return NULL; |
| 1646 | if (strlen(str) <= len)
|
| 1647 | return g_strdup(str);
|
| 1648 | if (g_utf8_validate(str, -1, NULL) == FALSE) |
| 1649 | return g_strdup(str);
|
| 1650 | |
| 1651 | while (*p != '\0') { |
| 1652 | mb_len = g_utf8_skip[*(guchar *)p]; |
| 1653 | if (mb_len == 0) |
| 1654 | break;
|
| 1655 | else if (new_len + mb_len > len) |
| 1656 | break;
|
| 1657 | |
| 1658 | new_len += mb_len; |
| 1659 | p += mb_len; |
| 1660 | } |
| 1661 | |
| 1662 | Xstrndup_a(new_str, str, new_len, return g_strdup(str));
|
| 1663 | return g_strconcat(new_str, "...", NULL); |
| 1664 | } |
| 1665 | |
| 1666 | gchar *trim_string_before(const gchar *str, gint len)
|
| 1667 | {
|
| 1668 | const gchar *p = str;
|
| 1669 | gint mb_len; |
| 1670 | gint new_len; |
| 1671 | |
| 1672 | if (!str) return NULL; |
| 1673 | if ((new_len = strlen(str)) <= len)
|
| 1674 | return g_strdup(str);
|
| 1675 | if (g_utf8_validate(str, -1, NULL) == FALSE) |
| 1676 | return g_strdup(str);
|
| 1677 | |
| 1678 | while (*p != '\0') { |
| 1679 | mb_len = g_utf8_skip[*(guchar *)p]; |
| 1680 | if (mb_len == 0) |
| 1681 | break;
|
| 1682 | |
| 1683 | new_len -= mb_len; |
| 1684 | p += mb_len; |
| 1685 | |
| 1686 | if (new_len <= len)
|
| 1687 | break;
|
| 1688 | } |
| 1689 | |
| 1690 | return g_strconcat("...", p, NULL); |
| 1691 | } |
| 1692 | |
| 1693 | GList *uri_list_extract_filenames(const gchar *uri_list)
|
| 1694 | {
|
| 1695 | GList *result = NULL;
|
| 1696 | gchar *file; |
| 1697 | |
| 1698 | #if GLIB_CHECK_VERSION(2, 6, 0) |
| 1699 | gchar **uris; |
| 1700 | gint i; |
| 1701 | |
| 1702 | uris = g_uri_list_extract_uris(uri_list); |
| 1703 | g_return_val_if_fail(uris != NULL, NULL); |
| 1704 | |
| 1705 | for (i = 0; uris[i] != NULL; i++) { |
| 1706 | file = g_filename_from_uri(uris[i], NULL, NULL); |
| 1707 | if (file)
|
| 1708 | result = g_list_append(result, file); |
| 1709 | } |
| 1710 | |
| 1711 | g_strfreev(uris); |
| 1712 | |
| 1713 | return result;
|
| 1714 | #else
|
| 1715 | const gchar *p, *q;
|
| 1716 | |
| 1717 | p = uri_list; |
| 1718 | |
| 1719 | while (p) {
|
| 1720 | if (*p != '#') { |
| 1721 | while (g_ascii_isspace(*p)) p++;
|
| 1722 | if (!strncmp(p, "file:", 5)) { |
| 1723 | p += 5;
|
| 1724 | while (*p == '/' && *(p + 1) == '/') p++; |
| 1725 | q = p; |
| 1726 | while (*q && *q != '\n' && *q != '\r') q++; |
| 1727 | |
| 1728 | if (q > p) {
|
| 1729 | q--; |
| 1730 | while (q > p && g_ascii_isspace(*q))
|
| 1731 | q--; |
| 1732 | file = g_malloc(q - p + 2);
|
| 1733 | strncpy(file, p, q - p + 1);
|
| 1734 | file[q - p + 1] = '\0'; |
| 1735 | decode_uri(file, file); |
| 1736 | result = g_list_append(result, file); |
| 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | p = strchr(p, '\n');
|
| 1741 | if (p) p++;
|
| 1742 | } |
| 1743 | |
| 1744 | return result;
|
| 1745 | #endif
|
| 1746 | } |
| 1747 | |
| 1748 | #define HEX_TO_INT(val, hex) \
|
| 1749 | { \
|
| 1750 | gchar c = hex; \ |
| 1751 | \ |
| 1752 | if ('0' <= c && c <= '9') { \ |
| 1753 | val = c - '0'; \
|
| 1754 | } else if ('a' <= c && c <= 'f') { \ |
| 1755 | val = c - 'a' + 10; \ |
| 1756 | } else if ('A' <= c && c <= 'F') { \ |
| 1757 | val = c - 'A' + 10; \ |
| 1758 | } else { \
|
| 1759 | val = 0; \
|
| 1760 | } \ |
| 1761 | } |
| 1762 | |
| 1763 | #define INT_TO_HEX(hex, val) \
|
| 1764 | { \
|
| 1765 | if ((val) < 10) \ |
| 1766 | hex = '0' + (val); \
|
| 1767 | else \
|
| 1768 | hex = 'a' + (val) - 10; \ |
| 1769 | } |
| 1770 | |
| 1771 | /* Converts two-digit hexadecimal to decimal. Used for unescaping escaped
|
| 1772 | * characters. |
| 1773 | */ |
| 1774 | static gint axtoi(const gchar *hex_str) |
| 1775 | {
|
| 1776 | gint hi, lo; |
| 1777 | |
| 1778 | HEX_TO_INT(hi, hex_str[0]);
|
| 1779 | HEX_TO_INT(lo, hex_str[1]);
|
| 1780 | |
| 1781 | return (hi << 4) + lo; |
| 1782 | } |
| 1783 | |
| 1784 | static void get_hex_str(gchar *out, guchar ch) |
| 1785 | {
|
| 1786 | gchar hex; |
| 1787 | |
| 1788 | INT_TO_HEX(hex, ch >> 4);
|
| 1789 | *out++ = hex; |
| 1790 | INT_TO_HEX(hex, ch & 0x0f);
|
| 1791 | *out++ = hex; |
| 1792 | } |
| 1793 | |
| 1794 | gboolean is_uri_string(const gchar *str)
|
| 1795 | {
|
| 1796 | return (g_ascii_strncasecmp(str, "http://", 7) == 0 || |
| 1797 | g_ascii_strncasecmp(str, "https://", 8) == 0 || |
| 1798 | g_ascii_strncasecmp(str, "ftp://", 6) == 0 || |
| 1799 | g_ascii_strncasecmp(str, "www.", 4) == 0); |
| 1800 | } |
| 1801 | |
| 1802 | gchar *get_uri_path(const gchar *uri)
|
| 1803 | {
|
| 1804 | if (g_ascii_strncasecmp(uri, "http://", 7) == 0) |
| 1805 | return (gchar *)(uri + 7); |
| 1806 | else if (g_ascii_strncasecmp(uri, "https://", 8) == 0) |
| 1807 | return (gchar *)(uri + 8); |
| 1808 | else if (g_ascii_strncasecmp(uri, "ftp://", 6) == 0) |
| 1809 | return (gchar *)(uri + 6); |
| 1810 | else
|
| 1811 | return (gchar *)uri;
|
| 1812 | } |
| 1813 | |
| 1814 | gint get_uri_len(const gchar *str)
|
| 1815 | {
|
| 1816 | const gchar *p;
|
| 1817 | |
| 1818 | if (is_uri_string(str)) {
|
| 1819 | for (p = str; *p != '\0'; p++) { |
| 1820 | if (!g_ascii_isgraph(*p) || strchr("()<>\"", *p)) |
| 1821 | break;
|
| 1822 | } |
| 1823 | return p - str;
|
| 1824 | } |
| 1825 | |
| 1826 | return 0; |
| 1827 | } |
| 1828 | |
| 1829 | /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
|
| 1830 | * plusses, and escape characters are used) |
| 1831 | * Note: decoded_uri and encoded_uri can point the same location |
| 1832 | */ |
| 1833 | void decode_uri(gchar *decoded_uri, const gchar *encoded_uri) |
| 1834 | {
|
| 1835 | gchar *dec = decoded_uri; |
| 1836 | const gchar *enc = encoded_uri;
|
| 1837 | |
| 1838 | while (*enc) {
|
| 1839 | if (*enc == '%') { |
| 1840 | enc++; |
| 1841 | if (g_ascii_isxdigit((guchar)enc[0]) && |
| 1842 | g_ascii_isxdigit((guchar)enc[1])) {
|
| 1843 | *dec = axtoi(enc); |
| 1844 | dec++; |
| 1845 | enc += 2;
|
| 1846 | } |
| 1847 | } else {
|
| 1848 | if (*enc == '+') |
| 1849 | *dec = ' ';
|
| 1850 | else
|
| 1851 | *dec = *enc; |
| 1852 | dec++; |
| 1853 | enc++; |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | *dec = '\0';
|
| 1858 | } |
| 1859 | |
| 1860 | void decode_xdigit_encoded_str(gchar *decoded, const gchar *encoded) |
| 1861 | {
|
| 1862 | gchar *dec = decoded; |
| 1863 | const gchar *enc = encoded;
|
| 1864 | |
| 1865 | while (*enc) {
|
| 1866 | if (*enc == '%') { |
| 1867 | enc++; |
| 1868 | if (g_ascii_isxdigit((guchar)enc[0]) && |
| 1869 | g_ascii_isxdigit((guchar)enc[1])) {
|
| 1870 | *dec++ = axtoi(enc); |
| 1871 | enc += 2;
|
| 1872 | } |
| 1873 | } else
|
| 1874 | *dec++ = *enc++; |
| 1875 | } |
| 1876 | |
| 1877 | *dec = '\0';
|
| 1878 | } |
| 1879 | |
| 1880 | gchar *encode_uri(const gchar *filename)
|
| 1881 | {
|
| 1882 | gchar *uri; |
| 1883 | |
| 1884 | uri = g_filename_to_uri(filename, NULL, NULL); |
| 1885 | if (!uri)
|
| 1886 | uri = g_strconcat("file://", filename, NULL); |
| 1887 | |
| 1888 | return uri;
|
| 1889 | } |
| 1890 | |
| 1891 | gchar *uriencode_for_filename(const gchar *filename)
|
| 1892 | {
|
| 1893 | const gchar *p = filename;
|
| 1894 | gchar *enc, *outp; |
| 1895 | |
| 1896 | outp = enc = g_malloc(strlen(filename) * 3 + 1); |
| 1897 | |
| 1898 | for (p = filename; *p != '\0'; p++) { |
| 1899 | if (strchr("\t\r\n\"'\\/:;*?<>|", *p)) { |
| 1900 | *outp++ = '%';
|
| 1901 | get_hex_str(outp, *p); |
| 1902 | outp += 2;
|
| 1903 | } else
|
| 1904 | *outp++ = *p; |
| 1905 | } |
| 1906 | |
| 1907 | *outp = '\0';
|
| 1908 | return enc;
|
| 1909 | } |
| 1910 | |
| 1911 | gchar *uriencode_for_mailto(const gchar *mailto)
|
| 1912 | {
|
| 1913 | const gchar *p = mailto;
|
| 1914 | gchar *enc, *outp; |
| 1915 | |
| 1916 | outp = enc = g_malloc(strlen(mailto) * 3 + 1); |
| 1917 | |
| 1918 | for (p = mailto; *p != '\0'; p++) { |
| 1919 | if (*p == '+') { |
| 1920 | *outp++ = '%';
|
| 1921 | get_hex_str(outp, *p); |
| 1922 | outp += 2;
|
| 1923 | } else
|
| 1924 | *outp++ = *p; |
| 1925 | } |
| 1926 | |
| 1927 | *outp = '\0';
|
| 1928 | return enc;
|
| 1929 | } |
| 1930 | |
| 1931 | gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
|
| 1932 | gchar **subject, gchar **inreplyto, gchar **body) |
| 1933 | {
|
| 1934 | gchar *tmp_mailto; |
| 1935 | gchar *p; |
| 1936 | |
| 1937 | Xstrdup_a(tmp_mailto, mailto, return -1); |
| 1938 | |
| 1939 | if (!strncmp(tmp_mailto, "mailto:", 7)) |
| 1940 | tmp_mailto += 7;
|
| 1941 | |
| 1942 | p = strchr(tmp_mailto, '?');
|
| 1943 | if (p) {
|
| 1944 | *p = '\0';
|
| 1945 | p++; |
| 1946 | } |
| 1947 | |
| 1948 | if (to && !*to) {
|
| 1949 | *to = g_malloc(strlen(tmp_mailto) + 1);
|
| 1950 | decode_uri(*to, tmp_mailto); |
| 1951 | } |
| 1952 | |
| 1953 | while (p) {
|
| 1954 | gchar *field, *value; |
| 1955 | |
| 1956 | field = p; |
| 1957 | |
| 1958 | p = strchr(p, '=');
|
| 1959 | if (!p) break; |
| 1960 | *p = '\0';
|
| 1961 | p++; |
| 1962 | |
| 1963 | value = p; |
| 1964 | |
| 1965 | p = strchr(p, '&');
|
| 1966 | if (p) {
|
| 1967 | *p = '\0';
|
| 1968 | p++; |
| 1969 | } |
| 1970 | |
| 1971 | if (*value == '\0') continue; |
| 1972 | |
| 1973 | if (cc && !*cc && !g_ascii_strcasecmp(field, "cc")) { |
| 1974 | *cc = g_malloc(strlen(value) + 1);
|
| 1975 | decode_uri(*cc, value); |
| 1976 | } else if (bcc && !*bcc && !g_ascii_strcasecmp(field, "bcc")) { |
| 1977 | *bcc = g_malloc(strlen(value) + 1);
|
| 1978 | decode_uri(*bcc, value); |
| 1979 | } else if (subject && !*subject && |
| 1980 | !g_ascii_strcasecmp(field, "subject")) {
|
| 1981 | *subject = g_malloc(strlen(value) + 1);
|
| 1982 | decode_uri(*subject, value); |
| 1983 | } else if (inreplyto && !*inreplyto && |
| 1984 | !g_ascii_strcasecmp(field, "in-reply-to")) {
|
| 1985 | *inreplyto = g_malloc(strlen(value) + 1);
|
| 1986 | decode_uri(*inreplyto, value); |
| 1987 | } else if (body && !*body && |
| 1988 | !g_ascii_strcasecmp(field, "body")) {
|
| 1989 | *body = g_malloc(strlen(value) + 1);
|
| 1990 | decode_uri(*body, value); |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | return 0; |
| 1995 | } |
| 1996 | |
| 1997 | static gchar *startup_dir = NULL; |
| 1998 | static gchar *rc_dir = NULL; |
| 1999 | |
| 2000 | void set_startup_dir(void) |
| 2001 | {
|
| 2002 | #ifdef G_OS_WIN32
|
| 2003 | if (!startup_dir) {
|
| 2004 | startup_dir = g_win32_get_package_installation_directory |
| 2005 | (NULL, NULL); |
| 2006 | if (startup_dir) {
|
| 2007 | if (g_chdir(startup_dir) < 0) { |
| 2008 | FILE_OP_ERROR(startup_dir, "chdir");
|
| 2009 | g_free(startup_dir); |
| 2010 | startup_dir = g_get_current_dir(); |
| 2011 | } |
| 2012 | } else
|
| 2013 | startup_dir = g_get_current_dir(); |
| 2014 | } |
| 2015 | #else
|
| 2016 | if (!startup_dir)
|
| 2017 | startup_dir = g_get_current_dir(); |
| 2018 | #endif
|
| 2019 | } |
| 2020 | |
| 2021 | void set_rc_dir(const gchar *dir) |
| 2022 | {
|
| 2023 | if (rc_dir)
|
| 2024 | g_free(rc_dir); |
| 2025 | |
| 2026 | if (dir) {
|
| 2027 | if (g_path_is_absolute(dir))
|
| 2028 | rc_dir = g_strdup(dir); |
| 2029 | else
|
| 2030 | rc_dir = g_strconcat(get_startup_dir(), |
| 2031 | G_DIR_SEPARATOR_S, dir, NULL);
|
| 2032 | } else
|
| 2033 | rc_dir = NULL;
|
| 2034 | } |
| 2035 | |
| 2036 | const gchar *get_startup_dir(void) |
| 2037 | {
|
| 2038 | if (!startup_dir)
|
| 2039 | set_startup_dir(); |
| 2040 | |
| 2041 | return startup_dir;
|
| 2042 | } |
| 2043 | |
| 2044 | #ifdef G_OS_WIN32
|
| 2045 | static gchar *get_win32_special_folder_path(gint nfolder)
|
| 2046 | {
|
| 2047 | gchar *folder = NULL;
|
| 2048 | HRESULT hr; |
| 2049 | |
| 2050 | if (G_WIN32_HAVE_WIDECHAR_API()) {
|
| 2051 | wchar_t path[MAX_PATH + 1];
|
| 2052 | hr = SHGetFolderPathW(NULL, nfolder, NULL, 0, path); |
| 2053 | if (hr == S_OK)
|
| 2054 | folder = g_utf16_to_utf8(path, -1, NULL, NULL, NULL); |
| 2055 | } else {
|
| 2056 | gchar path[MAX_PATH + 1];
|
| 2057 | hr = SHGetFolderPathA(NULL, nfolder, NULL, 0, path); |
| 2058 | if (hr == S_OK)
|
| 2059 | folder = g_locale_to_utf8(path, -1, NULL, NULL, NULL); |
| 2060 | } |
| 2061 | |
| 2062 | return folder;
|
| 2063 | } |
| 2064 | #endif
|
| 2065 | |
| 2066 | const gchar *get_home_dir(void) |
| 2067 | {
|
| 2068 | #ifdef G_OS_WIN32
|
| 2069 | static const gchar *home_dir = NULL; |
| 2070 | |
| 2071 | if (!home_dir) {
|
| 2072 | home_dir = g_get_home_dir(); |
| 2073 | if (!home_dir)
|
| 2074 | home_dir = "C:\\Sylpheed";
|
| 2075 | } |
| 2076 | |
| 2077 | return home_dir;
|
| 2078 | #else
|
| 2079 | return g_get_home_dir();
|
| 2080 | #endif
|
| 2081 | } |
| 2082 | |
| 2083 | const gchar *get_document_dir(void) |
| 2084 | {
|
| 2085 | #ifdef G_OS_WIN32
|
| 2086 | static const gchar *document_dir = NULL; |
| 2087 | HRESULT hr; |
| 2088 | |
| 2089 | if (!document_dir) {
|
| 2090 | document_dir = get_win32_special_folder_path(CSIDL_PERSONAL); |
| 2091 | if (!document_dir)
|
| 2092 | document_dir = get_home_dir(); |
| 2093 | } |
| 2094 | |
| 2095 | return document_dir;
|
| 2096 | #else
|
| 2097 | return get_home_dir();
|
| 2098 | #endif
|
| 2099 | } |
| 2100 | |
| 2101 | const gchar *get_rc_dir(void) |
| 2102 | {
|
| 2103 | if (!rc_dir) {
|
| 2104 | #ifdef G_OS_WIN32
|
| 2105 | gchar *appdata; |
| 2106 | |
| 2107 | appdata = get_win32_special_folder_path(CSIDL_APPDATA); |
| 2108 | if (appdata)
|
| 2109 | rc_dir = g_strconcat(appdata, G_DIR_SEPARATOR_S, |
| 2110 | RC_DIR, NULL);
|
| 2111 | else
|
| 2112 | rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, |
| 2113 | RC_DIR, NULL);
|
| 2114 | g_free(appdata); |
| 2115 | #else
|
| 2116 | rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, |
| 2117 | RC_DIR, NULL);
|
| 2118 | #endif
|
| 2119 | } |
| 2120 | |
| 2121 | return rc_dir;
|
| 2122 | } |
| 2123 | |
| 2124 | const gchar *get_old_rc_dir(void) |
| 2125 | {
|
| 2126 | static gchar *old_rc_dir = NULL; |
| 2127 | |
| 2128 | if (!old_rc_dir)
|
| 2129 | old_rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, |
| 2130 | OLD_RC_DIR, NULL);
|
| 2131 | |
| 2132 | return old_rc_dir;
|
| 2133 | } |
| 2134 | |
| 2135 | const gchar *get_mail_base_dir(void) |
| 2136 | {
|
| 2137 | #ifdef G_OS_WIN32
|
| 2138 | static gchar *mail_base_dir = NULL; |
| 2139 | |
| 2140 | if (!mail_base_dir)
|
| 2141 | mail_base_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 2142 | "Mailboxes", NULL); |
| 2143 | |
| 2144 | return mail_base_dir;
|
| 2145 | #else
|
| 2146 | return get_home_dir();
|
| 2147 | #endif
|
| 2148 | } |
| 2149 | |
| 2150 | const gchar *get_news_cache_dir(void) |
| 2151 | {
|
| 2152 | static gchar *news_cache_dir = NULL; |
| 2153 | |
| 2154 | if (!news_cache_dir)
|
| 2155 | news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 2156 | NEWS_CACHE_DIR, NULL);
|
| 2157 | |
| 2158 | return news_cache_dir;
|
| 2159 | } |
| 2160 | |
| 2161 | const gchar *get_imap_cache_dir(void) |
| 2162 | {
|
| 2163 | static gchar *imap_cache_dir = NULL; |
| 2164 | |
| 2165 | if (!imap_cache_dir)
|
| 2166 | imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 2167 | IMAP_CACHE_DIR, NULL);
|
| 2168 | |
| 2169 | return imap_cache_dir;
|
| 2170 | } |
| 2171 | |
| 2172 | const gchar *get_mime_tmp_dir(void) |
| 2173 | {
|
| 2174 | static gchar *mime_tmp_dir = NULL; |
| 2175 | |
| 2176 | if (!mime_tmp_dir)
|
| 2177 | mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 2178 | MIME_TMP_DIR, NULL);
|
| 2179 | |
| 2180 | return mime_tmp_dir;
|
| 2181 | } |
| 2182 | |
| 2183 | const gchar *get_template_dir(void) |
| 2184 | {
|
| 2185 | static gchar *template_dir = NULL; |
| 2186 | |
| 2187 | if (!template_dir)
|
| 2188 | template_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 2189 | TEMPLATE_DIR, NULL);
|
| 2190 | |
| 2191 | return template_dir;
|
| 2192 | } |
| 2193 | |
| 2194 | const gchar *get_tmp_dir(void) |
| 2195 | {
|
| 2196 | static gchar *tmp_dir = NULL; |
| 2197 | |
| 2198 | if (!tmp_dir)
|
| 2199 | tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 2200 | TMP_DIR, NULL);
|
| 2201 | |
| 2202 | return tmp_dir;
|
| 2203 | } |
| 2204 | |
| 2205 | gchar *get_tmp_file(void)
|
| 2206 | {
|
| 2207 | gchar *tmp_file; |
| 2208 | static guint32 id = 0; |
| 2209 | |
| 2210 | tmp_file = g_strdup_printf("%s%ctmpfile.%08x",
|
| 2211 | get_tmp_dir(), G_DIR_SEPARATOR, id++); |
| 2212 | |
| 2213 | return tmp_file;
|
| 2214 | } |
| 2215 | |
| 2216 | const gchar *get_domain_name(void) |
| 2217 | {
|
| 2218 | static gchar *domain_name = NULL; |
| 2219 | |
| 2220 | if (!domain_name) {
|
| 2221 | gchar buf[128] = ""; |
| 2222 | struct hostent *hp;
|
| 2223 | |
| 2224 | if (gethostname(buf, sizeof(buf)) < 0) { |
| 2225 | perror("gethostname");
|
| 2226 | domain_name = "unknown";
|
| 2227 | } else {
|
| 2228 | buf[sizeof(buf) - 1] = '\0'; |
| 2229 | if ((hp = my_gethostbyname(buf)) == NULL) { |
| 2230 | perror("gethostbyname");
|
| 2231 | domain_name = g_strdup(buf); |
| 2232 | } else {
|
| 2233 | domain_name = g_strdup(hp->h_name); |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | debug_print("domain name = %s\n", domain_name);
|
| 2238 | if (is_next_nonascii(domain_name)) {
|
| 2239 | g_warning("invalid domain name: %s\n", domain_name);
|
| 2240 | g_free(domain_name); |
| 2241 | domain_name = "unknown";
|
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | return domain_name;
|
| 2246 | } |
| 2247 | |
| 2248 | off_t get_file_size(const gchar *file)
|
| 2249 | {
|
| 2250 | struct stat s;
|
| 2251 | |
| 2252 | if (g_stat(file, &s) < 0) { |
| 2253 | FILE_OP_ERROR(file, "stat");
|
| 2254 | return -1; |
| 2255 | } |
| 2256 | |
| 2257 | return s.st_size;
|
| 2258 | } |
| 2259 | |
| 2260 | off_t get_file_size_as_crlf(const gchar *file)
|
| 2261 | {
|
| 2262 | FILE *fp; |
| 2263 | off_t size = 0;
|
| 2264 | gchar buf[BUFFSIZE]; |
| 2265 | |
| 2266 | if ((fp = g_fopen(file, "rb")) == NULL) { |
| 2267 | FILE_OP_ERROR(file, "fopen");
|
| 2268 | return -1; |
| 2269 | } |
| 2270 | |
| 2271 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 2272 | strretchomp(buf); |
| 2273 | size += strlen(buf) + 2;
|
| 2274 | } |
| 2275 | |
| 2276 | if (ferror(fp)) {
|
| 2277 | FILE_OP_ERROR(file, "fgets");
|
| 2278 | size = -1;
|
| 2279 | } |
| 2280 | |
| 2281 | fclose(fp); |
| 2282 | |
| 2283 | return size;
|
| 2284 | } |
| 2285 | |
| 2286 | off_t get_left_file_size(FILE *fp) |
| 2287 | {
|
| 2288 | glong pos; |
| 2289 | glong end; |
| 2290 | off_t size; |
| 2291 | |
| 2292 | if ((pos = ftell(fp)) < 0) { |
| 2293 | perror("ftell");
|
| 2294 | return -1; |
| 2295 | } |
| 2296 | if (fseek(fp, 0L, SEEK_END) < 0) { |
| 2297 | perror("fseek");
|
| 2298 | return -1; |
| 2299 | } |
| 2300 | if ((end = ftell(fp)) < 0) { |
| 2301 | perror("fseek");
|
| 2302 | return -1; |
| 2303 | } |
| 2304 | size = end - pos; |
| 2305 | if (fseek(fp, pos, SEEK_SET) < 0) { |
| 2306 | perror("fseek");
|
| 2307 | return -1; |
| 2308 | } |
| 2309 | |
| 2310 | return size;
|
| 2311 | } |
| 2312 | |
| 2313 | gboolean file_exist(const gchar *file, gboolean allow_fifo)
|
| 2314 | {
|
| 2315 | if (file == NULL) |
| 2316 | return FALSE;
|
| 2317 | |
| 2318 | if (allow_fifo) {
|
| 2319 | struct stat s;
|
| 2320 | |
| 2321 | if (g_stat(file, &s) < 0) { |
| 2322 | if (ENOENT != errno) FILE_OP_ERROR(file, "stat"); |
| 2323 | return FALSE;
|
| 2324 | } |
| 2325 | if (S_ISREG(s.st_mode) || S_ISFIFO(s.st_mode))
|
| 2326 | return TRUE;
|
| 2327 | } else {
|
| 2328 | return g_file_test(file, G_FILE_TEST_IS_REGULAR);
|
| 2329 | } |
| 2330 | |
| 2331 | return FALSE;
|
| 2332 | } |
| 2333 | |
| 2334 | gboolean is_dir_exist(const gchar *dir)
|
| 2335 | {
|
| 2336 | if (dir == NULL) |
| 2337 | return FALSE;
|
| 2338 | |
| 2339 | return g_file_test(dir, G_FILE_TEST_IS_DIR);
|
| 2340 | } |
| 2341 | |
| 2342 | gboolean is_file_entry_exist(const gchar *file)
|
| 2343 | {
|
| 2344 | if (file == NULL) |
| 2345 | return FALSE;
|
| 2346 | |
| 2347 | return g_file_test(file, G_FILE_TEST_EXISTS);
|
| 2348 | } |
| 2349 | |
| 2350 | gboolean dirent_is_regular_file(struct dirent *d)
|
| 2351 | {
|
| 2352 | #ifdef HAVE_DIRENT_D_TYPE
|
| 2353 | if (d->d_type == DT_REG)
|
| 2354 | return TRUE;
|
| 2355 | else if (d->d_type != DT_UNKNOWN) |
| 2356 | return FALSE;
|
| 2357 | #endif
|
| 2358 | |
| 2359 | return g_file_test(d->d_name, G_FILE_TEST_IS_REGULAR);
|
| 2360 | } |
| 2361 | |
| 2362 | gboolean dirent_is_directory(struct dirent *d)
|
| 2363 | {
|
| 2364 | #ifdef HAVE_DIRENT_D_TYPE
|
| 2365 | if (d->d_type == DT_DIR)
|
| 2366 | return TRUE;
|
| 2367 | else if (d->d_type != DT_UNKNOWN) |
| 2368 | return FALSE;
|
| 2369 | #endif
|
| 2370 | |
| 2371 | return g_file_test(d->d_name, G_FILE_TEST_IS_DIR);
|
| 2372 | } |
| 2373 | |
| 2374 | gint change_dir(const gchar *dir)
|
| 2375 | {
|
| 2376 | gchar *prevdir = NULL;
|
| 2377 | |
| 2378 | if (debug_mode)
|
| 2379 | prevdir = g_get_current_dir(); |
| 2380 | |
| 2381 | if (g_chdir(dir) < 0) { |
| 2382 | FILE_OP_ERROR(dir, "chdir");
|
| 2383 | if (debug_mode) g_free(prevdir);
|
| 2384 | return -1; |
| 2385 | } else if (debug_mode) { |
| 2386 | gchar *cwd; |
| 2387 | |
| 2388 | cwd = g_get_current_dir(); |
| 2389 | if (strcmp(prevdir, cwd) != 0) |
| 2390 | g_print("current dir: %s\n", cwd);
|
| 2391 | g_free(cwd); |
| 2392 | g_free(prevdir); |
| 2393 | } |
| 2394 | |
| 2395 | return 0; |
| 2396 | } |
| 2397 | |
| 2398 | gint make_dir(const gchar *dir)
|
| 2399 | {
|
| 2400 | if (g_mkdir(dir, S_IRWXU) < 0) { |
| 2401 | FILE_OP_ERROR(dir, "mkdir");
|
| 2402 | return -1; |
| 2403 | } |
| 2404 | if (g_chmod(dir, S_IRWXU) < 0) |
| 2405 | FILE_OP_ERROR(dir, "chmod");
|
| 2406 | |
| 2407 | return 0; |
| 2408 | } |
| 2409 | |
| 2410 | gint make_dir_hier(const gchar *dir)
|
| 2411 | {
|
| 2412 | gchar *parent_dir; |
| 2413 | const gchar *p;
|
| 2414 | |
| 2415 | for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) { |
| 2416 | parent_dir = g_strndup(dir, p - dir); |
| 2417 | if (*parent_dir != '\0') { |
| 2418 | if (!is_dir_exist(parent_dir)) {
|
| 2419 | if (make_dir(parent_dir) < 0) { |
| 2420 | g_free(parent_dir); |
| 2421 | return -1; |
| 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | g_free(parent_dir); |
| 2426 | } |
| 2427 | |
| 2428 | if (!is_dir_exist(dir)) {
|
| 2429 | if (make_dir(dir) < 0) |
| 2430 | return -1; |
| 2431 | } |
| 2432 | |
| 2433 | return 0; |
| 2434 | } |
| 2435 | |
| 2436 | gint remove_all_files(const gchar *dir)
|
| 2437 | {
|
| 2438 | GDir *dp; |
| 2439 | const gchar *dir_name;
|
| 2440 | gchar *prev_dir; |
| 2441 | |
| 2442 | prev_dir = g_get_current_dir(); |
| 2443 | |
| 2444 | if (g_chdir(dir) < 0) { |
| 2445 | FILE_OP_ERROR(dir, "chdir");
|
| 2446 | g_free(prev_dir); |
| 2447 | return -1; |
| 2448 | } |
| 2449 | |
| 2450 | if ((dp = g_dir_open(".", 0, NULL)) == NULL) { |
| 2451 | g_warning("failed to open directory: %s\n", dir);
|
| 2452 | g_free(prev_dir); |
| 2453 | return -1; |
| 2454 | } |
| 2455 | |
| 2456 | while ((dir_name = g_dir_read_name(dp)) != NULL) { |
| 2457 | if (g_unlink(dir_name) < 0) |
| 2458 | FILE_OP_ERROR(dir_name, "unlink");
|
| 2459 | } |
| 2460 | |
| 2461 | g_dir_close(dp); |
| 2462 | |
| 2463 | if (g_chdir(prev_dir) < 0) { |
| 2464 | FILE_OP_ERROR(prev_dir, "chdir");
|
| 2465 | g_free(prev_dir); |
| 2466 | return -1; |
| 2467 | } |
| 2468 | |
| 2469 | g_free(prev_dir); |
| 2470 | |
| 2471 | return 0; |
| 2472 | } |
| 2473 | |
| 2474 | gint remove_numbered_files(const gchar *dir, guint first, guint last)
|
| 2475 | {
|
| 2476 | GDir *dp; |
| 2477 | const gchar *dir_name;
|
| 2478 | gchar *prev_dir; |
| 2479 | gint file_no; |
| 2480 | |
| 2481 | prev_dir = g_get_current_dir(); |
| 2482 | |
| 2483 | if (g_chdir(dir) < 0) { |
| 2484 | FILE_OP_ERROR(dir, "chdir");
|
| 2485 | g_free(prev_dir); |
| 2486 | return -1; |
| 2487 | } |
| 2488 | |
| 2489 | if ((dp = g_dir_open(".", 0, NULL)) == NULL) { |
| 2490 | g_warning("failed to open directory: %s\n", dir);
|
| 2491 | g_free(prev_dir); |
| 2492 | return -1; |
| 2493 | } |
| 2494 | |
| 2495 | while ((dir_name = g_dir_read_name(dp)) != NULL) { |
| 2496 | file_no = to_number(dir_name); |
| 2497 | if (file_no > 0 && first <= file_no && file_no <= last) { |
| 2498 | if (is_dir_exist(dir_name))
|
| 2499 | continue;
|
| 2500 | if (g_unlink(dir_name) < 0) |
| 2501 | FILE_OP_ERROR(dir_name, "unlink");
|
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | g_dir_close(dp); |
| 2506 | |
| 2507 | if (g_chdir(prev_dir) < 0) { |
| 2508 | FILE_OP_ERROR(prev_dir, "chdir");
|
| 2509 | g_free(prev_dir); |
| 2510 | return -1; |
| 2511 | } |
| 2512 | |
| 2513 | g_free(prev_dir); |
| 2514 | |
| 2515 | return 0; |
| 2516 | } |
| 2517 | |
| 2518 | gint remove_all_numbered_files(const gchar *dir)
|
| 2519 | {
|
| 2520 | return remove_numbered_files(dir, 0, UINT_MAX); |
| 2521 | } |
| 2522 | |
| 2523 | gint remove_expired_files(const gchar *dir, guint hours)
|
| 2524 | {
|
| 2525 | GDir *dp; |
| 2526 | const gchar *dir_name;
|
| 2527 | struct stat s;
|
| 2528 | gchar *prev_dir; |
| 2529 | gint file_no; |
| 2530 | time_t mtime, now, expire_time; |
| 2531 | |
| 2532 | prev_dir = g_get_current_dir(); |
| 2533 | |
| 2534 | if (g_chdir(dir) < 0) { |
| 2535 | FILE_OP_ERROR(dir, "chdir");
|
| 2536 | g_free(prev_dir); |
| 2537 | return -1; |
| 2538 | } |
| 2539 | |
| 2540 | if ((dp = g_dir_open(".", 0, NULL)) == NULL) { |
| 2541 | g_warning("failed to open directory: %s\n", dir);
|
| 2542 | g_free(prev_dir); |
| 2543 | return -1; |
| 2544 | } |
| 2545 | |
| 2546 | now = time(NULL);
|
| 2547 | expire_time = hours * 60 * 60; |
| 2548 | |
| 2549 | while ((dir_name = g_dir_read_name(dp)) != NULL) { |
| 2550 | file_no = to_number(dir_name); |
| 2551 | if (file_no > 0) { |
| 2552 | if (g_stat(dir_name, &s) < 0) { |
| 2553 | FILE_OP_ERROR(dir_name, "stat");
|
| 2554 | continue;
|
| 2555 | } |
| 2556 | if (S_ISDIR(s.st_mode))
|
| 2557 | continue;
|
| 2558 | mtime = MAX(s.st_mtime, s.st_atime); |
| 2559 | if (now - mtime > expire_time) {
|
| 2560 | if (g_unlink(dir_name) < 0) |
| 2561 | FILE_OP_ERROR(dir_name, "unlink");
|
| 2562 | } |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | g_dir_close(dp); |
| 2567 | |
| 2568 | if (g_chdir(prev_dir) < 0) { |
| 2569 | FILE_OP_ERROR(prev_dir, "chdir");
|
| 2570 | g_free(prev_dir); |
| 2571 | return -1; |
| 2572 | } |
| 2573 | |
| 2574 | g_free(prev_dir); |
| 2575 | |
| 2576 | return 0; |
| 2577 | } |
| 2578 | |
| 2579 | static gint remove_dir_recursive_real(const gchar *dir) |
| 2580 | {
|
| 2581 | struct stat s;
|
| 2582 | GDir *dp; |
| 2583 | const gchar *dir_name;
|
| 2584 | gchar *prev_dir; |
| 2585 | |
| 2586 | if (g_stat(dir, &s) < 0) { |
| 2587 | FILE_OP_ERROR(dir, "stat");
|
| 2588 | if (ENOENT == errno) return 0; |
| 2589 | return -1; |
| 2590 | } |
| 2591 | |
| 2592 | if (!S_ISDIR(s.st_mode)) {
|
| 2593 | if (g_unlink(dir) < 0) { |
| 2594 | FILE_OP_ERROR(dir, "unlink");
|
| 2595 | return -1; |
| 2596 | } |
| 2597 | |
| 2598 | return 0; |
| 2599 | } |
| 2600 | |
| 2601 | prev_dir = g_get_current_dir(); |
| 2602 | /* g_print("prev_dir = %s\n", prev_dir); */
|
| 2603 | |
| 2604 | if (g_chdir(dir) < 0) { |
| 2605 | FILE_OP_ERROR(dir, "chdir");
|
| 2606 | g_free(prev_dir); |
| 2607 | return -1; |
| 2608 | } |
| 2609 | |
| 2610 | if ((dp = g_dir_open(".", 0, NULL)) == NULL) { |
| 2611 | g_warning("failed to open directory: %s\n", dir);
|
| 2612 | g_chdir(prev_dir); |
| 2613 | g_free(prev_dir); |
| 2614 | return -1; |
| 2615 | } |
| 2616 | |
| 2617 | /* remove all files in the directory */
|
| 2618 | while ((dir_name = g_dir_read_name(dp)) != NULL) { |
| 2619 | /* g_print("removing %s\n", dir_name); */
|
| 2620 | |
| 2621 | if (is_dir_exist(dir_name)) {
|
| 2622 | if (remove_dir_recursive_real(dir_name) < 0) { |
| 2623 | g_warning("can't remove directory\n");
|
| 2624 | return -1; |
| 2625 | } |
| 2626 | } else {
|
| 2627 | if (g_unlink(dir_name) < 0) |
| 2628 | FILE_OP_ERROR(dir_name, "unlink");
|
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | g_dir_close(dp); |
| 2633 | |
| 2634 | if (g_chdir(prev_dir) < 0) { |
| 2635 | FILE_OP_ERROR(prev_dir, "chdir");
|
| 2636 | g_free(prev_dir); |
| 2637 | return -1; |
| 2638 | } |
| 2639 | |
| 2640 | g_free(prev_dir); |
| 2641 | |
| 2642 | if (g_rmdir(dir) < 0) { |
| 2643 | if (ENOTDIR == errno) {
|
| 2644 | if (g_unlink(dir) < 0) { |
| 2645 | FILE_OP_ERROR(dir, "unlink");
|
| 2646 | return -1; |
| 2647 | } |
| 2648 | } else {
|
| 2649 | FILE_OP_ERROR(dir, "rmdir");
|
| 2650 | return -1; |
| 2651 | } |
| 2652 | } |
| 2653 | |
| 2654 | return 0; |
| 2655 | } |
| 2656 | |
| 2657 | gint remove_dir_recursive(const gchar *dir)
|
| 2658 | {
|
| 2659 | gchar *cur_dir; |
| 2660 | gint ret; |
| 2661 | |
| 2662 | cur_dir = g_get_current_dir(); |
| 2663 | |
| 2664 | if (g_chdir(dir) < 0) { |
| 2665 | FILE_OP_ERROR(dir, "chdir");
|
| 2666 | ret = -1;
|
| 2667 | goto leave;
|
| 2668 | } |
| 2669 | if (g_chdir("..") < 0) { |
| 2670 | FILE_OP_ERROR(dir, "chdir");
|
| 2671 | ret = -1;
|
| 2672 | goto leave;
|
| 2673 | } |
| 2674 | |
| 2675 | ret = remove_dir_recursive_real(dir); |
| 2676 | |
| 2677 | leave:
|
| 2678 | if (is_dir_exist(cur_dir)) {
|
| 2679 | if (g_chdir(cur_dir) < 0) { |
| 2680 | FILE_OP_ERROR(cur_dir, "chdir");
|
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | g_free(cur_dir); |
| 2685 | |
| 2686 | return ret;
|
| 2687 | } |
| 2688 | |
| 2689 | gint rename_force(const gchar *oldpath, const gchar *newpath) |
| 2690 | {
|
| 2691 | #if !defined(G_OS_UNIX) && !GLIB_CHECK_VERSION(2, 9, 1) |
| 2692 | if (!is_file_entry_exist(oldpath)) {
|
| 2693 | errno = ENOENT; |
| 2694 | return -1; |
| 2695 | } |
| 2696 | if (is_file_exist(newpath)) {
|
| 2697 | if (g_unlink(newpath) < 0) |
| 2698 | FILE_OP_ERROR(newpath, "unlink");
|
| 2699 | } |
| 2700 | #endif
|
| 2701 | return g_rename(oldpath, newpath);
|
| 2702 | } |
| 2703 | |
| 2704 | gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup) |
| 2705 | {
|
| 2706 | #ifdef G_OS_WIN32
|
| 2707 | wchar_t *wsrc; |
| 2708 | wchar_t *wdest; |
| 2709 | gchar *dest_bak = NULL;
|
| 2710 | gboolean err = FALSE; |
| 2711 | |
| 2712 | wsrc = g_utf8_to_utf16(src, -1, NULL, NULL, NULL); |
| 2713 | if (wsrc == NULL) { |
| 2714 | return -1; |
| 2715 | } |
| 2716 | wdest = g_utf8_to_utf16(dest, -1, NULL, NULL, NULL); |
| 2717 | if (wdest == NULL) { |
| 2718 | g_free(wsrc); |
| 2719 | return -1; |
| 2720 | } |
| 2721 | |
| 2722 | if (keep_backup == FALSE) {
|
| 2723 | if (CopyFileW(wsrc, wdest, FALSE) == 0) |
| 2724 | err = TRUE; |
| 2725 | g_free(wdest); |
| 2726 | g_free(wsrc); |
| 2727 | return err ? -1 : 0; |
| 2728 | } |
| 2729 | |
| 2730 | if (is_file_exist(dest)) {
|
| 2731 | dest_bak = g_strconcat(dest, ".bak", NULL); |
| 2732 | if (rename_force(dest, dest_bak) < 0) { |
| 2733 | FILE_OP_ERROR(dest, "rename");
|
| 2734 | g_free(dest_bak); |
| 2735 | g_free(wdest); |
| 2736 | g_free(wsrc); |
| 2737 | return -1; |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | if (CopyFileW(wsrc, wdest, FALSE) == 0) |
| 2742 | err = TRUE; |
| 2743 | |
| 2744 | g_free(wdest); |
| 2745 | g_free(wsrc); |
| 2746 | #else
|
| 2747 | gint srcfd, destfd; |
| 2748 | gint n_read; |
| 2749 | gchar buf[BUFFSIZE]; |
| 2750 | gchar *dest_bak = NULL;
|
| 2751 | gboolean err = FALSE; |
| 2752 | |
| 2753 | if ((srcfd = g_open(src, O_RDONLY, 0600)) < 0) { |
| 2754 | FILE_OP_ERROR(src, "open");
|
| 2755 | return -1; |
| 2756 | } |
| 2757 | if (is_file_exist(dest)) {
|
| 2758 | dest_bak = g_strconcat(dest, ".bak", NULL); |
| 2759 | if (rename_force(dest, dest_bak) < 0) { |
| 2760 | FILE_OP_ERROR(dest, "rename");
|
| 2761 | close(srcfd); |
| 2762 | g_free(dest_bak); |
| 2763 | return -1; |
| 2764 | } |
| 2765 | } |
| 2766 | |
| 2767 | if ((destfd = g_open(dest, O_WRONLY | O_CREAT, 0600)) < 0) { |
| 2768 | FILE_OP_ERROR(dest, "open");
|
| 2769 | close(srcfd); |
| 2770 | if (dest_bak) {
|
| 2771 | if (rename_force(dest_bak, dest) < 0) |
| 2772 | FILE_OP_ERROR(dest_bak, "rename");
|
| 2773 | g_free(dest_bak); |
| 2774 | } |
| 2775 | return -1; |
| 2776 | } |
| 2777 | |
| 2778 | while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) { |
| 2779 | gchar *p = buf; |
| 2780 | const gchar *endp = buf + n_read;
|
| 2781 | gint n_write; |
| 2782 | |
| 2783 | while (p < endp) {
|
| 2784 | if ((n_write = write(destfd, p, endp - p)) < 0) { |
| 2785 | g_warning(_("writing to %s failed.\n"), dest);
|
| 2786 | close(destfd); |
| 2787 | close(srcfd); |
| 2788 | g_unlink(dest); |
| 2789 | if (dest_bak) {
|
| 2790 | if (rename_force(dest_bak, dest) < 0) |
| 2791 | FILE_OP_ERROR(dest_bak, "rename");
|
| 2792 | g_free(dest_bak); |
| 2793 | } |
| 2794 | return -1; |
| 2795 | } |
| 2796 | p += n_write; |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | if (close(destfd) < 0) { |
| 2801 | FILE_OP_ERROR(dest, "close");
|
| 2802 | err = TRUE; |
| 2803 | } |
| 2804 | close(srcfd); |
| 2805 | #endif
|
| 2806 | |
| 2807 | if (err) {
|
| 2808 | g_unlink(dest); |
| 2809 | if (dest_bak) {
|
| 2810 | if (rename_force(dest_bak, dest) < 0) |
| 2811 | FILE_OP_ERROR(dest_bak, "rename");
|
| 2812 | g_free(dest_bak); |
| 2813 | } |
| 2814 | return -1; |
| 2815 | } |
| 2816 | |
| 2817 | if (keep_backup == FALSE && dest_bak)
|
| 2818 | g_unlink(dest_bak); |
| 2819 | |
| 2820 | g_free(dest_bak); |
| 2821 | |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
| 2825 | gint copy_dir(const gchar *src, const gchar *dest) |
| 2826 | {
|
| 2827 | GDir *dir; |
| 2828 | const gchar *dir_name;
|
| 2829 | gchar *src_file; |
| 2830 | gchar *dest_file; |
| 2831 | |
| 2832 | if ((dir = g_dir_open(src, 0, NULL)) == NULL) { |
| 2833 | g_warning("failed to open directory: %s\n", src);
|
| 2834 | return -1; |
| 2835 | } |
| 2836 | |
| 2837 | if (make_dir_hier(dest) < 0) { |
| 2838 | g_dir_close(dir); |
| 2839 | return -1; |
| 2840 | } |
| 2841 | |
| 2842 | while ((dir_name = g_dir_read_name(dir)) != NULL) { |
| 2843 | src_file = g_strconcat(src, G_DIR_SEPARATOR_S, dir_name, NULL);
|
| 2844 | dest_file = g_strconcat(dest, G_DIR_SEPARATOR_S, dir_name, |
| 2845 | NULL);
|
| 2846 | if (is_file_exist(src_file))
|
| 2847 | copy_file(src_file, dest_file, FALSE); |
| 2848 | g_free(dest_file); |
| 2849 | g_free(src_file); |
| 2850 | } |
| 2851 | |
| 2852 | g_dir_close(dir); |
| 2853 | |
| 2854 | return 0; |
| 2855 | } |
| 2856 | |
| 2857 | gint move_file(const gchar *src, const gchar *dest, gboolean overwrite) |
| 2858 | {
|
| 2859 | if (overwrite == FALSE && is_file_entry_exist(dest)) {
|
| 2860 | g_warning("move_file(): file %s already exists.", dest);
|
| 2861 | return -1; |
| 2862 | } |
| 2863 | |
| 2864 | if (rename_force(src, dest) == 0) return 0; |
| 2865 | |
| 2866 | if (EXDEV != errno) {
|
| 2867 | FILE_OP_ERROR(src, "rename");
|
| 2868 | return -1; |
| 2869 | } |
| 2870 | |
| 2871 | if (copy_file(src, dest, FALSE) < 0) return -1; |
| 2872 | |
| 2873 | g_unlink(src); |
| 2874 | |
| 2875 | return 0; |
| 2876 | } |
| 2877 | |
| 2878 | gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
|
| 2879 | {
|
| 2880 | FILE *dest_fp; |
| 2881 | gint n_read; |
| 2882 | gint bytes_left, to_read; |
| 2883 | gchar buf[BUFSIZ]; |
| 2884 | gboolean err = FALSE; |
| 2885 | |
| 2886 | if (fseek(fp, offset, SEEK_SET) < 0) { |
| 2887 | perror("fseek");
|
| 2888 | return -1; |
| 2889 | } |
| 2890 | |
| 2891 | if ((dest_fp = g_fopen(dest, "wb")) == NULL) { |
| 2892 | FILE_OP_ERROR(dest, "fopen");
|
| 2893 | return -1; |
| 2894 | } |
| 2895 | |
| 2896 | if (change_file_mode_rw(dest_fp, dest) < 0) { |
| 2897 | FILE_OP_ERROR(dest, "chmod");
|
| 2898 | g_warning("can't change file mode\n");
|
| 2899 | } |
| 2900 | |
| 2901 | bytes_left = length; |
| 2902 | to_read = MIN(bytes_left, sizeof(buf));
|
| 2903 | |
| 2904 | while ((n_read = fread(buf, sizeof(gchar), to_read, fp)) > 0) { |
| 2905 | if (n_read < to_read && ferror(fp))
|
| 2906 | break;
|
| 2907 | if (fwrite(buf, n_read, 1, dest_fp) < 1) { |
| 2908 | g_warning(_("writing to %s failed.\n"), dest);
|
| 2909 | fclose(dest_fp); |
| 2910 | g_unlink(dest); |
| 2911 | return -1; |
| 2912 | } |
| 2913 | bytes_left -= n_read; |
| 2914 | if (bytes_left == 0) |
| 2915 | break;
|
| 2916 | to_read = MIN(bytes_left, sizeof(buf));
|
| 2917 | } |
| 2918 | |
| 2919 | if (ferror(fp)) {
|
| 2920 | perror("fread");
|
| 2921 | err = TRUE; |
| 2922 | } |
| 2923 | if (fclose(dest_fp) == EOF) { |
| 2924 | FILE_OP_ERROR(dest, "fclose");
|
| 2925 | err = TRUE; |
| 2926 | } |
| 2927 | |
| 2928 | if (err) {
|
| 2929 | g_unlink(dest); |
| 2930 | return -1; |
| 2931 | } |
| 2932 | |
| 2933 | return 0; |
| 2934 | } |
| 2935 | |
| 2936 | /* convert line endings into CRLF. If the last line doesn't end with
|
| 2937 | * linebreak, add it. |
| 2938 | */ |
| 2939 | gchar *canonicalize_str(const gchar *str)
|
| 2940 | {
|
| 2941 | const gchar *p;
|
| 2942 | guint new_len = 0;
|
| 2943 | gchar *out, *outp; |
| 2944 | |
| 2945 | for (p = str; *p != '\0'; ++p) { |
| 2946 | if (*p != '\r') { |
| 2947 | ++new_len; |
| 2948 | if (*p == '\n') |
| 2949 | ++new_len; |
| 2950 | } |
| 2951 | } |
| 2952 | if (p == str || *(p - 1) != '\n') |
| 2953 | new_len += 2;
|
| 2954 | |
| 2955 | out = outp = g_malloc(new_len + 1);
|
| 2956 | for (p = str; *p != '\0'; ++p) { |
| 2957 | if (*p != '\r') { |
| 2958 | if (*p == '\n') |
| 2959 | *outp++ = '\r';
|
| 2960 | *outp++ = *p; |
| 2961 | } |
| 2962 | } |
| 2963 | if (p == str || *(p - 1) != '\n') { |
| 2964 | *outp++ = '\r';
|
| 2965 | *outp++ = '\n';
|
| 2966 | } |
| 2967 | *outp = '\0';
|
| 2968 | |
| 2969 | return out;
|
| 2970 | } |
| 2971 | |
| 2972 | gint canonicalize_file(const gchar *src, const gchar *dest) |
| 2973 | {
|
| 2974 | FILE *src_fp, *dest_fp; |
| 2975 | gchar buf[BUFFSIZE]; |
| 2976 | gint len; |
| 2977 | gboolean err = FALSE; |
| 2978 | gboolean last_linebreak = FALSE; |
| 2979 | |
| 2980 | if ((src_fp = g_fopen(src, "rb")) == NULL) { |
| 2981 | FILE_OP_ERROR(src, "fopen");
|
| 2982 | return -1; |
| 2983 | } |
| 2984 | |
| 2985 | if ((dest_fp = g_fopen(dest, "wb")) == NULL) { |
| 2986 | FILE_OP_ERROR(dest, "fopen");
|
| 2987 | fclose(src_fp); |
| 2988 | return -1; |
| 2989 | } |
| 2990 | |
| 2991 | if (change_file_mode_rw(dest_fp, dest) < 0) { |
| 2992 | FILE_OP_ERROR(dest, "chmod");
|
| 2993 | g_warning("can't change file mode\n");
|
| 2994 | } |
| 2995 | |
| 2996 | while (fgets(buf, sizeof(buf), src_fp) != NULL) { |
| 2997 | gint r = 0;
|
| 2998 | |
| 2999 | len = strlen(buf); |
| 3000 | if (len == 0) break; |
| 3001 | last_linebreak = FALSE; |
| 3002 | |
| 3003 | if (buf[len - 1] != '\n') { |
| 3004 | last_linebreak = TRUE; |
| 3005 | r = fputs(buf, dest_fp); |
| 3006 | } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') { |
| 3007 | r = fputs(buf, dest_fp); |
| 3008 | } else {
|
| 3009 | if (len > 1) { |
| 3010 | r = fwrite(buf, len - 1, 1, dest_fp); |
| 3011 | if (r != 1) |
| 3012 | r = EOF;
|
| 3013 | } |
| 3014 | if (r != EOF) |
| 3015 | r = fputs("\r\n", dest_fp);
|
| 3016 | } |
| 3017 | |
| 3018 | if (r == EOF) { |
| 3019 | g_warning("writing to %s failed.\n", dest);
|
| 3020 | fclose(dest_fp); |
| 3021 | fclose(src_fp); |
| 3022 | g_unlink(dest); |
| 3023 | return -1; |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | if (last_linebreak == TRUE) {
|
| 3028 | if (fputs("\r\n", dest_fp) == EOF) |
| 3029 | err = TRUE; |
| 3030 | } |
| 3031 | |
| 3032 | if (ferror(src_fp)) {
|
| 3033 | FILE_OP_ERROR(src, "fgets");
|
| 3034 | err = TRUE; |
| 3035 | } |
| 3036 | fclose(src_fp); |
| 3037 | if (fclose(dest_fp) == EOF) { |
| 3038 | FILE_OP_ERROR(dest, "fclose");
|
| 3039 | err = TRUE; |
| 3040 | } |
| 3041 | |
| 3042 | if (err) {
|
| 3043 | g_unlink(dest); |
| 3044 | return -1; |
| 3045 | } |
| 3046 | |
| 3047 | return 0; |
| 3048 | } |
| 3049 | |
| 3050 | gint canonicalize_file_replace(const gchar *file)
|
| 3051 | {
|
| 3052 | gchar *tmp_file; |
| 3053 | |
| 3054 | tmp_file = get_tmp_file(); |
| 3055 | |
| 3056 | if (canonicalize_file(file, tmp_file) < 0) { |
| 3057 | g_free(tmp_file); |
| 3058 | return -1; |
| 3059 | } |
| 3060 | |
| 3061 | if (move_file(tmp_file, file, TRUE) < 0) { |
| 3062 | g_warning("can't replace %s .\n", file);
|
| 3063 | g_unlink(tmp_file); |
| 3064 | g_free(tmp_file); |
| 3065 | return -1; |
| 3066 | } |
| 3067 | |
| 3068 | g_free(tmp_file); |
| 3069 | return 0; |
| 3070 | } |
| 3071 | |
| 3072 | FILE *canonicalize_file_stream(FILE *src_fp, gint *length) |
| 3073 | {
|
| 3074 | FILE *dest_fp; |
| 3075 | gchar buf[BUFFSIZE]; |
| 3076 | gint len; |
| 3077 | gint length_ = 0;
|
| 3078 | gboolean err = FALSE; |
| 3079 | gboolean last_linebreak = FALSE; |
| 3080 | |
| 3081 | if ((dest_fp = my_tmpfile()) == NULL) |
| 3082 | return NULL; |
| 3083 | |
| 3084 | while (fgets(buf, sizeof(buf), src_fp) != NULL) { |
| 3085 | gint r = 0;
|
| 3086 | |
| 3087 | len = strlen(buf); |
| 3088 | if (len == 0) break; |
| 3089 | last_linebreak = FALSE; |
| 3090 | |
| 3091 | if (buf[len - 1] != '\n') { |
| 3092 | last_linebreak = TRUE; |
| 3093 | r = fputs(buf, dest_fp); |
| 3094 | length_ += len; |
| 3095 | } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') { |
| 3096 | r = fputs(buf, dest_fp); |
| 3097 | length_ += len; |
| 3098 | } else {
|
| 3099 | if (len > 1) { |
| 3100 | r = fwrite(buf, len - 1, 1, dest_fp); |
| 3101 | if (r != 1) |
| 3102 | r = EOF;
|
| 3103 | else
|
| 3104 | length_ += len - 1;
|
| 3105 | } |
| 3106 | if (r != EOF) { |
| 3107 | r = fputs("\r\n", dest_fp);
|
| 3108 | length_ += 2;
|
| 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | if (r == EOF) { |
| 3113 | g_warning("writing to temporary file failed.\n");
|
| 3114 | fclose(dest_fp); |
| 3115 | return NULL; |
| 3116 | } |
| 3117 | } |
| 3118 | |
| 3119 | if (last_linebreak == TRUE) {
|
| 3120 | if (fputs("\r\n", dest_fp) == EOF) |
| 3121 | err = TRUE; |
| 3122 | else
|
| 3123 | length_ += 2;
|
| 3124 | } |
| 3125 | |
| 3126 | if (ferror(src_fp)) {
|
| 3127 | FILE_OP_ERROR("canonicalize_file_stream", "fgets"); |
| 3128 | err = TRUE; |
| 3129 | } |
| 3130 | if (fflush(dest_fp) == EOF) { |
| 3131 | FILE_OP_ERROR("canonicalize_file_stream", "fflush"); |
| 3132 | err = TRUE; |
| 3133 | } |
| 3134 | |
| 3135 | if (err) {
|
| 3136 | fclose(dest_fp); |
| 3137 | return NULL; |
| 3138 | } |
| 3139 | |
| 3140 | if (length)
|
| 3141 | *length = length_; |
| 3142 | |
| 3143 | rewind(dest_fp); |
| 3144 | return dest_fp;
|
| 3145 | } |
| 3146 | |
| 3147 | gint uncanonicalize_file(const gchar *src, const gchar *dest) |
| 3148 | {
|
| 3149 | FILE *src_fp, *dest_fp; |
| 3150 | gchar buf[BUFFSIZE]; |
| 3151 | gboolean err = FALSE; |
| 3152 | |
| 3153 | if ((src_fp = g_fopen(src, "rb")) == NULL) { |
| 3154 | FILE_OP_ERROR(src, "fopen");
|
| 3155 | return -1; |
| 3156 | } |
| 3157 | |
| 3158 | if ((dest_fp = g_fopen(dest, "wb")) == NULL) { |
| 3159 | FILE_OP_ERROR(dest, "fopen");
|
| 3160 | fclose(src_fp); |
| 3161 | return -1; |
| 3162 | } |
| 3163 | |
| 3164 | if (change_file_mode_rw(dest_fp, dest) < 0) { |
| 3165 | FILE_OP_ERROR(dest, "chmod");
|
| 3166 | g_warning("can't change file mode\n");
|
| 3167 | } |
| 3168 | |
| 3169 | while (fgets(buf, sizeof(buf), src_fp) != NULL) { |
| 3170 | strcrchomp(buf); |
| 3171 | if (fputs(buf, dest_fp) == EOF) { |
| 3172 | g_warning("writing to %s failed.\n", dest);
|
| 3173 | fclose(dest_fp); |
| 3174 | fclose(src_fp); |
| 3175 | g_unlink(dest); |
| 3176 | return -1; |
| 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | if (ferror(src_fp)) {
|
| 3181 | FILE_OP_ERROR(src, "fgets");
|
| 3182 | err = TRUE; |
| 3183 | } |
| 3184 | fclose(src_fp); |
| 3185 | if (fclose(dest_fp) == EOF) { |
| 3186 | FILE_OP_ERROR(dest, "fclose");
|
| 3187 | err = TRUE; |
| 3188 | } |
| 3189 | |
| 3190 | if (err) {
|
| 3191 | g_unlink(dest); |
| 3192 | return -1; |
| 3193 | } |
| 3194 | |
| 3195 | return 0; |
| 3196 | } |
| 3197 | |
| 3198 | gint uncanonicalize_file_replace(const gchar *file)
|
| 3199 | {
|
| 3200 | gchar *tmp_file; |
| 3201 | |
| 3202 | tmp_file = get_tmp_file(); |
| 3203 | |
| 3204 | if (uncanonicalize_file(file, tmp_file) < 0) { |
| 3205 | g_free(tmp_file); |
| 3206 | return -1; |
| 3207 | } |
| 3208 | |
| 3209 | if (move_file(tmp_file, file, TRUE) < 0) { |
| 3210 | g_warning("can't replace %s .\n", file);
|
| 3211 | g_unlink(tmp_file); |
| 3212 | g_free(tmp_file); |
| 3213 | return -1; |
| 3214 | } |
| 3215 | |
| 3216 | g_free(tmp_file); |
| 3217 | return 0; |
| 3218 | } |
| 3219 | |
| 3220 | gchar *normalize_newlines(const gchar *str)
|
| 3221 | {
|
| 3222 | const gchar *p = str;
|
| 3223 | gchar *out, *outp; |
| 3224 | |
| 3225 | out = outp = g_malloc(strlen(str) + 1);
|
| 3226 | for (p = str; *p != '\0'; ++p) { |
| 3227 | if (*p == '\r') { |
| 3228 | if (*(p + 1) != '\n') |
| 3229 | *outp++ = '\n';
|
| 3230 | } else
|
| 3231 | *outp++ = *p; |
| 3232 | } |
| 3233 | |
| 3234 | *outp = '\0';
|
| 3235 | |
| 3236 | return out;
|
| 3237 | } |
| 3238 | |
| 3239 | gchar *strchomp_all(const gchar *str)
|
| 3240 | {
|
| 3241 | const gchar *p = str;
|
| 3242 | const gchar *newline, *last;
|
| 3243 | gchar *out, *outp; |
| 3244 | |
| 3245 | out = outp = g_malloc(strlen(str) + 1);
|
| 3246 | while (*p != '\0') { |
| 3247 | newline = strchr(p, '\n');
|
| 3248 | if (newline) {
|
| 3249 | for (last = newline;
|
| 3250 | p < last && g_ascii_isspace(*(last - 1)); --last)
|
| 3251 | ; |
| 3252 | strncpy(outp, p, last - p); |
| 3253 | outp += last - p; |
| 3254 | |
| 3255 | if (p < newline && *(newline - 1) == '\r') { |
| 3256 | strncpy(outp, newline - 1, 2); |
| 3257 | outp += 2;
|
| 3258 | } else {
|
| 3259 | *outp++ = *newline; |
| 3260 | } |
| 3261 | |
| 3262 | p = newline + 1;
|
| 3263 | } else {
|
| 3264 | for (last = p + strlen(p);
|
| 3265 | p < last && g_ascii_isspace(*(last - 1)); --last)
|
| 3266 | ; |
| 3267 | strncpy(outp, p, last - p); |
| 3268 | outp += last - p; |
| 3269 | break;
|
| 3270 | } |
| 3271 | } |
| 3272 | |
| 3273 | *outp = '\0';
|
| 3274 | |
| 3275 | return out;
|
| 3276 | } |
| 3277 | |
| 3278 | FILE *get_outgoing_rfc2822_file(FILE *fp) |
| 3279 | {
|
| 3280 | gchar buf[BUFFSIZE]; |
| 3281 | FILE *outfp; |
| 3282 | |
| 3283 | outfp = my_tmpfile(); |
| 3284 | if (!outfp) {
|
| 3285 | FILE_OP_ERROR("get_outgoing_rfc2822_file", "my_tmpfile"); |
| 3286 | return NULL; |
| 3287 | } |
| 3288 | |
| 3289 | /* output header part */
|
| 3290 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 3291 | strretchomp(buf); |
| 3292 | if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) { |
| 3293 | gint next; |
| 3294 | |
| 3295 | for (;;) {
|
| 3296 | next = fgetc(fp); |
| 3297 | if (next == EOF) |
| 3298 | break;
|
| 3299 | else if (next != ' ' && next != '\t') { |
| 3300 | ungetc(next, fp); |
| 3301 | break;
|
| 3302 | } |
| 3303 | if (fgets(buf, sizeof(buf), fp) == NULL) |
| 3304 | break;
|
| 3305 | } |
| 3306 | } else {
|
| 3307 | if (fputs(buf, outfp) == EOF) |
| 3308 | goto file_error;
|
| 3309 | if (fputs("\r\n", outfp) == EOF) |
| 3310 | goto file_error;
|
| 3311 | if (buf[0] == '\0') |
| 3312 | break;
|
| 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | /* output body part */
|
| 3317 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 3318 | strretchomp(buf); |
| 3319 | if (buf[0] == '.') { |
| 3320 | if (fputc('.', outfp) == EOF) |
| 3321 | goto file_error;
|
| 3322 | } |
| 3323 | if (fputs(buf, outfp) == EOF) |
| 3324 | goto file_error;
|
| 3325 | if (fputs("\r\n", outfp) == EOF) |
| 3326 | goto file_error;
|
| 3327 | } |
| 3328 | |
| 3329 | if (fflush(outfp) == EOF) { |
| 3330 | FILE_OP_ERROR("get_outgoing_rfc2822_file", "fflush"); |
| 3331 | goto file_error;
|
| 3332 | } |
| 3333 | |
| 3334 | rewind(outfp); |
| 3335 | return outfp;
|
| 3336 | |
| 3337 | file_error:
|
| 3338 | g_warning("get_outgoing_rfc2822_file(): writing to temporary file failed.\n");
|
| 3339 | fclose(outfp); |
| 3340 | return NULL; |
| 3341 | } |
| 3342 | |
| 3343 | gchar *get_outgoing_rfc2822_str(FILE *fp) |
| 3344 | {
|
| 3345 | gchar buf[BUFFSIZE]; |
| 3346 | GString *str; |
| 3347 | gchar *ret; |
| 3348 | |
| 3349 | str = g_string_new(NULL);
|
| 3350 | |
| 3351 | /* output header part */
|
| 3352 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 3353 | strretchomp(buf); |
| 3354 | if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) { |
| 3355 | gint next; |
| 3356 | |
| 3357 | for (;;) {
|
| 3358 | next = fgetc(fp); |
| 3359 | if (next == EOF) |
| 3360 | break;
|
| 3361 | else if (next != ' ' && next != '\t') { |
| 3362 | ungetc(next, fp); |
| 3363 | break;
|
| 3364 | } |
| 3365 | if (fgets(buf, sizeof(buf), fp) == NULL) |
| 3366 | break;
|
| 3367 | } |
| 3368 | #if 0
|
| 3369 | } else if (!g_ascii_strncasecmp(buf, "Date:", 5)) {
|
| 3370 | get_rfc822_date(buf, sizeof(buf)); |
| 3371 | g_string_append_printf(str, "Date: %s\r\n", buf); |
| 3372 | #endif |
| 3373 | } else {
|
| 3374 | g_string_append(str, buf); |
| 3375 | g_string_append(str, "\r\n");
|
| 3376 | if (buf[0] == '\0') |
| 3377 | break;
|
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | /* output body part */
|
| 3382 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 3383 | strretchomp(buf); |
| 3384 | if (buf[0] == '.') |
| 3385 | g_string_append_c(str, '.');
|
| 3386 | g_string_append(str, buf); |
| 3387 | g_string_append(str, "\r\n");
|
| 3388 | } |
| 3389 | |
| 3390 | ret = str->str; |
| 3391 | g_string_free(str, FALSE); |
| 3392 | |
| 3393 | return ret;
|
| 3394 | } |
| 3395 | |
| 3396 | /*
|
| 3397 | * Create a new boundary in a way that it is very unlikely that this |
| 3398 | * will occur in the following text. It would be easy to ensure |
| 3399 | * uniqueness if everything is either quoted-printable or base64 |
| 3400 | * encoded (note that conversion is allowed), but because MIME bodies |
| 3401 | * may be nested, it may happen that the same boundary has already |
| 3402 | * been used. We avoid scanning the message for conflicts and hope the |
| 3403 | * best. |
| 3404 | * |
| 3405 | * boundary := 0*69<bchars> bcharsnospace |
| 3406 | * bchars := bcharsnospace / " " |
| 3407 | * bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
|
| 3408 | * "+" / "_" / "," / "-" / "." / |
| 3409 | * "/" / ":" / "=" / "?" |
| 3410 | * |
| 3411 | * some special characters removed because of buggy MTAs |
| 3412 | */ |
| 3413 | |
| 3414 | gchar *generate_mime_boundary(const gchar *prefix)
|
| 3415 | {
|
| 3416 | static gchar tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 3417 | "abcdefghijklmnopqrstuvwxyz"
|
| 3418 | "1234567890+_./=";
|
| 3419 | gchar buf_uniq[17];
|
| 3420 | gchar buf_date[64];
|
| 3421 | gint i; |
| 3422 | |
| 3423 | for (i = 0; i < sizeof(buf_uniq) - 1; i++) |
| 3424 | buf_uniq[i] = tbl[g_random_int_range(0, sizeof(tbl) - 1)]; |
| 3425 | buf_uniq[i] = '\0';
|
| 3426 | |
| 3427 | get_rfc822_date(buf_date, sizeof(buf_date));
|
| 3428 | subst_chars(buf_date, " ,:", '_'); |
| 3429 | |
| 3430 | return g_strdup_printf("%s=_%s_%s", prefix ? prefix : "Multipart", |
| 3431 | buf_date, buf_uniq); |
| 3432 | } |
| 3433 | |
| 3434 | gint change_file_mode_rw(FILE *fp, const gchar *file)
|
| 3435 | {
|
| 3436 | #ifdef G_OS_WIN32
|
| 3437 | DWORD attr; |
| 3438 | BOOL retval; |
| 3439 | |
| 3440 | if (G_WIN32_HAVE_WIDECHAR_API()) {
|
| 3441 | wchar_t *wpath; |
| 3442 | |
| 3443 | wpath = g_utf8_to_utf16(file, -1, NULL, NULL, NULL); |
| 3444 | if (wpath == NULL) |
| 3445 | return -1; |
| 3446 | |
| 3447 | attr = GetFileAttributesW(wpath); |
| 3448 | retval = SetFileAttributesW |
| 3449 | (wpath, attr & ~(FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN)); |
| 3450 | |
| 3451 | g_free(wpath); |
| 3452 | } else {
|
| 3453 | gchar *cp_path; |
| 3454 | |
| 3455 | cp_path = g_locale_from_utf8(file, -1, NULL, NULL, NULL); |
| 3456 | if (cp_path == NULL) |
| 3457 | return -1; |
| 3458 | |
| 3459 | attr = GetFileAttributesA(cp_path); |
| 3460 | retval = SetFileAttributesA |
| 3461 | (cp_path, attr & ~(FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN)); |
| 3462 | |
| 3463 | g_free(cp_path); |
| 3464 | } |
| 3465 | |
| 3466 | if (retval)
|
| 3467 | return 0; |
| 3468 | else
|
| 3469 | return -1; |
| 3470 | #else
|
| 3471 | #if HAVE_FCHMOD
|
| 3472 | if (fp)
|
| 3473 | return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
|
| 3474 | else
|
| 3475 | #endif
|
| 3476 | return g_chmod(file, S_IRUSR|S_IWUSR);
|
| 3477 | #endif
|
| 3478 | } |
| 3479 | |
| 3480 | #ifdef G_OS_WIN32
|
| 3481 | gchar *_s_tempnam(const gchar *dir, const gchar *prefix) |
| 3482 | {
|
| 3483 | if (G_WIN32_HAVE_WIDECHAR_API()) {
|
| 3484 | wchar_t *wpath; |
| 3485 | wchar_t *wprefix; |
| 3486 | wchar_t *wname; |
| 3487 | gint save_errno; |
| 3488 | gchar *name; |
| 3489 | |
| 3490 | wpath = g_utf8_to_utf16(dir, -1, NULL, NULL, NULL); |
| 3491 | if (wpath == NULL) { |
| 3492 | errno = EINVAL; |
| 3493 | return NULL; |
| 3494 | } |
| 3495 | wprefix = g_utf8_to_utf16(prefix, -1, NULL, NULL, NULL); |
| 3496 | if (wprefix == NULL) { |
| 3497 | errno = EINVAL; |
| 3498 | g_free(wpath); |
| 3499 | return NULL; |
| 3500 | } |
| 3501 | |
| 3502 | wname = _wtempnam(wpath, wprefix); |
| 3503 | save_errno = errno; |
| 3504 | |
| 3505 | name = g_utf16_to_utf8(wname, -1, NULL, NULL, NULL); |
| 3506 | if (name == NULL) { |
| 3507 | save_errno = EINVAL; |
| 3508 | } |
| 3509 | |
| 3510 | g_free(wname); |
| 3511 | g_free(wprefix); |
| 3512 | g_free(wpath); |
| 3513 | |
| 3514 | errno = save_errno; |
| 3515 | return name;
|
| 3516 | } else {
|
| 3517 | gchar *cp_path; |
| 3518 | gchar *cp_prefix; |
| 3519 | gchar *cp_name; |
| 3520 | gint save_errno; |
| 3521 | gchar *name; |
| 3522 | |
| 3523 | cp_path = g_locale_from_utf8(dir, -1, NULL, NULL, NULL); |
| 3524 | if (cp_path == NULL) { |
| 3525 | errno = EINVAL; |
| 3526 | return NULL; |
| 3527 | } |
| 3528 | |
| 3529 | cp_prefix = g_locale_from_utf8(prefix, -1, NULL, NULL, NULL); |
| 3530 | if (cp_prefix == NULL) { |
| 3531 | errno = EINVAL; |
| 3532 | g_free(cp_path); |
| 3533 | return NULL; |
| 3534 | } |
| 3535 | |
| 3536 | cp_name = _tempnam(cp_path, cp_prefix); |
| 3537 | save_errno = errno; |
| 3538 | |
| 3539 | name = g_locale_to_utf8(cp_name, -1, NULL, NULL, NULL); |
| 3540 | if (name == NULL) { |
| 3541 | save_errno = EINVAL; |
| 3542 | } |
| 3543 | |
| 3544 | g_free(cp_name); |
| 3545 | g_free(cp_prefix); |
| 3546 | g_free(cp_path); |
| 3547 | |
| 3548 | errno = save_errno; |
| 3549 | return name;
|
| 3550 | } |
| 3551 | } |
| 3552 | #endif
|
| 3553 | |
| 3554 | FILE *my_tmpfile(void)
|
| 3555 | {
|
| 3556 | #ifdef G_OS_WIN32
|
| 3557 | const gchar *tmpdir;
|
| 3558 | gchar *fname; |
| 3559 | gint fd; |
| 3560 | FILE *fp; |
| 3561 | |
| 3562 | tmpdir = get_tmp_dir(); |
| 3563 | fname = _s_tempnam(tmpdir, "sylph");
|
| 3564 | if (!fname)
|
| 3565 | return NULL; |
| 3566 | |
| 3567 | fd = g_open(fname, O_RDWR | O_CREAT | O_EXCL | |
| 3568 | _O_TEMPORARY | _O_SHORT_LIVED | _O_BINARY, 0600);
|
| 3569 | if (fd < 0) { |
| 3570 | g_free(fname); |
| 3571 | return NULL; |
| 3572 | } |
| 3573 | |
| 3574 | fp = fdopen(fd, "w+b");
|
| 3575 | if (!fp) {
|
| 3576 | perror("fdopen");
|
| 3577 | close(fd); |
| 3578 | } |
| 3579 | |
| 3580 | return fp;
|
| 3581 | #else
|
| 3582 | const gchar suffix[] = ".XXXXXX"; |
| 3583 | const gchar *tmpdir;
|
| 3584 | guint tmplen; |
| 3585 | const gchar *progname;
|
| 3586 | guint proglen; |
| 3587 | gchar *fname; |
| 3588 | gint fd; |
| 3589 | FILE *fp; |
| 3590 | |
| 3591 | tmpdir = get_tmp_dir(); |
| 3592 | tmplen = strlen(tmpdir); |
| 3593 | progname = g_get_prgname(); |
| 3594 | if (!progname)
|
| 3595 | progname = "sylph";
|
| 3596 | proglen = strlen(progname); |
| 3597 | Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix), |
| 3598 | return tmpfile());
|
| 3599 | |
| 3600 | memcpy(fname, tmpdir, tmplen); |
| 3601 | fname[tmplen] = G_DIR_SEPARATOR; |
| 3602 | memcpy(fname + tmplen + 1, progname, proglen);
|
| 3603 | memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix)); |
| 3604 | |
| 3605 | fd = g_mkstemp(fname); |
| 3606 | if (fd < 0) |
| 3607 | return tmpfile();
|
| 3608 | |
| 3609 | g_unlink(fname); |
| 3610 | |
| 3611 | fp = fdopen(fd, "w+b");
|
| 3612 | if (!fp)
|
| 3613 | close(fd); |
| 3614 | |
| 3615 | return fp;
|
| 3616 | #endif
|
| 3617 | } |
| 3618 | |
| 3619 | FILE *str_open_as_stream(const gchar *str)
|
| 3620 | {
|
| 3621 | FILE *fp; |
| 3622 | size_t len; |
| 3623 | |
| 3624 | g_return_val_if_fail(str != NULL, NULL); |
| 3625 | |
| 3626 | fp = my_tmpfile(); |
| 3627 | if (!fp) {
|
| 3628 | FILE_OP_ERROR("str_open_as_stream", "my_tmpfile"); |
| 3629 | return NULL; |
| 3630 | } |
| 3631 | |
| 3632 | len = strlen(str); |
| 3633 | if (len == 0) return fp; |
| 3634 | |
| 3635 | if (fwrite(str, len, 1, fp) != 1) { |
| 3636 | FILE_OP_ERROR("str_open_as_stream", "fwrite"); |
| 3637 | fclose(fp); |
| 3638 | return NULL; |
| 3639 | } |
| 3640 | if (fflush(fp) == EOF) { |
| 3641 | FILE_OP_ERROR("str_open_as_stream", "fflush"); |
| 3642 | fclose(fp); |
| 3643 | return NULL; |
| 3644 | } |
| 3645 | |
| 3646 | rewind(fp); |
| 3647 | return fp;
|
| 3648 | } |
| 3649 | |
| 3650 | gint str_write_to_file(const gchar *str, const gchar *file) |
| 3651 | {
|
| 3652 | FILE *fp; |
| 3653 | size_t len; |
| 3654 | |
| 3655 | g_return_val_if_fail(str != NULL, -1); |
| 3656 | g_return_val_if_fail(file != NULL, -1); |
| 3657 | |
| 3658 | if ((fp = g_fopen(file, "wb")) == NULL) { |
| 3659 | FILE_OP_ERROR(file, "fopen");
|
| 3660 | return -1; |
| 3661 | } |
| 3662 | |
| 3663 | len = strlen(str); |
| 3664 | if (len == 0) { |
| 3665 | fclose(fp); |
| 3666 | return 0; |
| 3667 | } |
| 3668 | |
| 3669 | if (fwrite(str, len, 1, fp) != 1) { |
| 3670 | FILE_OP_ERROR(file, "fwrite");
|
| 3671 | fclose(fp); |
| 3672 | g_unlink(file); |
| 3673 | return -1; |
| 3674 | } |
| 3675 | |
| 3676 | if (fclose(fp) == EOF) { |
| 3677 | FILE_OP_ERROR(file, "fclose");
|
| 3678 | g_unlink(file); |
| 3679 | return -1; |
| 3680 | } |
| 3681 | |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
| 3685 | gchar *file_read_to_str(const gchar *file)
|
| 3686 | {
|
| 3687 | FILE *fp; |
| 3688 | gchar *str; |
| 3689 | |
| 3690 | g_return_val_if_fail(file != NULL, NULL); |
| 3691 | |
| 3692 | if ((fp = g_fopen(file, "rb")) == NULL) { |
| 3693 | FILE_OP_ERROR(file, "fopen");
|
| 3694 | return NULL; |
| 3695 | } |
| 3696 | |
| 3697 | str = file_read_stream_to_str(fp); |
| 3698 | |
| 3699 | fclose(fp); |
| 3700 | |
| 3701 | return str;
|
| 3702 | } |
| 3703 | |
| 3704 | gchar *file_read_stream_to_str(FILE *fp) |
| 3705 | {
|
| 3706 | GByteArray *array; |
| 3707 | guchar buf[BUFSIZ]; |
| 3708 | gint n_read; |
| 3709 | gchar *str; |
| 3710 | |
| 3711 | g_return_val_if_fail(fp != NULL, NULL); |
| 3712 | |
| 3713 | array = g_byte_array_new(); |
| 3714 | |
| 3715 | while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) { |
| 3716 | if (n_read < sizeof(buf) && ferror(fp)) |
| 3717 | break;
|
| 3718 | g_byte_array_append(array, buf, n_read); |
| 3719 | } |
| 3720 | |
| 3721 | if (ferror(fp)) {
|
| 3722 | FILE_OP_ERROR("file stream", "fread"); |
| 3723 | g_byte_array_free(array, TRUE); |
| 3724 | return NULL; |
| 3725 | } |
| 3726 | |
| 3727 | buf[0] = '\0'; |
| 3728 | g_byte_array_append(array, buf, 1);
|
| 3729 | str = (gchar *)array->data; |
| 3730 | g_byte_array_free(array, FALSE); |
| 3731 | |
| 3732 | return str;
|
| 3733 | } |
| 3734 | |
| 3735 | #if defined(G_OS_WIN32) && !GLIB_CHECK_VERSION(2, 8, 2) |
| 3736 | static gchar **argv_utf8_to_locale(gchar **argv)
|
| 3737 | {
|
| 3738 | gint argc = 0, i;
|
| 3739 | gchar **cp_argv; |
| 3740 | |
| 3741 | while (argv[argc] != NULL) |
| 3742 | argc++; |
| 3743 | |
| 3744 | cp_argv = g_new(gchar *, argc + 1);
|
| 3745 | |
| 3746 | for (i = 0; i < argc; i++) { |
| 3747 | cp_argv[i] = g_locale_from_utf8(argv[i], -1, NULL, NULL, NULL); |
| 3748 | if (cp_argv[i] == NULL) { |
| 3749 | g_warning("Failed to convert from UTF-8 to locale encoding: %s\n", argv[i]);
|
| 3750 | g_strfreev(cp_argv); |
| 3751 | return NULL; |
| 3752 | } |
| 3753 | } |
| 3754 | cp_argv[i] = NULL;
|
| 3755 | |
| 3756 | return cp_argv;
|
| 3757 | } |
| 3758 | #endif
|
| 3759 | |
| 3760 | gint execute_async(gchar *const argv[])
|
| 3761 | {
|
| 3762 | #if defined(G_OS_WIN32) && !GLIB_CHECK_VERSION(2, 8, 2) |
| 3763 | gchar **cp_argv; |
| 3764 | |
| 3765 | g_return_val_if_fail(argv != NULL && argv[0] != NULL, -1); |
| 3766 | |
| 3767 | cp_argv = argv_utf8_to_locale((gchar **)argv); |
| 3768 | if (!cp_argv)
|
| 3769 | return -1; |
| 3770 | if (g_spawn_async(NULL, cp_argv, NULL, G_SPAWN_SEARCH_PATH, |
| 3771 | NULL, NULL, NULL, NULL) == FALSE) { |
| 3772 | g_warning("Can't execute command: %s\n", argv[0]); |
| 3773 | g_strfreev(cp_argv); |
| 3774 | return -1; |
| 3775 | } |
| 3776 | g_strfreev(cp_argv); |
| 3777 | #else
|
| 3778 | g_return_val_if_fail(argv != NULL && argv[0] != NULL, -1); |
| 3779 | |
| 3780 | if (g_spawn_async(NULL, (gchar **)argv, NULL, G_SPAWN_SEARCH_PATH, |
| 3781 | NULL, NULL, NULL, NULL) == FALSE) { |
| 3782 | g_warning("Can't execute command: %s\n", argv[0]); |
| 3783 | return -1; |
| 3784 | } |
| 3785 | #endif
|
| 3786 | |
| 3787 | return 0; |
| 3788 | } |
| 3789 | |
| 3790 | gint execute_sync(gchar *const argv[])
|
| 3791 | {
|
| 3792 | gint status; |
| 3793 | #if defined(G_OS_WIN32) && !GLIB_CHECK_VERSION(2, 8, 2) |
| 3794 | gchar **cp_argv; |
| 3795 | #endif
|
| 3796 | |
| 3797 | g_return_val_if_fail(argv != NULL && argv[0] != NULL, -1); |
| 3798 | |
| 3799 | #ifdef G_OS_WIN32
|
| 3800 | #if !GLIB_CHECK_VERSION(2, 8, 2) |
| 3801 | cp_argv = argv_utf8_to_locale((gchar **)argv); |
| 3802 | if (!cp_argv)
|
| 3803 | return -1; |
| 3804 | if (g_spawn_sync(NULL, cp_argv, NULL, |
| 3805 | G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN | |
| 3806 | G_SPAWN_LEAVE_DESCRIPTORS_OPEN, |
| 3807 | NULL, NULL, NULL, NULL, &status, NULL) == FALSE) { |
| 3808 | g_warning("Can't execute command: %s\n", argv[0]); |
| 3809 | g_strfreev(cp_argv); |
| 3810 | return -1; |
| 3811 | } |
| 3812 | g_strfreev(cp_argv); |
| 3813 | #else /* !GLIB_CHECK_VERSION */ |
| 3814 | if (g_spawn_sync(NULL, (gchar **)argv, NULL, |
| 3815 | G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN | |
| 3816 | G_SPAWN_LEAVE_DESCRIPTORS_OPEN, |
| 3817 | NULL, NULL, NULL, NULL, &status, NULL) == FALSE) { |
| 3818 | g_warning("Can't execute command: %s\n", argv[0]); |
| 3819 | return -1; |
| 3820 | } |
| 3821 | #endif /* !GLIB_CHECK_VERSION */ |
| 3822 | |
| 3823 | return status;
|
| 3824 | #else /* G_OS_WIN32 */ |
| 3825 | if (g_spawn_sync(NULL, (gchar **)argv, NULL, G_SPAWN_SEARCH_PATH, |
| 3826 | NULL, NULL, NULL, NULL, &status, NULL) == FALSE) { |
| 3827 | g_warning("Can't execute command: %s\n", argv[0]); |
| 3828 | return -1; |
| 3829 | } |
| 3830 | |
| 3831 | if (WIFEXITED(status))
|
| 3832 | return WEXITSTATUS(status);
|
| 3833 | else
|
| 3834 | return -1; |
| 3835 | #endif /* G_OS_WIN32 */ |
| 3836 | } |
| 3837 | |
| 3838 | gint execute_command_line(const gchar *cmdline, gboolean async)
|
| 3839 | {
|
| 3840 | gchar **argv; |
| 3841 | gint ret; |
| 3842 | |
| 3843 | if (debug_mode) {
|
| 3844 | gchar *utf8_cmdline; |
| 3845 | |
| 3846 | utf8_cmdline = g_filename_to_utf8 |
| 3847 | (cmdline, -1, NULL, NULL, NULL); |
| 3848 | debug_print("execute_command_line(): executing: %s\n",
|
| 3849 | utf8_cmdline ? utf8_cmdline : cmdline); |
| 3850 | g_free(utf8_cmdline); |
| 3851 | } |
| 3852 | |
| 3853 | argv = strsplit_with_quote(cmdline, " ", 0); |
| 3854 | |
| 3855 | if (async)
|
| 3856 | ret = execute_async(argv); |
| 3857 | else
|
| 3858 | ret = execute_sync(argv); |
| 3859 | |
| 3860 | g_strfreev(argv); |
| 3861 | |
| 3862 | return ret;
|
| 3863 | } |
| 3864 | |
| 3865 | #if USE_THREADS
|
| 3866 | typedef struct _CmdData |
| 3867 | {
|
| 3868 | const gchar *cmdline;
|
| 3869 | volatile gint flag;
|
| 3870 | gint status; |
| 3871 | } CmdData; |
| 3872 | |
| 3873 | static gpointer execute_command_line_async_func(gpointer data)
|
| 3874 | {
|
| 3875 | CmdData *cmd_data = (CmdData *)data; |
| 3876 | gchar **argv; |
| 3877 | |
| 3878 | argv = strsplit_with_quote(cmd_data->cmdline, " ", 0); |
| 3879 | cmd_data->status = execute_sync(argv); |
| 3880 | g_strfreev(argv); |
| 3881 | |
| 3882 | debug_print("execute_command_line_async_func: exec done: %s\n",
|
| 3883 | cmd_data->cmdline); |
| 3884 | cmd_data->flag = 1;
|
| 3885 | g_main_context_wakeup(NULL);
|
| 3886 | |
| 3887 | return GINT_TO_POINTER(0); |
| 3888 | } |
| 3889 | |
| 3890 | gint execute_command_line_async_wait(const gchar *cmdline)
|
| 3891 | {
|
| 3892 | CmdData data = {NULL, 0, 0};
|
| 3893 | GThread *thread; |
| 3894 | |
| 3895 | if (debug_mode) {
|
| 3896 | gchar *utf8_cmdline; |
| 3897 | |
| 3898 | utf8_cmdline = g_filename_to_utf8 |
| 3899 | (cmdline, -1, NULL, NULL, NULL); |
| 3900 | debug_print("execute_command_line(): executing: %s\n",
|
| 3901 | utf8_cmdline ? utf8_cmdline : cmdline); |
| 3902 | g_free(utf8_cmdline); |
| 3903 | } |
| 3904 | |
| 3905 | data.cmdline = cmdline; |
| 3906 | thread = g_thread_create(execute_command_line_async_func, &data, TRUE, |
| 3907 | NULL);
|
| 3908 | if (!thread)
|
| 3909 | return -1; |
| 3910 | |
| 3911 | debug_print("execute_command_line_async_wait: waiting thread\n");
|
| 3912 | while (data.flag == 0) |
| 3913 | event_loop_iterate(); |
| 3914 | |
| 3915 | debug_print("execute_command_line_async_wait: flagged\n");
|
| 3916 | g_thread_join(thread); |
| 3917 | debug_print("execute_command_line_async_wait: thread exited\n");
|
| 3918 | |
| 3919 | return data.status;
|
| 3920 | } |
| 3921 | #else /* USE_THREADS */ |
| 3922 | gint execute_command_line_async_wait(const gchar *cmdline)
|
| 3923 | {
|
| 3924 | return execute_command_line(cmdline, FALSE);
|
| 3925 | } |
| 3926 | #endif /* USE_THREADS */ |
| 3927 | |
| 3928 | gint execute_open_file(const gchar *file, const gchar *content_type) |
| 3929 | {
|
| 3930 | g_return_val_if_fail(file != NULL, -1); |
| 3931 | |
| 3932 | #ifdef G_OS_WIN32
|
| 3933 | log_print("opening %s - %s\n", file, content_type ? content_type : ""); |
| 3934 | |
| 3935 | if (G_WIN32_HAVE_WIDECHAR_API()) {
|
| 3936 | wchar_t *wpath; |
| 3937 | |
| 3938 | wpath = g_utf8_to_utf16(file, -1, NULL, NULL, NULL); |
| 3939 | if (wpath == NULL) |
| 3940 | return -1; |
| 3941 | |
| 3942 | ShellExecuteW(NULL, L"open", wpath, NULL, NULL, SW_SHOWNORMAL); |
| 3943 | |
| 3944 | g_free(wpath); |
| 3945 | |
| 3946 | return 0; |
| 3947 | } else {
|
| 3948 | gchar *cp_path; |
| 3949 | |
| 3950 | cp_path = g_locale_from_utf8(file, -1, NULL, NULL, NULL); |
| 3951 | if (cp_path == NULL) |
| 3952 | return -1; |
| 3953 | |
| 3954 | ShellExecuteA(NULL, "open", cp_path, NULL, NULL, SW_SHOWNORMAL); |
| 3955 | |
| 3956 | g_free(cp_path); |
| 3957 | |
| 3958 | return 0; |
| 3959 | } |
| 3960 | #endif
|
| 3961 | return 0; |
| 3962 | } |
| 3963 | |
| 3964 | gint execute_print_file(const gchar *file)
|
| 3965 | {
|
| 3966 | g_return_val_if_fail(file != NULL, -1); |
| 3967 | |
| 3968 | #ifdef G_OS_WIN32
|
| 3969 | log_print("printing %s\n", file);
|
| 3970 | |
| 3971 | if (G_WIN32_HAVE_WIDECHAR_API()) {
|
| 3972 | wchar_t *wpath; |
| 3973 | |
| 3974 | wpath = g_utf8_to_utf16(file, -1, NULL, NULL, NULL); |
| 3975 | if (wpath == NULL) |
| 3976 | return -1; |
| 3977 | |
| 3978 | ShellExecuteW(NULL, L"print", wpath, NULL, NULL, SW_SHOWNORMAL); |
| 3979 | |
| 3980 | g_free(wpath); |
| 3981 | |
| 3982 | return 0; |
| 3983 | } else {
|
| 3984 | gchar *cp_path; |
| 3985 | |
| 3986 | cp_path = g_locale_from_utf8(file, -1, NULL, NULL, NULL); |
| 3987 | if (cp_path == NULL) |
| 3988 | return -1; |
| 3989 | |
| 3990 | ShellExecuteA(NULL, "print", cp_path, NULL, NULL, |
| 3991 | SW_SHOWNORMAL); |
| 3992 | |
| 3993 | g_free(cp_path); |
| 3994 | |
| 3995 | return 0; |
| 3996 | } |
| 3997 | #endif
|
| 3998 | return 0; |
| 3999 | } |
| 4000 | |
| 4001 | gchar *get_command_output(const gchar *cmdline)
|
| 4002 | {
|
| 4003 | gchar *child_stdout; |
| 4004 | gint status; |
| 4005 | |
| 4006 | g_return_val_if_fail(cmdline != NULL, NULL); |
| 4007 | |
| 4008 | debug_print("get_command_output(): executing: %s\n", cmdline);
|
| 4009 | |
| 4010 | if (g_spawn_command_line_sync(cmdline, &child_stdout, NULL, &status, |
| 4011 | NULL) == FALSE) {
|
| 4012 | g_warning("Can't execute command: %s\n", cmdline);
|
| 4013 | return NULL; |
| 4014 | } |
| 4015 | |
| 4016 | return child_stdout;
|
| 4017 | } |
| 4018 | |
| 4019 | gint open_uri(const gchar *uri, const gchar *cmdline) |
| 4020 | {
|
| 4021 | gchar buf[BUFFSIZE]; |
| 4022 | |
| 4023 | g_return_val_if_fail(uri != NULL, -1); |
| 4024 | |
| 4025 | #ifdef G_OS_WIN32
|
| 4026 | if (!cmdline || cmdline[0] == '\0') |
| 4027 | return execute_open_file(uri, NULL); |
| 4028 | #endif
|
| 4029 | |
| 4030 | if (cmdline && str_find_format_times(cmdline, 's') == 1) |
| 4031 | g_snprintf(buf, sizeof(buf), cmdline, uri);
|
| 4032 | else {
|
| 4033 | if (cmdline)
|
| 4034 | g_warning("Open URI command line is invalid "
|
| 4035 | "(there must be only one '%%s'): %s",
|
| 4036 | cmdline); |
| 4037 | g_snprintf(buf, sizeof(buf), DEFAULT_BROWSER_CMD, uri);
|
| 4038 | } |
| 4039 | |
| 4040 | execute_command_line(buf, TRUE); |
| 4041 | |
| 4042 | return 0; |
| 4043 | } |
| 4044 | |
| 4045 | time_t remote_tzoffset_sec(const gchar *zone)
|
| 4046 | {
|
| 4047 | static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT"; |
| 4048 | gchar zone3[4];
|
| 4049 | gchar *p; |
| 4050 | gchar c; |
| 4051 | gint iustz; |
| 4052 | gint offset; |
| 4053 | time_t remoteoffset; |
| 4054 | |
| 4055 | strncpy(zone3, zone, 3);
|
| 4056 | zone3[3] = '\0'; |
| 4057 | remoteoffset = 0;
|
| 4058 | |
| 4059 | if (sscanf(zone, "%c%d", &c, &offset) == 2 && |
| 4060 | (c == '+' || c == '-')) { |
| 4061 | remoteoffset = ((offset / 100) * 60 + (offset % 100)) * 60; |
| 4062 | if (c == '-') |
| 4063 | remoteoffset = -remoteoffset; |
| 4064 | } else if (!strncmp(zone, "UT" , 2) || |
| 4065 | !strncmp(zone, "GMT", 2)) { |
| 4066 | remoteoffset = 0;
|
| 4067 | } else if (strlen(zone3) == 3) { |
| 4068 | for (p = ustzstr; *p != '\0'; p += 3) { |
| 4069 | if (!g_ascii_strncasecmp(p, zone3, 3)) { |
| 4070 | iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8; |
| 4071 | remoteoffset = iustz * 3600;
|
| 4072 | break;
|
| 4073 | } |
| 4074 | } |
| 4075 | if (*p == '\0') |
| 4076 | return -1; |
| 4077 | } else if (strlen(zone3) == 1) { |
| 4078 | switch (zone[0]) { |
| 4079 | case 'Z': remoteoffset = 0; break; |
| 4080 | case 'A': remoteoffset = -1; break; |
| 4081 | case 'B': remoteoffset = -2; break; |
| 4082 | case 'C': remoteoffset = -3; break; |
| 4083 | case 'D': remoteoffset = -4; break; |
| 4084 | case 'E': remoteoffset = -5; break; |
| 4085 | case 'F': remoteoffset = -6; break; |
| 4086 | case 'G': remoteoffset = -7; break; |
| 4087 | case 'H': remoteoffset = -8; break; |
| 4088 | case 'I': remoteoffset = -9; break; |
| 4089 | case 'K': remoteoffset = -10; break; /* J is not used */ |
| 4090 | case 'L': remoteoffset = -11; break; |
| 4091 | case 'M': remoteoffset = -12; break; |
| 4092 | case 'N': remoteoffset = 1; break; |
| 4093 | case 'O': remoteoffset = 2; break; |
| 4094 | case 'P': remoteoffset = 3; break; |
| 4095 | case 'Q': remoteoffset = 4; break; |
| 4096 | case 'R': remoteoffset = 5; break; |
| 4097 | case 'S': remoteoffset = 6; break; |
| 4098 | case 'T': remoteoffset = 7; break; |
| 4099 | case 'U': remoteoffset = 8; break; |
| 4100 | case 'V': remoteoffset = 9; break; |
| 4101 | case 'W': remoteoffset = 10; break; |
| 4102 | case 'X': remoteoffset = 11; break; |
| 4103 | case 'Y': remoteoffset = 12; break; |
| 4104 | default: remoteoffset = 0; break; |
| 4105 | } |
| 4106 | remoteoffset = remoteoffset * 3600;
|
| 4107 | } else
|
| 4108 | return -1; |
| 4109 | |
| 4110 | return remoteoffset;
|
| 4111 | } |
| 4112 | |
| 4113 | time_t tzoffset_sec(time_t *now) |
| 4114 | {
|
| 4115 | struct tm gmt, *tmp, *lt;
|
| 4116 | gint off; |
| 4117 | |
| 4118 | tmp = gmtime(now); |
| 4119 | g_return_val_if_fail(tmp != NULL, -1); |
| 4120 | gmt = *tmp; |
| 4121 | lt = localtime(now); |
| 4122 | g_return_val_if_fail(lt != NULL, -1); |
| 4123 | |
| 4124 | off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
|
| 4125 | |
| 4126 | if (lt->tm_year < gmt.tm_year)
|
| 4127 | off -= 24 * 60; |
| 4128 | else if (lt->tm_year > gmt.tm_year) |
| 4129 | off += 24 * 60; |
| 4130 | else if (lt->tm_yday < gmt.tm_yday) |
| 4131 | off -= 24 * 60; |
| 4132 | else if (lt->tm_yday > gmt.tm_yday) |
| 4133 | off += 24 * 60; |
| 4134 | |
| 4135 | if (off >= 24 * 60) /* should be impossible */ |
| 4136 | off = 23 * 60 + 59; /* if not, insert silly value */ |
| 4137 | if (off <= -24 * 60) |
| 4138 | off = -(23 * 60 + 59); |
| 4139 | |
| 4140 | return off * 60; |
| 4141 | } |
| 4142 | |
| 4143 | /* calculate timezone offset */
|
| 4144 | gchar *tzoffset(time_t *now) |
| 4145 | {
|
| 4146 | static gchar offset_string[6]; |
| 4147 | struct tm gmt, *tmp, *lt;
|
| 4148 | gint off; |
| 4149 | gchar sign = '+';
|
| 4150 | |
| 4151 | tmp = gmtime(now); |
| 4152 | g_return_val_if_fail(tmp != NULL, NULL); |
| 4153 | gmt = *tmp; |
| 4154 | lt = localtime(now); |
| 4155 | g_return_val_if_fail(lt != NULL, NULL); |
| 4156 | |
| 4157 | off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
|
| 4158 | |
| 4159 | if (lt->tm_year < gmt.tm_year)
|
| 4160 | off -= 24 * 60; |
| 4161 | else if (lt->tm_year > gmt.tm_year) |
| 4162 | off += 24 * 60; |
| 4163 | else if (lt->tm_yday < gmt.tm_yday) |
| 4164 | off -= 24 * 60; |
| 4165 | else if (lt->tm_yday > gmt.tm_yday) |
| 4166 | off += 24 * 60; |
| 4167 | |
| 4168 | if (off < 0) { |
| 4169 | sign = '-';
|
| 4170 | off = -off; |
| 4171 | } |
| 4172 | |
| 4173 | if (off >= 24 * 60) /* should be impossible */ |
| 4174 | off = 23 * 60 + 59; /* if not, insert silly value */ |
| 4175 | |
| 4176 | sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60); |
| 4177 | |
| 4178 | return offset_string;
|
| 4179 | } |
| 4180 | |
| 4181 | void get_rfc822_date(gchar *buf, gint len)
|
| 4182 | {
|
| 4183 | struct tm *lt;
|
| 4184 | time_t t; |
| 4185 | gchar day[4], mon[4]; |
| 4186 | gint dd, hh, mm, ss, yyyy; |
| 4187 | |
| 4188 | t = time(NULL);
|
| 4189 | lt = localtime(&t); |
| 4190 | |
| 4191 | sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
|
| 4192 | day, mon, &dd, &hh, &mm, &ss, &yyyy); |
| 4193 | g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
|
| 4194 | day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t)); |
| 4195 | } |
| 4196 | |
| 4197 | /* just a wrapper to suppress the warning of gcc about %c */
|
| 4198 | size_t my_strftime(gchar *s, size_t max, const gchar *format,
|
| 4199 | const struct tm *tm) |
| 4200 | {
|
| 4201 | return strftime(s, max, format, tm);
|
| 4202 | } |
| 4203 | |
| 4204 | /* UI hints */
|
| 4205 | |
| 4206 | static UIUpdateFunc ui_update_func = NULL; |
| 4207 | |
| 4208 | void set_ui_update_func(UIUpdateFunc func)
|
| 4209 | {
|
| 4210 | ui_update_func = func; |
| 4211 | } |
| 4212 | |
| 4213 | void ui_update(void) |
| 4214 | {
|
| 4215 | if (ui_update_func)
|
| 4216 | ui_update_func(); |
| 4217 | } |
| 4218 | |
| 4219 | static EventLoopFunc event_loop_func = NULL; |
| 4220 | |
| 4221 | void set_event_loop_func(EventLoopFunc func)
|
| 4222 | {
|
| 4223 | event_loop_func = func; |
| 4224 | } |
| 4225 | |
| 4226 | void event_loop_iterate(void) |
| 4227 | {
|
| 4228 | if (event_loop_func)
|
| 4229 | event_loop_func(); |
| 4230 | else
|
| 4231 | g_main_context_iteration(NULL, TRUE);
|
| 4232 | } |
| 4233 | |
| 4234 | static ProgressFunc progress_func = NULL; |
| 4235 | |
| 4236 | void set_progress_func(ProgressFunc func)
|
| 4237 | {
|
| 4238 | progress_func = func; |
| 4239 | } |
| 4240 | |
| 4241 | void progress_show(gint cur, gint total)
|
| 4242 | {
|
| 4243 | if (progress_func)
|
| 4244 | progress_func(cur, total); |
| 4245 | } |
| 4246 | |
| 4247 | /* user input */
|
| 4248 | |
| 4249 | static QueryPasswordFunc query_password_func = NULL; |
| 4250 | |
| 4251 | void set_input_query_password_func(QueryPasswordFunc func)
|
| 4252 | {
|
| 4253 | query_password_func = func; |
| 4254 | } |
| 4255 | |
| 4256 | gchar *input_query_password(const gchar *server, const gchar *user) |
| 4257 | {
|
| 4258 | if (query_password_func)
|
| 4259 | return query_password_func(server, user);
|
| 4260 | else
|
| 4261 | return NULL; |
| 4262 | } |
| 4263 | |
| 4264 | /* logging */
|
| 4265 | |
| 4266 | static FILE *log_fp = NULL; |
| 4267 | |
| 4268 | void set_log_file(const gchar *filename) |
| 4269 | {
|
| 4270 | if (log_fp) return; |
| 4271 | log_fp = g_fopen(filename, "w");
|
| 4272 | if (!log_fp)
|
| 4273 | FILE_OP_ERROR(filename, "fopen");
|
| 4274 | } |
| 4275 | |
| 4276 | void close_log_file(void) |
| 4277 | {
|
| 4278 | if (log_fp) {
|
| 4279 | fclose(log_fp); |
| 4280 | log_fp = NULL;
|
| 4281 | } |
| 4282 | } |
| 4283 | |
| 4284 | static guint log_verbosity_count = 0; |
| 4285 | |
| 4286 | void set_log_verbosity(gboolean verbose)
|
| 4287 | {
|
| 4288 | if (verbose)
|
| 4289 | log_verbosity_count++; |
| 4290 | else if (log_verbosity_count > 0) |
| 4291 | log_verbosity_count--; |
| 4292 | } |
| 4293 | |
| 4294 | gboolean get_debug_mode(void)
|
| 4295 | {
|
| 4296 | return debug_mode;
|
| 4297 | } |
| 4298 | |
| 4299 | void set_debug_mode(gboolean enable)
|
| 4300 | {
|
| 4301 | debug_mode = enable; |
| 4302 | } |
| 4303 | |
| 4304 | static void log_dummy_func(const gchar *str) |
| 4305 | {
|
| 4306 | } |
| 4307 | |
| 4308 | static LogFunc log_print_ui_func = log_dummy_func;
|
| 4309 | static LogFunc log_message_ui_func = log_dummy_func;
|
| 4310 | static LogFunc log_warning_ui_func = log_dummy_func;
|
| 4311 | static LogFunc log_error_ui_func = log_dummy_func;
|
| 4312 | |
| 4313 | static LogFunc log_show_status_func = log_dummy_func;
|
| 4314 | |
| 4315 | void set_log_ui_func(LogFunc print_func, LogFunc message_func,
|
| 4316 | LogFunc warning_func, LogFunc error_func) |
| 4317 | {
|
| 4318 | log_print_ui_func = print_func; |
| 4319 | log_message_ui_func = message_func; |
| 4320 | log_warning_ui_func = warning_func; |
| 4321 | log_error_ui_func = error_func; |
| 4322 | } |
| 4323 | |
| 4324 | void set_log_show_status_func(LogFunc status_func)
|
| 4325 | {
|
| 4326 | log_show_status_func = status_func; |
| 4327 | } |
| 4328 | |
| 4329 | void debug_print(const gchar *format, ...) |
| 4330 | {
|
| 4331 | va_list args; |
| 4332 | gchar buf[BUFFSIZE]; |
| 4333 | |
| 4334 | if (!debug_mode) return; |
| 4335 | |
| 4336 | va_start(args, format); |
| 4337 | g_vsnprintf(buf, sizeof(buf), format, args);
|
| 4338 | va_end(args); |
| 4339 | |
| 4340 | g_print("%s", buf);
|
| 4341 | } |
| 4342 | |
| 4343 | void status_print(const gchar *format, ...) |
| 4344 | {
|
| 4345 | va_list args; |
| 4346 | gchar buf[BUFFSIZE]; |
| 4347 | |
| 4348 | va_start(args, format); |
| 4349 | g_vsnprintf(buf, sizeof(buf), format, args);
|
| 4350 | va_end(args); |
| 4351 | |
| 4352 | log_show_status_func(buf); |
| 4353 | } |
| 4354 | |
| 4355 | #define TIME_LEN 11 |
| 4356 | |
| 4357 | void log_write(const gchar *str, const gchar *prefix) |
| 4358 | {
|
| 4359 | if (log_fp) {
|
| 4360 | gchar buf[TIME_LEN + 1];
|
| 4361 | time_t t; |
| 4362 | |
| 4363 | time(&t); |
| 4364 | strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t)); |
| 4365 | |
| 4366 | fputs(buf, log_fp); |
| 4367 | if (prefix)
|
| 4368 | fputs(prefix, log_fp); |
| 4369 | fputs(str, log_fp); |
| 4370 | fflush(log_fp); |
| 4371 | } |
| 4372 | } |
| 4373 | |
| 4374 | void log_print(const gchar *format, ...) |
| 4375 | {
|
| 4376 | va_list args; |
| 4377 | gchar buf[BUFFSIZE + TIME_LEN]; |
| 4378 | time_t t; |
| 4379 | |
| 4380 | time(&t); |
| 4381 | strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t)); |
| 4382 | |
| 4383 | va_start(args, format); |
| 4384 | g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args); |
| 4385 | va_end(args); |
| 4386 | |
| 4387 | if (debug_mode) g_print("%s", buf); |
| 4388 | log_print_ui_func(buf); |
| 4389 | if (log_fp) {
|
| 4390 | fputs(buf, log_fp); |
| 4391 | fflush(log_fp); |
| 4392 | } |
| 4393 | if (log_verbosity_count)
|
| 4394 | log_show_status_func(buf + TIME_LEN); |
| 4395 | } |
| 4396 | |
| 4397 | void log_message(const gchar *format, ...) |
| 4398 | {
|
| 4399 | va_list args; |
| 4400 | gchar buf[BUFFSIZE + TIME_LEN]; |
| 4401 | time_t t; |
| 4402 | |
| 4403 | time(&t); |
| 4404 | strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t)); |
| 4405 | |
| 4406 | va_start(args, format); |
| 4407 | g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args); |
| 4408 | va_end(args); |
| 4409 | |
| 4410 | if (debug_mode) g_message("%s", buf + TIME_LEN); |
| 4411 | log_message_ui_func(buf + TIME_LEN); |
| 4412 | if (log_fp) {
|
| 4413 | fwrite(buf, TIME_LEN, 1, log_fp);
|
| 4414 | fputs("* message: ", log_fp);
|
| 4415 | fputs(buf + TIME_LEN, log_fp); |
| 4416 | fflush(log_fp); |
| 4417 | } |
| 4418 | log_show_status_func(buf + TIME_LEN); |
| 4419 | } |
| 4420 | |
| 4421 | void log_warning(const gchar *format, ...) |
| 4422 | {
|
| 4423 | va_list args; |
| 4424 | gchar buf[BUFFSIZE + TIME_LEN]; |
| 4425 | time_t t; |
| 4426 | |
| 4427 | time(&t); |
| 4428 | strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t)); |
| 4429 | |
| 4430 | va_start(args, format); |
| 4431 | g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args); |
| 4432 | va_end(args); |
| 4433 | |
| 4434 | g_warning("%s", buf);
|
| 4435 | log_warning_ui_func(buf + TIME_LEN); |
| 4436 | if (log_fp) {
|
| 4437 | fwrite(buf, TIME_LEN, 1, log_fp);
|
| 4438 | fputs("** warning: ", log_fp);
|
| 4439 | fputs(buf + TIME_LEN, log_fp); |
| 4440 | fflush(log_fp); |
| 4441 | } |
| 4442 | } |
| 4443 | |
| 4444 | void log_error(const gchar *format, ...) |
| 4445 | {
|
| 4446 | va_list args; |
| 4447 | gchar buf[BUFFSIZE + TIME_LEN]; |
| 4448 | time_t t; |
| 4449 | |
| 4450 | time(&t); |
| 4451 | strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t)); |
| 4452 | |
| 4453 | va_start(args, format); |
| 4454 | g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args); |
| 4455 | va_end(args); |
| 4456 | |
| 4457 | g_warning("%s", buf);
|
| 4458 | log_error_ui_func(buf + TIME_LEN); |
| 4459 | if (log_fp) {
|
| 4460 | fwrite(buf, TIME_LEN, 1, log_fp);
|
| 4461 | fputs("*** error: ", log_fp);
|
| 4462 | fputs(buf + TIME_LEN, log_fp); |
| 4463 | fflush(log_fp); |
| 4464 | } |
| 4465 | } |