masqmail-0.2

diff src/readsock.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/readsock.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,182 @@
     1.4 +/*  MasqMail
     1.5 +    Copyright (C) 2000 Oliver Kurth
     1.6 +
     1.7 +    This program is free software; you can redistribute it and/or modify
     1.8 +    it under the terms of the GNU General Public License as published by
     1.9 +    the Free Software Foundation; either version 2 of the License, or
    1.10 +    (at your option) any later version.
    1.11 +
    1.12 +    This program is distributed in the hope that it will be useful,
    1.13 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 +    GNU General Public License for more details.
    1.16 +
    1.17 +    You should have received a copy of the GNU General Public License
    1.18 +    along with this program; if not, write to the Free Software
    1.19 +    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.20 +*/
    1.21 +
    1.22 +/*#include "masqmail.h"*/
    1.23 +#include <signal.h>
    1.24 +#include <stdio.h>
    1.25 +#include <stdlib.h>
    1.26 +#include <setjmp.h>
    1.27 +#include <unistd.h>
    1.28 +#include <ctype.h>
    1.29 +#include "readsock.h"
    1.30 +
    1.31 +jmp_buf jmp_timeout;
    1.32 +
    1.33 +static
    1.34 +void sig_timeout_handler(int sig)
    1.35 +{
    1.36 +  longjmp(jmp_timeout, 1);
    1.37 +}
    1.38 +
    1.39 +static struct sigaction old_sa_alrm;
    1.40 +
    1.41 +static
    1.42 +void alarm_on(int timeout)
    1.43 +{
    1.44 +  struct sigaction sa;
    1.45 +
    1.46 +  sa.sa_handler = sig_timeout_handler;
    1.47 +  sigemptyset(&(sa.sa_mask));
    1.48 +  sa.sa_flags = 0;
    1.49 +  sigaction(SIGALRM, &sa, &old_sa_alrm);
    1.50 +
    1.51 +  if(timeout > 0)
    1.52 +    alarm(timeout);
    1.53 +}
    1.54 +
    1.55 +static
    1.56 +void alarm_off()
    1.57 +{
    1.58 +  alarm(0);
    1.59 +
    1.60 +  sigaction(SIGALRM, &old_sa_alrm, NULL);
    1.61 +}
    1.62 +
    1.63 +static
    1.64 +void _read_chug(FILE *in)
    1.65 +{
    1.66 +  int c = 0;
    1.67 +
    1.68 +  c = fgetc(in);
    1.69 +  while(isspace(c) && (c != EOF)) c = fgetc(in);
    1.70 +  ungetc(c, in);
    1.71 +}
    1.72 +
    1.73 +static
    1.74 +int _read_line(FILE *in, char *buf, int buf_len, int timeout)
    1.75 +{
    1.76 +  int p = 0;
    1.77 +  int c = 0;
    1.78 +
    1.79 +  c = fgetc(in);
    1.80 +  while((c != '\n') && (c != EOF) && (p < buf_len-1)){
    1.81 +    buf[p++] = c;
    1.82 +    c = fgetc(in);
    1.83 +  }
    1.84 +
    1.85 +  buf[p] = 0;
    1.86 +
    1.87 +  if(c == EOF)
    1.88 +    return -1;
    1.89 +  else if(p >= buf_len){
    1.90 +    ungetc(c, in);
    1.91 +    return -2;
    1.92 +  }
    1.93 +
    1.94 +  buf[p++] = c; /* \n */
    1.95 +  buf[p] = 0;
    1.96 +
    1.97 +  return p;
    1.98 +}
    1.99 +
   1.100 +int read_sockline(FILE *in, char *buf, int buf_len, int timeout, unsigned int flags)
   1.101 +{
   1.102 +  int p = 0;
   1.103 +
   1.104 +  if(setjmp(jmp_timeout) != 0){
   1.105 +    alarm_off();
   1.106 +    return -3;
   1.107 +  }
   1.108 +
   1.109 +  alarm_on(timeout);
   1.110 +
   1.111 +  /* strip leading spaces */
   1.112 +  if(flags & READSOCKL_CHUG){
   1.113 +    _read_chug(in);
   1.114 +  }
   1.115 +
   1.116 +  p = _read_line(in, buf, buf_len, timeout);
   1.117 +
   1.118 +  alarm_off();
   1.119 +
   1.120 +  if(p > 1){
   1.121 +    /* here we are sure that buf[p-1] == '\n' */
   1.122 +    if(flags & READSOCKL_CVT_CRLF){
   1.123 +      if((buf[p-2] == '\r') && (buf[p-1] == '\n')){
   1.124 +	buf[p-2] = '\n';
   1.125 +	buf[p-1] = 0;
   1.126 +	p--;
   1.127 +      }
   1.128 +    }
   1.129 +  }
   1.130 +  return p;
   1.131 +}
   1.132 +
   1.133 +int read_sockline1(FILE *in, char **pbuf, int *buf_len, int timeout, unsigned int flags)
   1.134 +{
   1.135 +  int p = 0, size = *buf_len;
   1.136 +  char *buf;
   1.137 +
   1.138 +  if(setjmp(jmp_timeout) != 0){
   1.139 +    alarm_off();
   1.140 +    return -3;
   1.141 +  }
   1.142 +
   1.143 +  alarm_on(timeout);
   1.144 +
   1.145 +  /* strip leading spaces */
   1.146 +  if(flags & READSOCKL_CHUG){
   1.147 +    _read_chug(in);
   1.148 +  }
   1.149 +
   1.150 +  if(!*pbuf) *pbuf = malloc(size);
   1.151 +  buf = *pbuf;
   1.152 +  
   1.153 +  while(1){
   1.154 +    int pp;
   1.155 +
   1.156 +    pp = _read_line(in, buf, size, timeout);
   1.157 +    if(pp == -2){
   1.158 +      *pbuf = realloc(*pbuf, *buf_len + size);
   1.159 +      buf = *pbuf + *buf_len;
   1.160 +      *buf_len += size;
   1.161 +      p += size;
   1.162 +    }
   1.163 +    else{
   1.164 +      if(pp > 0) p += pp;
   1.165 +      else p = pp;
   1.166 +      break;
   1.167 +    }
   1.168 +  }
   1.169 +
   1.170 +  alarm_off();
   1.171 +
   1.172 +  if(p > 1){
   1.173 +    buf = *pbuf;
   1.174 +    /* here we are sure that buf[p-1] == '\n' */
   1.175 +    if(flags & READSOCKL_CVT_CRLF){
   1.176 +      if((buf[p-2] == '\r') && (buf[p-1] == '\n')){
   1.177 +	buf[p-2] = '\n';
   1.178 +	buf[p-1] = 0;
   1.179 +	p--;
   1.180 +      }
   1.181 +    }
   1.182 +  }
   1.183 +  return p;
   1.184 +}
   1.185 +