libpurple/util.c

changeset 21453
a90cf329cd48
parent 21264
0fb8b497d31b
parent 21451
ffa5c6863537
child 21468
4a0917337f8f
equal deleted inserted replaced
21273:2f91b326b3f0 21453:a90cf329cd48
1535 if(alt) { 1535 if(alt) {
1536 if(plain) 1536 if(plain)
1537 plain = g_string_append(plain, alt->str); 1537 plain = g_string_append(plain, alt->str);
1538 if(!src && xhtml) 1538 if(!src && xhtml)
1539 xhtml = g_string_append(xhtml, alt->str); 1539 xhtml = g_string_append(xhtml, alt->str);
1540 g_string_free(alt, TRUE);
1540 } 1541 }
1541 g_string_free(alt, TRUE);
1542 g_string_free(src, TRUE); 1542 g_string_free(src, TRUE);
1543 continue; 1543 continue;
1544 } 1544 }
1545 if(!g_ascii_strncasecmp(c, "<a", 2) && (*(c+2) == '>' || *(c+2) == ' ')) { 1545 if(!g_ascii_strncasecmp(c, "<a", 2) && (*(c+2) == '>' || *(c+2) == ' ')) {
1546 const char *p = c; 1546 const char *p = c;
1569 pt = g_new0(struct purple_parse_tag, 1); 1569 pt = g_new0(struct purple_parse_tag, 1);
1570 pt->src_tag = "a"; 1570 pt->src_tag = "a";
1571 pt->dest_tag = "a"; 1571 pt->dest_tag = "a";
1572 tags = g_list_prepend(tags, pt); 1572 tags = g_list_prepend(tags, pt);
1573 if(xhtml) 1573 if(xhtml)
1574 g_string_append_printf(xhtml, "<a href='%s'>", g_strstrip(url->str)); 1574 g_string_append_printf(xhtml, "<a href='%s'>", url ? g_strstrip(url->str) : "");
1575 continue; 1575 continue;
1576 } 1576 }
1577 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) { 1577 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) {
1578 const char *p = c; 1578 const char *p = c;
1579 GString *style = g_string_new(""); 1579 GString *style = g_string_new("");
1662 c = p; 1662 c = p;
1663 pt = g_new0(struct purple_parse_tag, 1); 1663 pt = g_new0(struct purple_parse_tag, 1);
1664 pt->src_tag = "font"; 1664 pt->src_tag = "font";
1665 pt->dest_tag = "span"; 1665 pt->dest_tag = "span";
1666 tags = g_list_prepend(tags, pt); 1666 tags = g_list_prepend(tags, pt);
1667 if(style->len) 1667 if(style->len && xhtml)
1668 g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str)); 1668 g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str));
1669 else 1669 else
1670 pt->ignore = TRUE; 1670 pt->ignore = TRUE;
1671 g_string_free(style, TRUE); 1671 g_string_free(style, TRUE);
1672 continue; 1672 continue;
2529 g_free(dir); 2529 g_free(dir);
2530 return -1; 2530 return -1;
2531 } 2531 }
2532 2532
2533 if (g_mkdir(dir, mode) < 0) { 2533 if (g_mkdir(dir, mode) < 0) {
2534 purple_debug_warning("build_dir", "mkdir: %s\n", strerror(errno)); 2534 purple_debug_warning("build_dir", "mkdir: %s\n", g_strerror(errno));
2535 g_strfreev(components); 2535 g_strfreev(components);
2536 g_free(dir); 2536 g_free(dir);
2537 return -1; 2537 return -1;
2538 } 2538 }
2539 } 2539 }
2565 if (!g_file_test(user_dir, G_FILE_TEST_IS_DIR)) 2565 if (!g_file_test(user_dir, G_FILE_TEST_IS_DIR))
2566 { 2566 {
2567 if (g_mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1) 2567 if (g_mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
2568 { 2568 {
2569 purple_debug_error("util", "Error creating directory %s: %s\n", 2569 purple_debug_error("util", "Error creating directory %s: %s\n",
2570 user_dir, strerror(errno)); 2570 user_dir, g_strerror(errno));
2571 return FALSE; 2571 return FALSE;
2572 } 2572 }
2573 } 2573 }
2574 2574
2575 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", user_dir, filename); 2575 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", user_dir, filename);
2585 { 2585 {
2586 gchar *filename_temp; 2586 gchar *filename_temp;
2587 FILE *file; 2587 FILE *file;
2588 size_t real_size, byteswritten; 2588 size_t real_size, byteswritten;
2589 struct stat st; 2589 struct stat st;
2590 #ifndef HAVE_FILENO
2591 int fd;
2592 #endif
2590 2593
2591 purple_debug_info("util", "Writing file %s\n", 2594 purple_debug_info("util", "Writing file %s\n",
2592 filename_full); 2595 filename_full);
2593 2596
2594 g_return_val_if_fail((size >= -1), FALSE); 2597 g_return_val_if_fail((size >= -1), FALSE);
2600 { 2603 {
2601 if (g_unlink(filename_temp) == -1) 2604 if (g_unlink(filename_temp) == -1)
2602 { 2605 {
2603 purple_debug_error("util", "Error removing old file " 2606 purple_debug_error("util", "Error removing old file "
2604 "%s: %s\n", 2607 "%s: %s\n",
2605 filename_temp, strerror(errno)); 2608 filename_temp, g_strerror(errno));
2606 } 2609 }
2607 } 2610 }
2608 2611
2609 /* Open file */ 2612 /* Open file */
2610 file = g_fopen(filename_temp, "wb"); 2613 file = g_fopen(filename_temp, "wb");
2611 if (file == NULL) 2614 if (file == NULL)
2612 { 2615 {
2613 purple_debug_error("util", "Error opening file %s for " 2616 purple_debug_error("util", "Error opening file %s for "
2614 "writing: %s\n", 2617 "writing: %s\n",
2615 filename_temp, strerror(errno)); 2618 filename_temp, g_strerror(errno));
2616 g_free(filename_temp); 2619 g_free(filename_temp);
2617 return FALSE; 2620 return FALSE;
2618 } 2621 }
2619 2622
2620 /* Write to file */ 2623 /* Write to file */
2621 real_size = (size == -1) ? strlen(data) : (size_t) size; 2624 real_size = (size == -1) ? strlen(data) : (size_t) size;
2622 byteswritten = fwrite(data, 1, real_size, file); 2625 byteswritten = fwrite(data, 1, real_size, file);
2623 2626
2627 #ifdef HAVE_FILENO
2628 /* Apparently XFS (and possibly other filesystems) do not
2629 * guarantee that file data is flushed before file metadata,
2630 * so this procedure is insufficient without some flushage. */
2631 if (fflush(file) < 0) {
2632 purple_debug_error("util", "Error flushing %s: %s\n",
2633 filename_temp, g_strerror(errno));
2634 g_free(filename_temp);
2635 fclose(file);
2636 return FALSE;
2637 }
2638 if (fsync(fileno(file)) < 0) {
2639 purple_debug_error("util", "Error syncing file contents for %s: %s\n",
2640 filename_temp, g_strerror(errno));
2641 g_free(filename_temp);
2642 fclose(file);
2643 return FALSE;
2644 }
2645 #endif
2646
2624 /* Close file */ 2647 /* Close file */
2625 if (fclose(file) != 0) 2648 if (fclose(file) != 0)
2626 { 2649 {
2627 purple_debug_error("util", "Error closing file %s: %s\n", 2650 purple_debug_error("util", "Error closing file %s: %s\n",
2628 filename_temp, strerror(errno)); 2651 filename_temp, g_strerror(errno));
2629 g_free(filename_temp); 2652 g_free(filename_temp);
2630 return FALSE; 2653 return FALSE;
2631 } 2654 }
2655
2656 #ifndef HAVE_FILENO
2657 /* This is the same effect (we hope) as the HAVE_FILENO block
2658 * above, but for systems without fileno(). */
2659 if ((fd = open(filename_temp, O_RDWR)) < 0) {
2660 purple_debug_error("util", "Error opening file %s for flush: %s\n",
2661 filename_temp, g_strerror(errno));
2662 g_free(filename_temp);
2663 return FALSE;
2664 }
2665 if (fsync(fd) < 0) {
2666 purple_debug_error("util", "Error syncing %s: %s\n",
2667 filename_temp, g_strerror(errno));
2668 g_free(filename_temp);
2669 close(fd);
2670 return FALSE;
2671 }
2672 if (close(fd) < 0) {
2673 purple_debug_error("util", "Error closing %s after sync: %s\n",
2674 filename_temp, g_strerror(errno));
2675 g_free(filename_temp);
2676 return FALSE;
2677 }
2678 #endif
2632 2679
2633 /* Ensure the file is the correct size */ 2680 /* Ensure the file is the correct size */
2634 if (byteswritten != real_size) 2681 if (byteswritten != real_size)
2635 { 2682 {
2636 purple_debug_error("util", "Error writing to file %s: Wrote %" 2683 purple_debug_error("util", "Error writing to file %s: Wrote %"
2655 #ifndef _WIN32 2702 #ifndef _WIN32
2656 /* Set file permissions */ 2703 /* Set file permissions */
2657 if (chmod(filename_temp, S_IRUSR | S_IWUSR) == -1) 2704 if (chmod(filename_temp, S_IRUSR | S_IWUSR) == -1)
2658 { 2705 {
2659 purple_debug_error("util", "Error setting permissions of file %s: %s\n", 2706 purple_debug_error("util", "Error setting permissions of file %s: %s\n",
2660 filename_temp, strerror(errno)); 2707 filename_temp, g_strerror(errno));
2661 } 2708 }
2662 #endif 2709 #endif
2663 2710
2664 /* Rename to the REAL name */ 2711 /* Rename to the REAL name */
2665 if (g_rename(filename_temp, filename_full) == -1) 2712 if (g_rename(filename_temp, filename_full) == -1)
2666 { 2713 {
2667 purple_debug_error("util", "Error renaming %s to %s: %s\n", 2714 purple_debug_error("util", "Error renaming %s to %s: %s\n",
2668 filename_temp, filename_full, 2715 filename_temp, filename_full,
2669 strerror(errno)); 2716 g_strerror(errno));
2670 } 2717 }
2671 2718
2672 g_free(filename_temp); 2719 g_free(filename_temp);
2673 2720
2674 return TRUE; 2721 return TRUE;
3091 if (text[i] == '\n' && text[i - 1] != '\r') 3138 if (text[i] == '\n' && text[i - 1] != '\r')
3092 ret[j++] = '\r'; 3139 ret[j++] = '\r';
3093 ret[j++] = text[i]; 3140 ret[j++] = text[i];
3094 } 3141 }
3095 3142
3096 purple_debug_misc("purple_str_add_cr", "got: %s, leaving with %s\n",
3097 text, ret);
3098
3099 return ret; 3143 return ret;
3100 } 3144 }
3101 3145
3102 void 3146 void
3103 purple_str_strip_char(char *text, char thechar) 3147 purple_str_strip_char(char *text, char thechar)
3223 } 3267 }
3224 3268
3225 char * 3269 char *
3226 purple_str_size_to_units(size_t size) 3270 purple_str_size_to_units(size_t size)
3227 { 3271 {
3228 static const char *size_str[4] = { "bytes", "KiB", "MiB", "GiB" }; 3272 static const char * const size_str[] = { "bytes", "KiB", "MiB", "GiB" };
3229 float size_mag; 3273 float size_mag;
3230 int size_index = 0; 3274 int size_index = 0;
3231 3275
3232 if (size == -1) { 3276 if (size == -1) {
3233 return g_strdup(_("Calculating...")); 3277 return g_strdup(_("Calculating..."));
3416 const char *at, *slash; 3460 const char *at, *slash;
3417 const char *turl; 3461 const char *turl;
3418 char host[256], path[256], user[256], passwd[256]; 3462 char host[256], path[256], user[256], passwd[256];
3419 int port = 0; 3463 int port = 0;
3420 /* hyphen at end includes it in control set */ 3464 /* hyphen at end includes it in control set */
3421 static char addr_ctrl[] = "A-Za-z0-9.-"; 3465 static const char addr_ctrl[] = "A-Za-z0-9.-";
3422 static char port_ctrl[] = "0-9"; 3466 static const char port_ctrl[] = "0-9";
3423 static char page_ctrl[] = "A-Za-z0-9.~_/:*!@&%%?=+^-"; 3467 static const char page_ctrl[] = "A-Za-z0-9.~_/:*!@&%%?=+^-";
3424 static char user_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-"; 3468 static const char user_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-";
3425 static char passwd_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-"; 3469 static const char passwd_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-";
3426 3470
3427 g_return_val_if_fail(url != NULL, FALSE); 3471 g_return_val_if_fail(url != NULL, FALSE);
3428 3472
3429 if ((turl = purple_strcasestr(url, "http://")) != NULL) 3473 if ((turl = purple_strcasestr(url, "http://")) != NULL)
3430 { 3474 {
3517 gchar *s; 3561 gchar *s;
3518 gchar *new_url, *temp_url, *end; 3562 gchar *new_url, *temp_url, *end;
3519 gboolean full; 3563 gboolean full;
3520 int len; 3564 int len;
3521 3565
3522 if ((s = g_strstr_len(data, data_len, "Location: ")) == NULL) 3566 if ((s = g_strstr_len(data, data_len, "\nLocation: ")) == NULL)
3523 /* We're not being redirected */ 3567 /* We're not being redirected */
3524 return FALSE; 3568 return FALSE;
3525 3569
3526 s += strlen("Location: "); 3570 s += strlen("Location: ");
3527 end = strchr(s, '\r'); 3571 end = strchr(s, '\r');
3715 3759
3716 new_data = g_try_malloc(content_len); 3760 new_data = g_try_malloc(content_len);
3717 if(new_data == NULL) { 3761 if(new_data == NULL) {
3718 purple_debug_error("util", 3762 purple_debug_error("util",
3719 "Failed to allocate %u bytes: %s\n", 3763 "Failed to allocate %u bytes: %s\n",
3720 content_len, strerror(errno)); 3764 content_len, g_strerror(errno));
3721 purple_util_fetch_url_error(gfud, 3765 purple_util_fetch_url_error(gfud,
3722 _("Unable to allocate enough memory to hold " 3766 _("Unable to allocate enough memory to hold "
3723 "the contents from %s. The web server may " 3767 "the contents from %s. The web server may "
3724 "be trying something malicious."), 3768 "be trying something malicious."),
3725 gfud->website.address); 3769 gfud->website.address);
3753 if(len < 0) { 3797 if(len < 0) {
3754 if(errno == EAGAIN) { 3798 if(errno == EAGAIN) {
3755 return; 3799 return;
3756 } else { 3800 } else {
3757 purple_util_fetch_url_error(gfud, _("Error reading from %s: %s"), 3801 purple_util_fetch_url_error(gfud, _("Error reading from %s: %s"),
3758 gfud->website.address, strerror(errno)); 3802 gfud->website.address, g_strerror(errno));
3759 return; 3803 return;
3760 } 3804 }
3761 } 3805 }
3762 3806
3763 if((len == 0) || got_eof) { 3807 if((len == 0) || got_eof) {
3784 3828
3785 if (len < 0 && errno == EAGAIN) 3829 if (len < 0 && errno == EAGAIN)
3786 return; 3830 return;
3787 else if (len < 0) { 3831 else if (len < 0) {
3788 purple_util_fetch_url_error(gfud, _("Error writing to %s: %s"), 3832 purple_util_fetch_url_error(gfud, _("Error writing to %s: %s"),
3789 gfud->website.address, strerror(errno)); 3833 gfud->website.address, g_strerror(errno));
3790 return; 3834 return;
3791 } 3835 }
3792 gfud->request_written += len; 3836 gfud->request_written += len;
3793 3837
3794 if (gfud->request_written < total_len) 3838 if (gfud->request_written < total_len)

mercurial