root / libsylph / utils.c @ 546
History | View | Annotate | Download (64.2 kB)
| 1 | /*
|
|---|---|
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | * Copyright (C) 1999-2005 Hiroyuki Yamamoto |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 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 | |
| 33 | #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
|
| 34 | # include <wchar.h> |
| 35 | # include <wctype.h> |
| 36 | #endif
|
| 37 | #include <stdlib.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <unistd.h> |
| 40 | #include <stdarg.h> |
| 41 | #include <sys/types.h> |
| 42 | #if HAVE_SYS_WAIT_H
|
| 43 | # include <sys/wait.h> |
| 44 | #endif
|
| 45 | #include <dirent.h> |
| 46 | #include <time.h> |
| 47 | |
| 48 | #ifdef G_OS_WIN32
|
| 49 | # include <direct.h> |
| 50 | # include <io.h> |
| 51 | #endif
|
| 52 | |
| 53 | #include "utils.h" |
| 54 | #include "socket.h" |
| 55 | |
| 56 | #define BUFFSIZE 8192 |
| 57 | |
| 58 | static gboolean debug_mode = FALSE;
|
| 59 | |
| 60 | |
| 61 | #if !GLIB_CHECK_VERSION(2, 7, 0) && !defined(G_OS_UNIX) |
| 62 | gint g_chdir(const gchar *path)
|
| 63 | {
|
| 64 | #ifdef G_OS_WIN32
|
| 65 | if (G_WIN32_HAVE_WIDECHAR_API()) {
|
| 66 | wchar_t *wpath; |
| 67 | gint retval; |
| 68 | gint save_errno; |
| 69 | |
| 70 | wpath = g_utf8_to_utf16(path, -1, NULL, NULL, NULL); |
| 71 | if (wpath == NULL) { |
| 72 | errno = EINVAL; |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | retval = _wchdir(wpath); |
| 77 | save_errno = errno; |
| 78 | |
| 79 | g_free(wpath); |
| 80 | |
| 81 | errno = save_errno; |
| 82 | return retval;
|
| 83 | } else {
|
| 84 | gchar *cp_path; |
| 85 | gint retval; |
| 86 | gint save_errno; |
| 87 | |
| 88 | cp_path = g_locale_from_utf8(path, -1, NULL, NULL, NULL); |
| 89 | if (cp_path == NULL) { |
| 90 | errno = EINVAL; |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | retval = chdir(cp_path); |
| 95 | save_errno = errno; |
| 96 | |
| 97 | g_free(cp_path); |
| 98 | |
| 99 | errno = save_errno; |
| 100 | return retval;
|
| 101 | } |
| 102 | #else
|
| 103 | return chdir(path);
|
| 104 | #endif
|
| 105 | } |
| 106 | |
| 107 | gint g_chmod(const gchar *path, gint mode)
|
| 108 | {
|
| 109 | #ifdef G_OS_WIN32
|
| 110 | if (G_WIN32_HAVE_WIDECHAR_API()) {
|
| 111 | wchar_t *wpath; |
| 112 | gint retval; |
| 113 | gint save_errno; |
| 114 | |
| 115 | wpath = g_utf8_to_utf16(path, -1, NULL, NULL, NULL); |
| 116 | if (wpath == NULL) { |
| 117 | errno = EINVAL; |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | retval = _wchmod(wpath, mode); |
| 122 | save_errno = errno; |
| 123 | |
| 124 | g_free(wpath); |
| 125 | |
| 126 | errno = save_errno; |
| 127 | return retval;
|
| 128 | } else {
|
| 129 | gchar *cp_path; |
| 130 | gint retval; |
| 131 | gint save_errno; |
| 132 | |
| 133 | cp_path = g_locale_from_utf8(path, -1, NULL, NULL, NULL); |
| 134 | if (cp_path == NULL) { |
| 135 | errno = EINVAL; |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | retval = chmod(cp_path, mode); |
| 140 | save_errno = errno; |
| 141 | |
| 142 | g_free(cp_path); |
| 143 | |
| 144 | errno = save_errno; |
| 145 | return retval;
|
| 146 | } |
| 147 | #else
|
| 148 | return chmod(path, mode);
|
| 149 | #endif
|
| 150 | } |
| 151 | #endif /* GLIB_CHECK_VERSION && G_OS_UNIX */ |
| 152 | |
| 153 | void list_free_strings(GList *list)
|
| 154 | {
|
| 155 | list = g_list_first(list); |
| 156 | |
| 157 | while (list != NULL) { |
| 158 | g_free(list->data); |
| 159 | list = list->next; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void slist_free_strings(GSList *list)
|
| 164 | {
|
| 165 | while (list != NULL) { |
| 166 | g_free(list->data); |
| 167 | list = list->next; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static void hash_free_strings_func(gpointer key, gpointer value, gpointer data) |
| 172 | {
|
| 173 | g_free(key); |
| 174 | } |
| 175 | |
| 176 | void hash_free_strings(GHashTable *table)
|
| 177 | {
|
| 178 | g_hash_table_foreach(table, hash_free_strings_func, NULL);
|
| 179 | } |
| 180 | |
| 181 | static void hash_free_value_mem_func(gpointer key, gpointer value, |
| 182 | gpointer data) |
| 183 | {
|
| 184 | g_free(value); |
| 185 | } |
| 186 | |
| 187 | void hash_free_value_mem(GHashTable *table)
|
| 188 | {
|
| 189 | g_hash_table_foreach(table, hash_free_value_mem_func, NULL);
|
| 190 | } |
| 191 | |
| 192 | gint str_case_equal(gconstpointer v, gconstpointer v2) |
| 193 | {
|
| 194 | return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0; |
| 195 | } |
| 196 | |
| 197 | guint str_case_hash(gconstpointer key) |
| 198 | {
|
| 199 | const gchar *p = key;
|
| 200 | guint h = *p; |
| 201 | |
| 202 | if (h) {
|
| 203 | h = g_ascii_tolower(h); |
| 204 | for (p += 1; *p != '\0'; p++) |
| 205 | h = (h << 5) - h + g_ascii_tolower(*p);
|
| 206 | } |
| 207 | |
| 208 | return h;
|
| 209 | } |
| 210 | |
| 211 | void ptr_array_free_strings(GPtrArray *array)
|
| 212 | {
|
| 213 | gint i; |
| 214 | gchar *str; |
| 215 | |
| 216 | g_return_if_fail(array != NULL);
|
| 217 | |
| 218 | for (i = 0; i < array->len; i++) { |
| 219 | str = g_ptr_array_index(array, i); |
| 220 | g_free(str); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | gboolean str_find(const gchar *haystack, const gchar *needle) |
| 225 | {
|
| 226 | return strstr(haystack, needle) != NULL ? TRUE : FALSE; |
| 227 | } |
| 228 | |
| 229 | gboolean str_case_find(const gchar *haystack, const gchar *needle) |
| 230 | {
|
| 231 | return strcasestr(haystack, needle) != NULL ? TRUE : FALSE; |
| 232 | } |
| 233 | |
| 234 | gboolean str_find_equal(const gchar *haystack, const gchar *needle) |
| 235 | {
|
| 236 | return strcmp(haystack, needle) == 0; |
| 237 | } |
| 238 | |
| 239 | gboolean str_case_find_equal(const gchar *haystack, const gchar *needle) |
| 240 | {
|
| 241 | return g_ascii_strcasecmp(haystack, needle) == 0; |
| 242 | } |
| 243 | |
| 244 | gint to_number(const gchar *nstr)
|
| 245 | {
|
| 246 | register const gchar *p; |
| 247 | |
| 248 | if (*nstr == '\0') return -1; |
| 249 | |
| 250 | for (p = nstr; *p != '\0'; p++) |
| 251 | if (!g_ascii_isdigit(*p)) return -1; |
| 252 | |
| 253 | return atoi(nstr);
|
| 254 | } |
| 255 | |
| 256 | /* convert integer into string,
|
| 257 | nstr must be not lower than 11 characters length */ |
| 258 | gchar *itos_buf(gchar *nstr, gint n) |
| 259 | {
|
| 260 | g_snprintf(nstr, 11, "%d", n); |
| 261 | return nstr;
|
| 262 | } |
| 263 | |
| 264 | /* convert integer into string */
|
| 265 | gchar *itos(gint n) |
| 266 | {
|
| 267 | static gchar nstr[11]; |
| 268 | |
| 269 | return itos_buf(nstr, n);
|
| 270 | } |
| 271 | |
| 272 | gchar *to_human_readable(off_t size) |
| 273 | {
|
| 274 | static gchar str[10]; |
| 275 | |
| 276 | if (size < 1024) |
| 277 | g_snprintf(str, sizeof(str), _("%dB"), (gint)size); |
| 278 | else if (size >> 10 < 1024) |
| 279 | g_snprintf(str, sizeof(str), _("%.1fKB"), (gfloat)size / (1 << 10)); |
| 280 | else if (size >> 20 < 1024) |
| 281 | g_snprintf(str, sizeof(str), _("%.2fMB"), (gfloat)size / (1 << 20)); |
| 282 | else
|
| 283 | g_snprintf(str, sizeof(str), _("%.2fGB"), (gfloat)size / (1 << 30)); |
| 284 | |
| 285 | return str;
|
| 286 | } |
| 287 | |
| 288 | /* strcmp with NULL-checking */
|
| 289 | gint strcmp2(const gchar *s1, const gchar *s2) |
| 290 | {
|
| 291 | if (s1 == NULL || s2 == NULL) |
| 292 | return -1; |
| 293 | else
|
| 294 | return strcmp(s1, s2);
|
| 295 | } |
| 296 | |
| 297 | /* compare paths */
|
| 298 | gint path_cmp(const gchar *s1, const gchar *s2) |
| 299 | {
|
| 300 | gint len1, len2; |
| 301 | #ifdef G_OS_WIN32
|
| 302 | gchar *s1_, *s2_; |
| 303 | #endif
|
| 304 | |
| 305 | if (s1 == NULL || s2 == NULL) return -1; |
| 306 | if (*s1 == '\0' || *s2 == '\0') return -1; |
| 307 | |
| 308 | len1 = strlen(s1); |
| 309 | len2 = strlen(s2); |
| 310 | |
| 311 | #ifdef G_OS_WIN32
|
| 312 | Xstrdup_a(s1_, s1, return -1); |
| 313 | Xstrdup_a(s2_, s2, return -1); |
| 314 | subst_char(s1_, '/', G_DIR_SEPARATOR);
|
| 315 | subst_char(s2_, '/', G_DIR_SEPARATOR);
|
| 316 | if (s1_[len1 - 1] == G_DIR_SEPARATOR) len1--; |
| 317 | if (s2_[len2 - 1] == G_DIR_SEPARATOR) len2--; |
| 318 | |
| 319 | return strncmp(s1_, s2_, MAX(len1, len2));
|
| 320 | #else
|
| 321 | if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--; |
| 322 | if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--; |
| 323 | |
| 324 | return strncmp(s1, s2, MAX(len1, len2));
|
| 325 | #endif
|
| 326 | } |
| 327 | |
| 328 | /* remove trailing return code */
|
| 329 | gchar *strretchomp(gchar *str) |
| 330 | {
|
| 331 | register gchar *s;
|
| 332 | |
| 333 | if (!*str) return str; |
| 334 | |
| 335 | for (s = str + strlen(str) - 1; |
| 336 | s >= str && (*s == '\n' || *s == '\r'); |
| 337 | s--) |
| 338 | *s = '\0';
|
| 339 | |
| 340 | return str;
|
| 341 | } |
| 342 | |
| 343 | /* remove trailing character */
|
| 344 | gchar *strtailchomp(gchar *str, gchar tail_char) |
| 345 | {
|
| 346 | register gchar *s;
|
| 347 | |
| 348 | if (!*str) return str; |
| 349 | if (tail_char == '\0') return str; |
| 350 | |
| 351 | for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--) |
| 352 | *s = '\0';
|
| 353 | |
| 354 | return str;
|
| 355 | } |
| 356 | |
| 357 | /* remove CR (carriage return) */
|
| 358 | gchar *strcrchomp(gchar *str) |
| 359 | {
|
| 360 | register gchar *s;
|
| 361 | |
| 362 | if (!*str) return str; |
| 363 | |
| 364 | s = str + strlen(str) - 1;
|
| 365 | if (*s == '\n' && s > str && *(s - 1) == '\r') { |
| 366 | *(s - 1) = '\n'; |
| 367 | *s = '\0';
|
| 368 | } |
| 369 | |
| 370 | return str;
|
| 371 | } |
| 372 | |
| 373 | /* Similar to `strstr' but this function ignores the case of both strings. */
|
| 374 | gchar *strcasestr(const gchar *haystack, const gchar *needle) |
| 375 | {
|
| 376 | register size_t haystack_len, needle_len;
|
| 377 | |
| 378 | haystack_len = strlen(haystack); |
| 379 | needle_len = strlen(needle); |
| 380 | |
| 381 | if (haystack_len < needle_len || needle_len == 0) |
| 382 | return NULL; |
| 383 | |
| 384 | while (haystack_len >= needle_len) {
|
| 385 | if (!g_ascii_strncasecmp(haystack, needle, needle_len))
|
| 386 | return (gchar *)haystack;
|
| 387 | else {
|
| 388 | haystack++; |
| 389 | haystack_len--; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | gpointer my_memmem(gconstpointer haystack, size_t haystacklen, |
| 397 | gconstpointer needle, size_t needlelen) |
| 398 | {
|
| 399 | const gchar *haystack_ = (const gchar *)haystack; |
| 400 | const gchar *needle_ = (const gchar *)needle; |
| 401 | const gchar *haystack_cur = (const gchar *)haystack; |
| 402 | |
| 403 | if (needlelen == 1) |
| 404 | return memchr(haystack_, *needle_, haystacklen);
|
| 405 | |
| 406 | while ((haystack_cur = memchr(haystack_cur, *needle_, haystacklen))
|
| 407 | != NULL) {
|
| 408 | if (haystacklen - (haystack_cur - haystack_) < needlelen)
|
| 409 | break;
|
| 410 | if (memcmp(haystack_cur + 1, needle_ + 1, needlelen - 1) == 0) |
| 411 | return (gpointer)haystack_cur;
|
| 412 | else
|
| 413 | haystack_cur++; |
| 414 | } |
| 415 | |
| 416 | return NULL; |
| 417 | } |
| 418 | |
| 419 | /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
|
| 420 | gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
|
| 421 | {
|
| 422 | register const gchar *s = src; |
| 423 | register gchar *d = dest;
|
| 424 | |
| 425 | while (--n && *s)
|
| 426 | *d++ = *s++; |
| 427 | *d = '\0';
|
| 428 | |
| 429 | return dest;
|
| 430 | } |
| 431 | |
| 432 | #if !HAVE_ISWALNUM
|
| 433 | int iswalnum(wint_t wc)
|
| 434 | {
|
| 435 | return g_ascii_isalnum((int)wc); |
| 436 | } |
| 437 | #endif
|
| 438 | |
| 439 | #if !HAVE_ISWSPACE
|
| 440 | int iswspace(wint_t wc)
|
| 441 | {
|
| 442 | return g_ascii_isspace((int)wc); |
| 443 | } |
| 444 | #endif
|
| 445 | |
| 446 | #if !HAVE_TOWLOWER
|
| 447 | wint_t towlower(wint_t wc) |
| 448 | {
|
| 449 | if (wc >= L'A' && wc <= L'Z') |
| 450 | return wc + L'a' - L'A'; |
| 451 | |
| 452 | return wc;
|
| 453 | } |
| 454 | #endif
|
| 455 | |
| 456 | #if !HAVE_WCSLEN
|
| 457 | size_t wcslen(const wchar_t *s)
|
| 458 | {
|
| 459 | size_t len = 0;
|
| 460 | |
| 461 | while (*s != L'\0') |
| 462 | ++len, ++s; |
| 463 | |
| 464 | return len;
|
| 465 | } |
| 466 | #endif
|
| 467 | |
| 468 | #if !HAVE_WCSCPY
|
| 469 | /* Copy SRC to DEST. */
|
| 470 | wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
|
| 471 | {
|
| 472 | wint_t c; |
| 473 | wchar_t *s = dest; |
| 474 | |
| 475 | do {
|
| 476 | c = *src++; |
| 477 | *dest++ = c; |
| 478 | } while (c != L'\0'); |
| 479 | |
| 480 | return s;
|
| 481 | } |
| 482 | #endif
|
| 483 | |
| 484 | #if !HAVE_WCSNCPY
|
| 485 | /* Copy no more than N wide-characters of SRC to DEST. */
|
| 486 | wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
|
| 487 | {
|
| 488 | wint_t c; |
| 489 | wchar_t *s = dest; |
| 490 | |
| 491 | do {
|
| 492 | c = *src++; |
| 493 | *dest++ = c; |
| 494 | if (--n == 0) |
| 495 | return s;
|
| 496 | } while (c != L'\0'); |
| 497 | |
| 498 | /* zero fill */
|
| 499 | do
|
| 500 | *dest++ = L'\0';
|
| 501 | while (--n > 0); |
| 502 | |
| 503 | return s;
|
| 504 | } |
| 505 | #endif
|
| 506 | |
| 507 | /* Duplicate S, returning an identical malloc'd string. */
|
| 508 | wchar_t *wcsdup(const wchar_t *s)
|
| 509 | {
|
| 510 | wchar_t *new_str; |
| 511 | |
| 512 | if (s) {
|
| 513 | new_str = g_new(wchar_t, wcslen(s) + 1);
|
| 514 | wcscpy(new_str, s); |
| 515 | } else
|
| 516 | new_str = NULL;
|
| 517 | |
| 518 | return new_str;
|
| 519 | } |
| 520 | |
| 521 | /* Duplicate no more than N wide-characters of S,
|
| 522 | returning an identical malloc'd string. */ |
| 523 | wchar_t *wcsndup(const wchar_t *s, size_t n)
|
| 524 | {
|
| 525 | wchar_t *new_str; |
| 526 | |
| 527 | if (s) {
|
| 528 | new_str = g_new(wchar_t, n + 1);
|
| 529 | wcsncpy(new_str, s, n); |
| 530 | new_str[n] = (wchar_t)0;
|
| 531 | } else
|
| 532 | new_str = NULL;
|
| 533 | |
| 534 | return new_str;
|
| 535 | } |
| 536 | |
| 537 | wchar_t *strdup_mbstowcs(const gchar *s)
|
| 538 | {
|
| 539 | wchar_t *new_str; |
| 540 | |
| 541 | if (s) {
|
| 542 | new_str = g_new(wchar_t, strlen(s) + 1);
|
| 543 | if (mbstowcs(new_str, s, strlen(s) + 1) < 0) { |
| 544 | g_free(new_str); |
| 545 | new_str = NULL;
|
| 546 | } else
|
| 547 | new_str = g_realloc(new_str, |
| 548 | sizeof(wchar_t) * (wcslen(new_str) + 1)); |
| 549 | } else
|
| 550 | new_str = NULL;
|
| 551 | |
| 552 | return new_str;
|
| 553 | } |
| 554 | |
| 555 | gchar *strdup_wcstombs(const wchar_t *s)
|
| 556 | {
|
| 557 | gchar *new_str; |
| 558 | size_t len; |
| 559 | |
| 560 | if (s) {
|
| 561 | len = wcslen(s) * MB_CUR_MAX + 1;
|
| 562 | new_str = g_new(gchar, len); |
| 563 | if (wcstombs(new_str, s, len) < 0) { |
| 564 | g_free(new_str); |
| 565 | new_str = NULL;
|
| 566 | } else
|
| 567 | new_str = g_realloc(new_str, strlen(new_str) + 1);
|
| 568 | } else
|
| 569 | new_str = NULL;
|
| 570 | |
| 571 | return new_str;
|
| 572 | } |
| 573 | |
| 574 | /* Compare S1 and S2, ignoring case. */
|
| 575 | gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n) |
| 576 | {
|
| 577 | wint_t c1; |
| 578 | wint_t c2; |
| 579 | |
| 580 | while (n--) {
|
| 581 | c1 = towlower(*s1++); |
| 582 | c2 = towlower(*s2++); |
| 583 | if (c1 != c2)
|
| 584 | return c1 - c2;
|
| 585 | else if (c1 == 0 && c2 == 0) |
| 586 | break;
|
| 587 | } |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
|
| 593 | wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle) |
| 594 | {
|
| 595 | register size_t haystack_len, needle_len;
|
| 596 | |
| 597 | haystack_len = wcslen(haystack); |
| 598 | needle_len = wcslen(needle); |
| 599 | |
| 600 | if (haystack_len < needle_len || needle_len == 0) |
| 601 | return NULL; |
| 602 | |
| 603 | while (haystack_len >= needle_len) {
|
| 604 | if (!wcsncasecmp(haystack, needle, needle_len))
|
| 605 | return (wchar_t *)haystack;
|
| 606 | else {
|
| 607 | haystack++; |
| 608 | haystack_len--; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | return NULL; |
| 613 | } |
| 614 | |
| 615 | gint get_mbs_len(const gchar *s)
|
| 616 | {
|
| 617 | const gchar *p = s;
|
| 618 | gint mb_len; |
| 619 | gint len = 0;
|
| 620 | |
| 621 | if (!p)
|
| 622 | return -1; |
| 623 | |
| 624 | while (*p != '\0') { |
| 625 | mb_len = g_utf8_skip[*(guchar *)p]; |
| 626 | if (mb_len == 0) |
| 627 | break;
|
| 628 | else
|
| 629 | len++; |
| 630 | |
| 631 | p += mb_len; |
| 632 | } |
| 633 | |
| 634 | return len;
|
| 635 | } |
| 636 | |
| 637 | /* Examine if next block is non-ASCII string */
|
| 638 | gboolean is_next_nonascii(const gchar *s)
|
| 639 | {
|
| 640 | const gchar *p;
|
| 641 | |
| 642 | /* skip head space */
|
| 643 | for (p = s; *p != '\0' && g_ascii_isspace(*p); p++) |
| 644 | ; |
| 645 | for (; *p != '\0' && !g_ascii_isspace(*p); p++) { |
| 646 | if (*(guchar *)p > 127 || *(guchar *)p < 32) |
| 647 | return TRUE;
|
| 648 | } |
| 649 | |
| 650 | return FALSE;
|
| 651 | } |
| 652 | |
| 653 | gint get_next_word_len(const gchar *s)
|
| 654 | {
|
| 655 | gint len = 0;
|
| 656 | |
| 657 | for (; *s != '\0' && !g_ascii_isspace(*s); s++, len++) |
| 658 | ; |
| 659 | |
| 660 | return len;
|
| 661 | } |
| 662 | |
| 663 | /* compare subjects */
|
| 664 | gint subject_compare(const gchar *s1, const gchar *s2) |
| 665 | {
|
| 666 | gchar *str1, *str2; |
| 667 | |
| 668 | if (!s1 || !s2) return -1; |
| 669 | if (!*s1 || !*s2) return -1; |
| 670 | |
| 671 | Xstrdup_a(str1, s1, return -1); |
| 672 | Xstrdup_a(str2, s2, return -1); |
| 673 | |
| 674 | trim_subject_for_compare(str1); |
| 675 | trim_subject_for_compare(str2); |
| 676 | |
| 677 | if (!*str1 || !*str2) return -1; |
| 678 | |
| 679 | return strcmp(str1, str2);
|
| 680 | } |
| 681 | |
| 682 | gint subject_compare_for_sort(const gchar *s1, const gchar *s2) |
| 683 | {
|
| 684 | gchar *str1, *str2; |
| 685 | |
| 686 | if (!s1 || !s2) return -1; |
| 687 | |
| 688 | Xstrdup_a(str1, s1, return -1); |
| 689 | Xstrdup_a(str2, s2, return -1); |
| 690 | |
| 691 | trim_subject_for_sort(str1); |
| 692 | trim_subject_for_sort(str2); |
| 693 | |
| 694 | return g_ascii_strcasecmp(str1, str2);
|
| 695 | } |
| 696 | |
| 697 | void trim_subject_for_compare(gchar *str)
|
| 698 | {
|
| 699 | gchar *srcp; |
| 700 | |
| 701 | eliminate_parenthesis(str, '[', ']'); |
| 702 | eliminate_parenthesis(str, '(', ')'); |
| 703 | g_strstrip(str); |
| 704 | |
| 705 | while (!g_ascii_strncasecmp(str, "Re:", 3)) { |
| 706 | srcp = str + 3;
|
| 707 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 708 | memmove(str, srcp, strlen(srcp) + 1);
|
| 709 | } |
| 710 | } |
| 711 | |
| 712 | void trim_subject_for_sort(gchar *str)
|
| 713 | {
|
| 714 | gchar *srcp; |
| 715 | |
| 716 | g_strstrip(str); |
| 717 | |
| 718 | while (!g_ascii_strncasecmp(str, "Re:", 3)) { |
| 719 | srcp = str + 3;
|
| 720 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 721 | memmove(str, srcp, strlen(srcp) + 1);
|
| 722 | } |
| 723 | } |
| 724 | |
| 725 | void trim_subject(gchar *str)
|
| 726 | {
|
| 727 | register gchar *srcp, *destp;
|
| 728 | gchar op, cl; |
| 729 | gint in_brace; |
| 730 | |
| 731 | destp = str; |
| 732 | while (!g_ascii_strncasecmp(destp, "Re:", 3)) { |
| 733 | destp += 3;
|
| 734 | while (g_ascii_isspace(*destp)) destp++;
|
| 735 | } |
| 736 | |
| 737 | if (*destp == '[') { |
| 738 | op = '[';
|
| 739 | cl = ']';
|
| 740 | } else if (*destp == '(') { |
| 741 | op = '(';
|
| 742 | cl = ')';
|
| 743 | } else
|
| 744 | return;
|
| 745 | |
| 746 | srcp = destp + 1;
|
| 747 | in_brace = 1;
|
| 748 | while (*srcp) {
|
| 749 | if (*srcp == op)
|
| 750 | in_brace++; |
| 751 | else if (*srcp == cl) |
| 752 | in_brace--; |
| 753 | srcp++; |
| 754 | if (in_brace == 0) |
| 755 | break;
|
| 756 | } |
| 757 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 758 | memmove(destp, srcp, strlen(srcp) + 1);
|
| 759 | } |
| 760 | |
| 761 | void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
|
| 762 | {
|
| 763 | register gchar *srcp, *destp;
|
| 764 | gint in_brace; |
| 765 | |
| 766 | srcp = destp = str; |
| 767 | |
| 768 | while ((destp = strchr(destp, op))) {
|
| 769 | in_brace = 1;
|
| 770 | srcp = destp + 1;
|
| 771 | while (*srcp) {
|
| 772 | if (*srcp == op)
|
| 773 | in_brace++; |
| 774 | else if (*srcp == cl) |
| 775 | in_brace--; |
| 776 | srcp++; |
| 777 | if (in_brace == 0) |
| 778 | break;
|
| 779 | } |
| 780 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 781 | memmove(destp, srcp, strlen(srcp) + 1);
|
| 782 | } |
| 783 | } |
| 784 | |
| 785 | void extract_parenthesis(gchar *str, gchar op, gchar cl)
|
| 786 | {
|
| 787 | register gchar *srcp, *destp;
|
| 788 | gint in_brace; |
| 789 | |
| 790 | srcp = destp = str; |
| 791 | |
| 792 | while ((srcp = strchr(destp, op))) {
|
| 793 | if (destp > str)
|
| 794 | *destp++ = ' ';
|
| 795 | memmove(destp, srcp + 1, strlen(srcp));
|
| 796 | in_brace = 1;
|
| 797 | while(*destp) {
|
| 798 | if (*destp == op)
|
| 799 | in_brace++; |
| 800 | else if (*destp == cl) |
| 801 | in_brace--; |
| 802 | |
| 803 | if (in_brace == 0) |
| 804 | break;
|
| 805 | |
| 806 | destp++; |
| 807 | } |
| 808 | } |
| 809 | *destp = '\0';
|
| 810 | } |
| 811 | |
| 812 | void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
|
| 813 | gchar op, gchar cl) |
| 814 | {
|
| 815 | register gchar *srcp, *destp;
|
| 816 | gint in_brace; |
| 817 | gboolean in_quote = FALSE; |
| 818 | |
| 819 | srcp = destp = str; |
| 820 | |
| 821 | while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
|
| 822 | if (destp > str)
|
| 823 | *destp++ = ' ';
|
| 824 | memmove(destp, srcp + 1, strlen(srcp));
|
| 825 | in_brace = 1;
|
| 826 | while(*destp) {
|
| 827 | if (*destp == op && !in_quote)
|
| 828 | in_brace++; |
| 829 | else if (*destp == cl && !in_quote) |
| 830 | in_brace--; |
| 831 | else if (*destp == quote_chr) |
| 832 | in_quote ^= TRUE; |
| 833 | |
| 834 | if (in_brace == 0) |
| 835 | break;
|
| 836 | |
| 837 | destp++; |
| 838 | } |
| 839 | } |
| 840 | *destp = '\0';
|
| 841 | } |
| 842 | |
| 843 | void eliminate_quote(gchar *str, gchar quote_chr)
|
| 844 | {
|
| 845 | register gchar *srcp, *destp;
|
| 846 | |
| 847 | srcp = destp = str; |
| 848 | |
| 849 | while ((destp = strchr(destp, quote_chr))) {
|
| 850 | if ((srcp = strchr(destp + 1, quote_chr))) { |
| 851 | srcp++; |
| 852 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 853 | memmove(destp, srcp, strlen(srcp) + 1);
|
| 854 | } else {
|
| 855 | *destp = '\0';
|
| 856 | break;
|
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | void extract_quote(gchar *str, gchar quote_chr)
|
| 862 | {
|
| 863 | register gchar *p;
|
| 864 | |
| 865 | if ((str = strchr(str, quote_chr))) {
|
| 866 | if ((p = strchr(str + 1, quote_chr))) { |
| 867 | *p = '\0';
|
| 868 | memmove(str, str + 1, p - str);
|
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | void eliminate_address_comment(gchar *str)
|
| 874 | {
|
| 875 | register gchar *srcp, *destp;
|
| 876 | gint in_brace; |
| 877 | |
| 878 | srcp = destp = str; |
| 879 | |
| 880 | while ((destp = strchr(destp, '"'))) { |
| 881 | if ((srcp = strchr(destp + 1, '"'))) { |
| 882 | srcp++; |
| 883 | if (*srcp == '@') { |
| 884 | destp = srcp + 1;
|
| 885 | } else {
|
| 886 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 887 | memmove(destp, srcp, strlen(srcp) + 1);
|
| 888 | } |
| 889 | } else {
|
| 890 | *destp = '\0';
|
| 891 | break;
|
| 892 | } |
| 893 | } |
| 894 | |
| 895 | srcp = destp = str; |
| 896 | |
| 897 | while ((destp = strchr_with_skip_quote(destp, '"', '('))) { |
| 898 | in_brace = 1;
|
| 899 | srcp = destp + 1;
|
| 900 | while (*srcp) {
|
| 901 | if (*srcp == '(') |
| 902 | in_brace++; |
| 903 | else if (*srcp == ')') |
| 904 | in_brace--; |
| 905 | srcp++; |
| 906 | if (in_brace == 0) |
| 907 | break;
|
| 908 | } |
| 909 | while (g_ascii_isspace(*srcp)) srcp++;
|
| 910 | memmove(destp, srcp, strlen(srcp) + 1);
|
| 911 | } |
| 912 | } |
| 913 | |
| 914 | gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
|
| 915 | {
|
| 916 | gboolean in_quote = FALSE; |
| 917 | |
| 918 | while (*str) {
|
| 919 | if (*str == c && !in_quote)
|
| 920 | return (gchar *)str;
|
| 921 | if (*str == quote_chr)
|
| 922 | in_quote ^= TRUE; |
| 923 | str++; |
| 924 | } |
| 925 | |
| 926 | return NULL; |
| 927 | } |
| 928 | |
| 929 | gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
|
| 930 | {
|
| 931 | gboolean in_quote = FALSE; |
| 932 | const gchar *p;
|
| 933 | |
| 934 | p = str + strlen(str) - 1;
|
| 935 | while (p >= str) {
|
| 936 | if (*p == c && !in_quote)
|
| 937 | return (gchar *)p;
|
| 938 | if (*p == quote_chr)
|
| 939 | in_quote ^= TRUE; |
| 940 | p--; |
| 941 | } |
| 942 | |
| 943 | return NULL; |
| 944 | } |
| 945 | |
| 946 | void extract_address(gchar *str)
|
| 947 | {
|
| 948 | eliminate_address_comment(str); |
| 949 | if (strchr_with_skip_quote(str, '"', '<')) |
| 950 | extract_parenthesis_with_skip_quote(str, '"', '<', '>'); |
| 951 | g_strstrip(str); |
| 952 | } |
| 953 | |
| 954 | void extract_list_id_str(gchar *str)
|
| 955 | {
|
| 956 | if (strchr_with_skip_quote(str, '"', '<')) |
| 957 | extract_parenthesis_with_skip_quote(str, '"', '<', '>'); |
| 958 | g_strstrip(str); |
| 959 | } |
| 960 | |
| 961 | GSList *address_list_append(GSList *addr_list, const gchar *str)
|
| 962 | {
|
| 963 | gchar *work; |
| 964 | gchar *workp; |
| 965 | |
| 966 | if (!str) return addr_list; |
| 967 | |
| 968 | Xstrdup_a(work, str, return addr_list);
|
| 969 | |
| 970 | eliminate_address_comment(work); |
| 971 | workp = work; |
| 972 | |
| 973 | while (workp && *workp) {
|
| 974 | gchar *p, *next; |
| 975 | |
| 976 | if ((p = strchr_with_skip_quote(workp, '"', ','))) { |
| 977 | *p = '\0';
|
| 978 | next = p + 1;
|
| 979 | } else
|
| 980 | next = NULL;
|
| 981 | |
| 982 | if (strchr_with_skip_quote(workp, '"', '<')) |
| 983 | extract_parenthesis_with_skip_quote |
| 984 | (workp, '"', '<', '>'); |
| 985 | |
| 986 | g_strstrip(workp); |
| 987 | if (*workp)
|
| 988 | addr_list = g_slist_append(addr_list, g_strdup(workp)); |
| 989 | |
| 990 | workp = next; |
| 991 | } |
| 992 | |
| 993 | return addr_list;
|
| 994 | } |
| 995 | |
| 996 | GSList *references_list_prepend(GSList *msgid_list, const gchar *str)
|
| 997 | {
|
| 998 | const gchar *strp;
|
| 999 | |
| 1000 | if (!str) return msgid_list; |
| 1001 | strp = str; |
| 1002 | |
| 1003 | while (strp && *strp) {
|
| 1004 | const gchar *start, *end;
|
| 1005 | gchar *msgid; |
| 1006 | |
| 1007 | if ((start = strchr(strp, '<')) != NULL) { |
| 1008 | end = strchr(start + 1, '>'); |
| 1009 | if (!end) break; |
| 1010 | } else
|
| 1011 | break;
|
| 1012 | |
| 1013 | msgid = g_strndup(start + 1, end - start - 1); |
| 1014 | g_strstrip(msgid); |
| 1015 | if (*msgid)
|
| 1016 | msgid_list = g_slist_prepend(msgid_list, msgid); |
| 1017 | else
|
| 1018 | g_free(msgid); |
| 1019 | |
| 1020 | strp = end + 1;
|
| 1021 | } |
| 1022 | |
| 1023 | return msgid_list;
|
| 1024 | } |
| 1025 | |
| 1026 | GSList *references_list_append(GSList *msgid_list, const gchar *str)
|
| 1027 | {
|
| 1028 | GSList *list; |
| 1029 | |
| 1030 | list = references_list_prepend(NULL, str);
|
| 1031 | list = g_slist_reverse(list); |
| 1032 | msgid_list = g_slist_concat(msgid_list, list); |
| 1033 | |
| 1034 | return msgid_list;
|
| 1035 | } |
| 1036 | |
| 1037 | GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
|
| 1038 | {
|
| 1039 | gchar *work; |
| 1040 | gchar *workp; |
| 1041 | |
| 1042 | if (!str) return group_list; |
| 1043 | |
| 1044 | Xstrdup_a(work, str, return group_list);
|
| 1045 | |
| 1046 | workp = work; |
| 1047 | |
| 1048 | while (workp && *workp) {
|
| 1049 | gchar *p, *next; |
| 1050 | |
| 1051 | if ((p = strchr_with_skip_quote(workp, '"', ','))) { |
| 1052 | *p = '\0';
|
| 1053 | next = p + 1;
|
| 1054 | } else
|
| 1055 | next = NULL;
|
| 1056 | |
| 1057 | g_strstrip(workp); |
| 1058 | if (*workp)
|
| 1059 | group_list = g_slist_append(group_list, |
| 1060 | g_strdup(workp)); |
| 1061 | |
| 1062 | workp = next; |
| 1063 | } |
| 1064 | |
| 1065 | return group_list;
|
| 1066 | } |
| 1067 | |
| 1068 | GList *add_history(GList *list, const gchar *str)
|
| 1069 | {
|
| 1070 | GList *old; |
| 1071 | |
| 1072 | g_return_val_if_fail(str != NULL, list);
|
| 1073 | |
| 1074 | old = g_list_find_custom(list, (gpointer)str, (GCompareFunc)strcmp2); |
| 1075 | if (old) {
|
| 1076 | g_free(old->data); |
| 1077 | list = g_list_remove(list, old->data); |
| 1078 | } else if (g_list_length(list) >= MAX_HISTORY_SIZE) { |
| 1079 | GList *last; |
| 1080 | |
| 1081 | last = g_list_last(list); |
| 1082 | if (last) {
|
| 1083 | g_free(last->data); |
| 1084 | g_list_remove(list, last->data); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | list = g_list_prepend(list, g_strdup(str)); |
| 1089 | |
| 1090 | return list;
|
| 1091 | } |
| 1092 | |
| 1093 | void remove_return(gchar *str)
|
| 1094 | {
|
| 1095 | register gchar *p = str;
|
| 1096 | |
| 1097 | while (*p) {
|
| 1098 | if (*p == '\n' || *p == '\r') |
| 1099 | memmove(p, p + 1, strlen(p));
|
| 1100 | else
|
| 1101 | p++; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | void remove_space(gchar *str)
|
| 1106 | {
|
| 1107 | register gchar *p = str;
|
| 1108 | register gint spc;
|
| 1109 | |
| 1110 | while (*p) {
|
| 1111 | spc = 0;
|
| 1112 | while (g_ascii_isspace(*(p + spc)))
|
| 1113 | spc++; |
| 1114 | if (spc)
|
| 1115 | memmove(p, p + spc, strlen(p + spc) + 1);
|
| 1116 | else
|
| 1117 | p++; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | void unfold_line(gchar *str)
|
| 1122 | {
|
| 1123 | register gchar *p = str;
|
| 1124 | register gint spc;
|
| 1125 | |
| 1126 | while (*p) {
|
| 1127 | if (*p == '\n' || *p == '\r') { |
| 1128 | *p++ = ' ';
|
| 1129 | spc = 0;
|
| 1130 | while (g_ascii_isspace(*(p + spc)))
|
| 1131 | spc++; |
| 1132 | if (spc)
|
| 1133 | memmove(p, p + spc, strlen(p + spc) + 1);
|
| 1134 | } else
|
| 1135 | p++; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | void subst_char(gchar *str, gchar orig, gchar subst)
|
| 1140 | {
|
| 1141 | register gchar *p = str;
|
| 1142 | |
| 1143 | while (*p) {
|
| 1144 | if (*p == orig)
|
| 1145 | *p = subst; |
| 1146 | p++; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | void subst_chars(gchar *str, gchar *orig, gchar subst)
|
| 1151 | {
|
| 1152 | register gchar *p = str;
|
| 1153 | |
| 1154 | while (*p) {
|
| 1155 | if (strchr(orig, *p) != NULL) |
| 1156 | *p = subst; |
| 1157 | p++; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | void subst_null(gchar *str, gint len, gchar subst)
|
| 1162 | {
|
| 1163 | register gchar *p = str;
|
| 1164 | |
| 1165 | while (len--) {
|
| 1166 | if (*p == '\0') |
| 1167 | *p = subst; |
| 1168 | p++; |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | void subst_for_filename(gchar *str)
|
| 1173 | {
|
| 1174 | subst_chars(str, " \t\r\n\"'/\\", '_'); |
| 1175 | } |
| 1176 | |
| 1177 | gboolean is_header_line(const gchar *str)
|
| 1178 | {
|
| 1179 | if (str[0] == ':') return FALSE; |
| 1180 | |
| 1181 | while (*str != '\0' && *str != ' ') { |
| 1182 | if (*str == ':') |
| 1183 | return TRUE;
|
| 1184 | str++; |
| 1185 | } |
| 1186 | |
| 1187 | return FALSE;
|
| 1188 | } |
| 1189 | |
| 1190 | gboolean is_ascii_str(const gchar *str)
|
| 1191 | {
|
| 1192 | const guchar *p = (const guchar *)str; |
| 1193 | |
| 1194 | while (*p != '\0') { |
| 1195 | if (*p != '\t' && *p != ' ' && |
| 1196 | *p != '\r' && *p != '\n' && |
| 1197 | (*p < 32 || *p >= 127)) |
| 1198 | return FALSE;
|
| 1199 | p++; |
| 1200 | } |
| 1201 | |
| 1202 | return TRUE;
|
| 1203 | } |
| 1204 | |
| 1205 | gint get_quote_level(const gchar *str)
|
| 1206 | {
|
| 1207 | const gchar *first_pos;
|
| 1208 | const gchar *last_pos;
|
| 1209 | const gchar *p = str;
|
| 1210 | gint quote_level = -1;
|
| 1211 | |
| 1212 | /* speed up line processing by only searching to the last '>' */
|
| 1213 | if ((first_pos = strchr(str, '>')) != NULL) { |
| 1214 | /* skip a line if it contains a '<' before the initial '>' */
|
| 1215 | if (memchr(str, '<', first_pos - str) != NULL) |
| 1216 | return -1; |
| 1217 | last_pos = strrchr(first_pos, '>');
|
| 1218 | } else
|
| 1219 | return -1; |
| 1220 | |
| 1221 | while (p <= last_pos) {
|
| 1222 | while (p < last_pos) {
|
| 1223 | if (g_ascii_isspace(*p))
|
| 1224 | p++; |
| 1225 | else
|
| 1226 | break;
|
| 1227 | } |
| 1228 | |
| 1229 | if (*p == '>') |
| 1230 | quote_level++; |
| 1231 | else if (*p != '-' && !g_ascii_isspace(*p) && p <= last_pos) { |
| 1232 | /* any characters are allowed except '-' and space */
|
| 1233 | while (*p != '-' && *p != '>' && !g_ascii_isspace(*p) && |
| 1234 | p < last_pos) |
| 1235 | p++; |
| 1236 | if (*p == '>') |
| 1237 | quote_level++; |
| 1238 | else
|
| 1239 | break;
|
| 1240 | } |
| 1241 | |
| 1242 | p++; |
| 1243 | } |
| 1244 | |
| 1245 | return quote_level;
|
| 1246 | } |
| 1247 | |
| 1248 | gint check_line_length(const gchar *str, gint max_chars, gint *line)
|
| 1249 | {
|
| 1250 | const gchar *p = str, *q;
|
| 1251 | gint cur_line = 0, len;
|
| 1252 | |
| 1253 | while ((q = strchr(p, '\n')) != NULL) { |
| 1254 | len = q - p + 1;
|
| 1255 | if (len > max_chars) {
|
| 1256 | if (line)
|
| 1257 | *line = cur_line; |
| 1258 | return -1; |
| 1259 | } |
| 1260 | p = q + 1;
|
| 1261 | ++cur_line; |
| 1262 | } |
| 1263 | |
| 1264 | len = strlen(p); |
| 1265 | if (len > max_chars) {
|
| 1266 | if (line)
|
| 1267 | *line = cur_line; |
| 1268 | return -1; |
| 1269 | } |
| 1270 | |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle) |
| 1275 | {
|
| 1276 | register guint haystack_len, needle_len;
|
| 1277 | gboolean in_squote = FALSE, in_dquote = FALSE; |
| 1278 | |
| 1279 | haystack_len = strlen(haystack); |
| 1280 | needle_len = strlen(needle); |
| 1281 | |
| 1282 | if (haystack_len < needle_len || needle_len == 0) |
| 1283 | return NULL; |
| 1284 | |
| 1285 | while (haystack_len >= needle_len) {
|
| 1286 | if (!in_squote && !in_dquote &&
|
| 1287 | !strncmp(haystack, needle, needle_len)) |
| 1288 | return (gchar *)haystack;
|
| 1289 | |
| 1290 | /* 'foo"bar"' -> foo"bar"
|
| 1291 | "foo'bar'" -> foo'bar' */ |
| 1292 | if (*haystack == '\'') { |
| 1293 | if (in_squote)
|
| 1294 | in_squote = FALSE; |
| 1295 | else if (!in_dquote) |
| 1296 | in_squote = TRUE; |
| 1297 | } else if (*haystack == '\"') { |
| 1298 | if (in_dquote)
|
| 1299 | in_dquote = FALSE; |
| 1300 | else if (!in_squote) |
| 1301 | in_dquote = TRUE; |
| 1302 | } |
| 1303 | |
| 1304 | haystack++; |
| 1305 | haystack_len--; |
| 1306 | } |
| 1307 | |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | |
| 1311 | gchar *strchr_parenthesis_close(const gchar *str, gchar op, gchar cl)
|
| 1312 | {
|
| 1313 | const gchar *p;
|
| 1314 | gchar quote_chr = '"';
|
| 1315 | gint in_brace; |
| 1316 | gboolean in_quote = FALSE; |
| 1317 | |
| 1318 | p = str; |
| 1319 | |
| 1320 | if ((p = strchr_with_skip_quote(p, quote_chr, op))) {
|
| 1321 | p++; |
| 1322 | in_brace = 1;
|
| 1323 | while (*p) {
|
| 1324 | if (*p == op && !in_quote)
|
| 1325 | in_brace++; |
| 1326 | else if (*p == cl && !in_quote) |
| 1327 | in_brace--; |
| 1328 | else if (*p == quote_chr) |
| 1329 | in_quote ^= TRUE; |
| 1330 | |
| 1331 | if (in_brace == 0) |
| 1332 | return (gchar *)p;
|
| 1333 | |
| 1334 | p++; |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | return NULL; |
| 1339 | } |
| 1340 | |
| 1341 | gchar **strsplit_parenthesis(const gchar *str, gchar op, gchar cl,
|
| 1342 | gint max_tokens) |
| 1343 | {
|
| 1344 | GSList *string_list = NULL, *slist;
|
| 1345 | gchar **str_array; |
| 1346 | const gchar *s_op, *s_cl;
|
| 1347 | guint i, n = 1;
|
| 1348 | |
| 1349 | g_return_val_if_fail(str != NULL, NULL); |
| 1350 | |
| 1351 | if (max_tokens < 1) |
| 1352 | max_tokens = G_MAXINT; |
| 1353 | |
| 1354 | s_op = strchr_with_skip_quote(str, '"', op);
|
| 1355 | if (!s_op) return NULL; |
| 1356 | str = s_op; |
| 1357 | s_cl = strchr_parenthesis_close(str, op, cl); |
| 1358 | if (s_cl) {
|
| 1359 | do {
|
| 1360 | guint len; |
| 1361 | gchar *new_string; |
| 1362 | |
| 1363 | str++; |
| 1364 | len = s_cl - str; |
| 1365 | new_string = g_new(gchar, len + 1);
|
| 1366 | strncpy(new_string, str, len); |
| 1367 | new_string[len] = 0;
|
| 1368 | string_list = g_slist_prepend(string_list, new_string); |
| 1369 | n++; |
| 1370 | str = s_cl + 1;
|
| 1371 | |
| 1372 | while (*str && g_ascii_isspace(*str)) str++;
|
| 1373 | if (*str != op) {
|
| 1374 | string_list = g_slist_prepend(string_list, |
| 1375 | g_strdup(""));
|
| 1376 | n++; |
| 1377 | s_op = strchr_with_skip_quote(str, '"', op);
|
| 1378 | if (!--max_tokens || !s_op) break; |
| 1379 | str = s_op; |
| 1380 | } else
|
| 1381 | s_op = str; |
| 1382 | s_cl = strchr_parenthesis_close(str, op, cl); |
| 1383 | } while (--max_tokens && s_cl);
|
| 1384 | } |
| 1385 | |
| 1386 | str_array = g_new(gchar*, n); |
| 1387 | |
| 1388 | i = n - 1;
|
| 1389 | |
| 1390 | str_array[i--] = NULL;
|
| 1391 | for (slist = string_list; slist; slist = slist->next)
|
| 1392 | str_array[i--] = slist->data; |
| 1393 | |
| 1394 | g_slist_free(string_list); |
| 1395 | |
| 1396 | return str_array;
|
| 1397 | } |
| 1398 | |
| 1399 | gchar **strsplit_with_quote(const gchar *str, const gchar *delim, |
| 1400 | gint max_tokens) |
| 1401 | {
|
| 1402 | GSList *string_list = NULL, *slist;
|
| 1403 | gchar **str_array, *s, *new_str; |
| 1404 | guint i, n = 1, len;
|
| 1405 | |
| 1406 | g_return_val_if_fail(str != NULL, NULL); |
| 1407 | g_return_val_if_fail(delim != NULL, NULL); |
| 1408 | |
| 1409 | if (max_tokens < 1) |
| 1410 | max_tokens = G_MAXINT; |
| 1411 | |
| 1412 | s = strstr_with_skip_quote(str, delim); |
| 1413 | if (s) {
|
| 1414 | guint delimiter_len = strlen(delim); |
| 1415 | |
| 1416 | do {
|
| 1417 | len = s - str; |
| 1418 | new_str = g_strndup(str, len); |
| 1419 | |
| 1420 | if (new_str[0] == '\'' || new_str[0] == '\"') { |
| 1421 | if (new_str[len - 1] == new_str[0]) { |
| 1422 | new_str[len - 1] = '\0'; |
| 1423 | memmove(new_str, new_str + 1, len - 1); |
| 1424 | } |
| 1425 | } |
| 1426 | string_list = g_slist_prepend(string_list, new_str); |
| 1427 | n++; |
| 1428 | str = s + delimiter_len; |
| 1429 | s = strstr_with_skip_quote(str, delim); |
| 1430 | } while (--max_tokens && s);
|
| 1431 | } |
| 1432 | |
| 1433 | if (*str) {
|
| 1434 | new_str = g_strdup(str); |
| 1435 | if (new_str[0] == '\'' || new_str[0] == '\"') { |
| 1436 | len = strlen(str); |
| 1437 | if (new_str[len - 1] == new_str[0]) { |
| 1438 | new_str[len - 1] = '\0'; |
| 1439 | memmove(new_str, new_str + 1, len - 1); |
| 1440 | } |
| 1441 | } |
| 1442 | string_list = g_slist_prepend(string_list, new_str); |
| 1443 | n++; |
| 1444 | } |
| 1445 | |
| 1446 | str_array = g_new(gchar*, n); |
| 1447 | |
| 1448 | i = n - 1;
|
| 1449 | |
| 1450 | str_array[i--] = NULL;
|
| 1451 | for (slist = string_list; slist; slist = slist->next)
|
| 1452 | str_array[i--] = slist->data; |
| 1453 | |
| 1454 | g_slist_free(string_list); |
| 1455 | |
| 1456 | return str_array;
|
| 1457 | } |
| 1458 | |
| 1459 | gchar *get_abbrev_newsgroup_name(const gchar *group, gint len)
|
| 1460 | {
|
| 1461 | gchar *abbrev_group; |
| 1462 | gchar *ap; |
| 1463 | const gchar *p = group;
|
| 1464 | const gchar *last;
|
| 1465 | |
| 1466 | last = group + strlen(group); |
| 1467 | abbrev_group = ap = g_malloc(strlen(group) + 1);
|
| 1468 | |
| 1469 | while (*p) {
|
| 1470 | while (*p == '.') |
| 1471 | *ap++ = *p++; |
| 1472 | if ((ap - abbrev_group) + (last - p) > len && strchr(p, '.')) { |
| 1473 | *ap++ = *p++; |
| 1474 | while (*p != '.') p++; |
| 1475 | } else {
|
| 1476 | strcpy(ap, p); |
| 1477 | return abbrev_group;
|
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | *ap = '\0';
|
| 1482 | return abbrev_group;
|
| 1483 | } |
| 1484 | |
| 1485 | gchar *trim_string(const gchar *str, gint len)
|
| 1486 | {
|
| 1487 | const gchar *p = str;
|
| 1488 | gint mb_len; |
| 1489 | gchar *new_str; |
| 1490 | gint new_len = 0;
|
| 1491 | |
| 1492 | if (!str) return NULL; |
| 1493 | if (strlen(str) <= len)
|
| 1494 | return g_strdup(str);
|
| 1495 | if (g_utf8_validate(str, -1, NULL) == FALSE) |
| 1496 | return g_strdup(str);
|
| 1497 | |
| 1498 | while (*p != '\0') { |
| 1499 | mb_len = g_utf8_skip[*(guchar *)p]; |
| 1500 | if (mb_len == 0) |
| 1501 | break;
|
| 1502 | else if (new_len + mb_len > len) |
| 1503 | break;
|
| 1504 | |
| 1505 | new_len += mb_len; |
| 1506 | p += mb_len; |
| 1507 | } |
| 1508 | |
| 1509 | Xstrndup_a(new_str, str, new_len, return g_strdup(str));
|
| 1510 | return g_strconcat(new_str, "...", NULL); |
| 1511 | } |
| 1512 | |
| 1513 | gchar *trim_string_before(const gchar *str, gint len)
|
| 1514 | {
|
| 1515 | const gchar *p = str;
|
| 1516 | gint mb_len; |
| 1517 | gint new_len; |
| 1518 | |
| 1519 | if (!str) return NULL; |
| 1520 | if ((new_len = strlen(str)) <= len)
|
| 1521 | return g_strdup(str);
|
| 1522 | if (g_utf8_validate(str, -1, NULL) == FALSE) |
| 1523 | return g_strdup(str);
|
| 1524 | |
| 1525 | while (*p != '\0') { |
| 1526 | mb_len = g_utf8_skip[*(guchar *)p]; |
| 1527 | if (mb_len == 0) |
| 1528 | break;
|
| 1529 | |
| 1530 | new_len -= mb_len; |
| 1531 | p += mb_len; |
| 1532 | |
| 1533 | if (new_len <= len)
|
| 1534 | break;
|
| 1535 | } |
| 1536 | |
| 1537 | return g_strconcat("...", p, NULL); |
| 1538 | } |
| 1539 | |
| 1540 | GList *uri_list_extract_filenames(const gchar *uri_list)
|
| 1541 | {
|
| 1542 | GList *result = NULL;
|
| 1543 | const gchar *p, *q;
|
| 1544 | gchar *file; |
| 1545 | |
| 1546 | p = uri_list; |
| 1547 | |
| 1548 | while (p) {
|
| 1549 | if (*p != '#') { |
| 1550 | while (g_ascii_isspace(*p)) p++;
|
| 1551 | if (!strncmp(p, "file:", 5)) { |
| 1552 | p += 5;
|
| 1553 | while (*p == '/' && *(p + 1) == '/') p++; |
| 1554 | q = p; |
| 1555 | while (*q && *q != '\n' && *q != '\r') q++; |
| 1556 | |
| 1557 | if (q > p) {
|
| 1558 | q--; |
| 1559 | while (q > p && g_ascii_isspace(*q))
|
| 1560 | q--; |
| 1561 | file = g_malloc(q - p + 2);
|
| 1562 | strncpy(file, p, q - p + 1);
|
| 1563 | file[q - p + 1] = '\0'; |
| 1564 | decode_uri(file, file); |
| 1565 | result = g_list_append(result,file); |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | p = strchr(p, '\n');
|
| 1570 | if (p) p++;
|
| 1571 | } |
| 1572 | |
| 1573 | return result;
|
| 1574 | } |
| 1575 | |
| 1576 | #define HEX_TO_INT(val, hex) \
|
| 1577 | { \
|
| 1578 | gchar c = hex; \ |
| 1579 | \ |
| 1580 | if ('0' <= c && c <= '9') { \ |
| 1581 | val = c - '0'; \
|
| 1582 | } else if ('a' <= c && c <= 'f') { \ |
| 1583 | val = c - 'a' + 10; \ |
| 1584 | } else if ('A' <= c && c <= 'F') { \ |
| 1585 | val = c - 'A' + 10; \ |
| 1586 | } else { \
|
| 1587 | val = 0; \
|
| 1588 | } \ |
| 1589 | } |
| 1590 | |
| 1591 | /* Converts two-digit hexadecimal to decimal. Used for unescaping escaped
|
| 1592 | * characters. |
| 1593 | */ |
| 1594 | static gint axtoi(const gchar *hex_str) |
| 1595 | {
|
| 1596 | gint hi, lo; |
| 1597 | |
| 1598 | HEX_TO_INT(hi, hex_str[0]);
|
| 1599 | HEX_TO_INT(lo, hex_str[1]);
|
| 1600 | |
| 1601 | return (hi << 4) + lo; |
| 1602 | } |
| 1603 | |
| 1604 | gboolean is_uri_string(const gchar *str)
|
| 1605 | {
|
| 1606 | return (g_ascii_strncasecmp(str, "http://", 7) == 0 || |
| 1607 | g_ascii_strncasecmp(str, "https://", 8) == 0 || |
| 1608 | g_ascii_strncasecmp(str, "ftp://", 6) == 0 || |
| 1609 | g_ascii_strncasecmp(str, "www.", 4) == 0); |
| 1610 | } |
| 1611 | |
| 1612 | gchar *get_uri_path(const gchar *uri)
|
| 1613 | {
|
| 1614 | if (g_ascii_strncasecmp(uri, "http://", 7) == 0) |
| 1615 | return (gchar *)(uri + 7); |
| 1616 | else if (g_ascii_strncasecmp(uri, "https://", 8) == 0) |
| 1617 | return (gchar *)(uri + 8); |
| 1618 | else if (g_ascii_strncasecmp(uri, "ftp://", 6) == 0) |
| 1619 | return (gchar *)(uri + 6); |
| 1620 | else
|
| 1621 | return (gchar *)uri;
|
| 1622 | } |
| 1623 | |
| 1624 | gint get_uri_len(const gchar *str)
|
| 1625 | {
|
| 1626 | const gchar *p;
|
| 1627 | |
| 1628 | if (is_uri_string(str)) {
|
| 1629 | for (p = str; *p != '\0'; p++) { |
| 1630 | if (!g_ascii_isgraph(*p) || strchr("()<>\"", *p)) |
| 1631 | break;
|
| 1632 | } |
| 1633 | return p - str;
|
| 1634 | } |
| 1635 | |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
|
| 1640 | * plusses, and escape characters are used) |
| 1641 | * Note: decoded_uri and encoded_uri can point the same location |
| 1642 | */ |
| 1643 | void decode_uri(gchar *decoded_uri, const gchar *encoded_uri) |
| 1644 | {
|
| 1645 | gchar *dec = decoded_uri; |
| 1646 | const gchar *enc = encoded_uri;
|
| 1647 | |
| 1648 | while (*enc) {
|
| 1649 | if (*enc == '%') { |
| 1650 | enc++; |
| 1651 | if (isxdigit((guchar)enc[0]) && |
| 1652 | isxdigit((guchar)enc[1])) {
|
| 1653 | *dec = axtoi(enc); |
| 1654 | dec++; |
| 1655 | enc += 2;
|
| 1656 | } |
| 1657 | } else {
|
| 1658 | if (*enc == '+') |
| 1659 | *dec = ' ';
|
| 1660 | else
|
| 1661 | *dec = *enc; |
| 1662 | dec++; |
| 1663 | enc++; |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | *dec = '\0';
|
| 1668 | } |
| 1669 | |
| 1670 | gchar *encode_uri(const gchar *filename)
|
| 1671 | {
|
| 1672 | gchar *uri; |
| 1673 | |
| 1674 | uri = g_filename_to_uri(filename, NULL, NULL); |
| 1675 | if (!uri)
|
| 1676 | uri = g_strconcat("file://", filename, NULL); |
| 1677 | |
| 1678 | return uri;
|
| 1679 | } |
| 1680 | |
| 1681 | gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
|
| 1682 | gchar **subject, gchar **body) |
| 1683 | {
|
| 1684 | gchar *tmp_mailto; |
| 1685 | gchar *p; |
| 1686 | |
| 1687 | Xstrdup_a(tmp_mailto, mailto, return -1); |
| 1688 | |
| 1689 | if (!strncmp(tmp_mailto, "mailto:", 7)) |
| 1690 | tmp_mailto += 7;
|
| 1691 | |
| 1692 | p = strchr(tmp_mailto, '?');
|
| 1693 | if (p) {
|
| 1694 | *p = '\0';
|
| 1695 | p++; |
| 1696 | } |
| 1697 | |
| 1698 | if (to && !*to)
|
| 1699 | *to = g_strdup(tmp_mailto); |
| 1700 | |
| 1701 | while (p) {
|
| 1702 | gchar *field, *value; |
| 1703 | |
| 1704 | field = p; |
| 1705 | |
| 1706 | p = strchr(p, '=');
|
| 1707 | if (!p) break; |
| 1708 | *p = '\0';
|
| 1709 | p++; |
| 1710 | |
| 1711 | value = p; |
| 1712 | |
| 1713 | p = strchr(p, '&');
|
| 1714 | if (p) {
|
| 1715 | *p = '\0';
|
| 1716 | p++; |
| 1717 | } |
| 1718 | |
| 1719 | if (*value == '\0') continue; |
| 1720 | |
| 1721 | if (cc && !*cc && !g_ascii_strcasecmp(field, "cc")) { |
| 1722 | *cc = g_strdup(value); |
| 1723 | } else if (bcc && !*bcc && !g_ascii_strcasecmp(field, "bcc")) { |
| 1724 | *bcc = g_strdup(value); |
| 1725 | } else if (subject && !*subject && |
| 1726 | !g_ascii_strcasecmp(field, "subject")) {
|
| 1727 | *subject = g_malloc(strlen(value) + 1);
|
| 1728 | decode_uri(*subject, value); |
| 1729 | } else if (body && !*body && |
| 1730 | !g_ascii_strcasecmp(field, "body")) {
|
| 1731 | *body = g_malloc(strlen(value) + 1);
|
| 1732 | decode_uri(*body, value); |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
| 1739 | const gchar *get_home_dir(void) |
| 1740 | {
|
| 1741 | #ifdef G_OS_WIN32
|
| 1742 | static const gchar *home_dir = NULL; |
| 1743 | |
| 1744 | if (!home_dir) {
|
| 1745 | home_dir = g_get_home_dir(); |
| 1746 | if (!home_dir)
|
| 1747 | home_dir = "C:\\Sylpheed";
|
| 1748 | } |
| 1749 | |
| 1750 | return home_dir;
|
| 1751 | #else
|
| 1752 | return g_get_home_dir();
|
| 1753 | #endif
|
| 1754 | } |
| 1755 | |
| 1756 | const gchar *get_rc_dir(void) |
| 1757 | {
|
| 1758 | static gchar *rc_dir = NULL; |
| 1759 | |
| 1760 | if (!rc_dir)
|
| 1761 | #ifdef G_OS_WIN32
|
| 1762 | rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, |
| 1763 | "Application Data", G_DIR_SEPARATOR_S,
|
| 1764 | RC_DIR, NULL);
|
| 1765 | #else
|
| 1766 | rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, |
| 1767 | RC_DIR, NULL);
|
| 1768 | #endif
|
| 1769 | |
| 1770 | return rc_dir;
|
| 1771 | } |
| 1772 | |
| 1773 | const gchar *get_old_rc_dir(void) |
| 1774 | {
|
| 1775 | static gchar *old_rc_dir = NULL; |
| 1776 | |
| 1777 | if (!old_rc_dir)
|
| 1778 | old_rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, |
| 1779 | OLD_RC_DIR, NULL);
|
| 1780 | |
| 1781 | return old_rc_dir;
|
| 1782 | } |
| 1783 | |
| 1784 | const gchar *get_mail_base_dir(void) |
| 1785 | {
|
| 1786 | #ifdef G_OS_WIN32
|
| 1787 | static gchar *mail_base_dir = NULL; |
| 1788 | |
| 1789 | if (!mail_base_dir)
|
| 1790 | mail_base_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 1791 | "Mailboxes", NULL); |
| 1792 | |
| 1793 | return mail_base_dir;
|
| 1794 | #else
|
| 1795 | return get_home_dir();
|
| 1796 | #endif
|
| 1797 | } |
| 1798 | |
| 1799 | const gchar *get_news_cache_dir(void) |
| 1800 | {
|
| 1801 | static gchar *news_cache_dir = NULL; |
| 1802 | |
| 1803 | if (!news_cache_dir)
|
| 1804 | news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 1805 | NEWS_CACHE_DIR, NULL);
|
| 1806 | |
| 1807 | return news_cache_dir;
|
| 1808 | } |
| 1809 | |
| 1810 | const gchar *get_imap_cache_dir(void) |
| 1811 | {
|
| 1812 | static gchar *imap_cache_dir = NULL; |
| 1813 | |
| 1814 | if (!imap_cache_dir)
|
| 1815 | imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 1816 | IMAP_CACHE_DIR, NULL);
|
| 1817 | |
| 1818 | return imap_cache_dir;
|
| 1819 | } |
| 1820 | |
| 1821 | const gchar *get_mime_tmp_dir(void) |
| 1822 | {
|
| 1823 | static gchar *mime_tmp_dir = NULL; |
| 1824 | |
| 1825 | if (!mime_tmp_dir)
|
| 1826 | mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 1827 | MIME_TMP_DIR, NULL);
|
| 1828 | |
| 1829 | return mime_tmp_dir;
|
| 1830 | } |
| 1831 | |
| 1832 | const gchar *get_template_dir(void) |
| 1833 | {
|
| 1834 | static gchar *template_dir = NULL; |
| 1835 | |
| 1836 | if (!template_dir)
|
| 1837 | template_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 1838 | TEMPLATE_DIR, NULL);
|
| 1839 | |
| 1840 | return template_dir;
|
| 1841 | } |
| 1842 | |
| 1843 | const gchar *get_tmp_dir(void) |
| 1844 | {
|
| 1845 | static gchar *tmp_dir = NULL; |
| 1846 | |
| 1847 | if (!tmp_dir)
|
| 1848 | tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 1849 | TMP_DIR, NULL);
|
| 1850 | |
| 1851 | return tmp_dir;
|
| 1852 | } |
| 1853 | |
| 1854 | gchar *get_tmp_file(void)
|
| 1855 | {
|
| 1856 | gchar *tmp_file; |
| 1857 | static guint32 id = 0; |
| 1858 | |
| 1859 | tmp_file = g_strdup_printf("%s%ctmpfile.%08x",
|
| 1860 | get_tmp_dir(), G_DIR_SEPARATOR, id++); |
| 1861 | |
| 1862 | return tmp_file;
|
| 1863 | } |
| 1864 | |
| 1865 | const gchar *get_domain_name(void) |
| 1866 | {
|
| 1867 | #ifdef G_OS_UNIX
|
| 1868 | static gchar *domain_name = NULL; |
| 1869 | |
| 1870 | if (!domain_name) {
|
| 1871 | gchar buf[128] = ""; |
| 1872 | struct hostent *hp;
|
| 1873 | |
| 1874 | if (gethostname(buf, sizeof(buf)) < 0) { |
| 1875 | perror("gethostname");
|
| 1876 | domain_name = "unknown";
|
| 1877 | } else {
|
| 1878 | buf[sizeof(buf) - 1] = '\0'; |
| 1879 | if ((hp = my_gethostbyname(buf)) == NULL) { |
| 1880 | perror("gethostbyname");
|
| 1881 | domain_name = g_strdup(buf); |
| 1882 | } else {
|
| 1883 | domain_name = g_strdup(hp->h_name); |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | debug_print("domain name = %s\n", domain_name);
|
| 1888 | } |
| 1889 | |
| 1890 | return domain_name;
|
| 1891 | #else
|
| 1892 | return "unknown"; |
| 1893 | #endif
|
| 1894 | } |
| 1895 | |
| 1896 | off_t get_file_size(const gchar *file)
|
| 1897 | {
|
| 1898 | struct stat s;
|
| 1899 | |
| 1900 | if (g_stat(file, &s) < 0) { |
| 1901 | FILE_OP_ERROR(file, "stat");
|
| 1902 | return -1; |
| 1903 | } |
| 1904 | |
| 1905 | return s.st_size;
|
| 1906 | } |
| 1907 | |
| 1908 | off_t get_file_size_as_crlf(const gchar *file)
|
| 1909 | {
|
| 1910 | FILE *fp; |
| 1911 | off_t size = 0;
|
| 1912 | gchar buf[BUFFSIZE]; |
| 1913 | |
| 1914 | if ((fp = g_fopen(file, "rb")) == NULL) { |
| 1915 | FILE_OP_ERROR(file, "fopen");
|
| 1916 | return -1; |
| 1917 | } |
| 1918 | |
| 1919 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 1920 | strretchomp(buf); |
| 1921 | size += strlen(buf) + 2;
|
| 1922 | } |
| 1923 | |
| 1924 | if (ferror(fp)) {
|
| 1925 | FILE_OP_ERROR(file, "fgets");
|
| 1926 | size = -1;
|
| 1927 | } |
| 1928 | |
| 1929 | fclose(fp); |
| 1930 | |
| 1931 | return size;
|
| 1932 | } |
| 1933 | |
| 1934 | off_t get_left_file_size(FILE *fp) |
| 1935 | {
|
| 1936 | glong pos; |
| 1937 | glong end; |
| 1938 | off_t size; |
| 1939 | |
| 1940 | if ((pos = ftell(fp)) < 0) { |
| 1941 | perror("ftell");
|
| 1942 | return -1; |
| 1943 | } |
| 1944 | if (fseek(fp, 0L, SEEK_END) < 0) { |
| 1945 | perror("fseek");
|
| 1946 | return -1; |
| 1947 | } |
| 1948 | if ((end = ftell(fp)) < 0) { |
| 1949 | perror("fseek");
|
| 1950 | return -1; |
| 1951 | } |
| 1952 | size = end - pos; |
| 1953 | if (fseek(fp, pos, SEEK_SET) < 0) { |
| 1954 | perror("fseek");
|
| 1955 | return -1; |
| 1956 | } |
| 1957 | |
| 1958 | return size;
|
| 1959 | } |
| 1960 | |
| 1961 | gboolean file_exist(const gchar *file, gboolean allow_fifo)
|
| 1962 | {
|
| 1963 | struct stat s;
|
| 1964 | |
| 1965 | if (file == NULL) |
| 1966 | return FALSE;
|
| 1967 | |
| 1968 | if (g_stat(file, &s) < 0) { |
| 1969 | if (ENOENT != errno) FILE_OP_ERROR(file, "stat"); |
| 1970 | return FALSE;
|
| 1971 | } |
| 1972 | |
| 1973 | if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
|
| 1974 | return TRUE;
|
| 1975 | |
| 1976 | return FALSE;
|
| 1977 | } |
| 1978 | |
| 1979 | gboolean is_dir_exist(const gchar *dir)
|
| 1980 | {
|
| 1981 | if (dir == NULL) |
| 1982 | return FALSE;
|
| 1983 | |
| 1984 | return g_file_test(dir, G_FILE_TEST_IS_DIR);
|
| 1985 | } |
| 1986 | |
| 1987 | gboolean is_file_entry_exist(const gchar *file)
|
| 1988 | {
|
| 1989 | if (file == NULL) |
| 1990 | return FALSE;
|
| 1991 | |
| 1992 | return g_file_test(file, G_FILE_TEST_EXISTS);
|
| 1993 | } |
| 1994 | |
| 1995 | gboolean dirent_is_regular_file(struct dirent *d)
|
| 1996 | {
|
| 1997 | #ifdef HAVE_DIRENT_D_TYPE
|
| 1998 | if (d->d_type == DT_REG)
|
| 1999 | return TRUE;
|
| 2000 | else if (d->d_type != DT_UNKNOWN) |
| 2001 | return FALSE;
|
| 2002 | #endif
|
| 2003 | |
| 2004 | return g_file_test(d->d_name, G_FILE_TEST_IS_REGULAR);
|
| 2005 | } |
| 2006 | |
| 2007 | gboolean dirent_is_directory(struct dirent *d)
|
| 2008 | {
|
| 2009 | #ifdef HAVE_DIRENT_D_TYPE
|
| 2010 | if (d->d_type == DT_DIR)
|
| 2011 | return TRUE;
|
| 2012 | else if (d->d_type != DT_UNKNOWN) |
| 2013 | return FALSE;
|
| 2014 | #endif
|
| 2015 | |
| 2016 | return g_file_test(d->d_name, G_FILE_TEST_IS_DIR);
|
| 2017 | } |
| 2018 | |
| 2019 | gint change_dir(const gchar *dir)
|
| 2020 | {
|
| 2021 | gchar *prevdir = NULL;
|
| 2022 | |
| 2023 | if (debug_mode)
|
| 2024 | prevdir = g_get_current_dir(); |
| 2025 | |
| 2026 | if (g_chdir(dir) < 0) { |
| 2027 | FILE_OP_ERROR(dir, "chdir");
|
| 2028 | if (debug_mode) g_free(prevdir);
|
| 2029 | return -1; |
| 2030 | } else if (debug_mode) { |
| 2031 | gchar *cwd; |
| 2032 | |
| 2033 | cwd = g_get_current_dir(); |
| 2034 | if (strcmp(prevdir, cwd) != 0) |
| 2035 | g_print("current dir: %s\n", cwd);
|
| 2036 | g_free(cwd); |
| 2037 | g_free(prevdir); |
| 2038 | } |
| 2039 | |
| 2040 | return 0; |
| 2041 | } |
| 2042 | |
| 2043 | gint make_dir(const gchar *dir)
|
| 2044 | {
|
| 2045 | if (g_mkdir(dir, S_IRWXU) < 0) { |
| 2046 | FILE_OP_ERROR(dir, "mkdir");
|
| 2047 | return -1; |
| 2048 | } |
| 2049 | if (g_chmod(dir, S_IRWXU) < 0) |
| 2050 | FILE_OP_ERROR(dir, "chmod");
|
| 2051 | |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
| 2055 | gint make_dir_hier(const gchar *dir)
|
| 2056 | {
|
| 2057 | gchar *parent_dir; |
| 2058 | const gchar *p;
|
| 2059 | |
| 2060 | for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) { |
| 2061 | parent_dir = g_strndup(dir, p - dir); |
| 2062 | if (*parent_dir != '\0') { |
| 2063 | if (!is_dir_exist(parent_dir)) {
|
| 2064 | if (make_dir(parent_dir) < 0) { |
| 2065 | g_free(parent_dir); |
| 2066 | return -1; |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | g_free(parent_dir); |
| 2071 | } |
| 2072 | |
| 2073 | if (!is_dir_exist(dir)) {
|
| 2074 | if (make_dir(dir) < 0) |
| 2075 | return -1; |
| 2076 | } |
| 2077 | |
| 2078 | return 0; |
| 2079 | } |
| 2080 | |
| 2081 | gint remove_all_files(const gchar *dir)
|
| 2082 | {
|
| 2083 | GDir *dp; |
| 2084 | const gchar *dir_name;
|
| 2085 | gchar *prev_dir; |
| 2086 | |
| 2087 | prev_dir = g_get_current_dir(); |
| 2088 | |
| 2089 | if (g_chdir(dir) < 0) { |
| 2090 | FILE_OP_ERROR(dir, "chdir");
|
| 2091 | g_free(prev_dir); |
| 2092 | return -1; |
| 2093 | } |
| 2094 | |
| 2095 | if ((dp = g_dir_open(".", 0, NULL)) == NULL) { |
| 2096 | g_warning("failed to open directory: %s\n", dir);
|
| 2097 | g_free(prev_dir); |
| 2098 | return -1; |
| 2099 | } |
| 2100 | |
| 2101 | while ((dir_name = g_dir_read_name(dp)) != NULL) { |
| 2102 | if (g_unlink(dir_name) < 0) |
| 2103 | FILE_OP_ERROR(dir_name, "unlink");
|
| 2104 | } |
| 2105 | |
| 2106 | g_dir_close(dp); |
| 2107 | |
| 2108 | if (g_chdir(prev_dir) < 0) { |
| 2109 | FILE_OP_ERROR(prev_dir, "chdir");
|
| 2110 | g_free(prev_dir); |
| 2111 | return -1; |
| 2112 | } |
| 2113 | |
| 2114 | g_free(prev_dir); |
| 2115 | |
| 2116 | return 0; |
| 2117 | } |
| 2118 | |
| 2119 | gint remove_numbered_files(const gchar *dir, guint first, guint last)
|
| 2120 | {
|
| 2121 | GDir *dp; |
| 2122 | const gchar *dir_name;
|
| 2123 | gchar *prev_dir; |
| 2124 | gint file_no; |
| 2125 | |
| 2126 | prev_dir = g_get_current_dir(); |
| 2127 | |
| 2128 | if (g_chdir(dir) < 0) { |
| 2129 | FILE_OP_ERROR(dir, "chdir");
|
| 2130 | g_free(prev_dir); |
| 2131 | return -1; |
| 2132 | } |
| 2133 | |
| 2134 | if ((dp = g_dir_open(".", 0, NULL)) == NULL) { |
| 2135 | g_warning("failed to open directory: %s\n", dir);
|
| 2136 | g_free(prev_dir); |
| 2137 | return -1; |
| 2138 | } |
| 2139 | |
| 2140 | while ((dir_name = g_dir_read_name(dp)) != NULL) { |
| 2141 | file_no = to_number(dir_name); |
| 2142 | if (file_no > 0 && first <= file_no && file_no <= last) { |
| 2143 | if (is_dir_exist(dir_name))
|
| 2144 | continue;
|
| 2145 | if (g_unlink(dir_name) < 0) |
| 2146 | FILE_OP_ERROR(dir_name, "unlink");
|
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | g_dir_close(dp); |
| 2151 | |
| 2152 | if (g_chdir(prev_dir) < 0) { |
| 2153 | FILE_OP_ERROR(prev_dir, "chdir");
|
| 2154 | g_free(prev_dir); |
| 2155 | return -1; |
| 2156 | } |
| 2157 | |
| 2158 | g_free(prev_dir); |
| 2159 | |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | gint remove_all_numbered_files(const gchar *dir)
|
| 2164 | {
|
| 2165 | return remove_numbered_files(dir, 0, UINT_MAX); |
| 2166 | } |
| 2167 | |
| 2168 | gint remove_expired_files(const gchar *dir, guint hours)
|
| 2169 | {
|
| 2170 | GDir *dp; |
| 2171 | const gchar *dir_name;
|
| 2172 | struct stat s;
|
| 2173 | gchar *prev_dir; |
| 2174 | gint file_no; |
| 2175 | time_t mtime, now, expire_time; |
| 2176 | |
| 2177 | prev_dir = g_get_current_dir(); |
| 2178 | |
| 2179 | if (g_chdir(dir) < 0) { |
| 2180 | FILE_OP_ERROR(dir, "chdir");
|
| 2181 | g_free(prev_dir); |
| 2182 | return -1; |
| 2183 | } |
| 2184 | |
| 2185 | if ((dp = g_dir_open(".", 0, NULL)) == NULL) { |
| 2186 | g_warning("failed to open directory: %s\n", dir);
|
| 2187 | g_free(prev_dir); |
| 2188 | return -1; |
| 2189 | } |
| 2190 | |
| 2191 | now = time(NULL);
|
| 2192 | expire_time = hours * 60 * 60; |
| 2193 | |
| 2194 | while ((dir_name = g_dir_read_name(dp)) != NULL) { |
| 2195 | file_no = to_number(dir_name); |
| 2196 | if (file_no > 0) { |
| 2197 | if (g_stat(dir_name, &s) < 0) { |
| 2198 | FILE_OP_ERROR(dir_name, "stat");
|
| 2199 | continue;
|
| 2200 | } |
| 2201 | if (S_ISDIR(s.st_mode))
|
| 2202 | continue;
|
| 2203 | mtime = MAX(s.st_mtime, s.st_atime); |
| 2204 | if (now - mtime > expire_time) {
|
| 2205 | if (g_unlink(dir_name) < 0) |
| 2206 | FILE_OP_ERROR(dir_name, "unlink");
|
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | g_dir_close(dp); |
| 2212 | |
| 2213 | if (g_chdir(prev_dir) < 0) { |
| 2214 | FILE_OP_ERROR(prev_dir, "chdir");
|
| 2215 | g_free(prev_dir); |
| 2216 | return -1; |
| 2217 | } |
| 2218 | |
| 2219 | g_free(prev_dir); |
| 2220 | |
| 2221 | return 0; |
| 2222 | } |
| 2223 | |
| 2224 | static gint remove_dir_recursive_real(const gchar *dir) |
| 2225 | {
|
| 2226 | struct stat s;
|
| 2227 | GDir *dp; |
| 2228 | const gchar *dir_name;
|
| 2229 | gchar *prev_dir; |
| 2230 | |
| 2231 | if (g_stat(dir, &s) < 0) { |
| 2232 | FILE_OP_ERROR(dir, "stat");
|
| 2233 | if (ENOENT == errno) return 0; |
| 2234 | return -1; |
| 2235 | } |
| 2236 | |
| 2237 | if (!S_ISDIR(s.st_mode)) {
|
| 2238 | if (g_unlink(dir) < 0) { |
| 2239 | FILE_OP_ERROR(dir, "unlink");
|
| 2240 | return -1; |
| 2241 | } |
| 2242 | |
| 2243 | return 0; |
| 2244 | } |
| 2245 | |
| 2246 | prev_dir = g_get_current_dir(); |
| 2247 | /* g_print("prev_dir = %s\n", prev_dir); */
|
| 2248 | |
| 2249 | if (g_chdir(dir) < 0) { |
| 2250 | FILE_OP_ERROR(dir, "chdir");
|
| 2251 | g_free(prev_dir); |
| 2252 | return -1; |
| 2253 | } |
| 2254 | |
| 2255 | if ((dp = g_dir_open(".", 0, NULL)) == NULL) { |
| 2256 | g_warning("failed to open directory: %s\n", dir);
|
| 2257 | g_chdir(prev_dir); |
| 2258 | g_free(prev_dir); |
| 2259 | return -1; |
| 2260 | } |
| 2261 | |
| 2262 | /* remove all files in the directory */
|
| 2263 | while ((dir_name = g_dir_read_name(dp)) != NULL) { |
| 2264 | /* g_print("removing %s\n", dir_name); */
|
| 2265 | |
| 2266 | if (is_dir_exist(dir_name)) {
|
| 2267 | if (remove_dir_recursive_real(dir_name) < 0) { |
| 2268 | g_warning("can't remove directory\n");
|
| 2269 | return -1; |
| 2270 | } |
| 2271 | } else {
|
| 2272 | if (g_unlink(dir_name) < 0) |
| 2273 | FILE_OP_ERROR(dir_name, "unlink");
|
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | g_dir_close(dp); |
| 2278 | |
| 2279 | if (g_chdir(prev_dir) < 0) { |
| 2280 | FILE_OP_ERROR(prev_dir, "chdir");
|
| 2281 | g_free(prev_dir); |
| 2282 | return -1; |
| 2283 | } |
| 2284 | |
| 2285 | g_free(prev_dir); |
| 2286 | |
| 2287 | if (g_rmdir(dir) < 0) { |
| 2288 | FILE_OP_ERROR(dir, "rmdir");
|
| 2289 | return -1; |
| 2290 | } |
| 2291 | |
| 2292 | return 0; |
| 2293 | } |
| 2294 | |
| 2295 | gint remove_dir_recursive(const gchar *dir)
|
| 2296 | {
|
| 2297 | gchar *cur_dir; |
| 2298 | gint ret; |
| 2299 | |
| 2300 | cur_dir = g_get_current_dir(); |
| 2301 | |
| 2302 | if (g_chdir(dir) < 0) { |
| 2303 | FILE_OP_ERROR(dir, "chdir");
|
| 2304 | ret = -1;
|
| 2305 | goto leave;
|
| 2306 | } |
| 2307 | if (g_chdir("..") < 0) { |
| 2308 | FILE_OP_ERROR(dir, "chdir");
|
| 2309 | ret = -1;
|
| 2310 | goto leave;
|
| 2311 | } |
| 2312 | |
| 2313 | ret = remove_dir_recursive_real(dir); |
| 2314 | |
| 2315 | leave:
|
| 2316 | if (is_dir_exist(cur_dir)) {
|
| 2317 | if (g_chdir(cur_dir) < 0) { |
| 2318 | FILE_OP_ERROR(cur_dir, "chdir");
|
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | g_free(cur_dir); |
| 2323 | |
| 2324 | return ret;
|
| 2325 | } |
| 2326 | |
| 2327 | gint rename_force(const gchar *oldpath, const gchar *newpath) |
| 2328 | {
|
| 2329 | #ifndef G_OS_UNIX
|
| 2330 | if (!is_file_entry_exist(oldpath)) {
|
| 2331 | errno = ENOENT; |
| 2332 | return -1; |
| 2333 | } |
| 2334 | if (is_file_exist(newpath)) {
|
| 2335 | if (g_unlink(newpath) < 0) |
| 2336 | FILE_OP_ERROR(newpath, "unlink");
|
| 2337 | } |
| 2338 | #endif
|
| 2339 | return g_rename(oldpath, newpath);
|
| 2340 | } |
| 2341 | |
| 2342 | gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup) |
| 2343 | {
|
| 2344 | FILE *src_fp, *dest_fp; |
| 2345 | gint n_read; |
| 2346 | gchar buf[BUFSIZ]; |
| 2347 | gchar *dest_bak = NULL;
|
| 2348 | gboolean err = FALSE; |
| 2349 | |
| 2350 | if ((src_fp = g_fopen(src, "rb")) == NULL) { |
| 2351 | FILE_OP_ERROR(src, "fopen");
|
| 2352 | return -1; |
| 2353 | } |
| 2354 | if (is_file_exist(dest)) {
|
| 2355 | dest_bak = g_strconcat(dest, ".bak", NULL); |
| 2356 | if (rename_force(dest, dest_bak) < 0) { |
| 2357 | FILE_OP_ERROR(dest, "rename");
|
| 2358 | fclose(src_fp); |
| 2359 | g_free(dest_bak); |
| 2360 | return -1; |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | if ((dest_fp = g_fopen(dest, "wb")) == NULL) { |
| 2365 | FILE_OP_ERROR(dest, "fopen");
|
| 2366 | fclose(src_fp); |
| 2367 | if (dest_bak) {
|
| 2368 | if (rename_force(dest_bak, dest) < 0) |
| 2369 | FILE_OP_ERROR(dest_bak, "rename");
|
| 2370 | g_free(dest_bak); |
| 2371 | } |
| 2372 | return -1; |
| 2373 | } |
| 2374 | |
| 2375 | if (change_file_mode_rw(dest_fp, dest) < 0) { |
| 2376 | FILE_OP_ERROR(dest, "chmod");
|
| 2377 | g_warning(_("can't change file mode\n"));
|
| 2378 | } |
| 2379 | |
| 2380 | while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) { |
| 2381 | if (n_read < sizeof(buf) && ferror(src_fp)) |
| 2382 | break;
|
| 2383 | if (fwrite(buf, n_read, 1, dest_fp) < 1) { |
| 2384 | g_warning(_("writing to %s failed.\n"), dest);
|
| 2385 | fclose(dest_fp); |
| 2386 | fclose(src_fp); |
| 2387 | g_unlink(dest); |
| 2388 | if (dest_bak) {
|
| 2389 | if (rename_force(dest_bak, dest) < 0) |
| 2390 | FILE_OP_ERROR(dest_bak, "rename");
|
| 2391 | g_free(dest_bak); |
| 2392 | } |
| 2393 | return -1; |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | if (ferror(src_fp)) {
|
| 2398 | FILE_OP_ERROR(src, "fread");
|
| 2399 | err = TRUE; |
| 2400 | } |
| 2401 | fclose(src_fp); |
| 2402 | if (fclose(dest_fp) == EOF) { |
| 2403 | FILE_OP_ERROR(dest, "fclose");
|
| 2404 | err = TRUE; |
| 2405 | } |
| 2406 | |
| 2407 | if (err) {
|
| 2408 | g_unlink(dest); |
| 2409 | if (dest_bak) {
|
| 2410 | if (rename_force(dest_bak, dest) < 0) |
| 2411 | FILE_OP_ERROR(dest_bak, "rename");
|
| 2412 | g_free(dest_bak); |
| 2413 | } |
| 2414 | return -1; |
| 2415 | } |
| 2416 | |
| 2417 | if (keep_backup == FALSE && dest_bak)
|
| 2418 | g_unlink(dest_bak); |
| 2419 | |
| 2420 | g_free(dest_bak); |
| 2421 | |
| 2422 | return 0; |
| 2423 | } |
| 2424 | |
| 2425 | gint copy_dir(const gchar *src, const gchar *dest) |
| 2426 | {
|
| 2427 | GDir *dir; |
| 2428 | const gchar *dir_name;
|
| 2429 | gchar *src_file; |
| 2430 | gchar *dest_file; |
| 2431 | |
| 2432 | if ((dir = g_dir_open(src, 0, NULL)) == NULL) { |
| 2433 | g_warning("failed to open directory: %s\n", src);
|
| 2434 | return -1; |
| 2435 | } |
| 2436 | |
| 2437 | if (make_dir_hier(dest) < 0) { |
| 2438 | g_dir_close(dir); |
| 2439 | return -1; |
| 2440 | } |
| 2441 | |
| 2442 | while ((dir_name = g_dir_read_name(dir)) != NULL) { |
| 2443 | src_file = g_strconcat(src, G_DIR_SEPARATOR_S, dir_name, NULL);
|
| 2444 | dest_file = g_strconcat(dest, G_DIR_SEPARATOR_S, dir_name, |
| 2445 | NULL);
|
| 2446 | if (is_file_exist(src_file))
|
| 2447 | copy_file(src_file, dest_file, FALSE); |
| 2448 | g_free(dest_file); |
| 2449 | g_free(src_file); |
| 2450 | } |
| 2451 | |
| 2452 | g_dir_close(dir); |
| 2453 | |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
| 2457 | gint move_file(const gchar *src, const gchar *dest, gboolean overwrite) |
| 2458 | {
|
| 2459 | if (overwrite == FALSE && is_file_exist(dest)) {
|
| 2460 | g_warning("move_file(): file %s already exists.", dest);
|
| 2461 | return -1; |
| 2462 | } |
| 2463 | |
| 2464 | if (rename_force(src, dest) == 0) return 0; |
| 2465 | |
| 2466 | if (EXDEV != errno) {
|
| 2467 | FILE_OP_ERROR(src, "rename");
|
| 2468 | return -1; |
| 2469 | } |
| 2470 | |
| 2471 | if (copy_file(src, dest, FALSE) < 0) return -1; |
| 2472 | |
| 2473 | g_unlink(src); |
| 2474 | |
| 2475 | return 0; |
| 2476 | } |
| 2477 | |
| 2478 | gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
|
| 2479 | {
|
| 2480 | FILE *dest_fp; |
| 2481 | gint n_read; |
| 2482 | gint bytes_left, to_read; |
| 2483 | gchar buf[BUFSIZ]; |
| 2484 | gboolean err = FALSE; |
| 2485 | |
| 2486 | if (fseek(fp, offset, SEEK_SET) < 0) { |
| 2487 | perror("fseek");
|
| 2488 | return -1; |
| 2489 | } |
| 2490 | |
| 2491 | if ((dest_fp = g_fopen(dest, "wb")) == NULL) { |
| 2492 | FILE_OP_ERROR(dest, "fopen");
|
| 2493 | return -1; |
| 2494 | } |
| 2495 | |
| 2496 | if (change_file_mode_rw(dest_fp, dest) < 0) { |
| 2497 | FILE_OP_ERROR(dest, "chmod");
|
| 2498 | g_warning("can't change file mode\n");
|
| 2499 | } |
| 2500 | |
| 2501 | bytes_left = length; |
| 2502 | to_read = MIN(bytes_left, sizeof(buf));
|
| 2503 | |
| 2504 | while ((n_read = fread(buf, sizeof(gchar), to_read, fp)) > 0) { |
| 2505 | if (n_read < to_read && ferror(fp))
|
| 2506 | break;
|
| 2507 | if (fwrite(buf, n_read, 1, dest_fp) < 1) { |
| 2508 | g_warning(_("writing to %s failed.\n"), dest);
|
| 2509 | fclose(dest_fp); |
| 2510 | g_unlink(dest); |
| 2511 | return -1; |
| 2512 | } |
| 2513 | bytes_left -= n_read; |
| 2514 | if (bytes_left == 0) |
| 2515 | break;
|
| 2516 | to_read = MIN(bytes_left, sizeof(buf));
|
| 2517 | } |
| 2518 | |
| 2519 | if (ferror(fp)) {
|
| 2520 | perror("fread");
|
| 2521 | err = TRUE; |
| 2522 | } |
| 2523 | if (fclose(dest_fp) == EOF) { |
| 2524 | FILE_OP_ERROR(dest, "fclose");
|
| 2525 | err = TRUE; |
| 2526 | } |
| 2527 | |
| 2528 | if (err) {
|
| 2529 | g_unlink(dest); |
| 2530 | return -1; |
| 2531 | } |
| 2532 | |
| 2533 | return 0; |
| 2534 | } |
| 2535 | |
| 2536 | /* convert line endings into CRLF. If the last line doesn't end with
|
| 2537 | * linebreak, add it. |
| 2538 | */ |
| 2539 | gchar *canonicalize_str(const gchar *str)
|
| 2540 | {
|
| 2541 | const gchar *p;
|
| 2542 | guint new_len = 0;
|
| 2543 | gchar *out, *outp; |
| 2544 | |
| 2545 | for (p = str; *p != '\0'; ++p) { |
| 2546 | if (*p != '\r') { |
| 2547 | ++new_len; |
| 2548 | if (*p == '\n') |
| 2549 | ++new_len; |
| 2550 | } |
| 2551 | } |
| 2552 | if (p == str || *(p - 1) != '\n') |
| 2553 | new_len += 2;
|
| 2554 | |
| 2555 | out = outp = g_malloc(new_len + 1);
|
| 2556 | for (p = str; *p != '\0'; ++p) { |
| 2557 | if (*p != '\r') { |
| 2558 | if (*p == '\n') |
| 2559 | *outp++ = '\r';
|
| 2560 | *outp++ = *p; |
| 2561 | } |
| 2562 | } |
| 2563 | if (p == str || *(p - 1) != '\n') { |
| 2564 | *outp++ = '\r';
|
| 2565 | *outp++ = '\n';
|
| 2566 | } |
| 2567 | *outp = '\0';
|
| 2568 | |
| 2569 | return out;
|
| 2570 | } |
| 2571 | |
| 2572 | gint canonicalize_file(const gchar *src, const gchar *dest) |
| 2573 | {
|
| 2574 | FILE *src_fp, *dest_fp; |
| 2575 | gchar buf[BUFFSIZE]; |
| 2576 | gint len; |
| 2577 | gboolean err = FALSE; |
| 2578 | gboolean last_linebreak = FALSE; |
| 2579 | |
| 2580 | if ((src_fp = g_fopen(src, "rb")) == NULL) { |
| 2581 | FILE_OP_ERROR(src, "fopen");
|
| 2582 | return -1; |
| 2583 | } |
| 2584 | |
| 2585 | if ((dest_fp = g_fopen(dest, "wb")) == NULL) { |
| 2586 | FILE_OP_ERROR(dest, "fopen");
|
| 2587 | fclose(src_fp); |
| 2588 | return -1; |
| 2589 | } |
| 2590 | |
| 2591 | if (change_file_mode_rw(dest_fp, dest) < 0) { |
| 2592 | FILE_OP_ERROR(dest, "chmod");
|
| 2593 | g_warning("can't change file mode\n");
|
| 2594 | } |
| 2595 | |
| 2596 | while (fgets(buf, sizeof(buf), src_fp) != NULL) { |
| 2597 | gint r = 0;
|
| 2598 | |
| 2599 | len = strlen(buf); |
| 2600 | if (len == 0) break; |
| 2601 | last_linebreak = FALSE; |
| 2602 | |
| 2603 | if (buf[len - 1] != '\n') { |
| 2604 | last_linebreak = TRUE; |
| 2605 | r = fputs(buf, dest_fp); |
| 2606 | } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') { |
| 2607 | r = fputs(buf, dest_fp); |
| 2608 | } else {
|
| 2609 | if (len > 1) { |
| 2610 | r = fwrite(buf, len - 1, 1, dest_fp); |
| 2611 | if (r != 1) |
| 2612 | r = EOF;
|
| 2613 | } |
| 2614 | if (r != EOF) |
| 2615 | r = fputs("\r\n", dest_fp);
|
| 2616 | } |
| 2617 | |
| 2618 | if (r == EOF) { |
| 2619 | g_warning("writing to %s failed.\n", dest);
|
| 2620 | fclose(dest_fp); |
| 2621 | fclose(src_fp); |
| 2622 | g_unlink(dest); |
| 2623 | return -1; |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | if (last_linebreak == TRUE) {
|
| 2628 | if (fputs("\r\n", dest_fp) == EOF) |
| 2629 | err = TRUE; |
| 2630 | } |
| 2631 | |
| 2632 | if (ferror(src_fp)) {
|
| 2633 | FILE_OP_ERROR(src, "fgets");
|
| 2634 | err = TRUE; |
| 2635 | } |
| 2636 | fclose(src_fp); |
| 2637 | if (fclose(dest_fp) == EOF) { |
| 2638 | FILE_OP_ERROR(dest, "fclose");
|
| 2639 | err = TRUE; |
| 2640 | } |
| 2641 | |
| 2642 | if (err) {
|
| 2643 | g_unlink(dest); |
| 2644 | return -1; |
| 2645 | } |
| 2646 | |
| 2647 | return 0; |
| 2648 | } |
| 2649 | |
| 2650 | gint canonicalize_file_replace(const gchar *file)
|
| 2651 | {
|
| 2652 | gchar *tmp_file; |
| 2653 | |
| 2654 | tmp_file = get_tmp_file(); |
| 2655 | |
| 2656 | if (canonicalize_file(file, tmp_file) < 0) { |
| 2657 | g_free(tmp_file); |
| 2658 | return -1; |
| 2659 | } |
| 2660 | |
| 2661 | if (move_file(tmp_file, file, TRUE) < 0) { |
| 2662 | g_warning("can't replace %s .\n", file);
|
| 2663 | g_unlink(tmp_file); |
| 2664 | g_free(tmp_file); |
| 2665 | return -1; |
| 2666 | } |
| 2667 | |
| 2668 | g_free(tmp_file); |
| 2669 | return 0; |
| 2670 | } |
| 2671 | |
| 2672 | gint uncanonicalize_file(const gchar *src, const gchar *dest) |
| 2673 | {
|
| 2674 | FILE *src_fp, *dest_fp; |
| 2675 | gchar buf[BUFFSIZE]; |
| 2676 | gboolean err = FALSE; |
| 2677 | |
| 2678 | if ((src_fp = g_fopen(src, "rb")) == NULL) { |
| 2679 | FILE_OP_ERROR(src, "fopen");
|
| 2680 | return -1; |
| 2681 | } |
| 2682 | |
| 2683 | if ((dest_fp = g_fopen(dest, "wb")) == NULL) { |
| 2684 | FILE_OP_ERROR(dest, "fopen");
|
| 2685 | fclose(src_fp); |
| 2686 | return -1; |
| 2687 | } |
| 2688 | |
| 2689 | if (change_file_mode_rw(dest_fp, dest) < 0) { |
| 2690 | FILE_OP_ERROR(dest, "chmod");
|
| 2691 | g_warning("can't change file mode\n");
|
| 2692 | } |
| 2693 | |
| 2694 | while (fgets(buf, sizeof(buf), src_fp) != NULL) { |
| 2695 | strcrchomp(buf); |
| 2696 | if (fputs(buf, dest_fp) == EOF) { |
| 2697 | g_warning("writing to %s failed.\n", dest);
|
| 2698 | fclose(dest_fp); |
| 2699 | fclose(src_fp); |
| 2700 | g_unlink(dest); |
| 2701 | return -1; |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | if (ferror(src_fp)) {
|
| 2706 | FILE_OP_ERROR(src, "fgets");
|
| 2707 | err = TRUE; |
| 2708 | } |
| 2709 | fclose(src_fp); |
| 2710 | if (fclose(dest_fp) == EOF) { |
| 2711 | FILE_OP_ERROR(dest, "fclose");
|
| 2712 | err = TRUE; |
| 2713 | } |
| 2714 | |
| 2715 | if (err) {
|
| 2716 | g_unlink(dest); |
| 2717 | return -1; |
| 2718 | } |
| 2719 | |
| 2720 | return 0; |
| 2721 | } |
| 2722 | |
| 2723 | gint uncanonicalize_file_replace(const gchar *file)
|
| 2724 | {
|
| 2725 | gchar *tmp_file; |
| 2726 | |
| 2727 | tmp_file = get_tmp_file(); |
| 2728 | |
| 2729 | if (uncanonicalize_file(file, tmp_file) < 0) { |
| 2730 | g_free(tmp_file); |
| 2731 | return -1; |
| 2732 | } |
| 2733 | |
| 2734 | if (move_file(tmp_file, file, TRUE) < 0) { |
| 2735 | g_warning("can't replace %s .\n", file);
|
| 2736 | g_unlink(tmp_file); |
| 2737 | g_free(tmp_file); |
| 2738 | return -1; |
| 2739 | } |
| 2740 | |
| 2741 | g_free(tmp_file); |
| 2742 | return 0; |
| 2743 | } |
| 2744 | |
| 2745 | gchar *normalize_newlines(const gchar *str)
|
| 2746 | {
|
| 2747 | const gchar *p = str;
|
| 2748 | gchar *out, *outp; |
| 2749 | |
| 2750 | out = outp = g_malloc(strlen(str) + 1);
|
| 2751 | for (p = str; *p != '\0'; ++p) { |
| 2752 | if (*p == '\r') { |
| 2753 | if (*(p + 1) != '\n') |
| 2754 | *outp++ = '\n';
|
| 2755 | } else
|
| 2756 | *outp++ = *p; |
| 2757 | } |
| 2758 | |
| 2759 | *outp = '\0';
|
| 2760 | |
| 2761 | return out;
|
| 2762 | } |
| 2763 | |
| 2764 | gchar *get_outgoing_rfc2822_str(FILE *fp) |
| 2765 | {
|
| 2766 | gchar buf[BUFFSIZE]; |
| 2767 | GString *str; |
| 2768 | gchar *ret; |
| 2769 | |
| 2770 | str = g_string_new(NULL);
|
| 2771 | |
| 2772 | /* output header part */
|
| 2773 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 2774 | strretchomp(buf); |
| 2775 | if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) { |
| 2776 | gint next; |
| 2777 | |
| 2778 | for (;;) {
|
| 2779 | next = fgetc(fp); |
| 2780 | if (next == EOF) |
| 2781 | break;
|
| 2782 | else if (next != ' ' && next != '\t') { |
| 2783 | ungetc(next, fp); |
| 2784 | break;
|
| 2785 | } |
| 2786 | if (fgets(buf, sizeof(buf), fp) == NULL) |
| 2787 | break;
|
| 2788 | } |
| 2789 | #if 0
|
| 2790 | } else if (!g_ascii_strncasecmp(buf, "Date:", 5)) {
|
| 2791 | get_rfc822_date(buf, sizeof(buf)); |
| 2792 | g_string_append_printf(str, "Date: %s\r\n", buf); |
| 2793 | #endif |
| 2794 | } else {
|
| 2795 | g_string_append(str, buf); |
| 2796 | g_string_append(str, "\r\n");
|
| 2797 | if (buf[0] == '\0') |
| 2798 | break;
|
| 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | /* output body part */
|
| 2803 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 2804 | strretchomp(buf); |
| 2805 | if (buf[0] == '.') |
| 2806 | g_string_append_c(str, '.');
|
| 2807 | g_string_append(str, buf); |
| 2808 | g_string_append(str, "\r\n");
|
| 2809 | } |
| 2810 | |
| 2811 | ret = str->str; |
| 2812 | g_string_free(str, FALSE); |
| 2813 | |
| 2814 | return ret;
|
| 2815 | } |
| 2816 | |
| 2817 | /*
|
| 2818 | * Create a new boundary in a way that it is very unlikely that this |
| 2819 | * will occur in the following text. It would be easy to ensure |
| 2820 | * uniqueness if everything is either quoted-printable or base64 |
| 2821 | * encoded (note that conversion is allowed), but because MIME bodies |
| 2822 | * may be nested, it may happen that the same boundary has already |
| 2823 | * been used. We avoid scanning the message for conflicts and hope the |
| 2824 | * best. |
| 2825 | * |
| 2826 | * boundary := 0*69<bchars> bcharsnospace |
| 2827 | * bchars := bcharsnospace / " " |
| 2828 | * bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
|
| 2829 | * "+" / "_" / "," / "-" / "." / |
| 2830 | * "/" / ":" / "=" / "?" |
| 2831 | * |
| 2832 | * some special characters removed because of buggy MTAs |
| 2833 | */ |
| 2834 | |
| 2835 | gchar *generate_mime_boundary(const gchar *prefix)
|
| 2836 | {
|
| 2837 | static gchar tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 2838 | "abcdefghijklmnopqrstuvwxyz"
|
| 2839 | "1234567890+_./=";
|
| 2840 | gchar buf_uniq[17];
|
| 2841 | gchar buf_date[64];
|
| 2842 | gint i; |
| 2843 | |
| 2844 | for (i = 0; i < sizeof(buf_uniq) - 1; i++) |
| 2845 | buf_uniq[i] = tbl[g_random_int_range(0, sizeof(tbl) - 1)]; |
| 2846 | buf_uniq[i] = '\0';
|
| 2847 | |
| 2848 | get_rfc822_date(buf_date, sizeof(buf_date));
|
| 2849 | subst_char(buf_date, ' ', '_'); |
| 2850 | subst_char(buf_date, ',', '_'); |
| 2851 | subst_char(buf_date, ':', '_'); |
| 2852 | |
| 2853 | return g_strdup_printf("%s=_%s_%s", prefix ? prefix : "Multipart", |
| 2854 | buf_date, buf_uniq); |
| 2855 | } |
| 2856 | |
| 2857 | gint change_file_mode_rw(FILE *fp, const gchar *file)
|
| 2858 | {
|
| 2859 | #if HAVE_FCHMOD
|
| 2860 | return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
|
| 2861 | #else
|
| 2862 | return g_chmod(file, S_IRUSR|S_IWUSR);
|
| 2863 | #endif
|
| 2864 | } |
| 2865 | |
| 2866 | FILE *my_tmpfile(void)
|
| 2867 | {
|
| 2868 | #if HAVE_MKSTEMP
|
| 2869 | const gchar suffix[] = ".XXXXXX"; |
| 2870 | const gchar *tmpdir;
|
| 2871 | guint tmplen; |
| 2872 | const gchar *progname;
|
| 2873 | guint proglen; |
| 2874 | gchar *fname; |
| 2875 | gint fd; |
| 2876 | FILE *fp; |
| 2877 | |
| 2878 | tmpdir = get_tmp_dir(); |
| 2879 | tmplen = strlen(tmpdir); |
| 2880 | progname = g_get_prgname(); |
| 2881 | proglen = strlen(progname); |
| 2882 | Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix), |
| 2883 | return tmpfile());
|
| 2884 | |
| 2885 | memcpy(fname, tmpdir, tmplen); |
| 2886 | fname[tmplen] = G_DIR_SEPARATOR; |
| 2887 | memcpy(fname + tmplen + 1, progname, proglen);
|
| 2888 | memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix)); |
| 2889 | |
| 2890 | fd = mkstemp(fname); |
| 2891 | if (fd < 0) |
| 2892 | return tmpfile();
|
| 2893 | |
| 2894 | g_unlink(fname); |
| 2895 | |
| 2896 | fp = fdopen(fd, "w+b");
|
| 2897 | if (!fp)
|
| 2898 | close(fd); |
| 2899 | else
|
| 2900 | return fp;
|
| 2901 | #endif /* HAVE_MKSTEMP */ |
| 2902 | |
| 2903 | return tmpfile();
|
| 2904 | } |
| 2905 | |
| 2906 | FILE *str_open_as_stream(const gchar *str)
|
| 2907 | {
|
| 2908 | FILE *fp; |
| 2909 | size_t len; |
| 2910 | |
| 2911 | g_return_val_if_fail(str != NULL, NULL); |
| 2912 | |
| 2913 | fp = my_tmpfile(); |
| 2914 | if (!fp) {
|
| 2915 | FILE_OP_ERROR("str_open_as_stream", "my_tmpfile"); |
| 2916 | return NULL; |
| 2917 | } |
| 2918 | |
| 2919 | len = strlen(str); |
| 2920 | if (len == 0) return fp; |
| 2921 | |
| 2922 | if (fwrite(str, len, 1, fp) != 1) { |
| 2923 | FILE_OP_ERROR("str_open_as_stream", "fwrite"); |
| 2924 | fclose(fp); |
| 2925 | return NULL; |
| 2926 | } |
| 2927 | |
| 2928 | rewind(fp); |
| 2929 | return fp;
|
| 2930 | } |
| 2931 | |
| 2932 | gint str_write_to_file(const gchar *str, const gchar *file) |
| 2933 | {
|
| 2934 | FILE *fp; |
| 2935 | size_t len; |
| 2936 | |
| 2937 | g_return_val_if_fail(str != NULL, -1); |
| 2938 | g_return_val_if_fail(file != NULL, -1); |
| 2939 | |
| 2940 | if ((fp = g_fopen(file, "wb")) == NULL) { |
| 2941 | FILE_OP_ERROR(file, "fopen");
|
| 2942 | return -1; |
| 2943 | } |
| 2944 | |
| 2945 | len = strlen(str); |
| 2946 | if (len == 0) { |
| 2947 | fclose(fp); |
| 2948 | return 0; |
| 2949 | } |
| 2950 | |
| 2951 | if (fwrite(str, len, 1, fp) != 1) { |
| 2952 | FILE_OP_ERROR(file, "fwrite");
|
| 2953 | fclose(fp); |
| 2954 | g_unlink(file); |
| 2955 | return -1; |
| 2956 | } |
| 2957 | |
| 2958 | if (fclose(fp) == EOF) { |
| 2959 | FILE_OP_ERROR(file, "fclose");
|
| 2960 | g_unlink(file); |
| 2961 | return -1; |
| 2962 | } |
| 2963 | |
| 2964 | return 0; |
| 2965 | } |
| 2966 | |
| 2967 | gchar *file_read_to_str(const gchar *file)
|
| 2968 | {
|
| 2969 | FILE *fp; |
| 2970 | gchar *str; |
| 2971 | |
| 2972 | g_return_val_if_fail(file != NULL, NULL); |
| 2973 | |
| 2974 | if ((fp = g_fopen(file, "rb")) == NULL) { |
| 2975 | FILE_OP_ERROR(file, "fopen");
|
| 2976 | return NULL; |
| 2977 | } |
| 2978 | |
| 2979 | str = file_read_stream_to_str(fp); |
| 2980 | |
| 2981 | fclose(fp); |
| 2982 | |
| 2983 | return str;
|
| 2984 | } |
| 2985 | |
| 2986 | gchar *file_read_stream_to_str(FILE *fp) |
| 2987 | {
|
| 2988 | GByteArray *array; |
| 2989 | guchar buf[BUFSIZ]; |
| 2990 | gint n_read; |
| 2991 | gchar *str; |
| 2992 | |
| 2993 | g_return_val_if_fail(fp != NULL, NULL); |
| 2994 | |
| 2995 | array = g_byte_array_new(); |
| 2996 | |
| 2997 | while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) { |
| 2998 | if (n_read < sizeof(buf) && ferror(fp)) |
| 2999 | break;
|
| 3000 | g_byte_array_append(array, buf, n_read); |
| 3001 | } |
| 3002 | |
| 3003 | if (ferror(fp)) {
|
| 3004 | FILE_OP_ERROR("file stream", "fread"); |
| 3005 | g_byte_array_free(array, TRUE); |
| 3006 | return NULL; |
| 3007 | } |
| 3008 | |
| 3009 | buf[0] = '\0'; |
| 3010 | g_byte_array_append(array, buf, 1);
|
| 3011 | str = (gchar *)array->data; |
| 3012 | g_byte_array_free(array, FALSE); |
| 3013 | |
| 3014 | return str;
|
| 3015 | } |
| 3016 | |
| 3017 | gint execute_async(gchar *const argv[])
|
| 3018 | {
|
| 3019 | g_return_val_if_fail(argv != NULL && argv[0] != NULL, -1); |
| 3020 | |
| 3021 | if (g_spawn_async(NULL, (gchar **)argv, NULL, G_SPAWN_SEARCH_PATH, |
| 3022 | NULL, NULL, NULL, FALSE) == FALSE) { |
| 3023 | g_warning("Can't execute command: %s\n", argv[0]); |
| 3024 | return -1; |
| 3025 | } |
| 3026 | |
| 3027 | return 0; |
| 3028 | } |
| 3029 | |
| 3030 | gint execute_sync(gchar *const argv[])
|
| 3031 | {
|
| 3032 | gint status; |
| 3033 | |
| 3034 | g_return_val_if_fail(argv != NULL && argv[0] != NULL, -1); |
| 3035 | |
| 3036 | if (g_spawn_sync(NULL, (gchar **)argv, NULL, G_SPAWN_SEARCH_PATH, |
| 3037 | NULL, NULL, NULL, NULL, &status, NULL) == FALSE) { |
| 3038 | g_warning("Can't execute command: %s\n", argv[0]); |
| 3039 | return -1; |
| 3040 | } |
| 3041 | |
| 3042 | #ifdef G_OS_UNIX
|
| 3043 | if (WIFEXITED(status))
|
| 3044 | return WEXITSTATUS(status);
|
| 3045 | else
|
| 3046 | return -1; |
| 3047 | #else
|
| 3048 | return status;
|
| 3049 | #endif
|
| 3050 | } |
| 3051 | |
| 3052 | gint execute_command_line(const gchar *cmdline, gboolean async)
|
| 3053 | {
|
| 3054 | gchar **argv; |
| 3055 | gint ret; |
| 3056 | |
| 3057 | debug_print("execute_command_line(): executing: %s\n", cmdline);
|
| 3058 | |
| 3059 | argv = strsplit_with_quote(cmdline, " ", 0); |
| 3060 | |
| 3061 | if (async)
|
| 3062 | ret = execute_async(argv); |
| 3063 | else
|
| 3064 | ret = execute_sync(argv); |
| 3065 | |
| 3066 | g_strfreev(argv); |
| 3067 | |
| 3068 | return ret;
|
| 3069 | } |
| 3070 | |
| 3071 | gchar *get_command_output(const gchar *cmdline)
|
| 3072 | {
|
| 3073 | gchar *child_stdout; |
| 3074 | gint status; |
| 3075 | |
| 3076 | g_return_val_if_fail(cmdline != NULL, NULL); |
| 3077 | |
| 3078 | debug_print("get_command_output(): executing: %s\n", cmdline);
|
| 3079 | |
| 3080 | if (g_spawn_command_line_sync(cmdline, &child_stdout, NULL, &status, |
| 3081 | NULL) == FALSE) {
|
| 3082 | g_warning("Can't execute command: %s\n", cmdline);
|
| 3083 | return NULL; |
| 3084 | } |
| 3085 | |
| 3086 | return child_stdout;
|
| 3087 | } |
| 3088 | |
| 3089 | gint open_uri(const gchar *uri, const gchar *cmdline) |
| 3090 | {
|
| 3091 | gchar buf[BUFFSIZE]; |
| 3092 | gchar *p; |
| 3093 | |
| 3094 | g_return_val_if_fail(uri != NULL, -1); |
| 3095 | |
| 3096 | if (cmdline &&
|
| 3097 | (p = strchr(cmdline, '%')) && *(p + 1) == 's' && |
| 3098 | !strchr(p + 2, '%')) |
| 3099 | g_snprintf(buf, sizeof(buf), cmdline, uri);
|
| 3100 | else {
|
| 3101 | if (cmdline)
|
| 3102 | g_warning("Open URI command line is invalid "
|
| 3103 | "(there must be only one '%%s'): %s",
|
| 3104 | cmdline); |
| 3105 | g_snprintf(buf, sizeof(buf), DEFAULT_BROWSER_CMD, uri);
|
| 3106 | } |
| 3107 | |
| 3108 | execute_command_line(buf, TRUE); |
| 3109 | |
| 3110 | return 0; |
| 3111 | } |
| 3112 | |
| 3113 | time_t remote_tzoffset_sec(const gchar *zone)
|
| 3114 | {
|
| 3115 | static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT"; |
| 3116 | gchar zone3[4];
|
| 3117 | gchar *p; |
| 3118 | gchar c; |
| 3119 | gint iustz; |
| 3120 | gint offset; |
| 3121 | time_t remoteoffset; |
| 3122 | |
| 3123 | strncpy(zone3, zone, 3);
|
| 3124 | zone3[3] = '\0'; |
| 3125 | remoteoffset = 0;
|
| 3126 | |
| 3127 | if (sscanf(zone, "%c%d", &c, &offset) == 2 && |
| 3128 | (c == '+' || c == '-')) { |
| 3129 | remoteoffset = ((offset / 100) * 60 + (offset % 100)) * 60; |
| 3130 | if (c == '-') |
| 3131 | remoteoffset = -remoteoffset; |
| 3132 | } else if (!strncmp(zone, "UT" , 2) || |
| 3133 | !strncmp(zone, "GMT", 2)) { |
| 3134 | remoteoffset = 0;
|
| 3135 | } else if (strlen(zone3) == 3) { |
| 3136 | for (p = ustzstr; *p != '\0'; p += 3) { |
| 3137 | if (!g_ascii_strncasecmp(p, zone3, 3)) { |
| 3138 | iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8; |
| 3139 | remoteoffset = iustz * 3600;
|
| 3140 | break;
|
| 3141 | } |
| 3142 | } |
| 3143 | if (*p == '\0') |
| 3144 | return -1; |
| 3145 | } else if (strlen(zone3) == 1) { |
| 3146 | switch (zone[0]) { |
| 3147 | case 'Z': remoteoffset = 0; break; |
| 3148 | case 'A': remoteoffset = -1; break; |
| 3149 | case 'B': remoteoffset = -2; break; |
| 3150 | case 'C': remoteoffset = -3; break; |
| 3151 | case 'D': remoteoffset = -4; break; |
| 3152 | case 'E': remoteoffset = -5; break; |
| 3153 | case 'F': remoteoffset = -6; break; |
| 3154 | case 'G': remoteoffset = -7; break; |
| 3155 | case 'H': remoteoffset = -8; break; |
| 3156 | case 'I': remoteoffset = -9; break; |
| 3157 | case 'K': remoteoffset = -10; break; /* J is not used */ |
| 3158 | case 'L': remoteoffset = -11; break; |
| 3159 | case 'M': remoteoffset = -12; break; |
| 3160 | case 'N': remoteoffset = 1; break; |
| 3161 | case 'O': remoteoffset = 2; break; |
| 3162 | case 'P': remoteoffset = 3; break; |
| 3163 | case 'Q': remoteoffset = 4; break; |
| 3164 | case 'R': remoteoffset = 5; break; |
| 3165 | case 'S': remoteoffset = 6; break; |
| 3166 | case 'T': remoteoffset = 7; break; |
| 3167 | case 'U': remoteoffset = 8; break; |
| 3168 | case 'V': remoteoffset = 9; break; |
| 3169 | case 'W': remoteoffset = 10; break; |
| 3170 | case 'X': remoteoffset = 11; break; |
| 3171 | case 'Y': remoteoffset = 12; break; |
| 3172 | default: remoteoffset = 0; break; |
| 3173 | } |
| 3174 | remoteoffset = remoteoffset * 3600;
|
| 3175 | } else
|
| 3176 | return -1; |
| 3177 | |
| 3178 | return remoteoffset;
|
| 3179 | } |
| 3180 | |
| 3181 | time_t tzoffset_sec(time_t *now) |
| 3182 | {
|
| 3183 | struct tm gmt, *lt;
|
| 3184 | gint off; |
| 3185 | |
| 3186 | gmt = *gmtime(now); |
| 3187 | lt = localtime(now); |
| 3188 | |
| 3189 | off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
|
| 3190 | |
| 3191 | if (lt->tm_year < gmt.tm_year)
|
| 3192 | off -= 24 * 60; |
| 3193 | else if (lt->tm_year > gmt.tm_year) |
| 3194 | off += 24 * 60; |
| 3195 | else if (lt->tm_yday < gmt.tm_yday) |
| 3196 | off -= 24 * 60; |
| 3197 | else if (lt->tm_yday > gmt.tm_yday) |
| 3198 | off += 24 * 60; |
| 3199 | |
| 3200 | if (off >= 24 * 60) /* should be impossible */ |
| 3201 | off = 23 * 60 + 59; /* if not, insert silly value */ |
| 3202 | if (off <= -24 * 60) |
| 3203 | off = -(23 * 60 + 59); |
| 3204 | |
| 3205 | return off * 60; |
| 3206 | } |
| 3207 | |
| 3208 | /* calculate timezone offset */
|
| 3209 | gchar *tzoffset(time_t *now) |
| 3210 | {
|
| 3211 | static gchar offset_string[6]; |
| 3212 | struct tm gmt, *lt;
|
| 3213 | gint off; |
| 3214 | gchar sign = '+';
|
| 3215 | |
| 3216 | gmt = *gmtime(now); |
| 3217 | lt = localtime(now); |
| 3218 | |
| 3219 | off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
|
| 3220 | |
| 3221 | if (lt->tm_year < gmt.tm_year)
|
| 3222 | off -= 24 * 60; |
| 3223 | else if (lt->tm_year > gmt.tm_year) |
| 3224 | off += 24 * 60; |
| 3225 | else if (lt->tm_yday < gmt.tm_yday) |
| 3226 | off -= 24 * 60; |
| 3227 | else if (lt->tm_yday > gmt.tm_yday) |
| 3228 | off += 24 * 60; |
| 3229 | |
| 3230 | if (off < 0) { |
| 3231 | sign = '-';
|
| 3232 | off = -off; |
| 3233 | } |
| 3234 | |
| 3235 | if (off >= 24 * 60) /* should be impossible */ |
| 3236 | off = 23 * 60 + 59; /* if not, insert silly value */ |
| 3237 | |
| 3238 | sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60); |
| 3239 | |
| 3240 | return offset_string;
|
| 3241 | } |
| 3242 | |
| 3243 | void get_rfc822_date(gchar *buf, gint len)
|
| 3244 | {
|
| 3245 | struct tm *lt;
|
| 3246 | time_t t; |
| 3247 | gchar day[4], mon[4]; |
| 3248 | gint dd, hh, mm, ss, yyyy; |
| 3249 | |
| 3250 | t = time(NULL);
|
| 3251 | lt = localtime(&t); |
| 3252 | |
| 3253 | sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
|
| 3254 | day, mon, &dd, &hh, &mm, &ss, &yyyy); |
| 3255 | g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
|
| 3256 | day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t)); |
| 3257 | } |
| 3258 | |
| 3259 | /* just a wrapper to suppress the warning of gcc about %c */
|
| 3260 | size_t my_strftime(gchar *s, size_t max, const gchar *format,
|
| 3261 | const struct tm *tm) |
| 3262 | {
|
| 3263 | return strftime(s, max, format, tm);
|
| 3264 | } |
| 3265 | |
| 3266 | /* user input */
|
| 3267 | |
| 3268 | static QueryPasswordFunc query_password_func = NULL; |
| 3269 | |
| 3270 | void set_input_query_password_func(QueryPasswordFunc func)
|
| 3271 | {
|
| 3272 | query_password_func = func; |
| 3273 | } |
| 3274 | |
| 3275 | gchar *input_query_password(const gchar *server, const gchar *user) |
| 3276 | {
|
| 3277 | if (query_password_func)
|
| 3278 | return query_password_func(server, user);
|
| 3279 | else
|
| 3280 | return NULL; |
| 3281 | } |
| 3282 | |
| 3283 | /* logging */
|
| 3284 | |
| 3285 | static FILE *log_fp = NULL; |
| 3286 | |
| 3287 | void set_log_file(const gchar *filename) |
| 3288 | {
|
| 3289 | if (log_fp) return; |
| 3290 | log_fp = g_fopen(filename, "wb");
|
| 3291 | if (!log_fp)
|
| 3292 | FILE_OP_ERROR(filename, "fopen");
|
| 3293 | } |
| 3294 | |
| 3295 | void close_log_file(void) |
| 3296 | {
|
| 3297 | if (log_fp) {
|
| 3298 | fclose(log_fp); |
| 3299 | log_fp = NULL;
|
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | static guint log_verbosity_count = 0; |
| 3304 | |
| 3305 | void set_log_verbosity(gboolean verbose)
|
| 3306 | {
|
| 3307 | if (verbose)
|
| 3308 | log_verbosity_count++; |
| 3309 | else if (log_verbosity_count > 0) |
| 3310 | log_verbosity_count--; |
| 3311 | } |
| 3312 | |
| 3313 | gboolean get_debug_mode(void)
|
| 3314 | {
|
| 3315 | return debug_mode;
|
| 3316 | } |
| 3317 | |
| 3318 | void set_debug_mode(gboolean enable)
|
| 3319 | {
|
| 3320 | debug_mode = enable; |
| 3321 | } |
| 3322 | |
| 3323 | static void log_dummy_func(const gchar *str) |
| 3324 | {
|
| 3325 | } |
| 3326 | |
| 3327 | static LogFunc log_print_ui_func = log_dummy_func;
|
| 3328 | static LogFunc log_message_ui_func = log_dummy_func;
|
| 3329 | static LogFunc log_warning_ui_func = log_dummy_func;
|
| 3330 | static LogFunc log_error_ui_func = log_dummy_func;
|
| 3331 | |
| 3332 | static LogFunc log_show_status_func = log_dummy_func;
|
| 3333 | |
| 3334 | void set_log_ui_func(LogFunc print_func, LogFunc message_func,
|
| 3335 | LogFunc warning_func, LogFunc error_func) |
| 3336 | {
|
| 3337 | log_print_ui_func = print_func; |
| 3338 | log_message_ui_func = message_func; |
| 3339 | log_warning_ui_func = warning_func; |
| 3340 | log_error_ui_func = error_func; |
| 3341 | } |
| 3342 | |
| 3343 | void set_log_show_status_func(LogFunc status_func)
|
| 3344 | {
|
| 3345 | log_show_status_func = status_func; |
| 3346 | } |
| 3347 | |
| 3348 | void debug_print(const gchar *format, ...) |
| 3349 | {
|
| 3350 | va_list args; |
| 3351 | gchar buf[BUFFSIZE]; |
| 3352 | |
| 3353 | if (!debug_mode) return; |
| 3354 | |
| 3355 | va_start(args, format); |
| 3356 | g_vsnprintf(buf, sizeof(buf), format, args);
|
| 3357 | va_end(args); |
| 3358 | |
| 3359 | g_print("%s", buf);
|
| 3360 | } |
| 3361 | |
| 3362 | void print_status(const gchar *format, ...) |
| 3363 | {
|
| 3364 | va_list args; |
| 3365 | gchar buf[BUFFSIZE]; |
| 3366 | |
| 3367 | va_start(args, format); |
| 3368 | g_vsnprintf(buf, sizeof(buf), format, args);
|
| 3369 | va_end(args); |
| 3370 | |
| 3371 | log_show_status_func(buf); |
| 3372 | } |
| 3373 | |
| 3374 | #define TIME_LEN 11 |
| 3375 | |
| 3376 | void log_print(const gchar *format, ...) |
| 3377 | {
|
| 3378 | va_list args; |
| 3379 | gchar buf[BUFFSIZE + TIME_LEN]; |
| 3380 | time_t t; |
| 3381 | |
| 3382 | time(&t); |
| 3383 | strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t)); |
| 3384 | |
| 3385 | va_start(args, format); |
| 3386 | g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args); |
| 3387 | va_end(args); |
| 3388 | |
| 3389 | if (debug_mode) fputs(buf, stdout);
|
| 3390 | log_print_ui_func(buf); |
| 3391 | if (log_fp) {
|
| 3392 | fputs(buf, log_fp); |
| 3393 | fflush(log_fp); |
| 3394 | } |
| 3395 | if (log_verbosity_count)
|
| 3396 | log_show_status_func(buf + TIME_LEN); |
| 3397 | } |
| 3398 | |
| 3399 | void log_message(const gchar *format, ...) |
| 3400 | {
|
| 3401 | va_list args; |
| 3402 | gchar buf[BUFFSIZE + TIME_LEN]; |
| 3403 | time_t t; |
| 3404 | |
| 3405 | time(&t); |
| 3406 | strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t)); |
| 3407 | |
| 3408 | va_start(args, format); |
| 3409 | g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args); |
| 3410 | va_end(args); |
| 3411 | |
| 3412 | if (debug_mode) g_message("%s", buf + TIME_LEN); |
| 3413 | log_message_ui_func(buf + TIME_LEN); |
| 3414 | if (log_fp) {
|
| 3415 | fwrite(buf, TIME_LEN, 1, log_fp);
|
| 3416 | fputs("* message: ", log_fp);
|
| 3417 | fputs(buf + TIME_LEN, log_fp); |
| 3418 | fflush(log_fp); |
| 3419 | } |
| 3420 | log_show_status_func(buf + TIME_LEN); |
| 3421 | } |
| 3422 | |
| 3423 | void log_warning(const gchar *format, ...) |
| 3424 | {
|
| 3425 | va_list args; |
| 3426 | gchar buf[BUFFSIZE + TIME_LEN]; |
| 3427 | time_t t; |
| 3428 | |
| 3429 | time(&t); |
| 3430 | strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t)); |
| 3431 | |
| 3432 | va_start(args, format); |
| 3433 | g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args); |
| 3434 | va_end(args); |
| 3435 | |
| 3436 | g_warning("%s", buf);
|
| 3437 | log_warning_ui_func(buf + TIME_LEN); |
| 3438 | if (log_fp) {
|
| 3439 | fwrite(buf, TIME_LEN, 1, log_fp);
|
| 3440 | fputs("** warning: ", log_fp);
|
| 3441 | fputs(buf + TIME_LEN, log_fp); |
| 3442 | fflush(log_fp); |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | void log_error(const gchar *format, ...) |
| 3447 | {
|
| 3448 | va_list args; |
| 3449 | gchar buf[BUFFSIZE + TIME_LEN]; |
| 3450 | time_t t; |
| 3451 | |
| 3452 | time(&t); |
| 3453 | strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t)); |
| 3454 | |
| 3455 | va_start(args, format); |
| 3456 | g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args); |
| 3457 | va_end(args); |
| 3458 | |
| 3459 | g_warning("%s", buf);
|
| 3460 | log_error_ui_func(buf + TIME_LEN); |
| 3461 | if (log_fp) {
|
| 3462 | fwrite(buf, TIME_LEN, 1, log_fp);
|
| 3463 | fputs("*** error: ", log_fp);
|
| 3464 | fputs(buf + TIME_LEN, log_fp); |
| 3465 | fflush(log_fp); |
| 3466 | } |
| 3467 | } |