


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <io.h>
#include <dos.h>
#include <unistd.h>
#include <pwd.h>

#include "sckstdio.h"


#define fred(a)  a##_bolox

char remotehost[64];
char hostname[64];

char *mail;

int exitplease;

int uniquefileid=0;



/*******************************************************************/
FILE *fopenuniquename(char *fn)
	{
	FILE *fp;
	char *e = strchr(fn,'.')+1;

	while(1)
		{
		sprintf(e,"%d",uniquefileid);
		if (++uniquefileid>999) uniquefileid = 0;
		if (access(fn,F_OK)) if (fp = fopen(fn,"w")) break;
		}

fred(wibble);

	return fp;
	}


void mputc(char **mem,char c)
	{
	int len = (*mem)?strlen(*mem):0;
	*mem = realloc(*mem,len+2);

	(*mem)[len+1] = '\0';
	(*mem)[len] = c;

	}

void mputs(char **mem,char *s)
	{
	int n,len=(*mem)?strlen(*mem):0;

	*mem = realloc(*mem,len+strlen(s)+1);
	strcpy(*mem+strlen(*mem),s);
	}

void mprintf(char **mem, char *s, ...)
	{
	va_list ap;
	char buf[1024];

	va_start(ap,s);
	vsprintf(buf,s,ap);

	va_end(ap);

	mputs(mem,buf);
	}

char *mopen()
	{
	char *mem;

	mem = malloc(1);
	mem[0] = '\0';
	return mem;
	}

char *mclose(char **mem)
	{
	if (*mem) free(*mem);
	*mem = NULL;
	}

char *mtofile(char *mem,char *fn,char *mode)
	{
	FILE *fp;

	if (fp = fopen(fn,mode))
		{
		fwrite(mem,strlen(mem),1,fp);
		fclose(fp);
		}
	}


char *GetString(char *msg)
	{
	char *s,*p;
	static char buf[512];

	if (s = strchr(msg,'<'))
		{
		if (p = strchr(msg,'>'))
			{
			strncpy(buf,s+1,p-s-1);
			buf[p-s-1] = '\0';
			return buf;
			}
		}
	return NULL;
	}


char *SOCKfgetline(char *buf,int len,FILE *sin,FILE *sout)
	{
	struct timeval t;
	int s = sin->_file;
	int readbit,writebit,excludebit;
	FILE *fp;
	char *p;
	char *b,c;

	SOCKfflush(sout);

	b = buf;
	while (len-->1)
		{
		writebit = 0;
		readbit = 1<<s;
		excludebit = 0;

		t.tv_sec = 120;
		t.tv_usec = 0;

		if (sin->_cnt<=0)
			if (select(s+1,(struct fd_set *)&readbit,(struct fd_set *)&writebit,(struct fd_set *)&excludebit,&t)!=1) return NULL;
		c = SOCKgetc(sin);
		if (c==EOF) return NULL;
		if (c=='\n')
			{
			*b = '\0';
			break;
			}
		*b++ = c;
		}

	return buf;
	}


/**************************************************************/

void ProcessSock(FILE *sin, FILE *sout)
	{
	char buf[1024];
	FILE *out;

	out = fopen("proxy.log","a");
	fprintf(out,"---\n");

	while (SOCKfgetline(buf,1024,sin,sout))
		{
		fprintf(out,"%s\n",buf);
		fflush(out);
		}
	fclose(out);
	
	}


main(int argc, char *argv[])
	{
	int s,bytes;
	FILE *fp;
	FILE *sin,*sout;
	int bit;
	int iobit,writebit,readbit,exceptbit=0;
	struct timeval t;
	long		nonBlocking = 1l;
	struct sockaddr_in s_incoming;
	int len;
	struct passwd *pw;

/* Start the Daemon looping */

	while (1)
		{

/* Check for pending messages */

		if ((s = so_daemon(argv,1))>=0)
			{

			sethostent();

/* get the remote host's name and number */

#if 0
			len = sizeof(s_incoming);
			getpeername(s,(struct sockaddr *)(&s_incoming),&len);

/* Set the port for writing */
			iobit = writebit = bit = 1L<<s;
			readbit = 0;
			t.tv_sec = 50;
			t.tv_usec = 0;
			iobit = select(s+1,(struct fd_set *)&readbit,(struct fd_set *)&writebit,(struct fd_set *)&exceptbit,&t);	/* Get read/write status */

#endif

			sin = SOCKfdopen(s,"r");
			sout = SOCKfdopen(s,"w");

			ProcessSock(sin,sout);

			SOCKfclose(sin);
			SOCKfclose(sout);

			endhostent();
			so_close(s);
			}
		else break;
		}


	}
