#include <bsdsocket.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <exec/libraries.h>
#include <dos/dosextens.h>
#include <dos/dos.h>
#include <clib/dos_protos.h>
#include <string.h>

#define NBBY	8				/* number of bits in a byte */
#define NFDBITS (sizeof(fd_mask) * NBBY)        /* bits per mask */
#define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
#define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
#define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
#define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))

#include <sys/time.h>

#include <stdio.h>
#include <stdarg.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;

	WaitSelect(16,&rd,0L,&ex,&t,&flgs);

	if (FD_ISSET(sock,&rd))
	{	n = recv(sock, &c, 1, 0);
		if (n == 1)
			return c;
		else return -1;
	}

	else return -1;
}

void joinpaths(char *str, char *path, char *file)
{
    if (strlen(path)) {
	if (path[strlen(path)-1] == '/' || path[strlen(path)-1] == ':')
	    sprintf(str, "%s%s", path, file);
	else
	    sprintf(str, "%s/%s", path, file);
    } else strcpy(str, file);
}
