/*
   3APA3A simpliest proxy server
   (c) 2002-2003 by ZARAZA <3APA3A@security.nnov.ru>

   please read License Agreement
*/

#include "proxy.h"

int sockgetchar(SOCKET sock, int timeosec, int timeousec){
 unsigned char buf;
 fd_set fds;
 struct timeval tv;

 tv.tv_sec = timeosec;
 tv.tv_usec = timeousec;
 FD_ZERO(&fds);
 FD_SET(sock, &fds);
 if (select (sock+1, &fds, NULL, NULL, &tv)!=1) return EOF;
 if (recv(sock, &buf, 1, 0)!=1) return EOF;
/*
fprintf(stderr, " -%d- ", (int)buf);
fflush(stderr);
*/
 return((int)buf);
}

int sockgetline(SOCKET sock, unsigned char * buf, int bufsize, int delim, int to){
 int c;
 int i=0, tos, tou;
 if(bufsize<2) return 0;
 c = sockgetchar(sock, to, 0);
 if (c == EOF) {
	return 0;
 }
 tos = to/16;
 tou = ((to * 1000) / bufsize)%1000;
 do {
	buf[i++] = c;
	if(delim != EOF && c == delim) break;
 }while(i < bufsize && (c = sockgetchar(sock, tos, tou)) != EOF);
 return i;
}
