/* -TAG:LICENSE-
   
   This file is under the DigitalSpark Freeware Class License, see the
   file LICENSE for complete licensing information.
                                                         
                                                         -TAG:LICENSE- */
#include <netdb.h>
#include <Threading.h>
#include "tricky.h"

char *NetGets( int sock )
{
char	*GetsRet;
char	temp[1024];
char	in[10];
int	i=0,slot=0;

in[0] = 0;
in[1] = 0;

/* Lock the socket */
while ( slot <= MAX_CLIENTS )
   {
   if ( Client[slot].State != CS_FREE )
      {
      if ( Client[slot].Sock == sock )
         {
         pthread_mutex_lock(&Client[slot].Mutex);
         break;
         }
      }
   slot++;
   }
if ( slot > MAX_CLIENTS )
   slot=-1;

while (( in[0] != 0x0a ) && ( i < 1023 ))
   {
   if ( read(sock,&in,1) < 1 )
      {
      GetsRet = 0;
      return 0;
      }
   temp[i] = in[0];
   i++;
   }

temp[i] = 0;
if ( temp[strlen(temp)-1] == 0x0a )
   temp[strlen(temp)-1] = 0;
if ( temp[strlen(temp)-1] == 0x0d )
   temp[strlen(temp)-1] = 0;
if ( temp[strlen(temp)-1] == 0x0a )
   temp[strlen(temp)-1] = 0;

if ( i != -1 )
   pthread_mutex_unlock(&Client[i].Mutex);

GetsRet = malloc(strlen(temp)+1);
strcpy(GetsRet,temp);
return GetsRet;
}
