Mercurial > masqmail
comparison src/readsock.c @ 10:26e34ae9a3e3
changed indention and line wrapping to a more consistent style
author | meillo@marmaro.de |
---|---|
date | Mon, 27 Oct 2008 16:23:10 +0100 |
parents | 08114f7dcc23 |
children | f671821d8222 |
comparison
equal
deleted
inserted
replaced
9:31cc8a89cb74 | 10:26e34ae9a3e3 |
---|---|
25 #include <ctype.h> | 25 #include <ctype.h> |
26 #include "readsock.h" | 26 #include "readsock.h" |
27 | 27 |
28 jmp_buf jmp_timeout; | 28 jmp_buf jmp_timeout; |
29 | 29 |
30 static | 30 static void |
31 void sig_timeout_handler(int sig) | 31 sig_timeout_handler(int sig) |
32 { | 32 { |
33 longjmp(jmp_timeout, 1); | 33 longjmp(jmp_timeout, 1); |
34 } | 34 } |
35 | 35 |
36 static struct sigaction old_sa_alrm; | 36 static struct sigaction old_sa_alrm; |
37 | 37 |
38 static | 38 static void |
39 void alarm_on(int timeout) | 39 alarm_on(int timeout) |
40 { | 40 { |
41 struct sigaction sa; | 41 struct sigaction sa; |
42 | 42 |
43 sa.sa_handler = sig_timeout_handler; | 43 sa.sa_handler = sig_timeout_handler; |
44 sigemptyset(&(sa.sa_mask)); | 44 sigemptyset(&(sa.sa_mask)); |
45 sa.sa_flags = 0; | 45 sa.sa_flags = 0; |
46 sigaction(SIGALRM, &sa, &old_sa_alrm); | 46 sigaction(SIGALRM, &sa, &old_sa_alrm); |
47 | 47 |
48 if(timeout > 0) | 48 if (timeout > 0) |
49 alarm(timeout); | 49 alarm(timeout); |
50 } | 50 } |
51 | 51 |
52 static | 52 static void |
53 void alarm_off() | 53 alarm_off() |
54 { | 54 { |
55 alarm(0); | 55 alarm(0); |
56 | 56 |
57 sigaction(SIGALRM, &old_sa_alrm, NULL); | 57 sigaction(SIGALRM, &old_sa_alrm, NULL); |
58 } | 58 } |
59 | 59 |
60 static | 60 static void |
61 void _read_chug(FILE *in) | 61 _read_chug(FILE * in) |
62 { | 62 { |
63 int c = 0; | 63 int c = 0; |
64 | 64 |
65 c = fgetc(in); | 65 c = fgetc(in); |
66 while(isspace(c) && (c != EOF)) c = fgetc(in); | 66 while (isspace(c) && (c != EOF)) |
67 ungetc(c, in); | 67 c = fgetc(in); |
68 ungetc(c, in); | |
68 } | 69 } |
69 | 70 |
70 static | 71 static int |
71 int _read_line(FILE *in, char *buf, int buf_len, int timeout) | 72 _read_line(FILE * in, char *buf, int buf_len, int timeout) |
72 { | 73 { |
73 int p = 0; | 74 int p = 0; |
74 int c = 0; | 75 int c = 0; |
75 | 76 |
76 c = fgetc(in); | 77 c = fgetc(in); |
77 while((c != '\n') && (c != EOF) && (p < buf_len-1)){ | 78 while ((c != '\n') && (c != EOF) && (p < buf_len - 1)) { |
78 buf[p++] = c; | 79 buf[p++] = c; |
79 c = fgetc(in); | 80 c = fgetc(in); |
80 } | 81 } |
81 | 82 |
82 buf[p] = 0; | 83 buf[p] = 0; |
83 | 84 |
84 if(c == EOF) | 85 if (c == EOF) |
85 return -1; | 86 return -1; |
86 else if(p >= buf_len){ | 87 else if (p >= buf_len) { |
87 ungetc(c, in); | 88 ungetc(c, in); |
88 return -2; | 89 return -2; |
89 } | 90 } |
90 | 91 |
91 buf[p++] = c; /* \n */ | 92 buf[p++] = c; /* \n */ |
92 buf[p] = 0; | 93 buf[p] = 0; |
93 | 94 |
94 return p; | 95 return p; |
95 } | 96 } |
96 | 97 |
97 int read_sockline(FILE *in, char *buf, int buf_len, int timeout, unsigned int flags) | 98 int |
99 read_sockline(FILE * in, char *buf, int buf_len, int timeout, unsigned int flags) | |
98 { | 100 { |
99 int p = 0; | 101 int p = 0; |
100 | 102 |
101 if(setjmp(jmp_timeout) != 0){ | 103 if (setjmp(jmp_timeout) != 0) { |
102 alarm_off(); | 104 alarm_off(); |
103 return -3; | 105 return -3; |
104 } | 106 } |
105 | 107 |
106 alarm_on(timeout); | 108 alarm_on(timeout); |
107 | 109 |
108 /* strip leading spaces */ | 110 /* strip leading spaces */ |
109 if(flags & READSOCKL_CHUG){ | 111 if (flags & READSOCKL_CHUG) { |
110 _read_chug(in); | 112 _read_chug(in); |
111 } | 113 } |
112 | 114 |
113 p = _read_line(in, buf, buf_len, timeout); | 115 p = _read_line(in, buf, buf_len, timeout); |
114 | 116 |
115 alarm_off(); | 117 alarm_off(); |
116 | 118 |
117 if(p > 1){ | 119 if (p > 1) { |
118 /* here we are sure that buf[p-1] == '\n' */ | 120 /* here we are sure that buf[p-1] == '\n' */ |
119 if(flags & READSOCKL_CVT_CRLF){ | 121 if (flags & READSOCKL_CVT_CRLF) { |
120 if((buf[p-2] == '\r') && (buf[p-1] == '\n')){ | 122 if ((buf[p - 2] == '\r') && (buf[p - 1] == '\n')) { |
121 buf[p-2] = '\n'; | 123 buf[p - 2] = '\n'; |
122 buf[p-1] = 0; | 124 buf[p - 1] = 0; |
123 p--; | 125 p--; |
124 } | 126 } |
125 } | 127 } |
126 } | 128 } |
127 return p; | 129 return p; |
128 } | 130 } |
129 | 131 |
130 int read_sockline1(FILE *in, char **pbuf, int *buf_len, int timeout, unsigned int flags) | 132 int |
133 read_sockline1(FILE * in, char **pbuf, int *buf_len, int timeout, unsigned int flags) | |
131 { | 134 { |
132 int p = 0, size = *buf_len; | 135 int p = 0, size = *buf_len; |
133 char *buf; | 136 char *buf; |
134 | 137 |
135 if(setjmp(jmp_timeout) != 0){ | 138 if (setjmp(jmp_timeout) != 0) { |
136 alarm_off(); | 139 alarm_off(); |
137 return -3; | 140 return -3; |
138 } | 141 } |
139 | 142 |
140 alarm_on(timeout); | 143 alarm_on(timeout); |
141 | 144 |
142 /* strip leading spaces */ | 145 /* strip leading spaces */ |
143 if(flags & READSOCKL_CHUG){ | 146 if (flags & READSOCKL_CHUG) { |
144 _read_chug(in); | 147 _read_chug(in); |
145 } | 148 } |
146 | 149 |
147 if(!*pbuf) *pbuf = malloc(size); | 150 if (!*pbuf) |
148 buf = *pbuf; | 151 *pbuf = malloc(size); |
149 | 152 buf = *pbuf; |
150 while(1){ | |
151 int pp; | |
152 | 153 |
153 pp = _read_line(in, buf, size, timeout); | 154 while (1) { |
154 if(pp == -2){ | 155 int pp; |
155 *pbuf = realloc(*pbuf, *buf_len + size); | |
156 buf = *pbuf + *buf_len; | |
157 *buf_len += size; | |
158 p += size; | |
159 } | |
160 else{ | |
161 if(pp > 0) p += pp; | |
162 else p = pp; | |
163 break; | |
164 } | |
165 } | |
166 | 156 |
167 alarm_off(); | 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 } | |
168 | 171 |
169 if(p > 1){ | 172 alarm_off(); |
170 buf = *pbuf; | 173 |
171 /* here we are sure that buf[p-1] == '\n' */ | 174 if (p > 1) { |
172 if(flags & READSOCKL_CVT_CRLF){ | 175 buf = *pbuf; |
173 if((buf[p-2] == '\r') && (buf[p-1] == '\n')){ | 176 /* here we are sure that buf[p-1] == '\n' */ |
174 buf[p-2] = '\n'; | 177 if (flags & READSOCKL_CVT_CRLF) { |
175 buf[p-1] = 0; | 178 if ((buf[p - 2] == '\r') && (buf[p - 1] == '\n')) { |
176 p--; | 179 buf[p - 2] = '\n'; |
177 } | 180 buf[p - 1] = 0; |
178 } | 181 p--; |
179 } | 182 } |
180 return p; | 183 } |
184 } | |
185 return p; | |
181 } | 186 } |
182 |