#ifdef AMI_TCP
#include <bsdsocket.h>
#else
#include <ss/socket.h>
#endif
#include <sys/types.h>
#include <signal.h>
#include <exec/libraries.h>
#include <dos/dosextens.h>
#include <dos/dos.h>
#include <clib/dos_protos.h>
#ifdef __SASC
#include <string.h> /* for bzero, to make FD_ZERO work */
#endif

#include <sys/time.h>

int sgetc(int sock)
{
	unsigned char c;
	fd_set rd,ex;
	long flgs;
	int n;

	struct timeval t;
	t.tv_sec = 10L;
	t.tv_usec = 0;

	FD_ZERO(&rd);
	FD_ZERO(&ex);

	FD_SET(sock,&rd);
	FD_SET(sock,&ex);
	flgs = SIGBREAKF_CTRL_D;
	
#ifdef AMI_TCP
	WaitSelect(16,&rd,0L,&ex,&t,&flgs);
#else
	selectwait(16,&rd,0L,&ex,&t,&flgs);
#endif
	if (FD_ISSET(sock,&rd))
	{	n = recv(sock, &c, 1, 0);
		if (n == 1)
			return c;
		else return -1;
	}

	else return -1;
}

/* 
 * This getenv and setenv will use the WB2.0 calls if you have the new
 * rom. If not, it resorts to looking in the ENV: directory. 
 */

extern struct DosLibrary *DOSBase;

extern char *getenv();

char *mygetenv (name)
	register const char *name;
{
	static char value[256];

	/* 2.0 style? */
	if (DOSBase->dl_lib.lib_Version >= 36) {
		if (GetVar ((char *)name,value,256,0L) == -1) {
			return 0;
		} else {
			return value;
		}
	} else {
		return getenv(name);
	}
}

mygettimeofday(struct timeval *tp, struct timezone *tzp)
{
	struct DateStamp ds;

	DateStamp(&ds);

	tp->tv_sec = (ds.ds_Days * 60 * 24 + ds.ds_Minute )* 60 +
		ds.ds_Tick / 50;
	tp->tv_usec = (ds.ds_Tick % 50) * 20000;
}

/* 
 * joinpath tacks a file (or sub dir) on to the end of a directory name.
 * Not just as simple as putting a '/' between the two, as the directory
 * name may be an assign! 
 */

void joinpath (str, dir, file)
	char *str, *dir, *file;
{	
	char c;

	if (strlen (dir) == 0) {
		strcpy (str, file);
		return;
	}
	c = dir[strlen(dir)-1];
	if (c=='/' || c==':') {
		sprintf (str, "%s%s", dir, file);
	} else {
		sprintf (str, "%s/%s", dir, file);
	}	
}
