/* This file is part of Lcrzo.
*/

/*-------------------------------------------------------------*/
int lcrzo_priv_wait_ipc_read(lcrzo_ipc *pipc)
{ lcrzo_data ptr;
  lcrzo_uint32 readsize;

  lcrzo_er(lcrzo_data_alloc(LCRZO_PRIV_READMAXMSGSIZE, &ptr));

#if defined LCRZODEF_SYSTEM_Unix
  readsize=read(pipc->readpipe[pipc->side], ptr, LCRZO_PRIV_READMAXMSGSIZE);
  /* an error occured*/
  if (readsize==(lcrzo_uint32)-1)
  { lcrzo_data_free(ptr);
    if (errno==EAGAIN)
    { /* nothing was available */
      errno=0;
      return(LCRZO_ERR_OKTEMPDATAEND);
    }
    else if (errno==EBADF)
    { /* reached end */
      errno=0;
      return(LCRZO_ERR_OKDATAEND);
    }
    else
    { /* fatal error */
      return(LCRZO_ERR_FUREAD);
    }
  }
  /* if we read 0, we are at end of file */
  if (readsize==0)
  { lcrzo_data_free(ptr);
    return(LCRZO_ERR_OKDATAEND);
  }
#elif defined LCRZODEF_SYSTEM_Windows
  { BOOL err;
    err=ReadFile(pipc->readhandle[pipc->side], ptr, LCRZO_PRIV_READMAXMSGSIZE,
		 &readsize, NULL);
    if (!err)
    { lcrzo_data_free(ptr);
      return(LCRZO_ERR_OKDATAEND);
    }
    if (readsize==0)
    { lcrzo_data_free(ptr);
      return(LCRZO_ERR_OKTEMPDATAEND);
    }
  }
#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif

  /* put read data in stored data */
  lcrzo_er(lcrzo_priv_storeddata_append(ptr, readsize, &(pipc->storeddata),
					&(pipc->storeddatasize)));
  lcrzo_data_free(ptr);
  return(LCRZO_ERR_OK);
}


