2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 gint recv_write_to_file(gint sock, const gchar *filename)
40 g_return_val_if_fail(filename != NULL, -1);
42 if ((fp = fopen(filename, "w")) == NULL) {
43 FILE_OP_ERROR(filename, "fopen");
44 recv_write(sock, NULL);
48 if (change_file_mode_rw(fp, filename) < 0)
49 FILE_OP_ERROR(filename, "chmod");
51 if (recv_write(sock, fp) < 0) {
57 if (fclose(fp) == EOF) {
58 FILE_OP_ERROR(filename, "fclose");
66 gint recv_bytes_write_to_file(gint sock, glong size, const gchar *filename)
70 g_return_val_if_fail(filename != NULL, -1);
72 if ((fp = fopen(filename, "w")) == NULL) {
73 FILE_OP_ERROR(filename, "fopen");
74 recv_write(sock, NULL);
78 if (change_file_mode_rw(fp, filename) < 0)
79 FILE_OP_ERROR(filename, "chmod");
81 if (recv_bytes_write(sock, size, fp) < 0) {
87 if (fclose(fp) == EOF) {
88 FILE_OP_ERROR(filename, "fclose");
96 gint recv_write(gint sock, FILE *fp)
102 nb = sock_is_nonblocking_mode(sock);
103 if (nb) sock_set_nonblocking_mode(sock, FALSE);
106 if (sock_read(sock, buf, sizeof(buf)) < 0) {
107 g_warning(_("error occurred while retrieving data.\n"));
108 if (nb) sock_set_nonblocking_mode(sock, TRUE);
113 if (len > 1 && buf[0] == '.' && buf[1] == '\r') break;
115 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
120 if (buf[0] == '.' && buf[1] == '.')
121 memmove(buf, buf + 1, strlen(buf));
123 if (!strncmp(buf, ">From ", 6))
124 memmove(buf, buf + 1, strlen(buf));
126 if (fp && fputs(buf, fp) == EOF) {
128 g_warning(_("Can't write to file.\n"));
133 if (nb) sock_set_nonblocking_mode(sock, TRUE);
140 gint recv_bytes_write(gint sock, glong size, FILE *fp)
147 nb = sock_is_nonblocking_mode(sock);
148 if (nb) sock_set_nonblocking_mode(sock, FALSE);
150 Xalloca(buf, size, return -1);
155 read_count = read(sock, buf + count, size - count);
156 if (read_count < 0) {
157 if (nb) sock_set_nonblocking_mode(sock, TRUE);
161 } while (count < size);
164 while ((cur = memchr(prev, '\r', size)) != NULL) {
165 if (cur - buf + 1 < size && *(cur + 1) == '\n') {
166 if (fwrite(prev, sizeof(gchar), cur - prev, fp) == EOF ||
167 fwrite("\n", sizeof(gchar), 1, fp) == EOF) {
169 g_warning(_("Can't write to file.\n"));
170 if (nb) sock_set_nonblocking_mode(sock, TRUE);
174 if (prev - buf >= size) break;
178 if (prev - buf < size && fwrite(buf, sizeof(gchar),
179 size - (prev - buf), fp) == EOF) {
181 g_warning(_("Can't write to file.\n"));
182 if (nb) sock_set_nonblocking_mode(sock, TRUE);
186 if (nb) sock_set_nonblocking_mode(sock, TRUE);