

/******************************************************************************
 *
 * RCS ID
 * $Id: fd,v 1.5 2002/11/17 16:43:22 david Exp $
 *
 * HISTORY
 * $Log: fd,v $
 * Revision 1.5  2002/11/17 16:43:22  david
 * Updates to compiler with new 32-bit compiler
 *
 * Revision 1.4  2001/04/20 14:39:01  david
 * Updates for 0.7a
 * Prevent index of fd outside fd array
 *
 *
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

#include "kernel.h"
#include "socklib.h"
#include "unixlib.h"
#include "inetlib.h"
/*#include "sockets:include.h.errno"*/
#include "sys/errno.h"
/*#include "sockets:include.h.netdb"*/
#include "netdb.h"
#include "sys/ioctl.h"                 

#include "mytypes.h"

#include "local.h"

#include "byteorder.h"

#include "smb.h"      

#include "smbserver.h"


               
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE  1
#endif

 
#define NUM_SOCK_SETS 2

/*************************** TYPE DECLARATIONS  *******************************/



/*************** EXTERNAL GLOBAL VARIABLE DECLARATIONS SECTION ****************/


/******************** GLOBAL VARIABLE DECLARATIONS SECTION ********************/

                         
/* the fd set on which we are watching for events */
fd_set input_set[NUM_SOCK_SETS]; /* 0 = smbd sockets, 1 = nmbd sockets */ 

/* the fd set on which an event has occured */
fd_set read_input_set[NUM_SOCK_SETS];

/**************************** LOCAL ROUTINES SECTION **************************/
                                   
/*************************** GLOBAL ROUTINES SECTION **************************/

void fd_zero(int whichSet)
{
  if (whichSet<NUM_SOCK_SETS)
  {
    FD_ZERO(&input_set[whichSet]);              
    FD_ZERO(&read_input_set[whichSet]);
  }
}

int fd_isset(int whichSet, int fd)
{                            
  if ((whichSet<NUM_SOCK_SETS) && (fd<FD_SETSIZE))
  {
    return FD_ISSET(fd, &input_set[whichSet]);
  }
  else
  {
    return 0;
  }
}

void event_fd_set(int whichSet, int fd)
{
  if ((whichSet<NUM_SOCK_SETS) && (fd<FD_SETSIZE))
  {
    FD_SET(fd, &input_set[whichSet]);
  }
}

void event_read_fd_set(int whichSet, int fd)
{  
  if ((whichSet<NUM_SOCK_SETS) && (fd<FD_SETSIZE))
  {
    FD_SET(fd, &read_input_set[whichSet]);
  }
}

void event_fd_clr(int whichSet, int fd)
{  
  if ((whichSet<NUM_SOCK_SETS) && (fd<FD_SETSIZE))
  {
    FD_CLR(fd, &input_set[whichSet]); 
  }
}

void event_read_fd_clr(int whichSet, int fd)
{  
  if ((whichSet<NUM_SOCK_SETS) && (fd<FD_SETSIZE))
  {
    FD_CLR(fd, &read_input_set[whichSet]);
  }
}

void event_select(_kernel_swi_regs *r)
{                                
  /* r0 is whichSet */
  /* r1 is pointer to fd_set */
   
  if (r->r[0]<NUM_SOCK_SETS)
  {
    r->r[1]=(int)&read_input_set[r->r[0]];
/*
    _kernel_irqs_off();
    memcpy((void*)r->r[1], (void*)&read_input_set[r->r[0]], sizeof(fd_set));
    FD_ZERO(&read_input_set[r->r[0]]);
    _kernel_irqs_on(); 
*/       
  }
}
 
void print_fds(int whichSet)
{
  int i;
   
  if (whichSet<NUM_SOCK_SETS)
  {
    for (i=0;i<howmany(FD_SETSIZE, NFDBITS);i++)
    {
       printf("%.8lx",input_set[whichSet].fds_bits[i]);
    }
    printf("\n");  
  }
}

