#include "../constants.h"

int fetchsms (char *pdu, int sim)
{
  char answer[ANSWERSIZE];
  int position;
  int beginning;
  int end;
  int  foo,err;

  /* Input magically appears */
  answer[ANSWERSIZE-1] = EOS;

  /* Still chopped down a bit from the original in the interests of
   * not being heinous */
  if (nondet_int() == 0) {
    /* Original program uses a different needle here */
    position=istrstr(answer, NEEDLE);
    if (position != -1) {
      end=position+NEEDLE_SZ;
      while (answer[end] < '9' && answer[end] > '0')
        end++;
      position = 0;
    }
  } else {
    position=istrstr(answer, NEEDLE);
  }

  /* Skip the needle */
  if (position==-1)
    return 0;
  beginning = position + NEEDLE_SZ + 1;

  /* Answer must contain NEEDLE2; we don't need to skip it. */ 
  if (istrstr(answer, NEEDLE2) == -1)
    return 0;

  /* Find (something)\r(something)\r, where each (something) is at
   * least MIN_DIFF characters
   *
   * If we don't find anything satisfying that, abort
   */
  for( end=beginning ; answer[end] != EOS && answer[end] != '\r' ; end++ );
  if ( answer[end] == EOS || end-beginning < MIN_DIFF)
    return 0;
  for( end=end+1 ; answer[end] != EOS && answer[end] !='\r' ; end++ );
  if ( answer[end] == EOS || end-beginning < MIN_DIFF )
    return 0;

  /* Change the last '\r' to an EOS */
  answer[end] = EOS;

  /* OK */
  r_strcpy(pdu,answer+beginning);
  
  return sim;
}

int main ()
{
  char pdu [ANSWERSIZE];
  int sim = 0;

  fetchsms (pdu, sim);

  return 0;
}
