masqmail-0.2

view src/readsock.c @ 12:9fb7ddbaf129

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