# include "imregister.h"
# include "imreg.h"
# include <errno.h>
# define MAXTIME 10

int SockScan (char *string, Fifo *fifo, int sock) {
  
  int i, done, status;

  done = 0;
  for (i = 0; (i < MAXTIME) && !done; i++) {
    ShiftFifo (fifo);
    status = ReadtoFifo (fifo, sock);
    switch (status) {
    case 0:
      break;
    case -1:
      usleep (1000);
      break;
    default:
      done = memstr (fifo[0].buffer, string, fifo[0].Nbuffer);
      break;
    }
  }
  return (status);
}

/* returns (offset to m2) + 1, or 0 if failure */ 
int memstr (char *m1, char *m2, int n) {

  int i, N;

  N = strlen (m2);
  for (i = 0; (i < n - N + 1) && memcmp (m1, m2, N); i++, m1++);
  if (memcmp (m1, m2, N)) {
    return (0);
  }
  else {
    return (i+1);
  }

}
 
/* SockScan reads from the given socket looking for the given string
   SockScan stores data read from socket in the fifo buffer to ensure 
   it catches the given string.  a single invocation of SockScan will 
   wait for up to 10 msec for data to come down the pipe before giving up.
   SockScan can only search for one string in the output stream.
   */


/* need a way of signalling that the socket has closed...
   maybe change the sock entry to 0?
*/
