rev |
line source |
meillo@0
|
1 /* MasqMail
|
meillo@0
|
2 Copyright (C) 2000 Oliver Kurth
|
meillo@0
|
3
|
meillo@0
|
4 This program is free software; you can redistribute it and/or modify
|
meillo@0
|
5 it under the terms of the GNU General Public License as published by
|
meillo@0
|
6 the Free Software Foundation; either version 2 of the License, or
|
meillo@0
|
7 (at your option) any later version.
|
meillo@0
|
8
|
meillo@0
|
9 This program is distributed in the hope that it will be useful,
|
meillo@0
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
meillo@0
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
meillo@0
|
12 GNU General Public License for more details.
|
meillo@0
|
13
|
meillo@0
|
14 You should have received a copy of the GNU General Public License
|
meillo@0
|
15 along with this program; if not, write to the Free Software
|
meillo@0
|
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
meillo@0
|
17 */
|
meillo@0
|
18
|
meillo@0
|
19 /*#include "masqmail.h"*/
|
meillo@0
|
20 #include <signal.h>
|
meillo@0
|
21 #include <stdio.h>
|
meillo@0
|
22 #include <stdlib.h>
|
meillo@0
|
23 #include <setjmp.h>
|
meillo@0
|
24 #include <unistd.h>
|
meillo@0
|
25 #include <ctype.h>
|
meillo@0
|
26 #include "readsock.h"
|
meillo@0
|
27
|
meillo@0
|
28 jmp_buf jmp_timeout;
|
meillo@0
|
29
|
meillo@10
|
30 static void
|
meillo@10
|
31 sig_timeout_handler(int sig)
|
meillo@0
|
32 {
|
meillo@10
|
33 longjmp(jmp_timeout, 1);
|
meillo@0
|
34 }
|
meillo@0
|
35
|
meillo@0
|
36 static struct sigaction old_sa_alrm;
|
meillo@0
|
37
|
meillo@10
|
38 static void
|
meillo@10
|
39 alarm_on(int timeout)
|
meillo@0
|
40 {
|
meillo@10
|
41 struct sigaction sa;
|
meillo@0
|
42
|
meillo@10
|
43 sa.sa_handler = sig_timeout_handler;
|
meillo@10
|
44 sigemptyset(&(sa.sa_mask));
|
meillo@10
|
45 sa.sa_flags = 0;
|
meillo@10
|
46 sigaction(SIGALRM, &sa, &old_sa_alrm);
|
meillo@0
|
47
|
meillo@10
|
48 if (timeout > 0)
|
meillo@10
|
49 alarm(timeout);
|
meillo@0
|
50 }
|
meillo@0
|
51
|
meillo@10
|
52 static void
|
meillo@10
|
53 alarm_off()
|
meillo@0
|
54 {
|
meillo@10
|
55 alarm(0);
|
meillo@0
|
56
|
meillo@10
|
57 sigaction(SIGALRM, &old_sa_alrm, NULL);
|
meillo@0
|
58 }
|
meillo@0
|
59
|
meillo@10
|
60 static void
|
meillo@10
|
61 _read_chug(FILE * in)
|
meillo@0
|
62 {
|
meillo@10
|
63 int c = 0;
|
meillo@0
|
64
|
meillo@10
|
65 c = fgetc(in);
|
meillo@10
|
66 while (isspace(c) && (c != EOF))
|
meillo@10
|
67 c = fgetc(in);
|
meillo@10
|
68 ungetc(c, in);
|
meillo@0
|
69 }
|
meillo@0
|
70
|
meillo@10
|
71 static int
|
meillo@10
|
72 _read_line(FILE * in, char *buf, int buf_len, int timeout)
|
meillo@0
|
73 {
|
meillo@10
|
74 int p = 0;
|
meillo@10
|
75 int c = 0;
|
meillo@0
|
76
|
meillo@10
|
77 c = fgetc(in);
|
meillo@10
|
78 while ((c != '\n') && (c != EOF) && (p < buf_len - 1)) {
|
meillo@10
|
79 buf[p++] = c;
|
meillo@10
|
80 c = fgetc(in);
|
meillo@10
|
81 }
|
meillo@0
|
82
|
meillo@10
|
83 buf[p] = 0;
|
meillo@0
|
84
|
meillo@10
|
85 if (c == EOF)
|
meillo@10
|
86 return -1;
|
meillo@10
|
87 else if (p >= buf_len) {
|
meillo@10
|
88 ungetc(c, in);
|
meillo@10
|
89 return -2;
|
meillo@10
|
90 }
|
meillo@0
|
91
|
meillo@10
|
92 buf[p++] = c; /* \n */
|
meillo@10
|
93 buf[p] = 0;
|
meillo@0
|
94
|
meillo@10
|
95 return p;
|
meillo@0
|
96 }
|
meillo@0
|
97
|
meillo@10
|
98 int
|
meillo@10
|
99 read_sockline(FILE * in, char *buf, int buf_len, int timeout, unsigned int flags)
|
meillo@0
|
100 {
|
meillo@10
|
101 int p = 0;
|
meillo@0
|
102
|
meillo@10
|
103 if (setjmp(jmp_timeout) != 0) {
|
meillo@10
|
104 alarm_off();
|
meillo@10
|
105 return -3;
|
meillo@10
|
106 }
|
meillo@0
|
107
|
meillo@10
|
108 alarm_on(timeout);
|
meillo@0
|
109
|
meillo@10
|
110 /* strip leading spaces */
|
meillo@10
|
111 if (flags & READSOCKL_CHUG) {
|
meillo@10
|
112 _read_chug(in);
|
meillo@10
|
113 }
|
meillo@0
|
114
|
meillo@10
|
115 p = _read_line(in, buf, buf_len, timeout);
|
meillo@0
|
116
|
meillo@10
|
117 alarm_off();
|
meillo@0
|
118
|
meillo@10
|
119 if (p > 1) {
|
meillo@10
|
120 /* here we are sure that buf[p-1] == '\n' */
|
meillo@10
|
121 if (flags & READSOCKL_CVT_CRLF) {
|
meillo@10
|
122 if ((buf[p - 2] == '\r') && (buf[p - 1] == '\n')) {
|
meillo@10
|
123 buf[p - 2] = '\n';
|
meillo@10
|
124 buf[p - 1] = 0;
|
meillo@10
|
125 p--;
|
meillo@10
|
126 }
|
meillo@10
|
127 }
|
meillo@10
|
128 }
|
meillo@10
|
129 return p;
|
meillo@0
|
130 }
|
meillo@0
|
131
|
meillo@10
|
132 int
|
meillo@10
|
133 read_sockline1(FILE * in, char **pbuf, int *buf_len, int timeout, unsigned int flags)
|
meillo@0
|
134 {
|
meillo@10
|
135 int p = 0, size = *buf_len;
|
meillo@10
|
136 char *buf;
|
meillo@0
|
137
|
meillo@10
|
138 if (setjmp(jmp_timeout) != 0) {
|
meillo@10
|
139 alarm_off();
|
meillo@10
|
140 return -3;
|
meillo@10
|
141 }
|
meillo@0
|
142
|
meillo@10
|
143 alarm_on(timeout);
|
meillo@0
|
144
|
meillo@10
|
145 /* strip leading spaces */
|
meillo@10
|
146 if (flags & READSOCKL_CHUG) {
|
meillo@10
|
147 _read_chug(in);
|
meillo@10
|
148 }
|
meillo@0
|
149
|
meillo@10
|
150 if (!*pbuf)
|
meillo@10
|
151 *pbuf = malloc(size);
|
meillo@10
|
152 buf = *pbuf;
|
meillo@0
|
153
|
meillo@10
|
154 while (1) {
|
meillo@10
|
155 int pp;
|
meillo@0
|
156
|
meillo@10
|
157 pp = _read_line(in, buf, size, timeout);
|
meillo@10
|
158 if (pp == -2) {
|
meillo@10
|
159 *pbuf = realloc(*pbuf, *buf_len + size);
|
meillo@10
|
160 buf = *pbuf + *buf_len;
|
meillo@10
|
161 *buf_len += size;
|
meillo@10
|
162 p += size;
|
meillo@10
|
163 } else {
|
meillo@10
|
164 if (pp > 0)
|
meillo@10
|
165 p += pp;
|
meillo@10
|
166 else
|
meillo@10
|
167 p = pp;
|
meillo@10
|
168 break;
|
meillo@10
|
169 }
|
meillo@10
|
170 }
|
meillo@0
|
171
|
meillo@10
|
172 alarm_off();
|
meillo@10
|
173
|
meillo@10
|
174 if (p > 1) {
|
meillo@10
|
175 buf = *pbuf;
|
meillo@10
|
176 /* here we are sure that buf[p-1] == '\n' */
|
meillo@10
|
177 if (flags & READSOCKL_CVT_CRLF) {
|
meillo@10
|
178 if ((buf[p - 2] == '\r') && (buf[p - 1] == '\n')) {
|
meillo@10
|
179 buf[p - 2] = '\n';
|
meillo@10
|
180 buf[p - 1] = 0;
|
meillo@10
|
181 p--;
|
meillo@10
|
182 }
|
meillo@10
|
183 }
|
meillo@10
|
184 }
|
meillo@10
|
185 return p;
|
meillo@0
|
186 }
|