masqmail

view src/readsock.c @ 368:b35c17fcf69d

Added cast to avoid compiler warning.
author markus schnalke <meillo@marmaro.de>
date Thu, 20 Oct 2011 10:22:40 +0200
parents b27f66555ba8
children 9bc3e47b0222
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 2000 Oliver Kurth
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 */
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <setjmp.h>
24 #include <unistd.h>
25 #include <ctype.h>
27 #include "readsock.h"
28 /*#include "masqmail.h"*/
30 jmp_buf jmp_timeout;
32 static void
33 sig_timeout_handler(int sig)
34 {
35 longjmp(jmp_timeout, 1);
36 }
38 static struct sigaction old_sa_alrm;
40 static void
41 alarm_on(int timeout)
42 {
43 struct sigaction sa;
45 sa.sa_handler = sig_timeout_handler;
46 sigemptyset(&(sa.sa_mask));
47 sa.sa_flags = 0;
48 sigaction(SIGALRM, &sa, &old_sa_alrm);
50 if (timeout > 0)
51 alarm(timeout);
52 }
54 static void
55 alarm_off()
56 {
57 alarm(0);
59 sigaction(SIGALRM, &old_sa_alrm, NULL);
60 }
62 static void
63 _read_chug(FILE *in)
64 {
65 int c = 0;
67 c = fgetc(in);
68 while (isspace(c) && (c != EOF))
69 c = fgetc(in);
70 ungetc(c, in);
71 }
73 static int
74 _read_line(FILE *in, char *buf, int buf_len, int timeout)
75 {
76 int p = 0;
77 int c = 0;
79 c = fgetc(in);
80 while ((c != '\n') && (c != EOF) && (p < buf_len - 1)) {
81 buf[p++] = c;
82 c = fgetc(in);
83 }
85 buf[p] = '\0';
87 if (c == EOF)
88 return -1;
89 else if (p >= buf_len) {
90 ungetc(c, in);
91 return -2;
92 }
94 buf[p++] = c; /* \n */
95 buf[p] = '\0';
97 return p;
98 }
100 int
101 read_sockline(FILE *in, char *buf, int buf_len, int timeout, unsigned int flags)
102 {
103 int p = 0;
105 if (setjmp(jmp_timeout) != 0) {
106 alarm_off();
107 return -3;
108 }
110 alarm_on(timeout);
112 /* strip leading spaces */
113 if (flags & READSOCKL_CHUG) {
114 _read_chug(in);
115 }
117 p = _read_line(in, buf, buf_len, timeout);
119 alarm_off();
121 if (p > 1) {
122 /* here we are sure that buf[p-1] == '\n' */
123 if (flags & READSOCKL_CVT_CRLF) {
124 if ((buf[p - 2] == '\r') && (buf[p - 1] == '\n')) {
125 buf[p - 2] = '\n';
126 buf[p - 1] = 0;
127 p--;
128 }
129 }
130 }
131 return p;
132 }
134 int
135 read_sockline1(FILE *in, char **pbuf, int *buf_len, int timeout,
136 unsigned int flags)
137 {
138 int p = 0, size = *buf_len;
139 char *buf;
141 if (setjmp(jmp_timeout) != 0) {
142 alarm_off();
143 return -3;
144 }
146 alarm_on(timeout);
148 /* strip leading spaces */
149 if (flags & READSOCKL_CHUG) {
150 _read_chug(in);
151 }
153 if (!*pbuf)
154 *pbuf = (char *) g_malloc(size);
155 buf = *pbuf;
157 while (1) {
158 int pp;
160 pp = _read_line(in, buf, size, timeout);
161 if (pp == -2) {
162 *pbuf = realloc(*pbuf, *buf_len + size);
163 buf = *pbuf + *buf_len;
164 *buf_len += size;
165 p += size;
166 } else {
167 if (pp > 0)
168 p += pp;
169 else
170 p = pp;
171 break;
172 }
173 }
175 alarm_off();
177 if (p > 1) {
178 buf = *pbuf;
179 /* here we are sure that buf[p-1] == '\n' */
180 if (flags & READSOCKL_CVT_CRLF) {
181 if ((buf[p - 2] == '\r') && (buf[p - 1] == '\n')) {
182 buf[p - 2] = '\n';
183 buf[p - 1] = '\0';
184 p--;
185 }
186 }
187 }
188 return p;
189 }