#include <mintbind.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

long tmr ( void )
{
  static int i=0;
  static long h200,oldssp;
  if (!i)
  {
    i=1;
    oldssp=Super(NULL);
    h200=*(long*)0x4ba;
    return 0;
  }
  else
  {
    i=0;
    Super((void*)oldssp);
    return ((*(long*)0x4ba)-h200);
  }
}

int main ( void )
{
  int ph;
  long blksiz=0,offset=0;
  char *p;
  printf("\nPipe speed test ..."
         "\nChoose write block size : 1 = 1k"
         "\n                          2 = 4k"
         "\n                          3 = 10k"
         "\n                          4 = 100k"
         "\n                          5 = 1M"
         "\n                          0 = Cancel test"
         "\n --> ");
  do
  {
    char c;
    c=getchar();
    switch (c)
    {
      case '1' : blksiz=1024; break;
      case '2' : blksiz=4096; break;
      case '3' : blksiz=10240; break;
      case '4' : blksiz=102400; break;
      case '5' : blksiz=1048576; break;
      case '0' : return 0;
    }
  } while (!blksiz);
  if (blksiz>=1048576)
    printf("%ld MByte\n",blksiz/1024/1024);
  else
  if (blksiz>=1024)
    printf("%ld kByte\n",blksiz/1024);
  else
    printf("%ld Byte\n",blksiz);

  p=Malloc(blksiz);
  if (!p)
  {
    printf("\nError allocating memory !\n");
    return -1;
  }

  printf("\nTrying to open pipe 'pipetest.pip' ..."
         "\n(Abort via Pkill() !!!)\n");
  do
    ph=Fcreate("u:\\pipe\\pipetest.pip",0x01 | 0x20);
  while (ph<0);

  printf("\nWriting ...");
  tmr();
  do
  {
    long d;
    if (offset+blksiz>10485760)
      blksiz=10485760-offset;
    for (d=blksiz;d!=0;)
      d-=Fwrite(ph,d,p);
    offset+=blksiz;
  } while (offset<10485760);
  printf("\n10 MB transfer took %f seconds",(float)((float)tmr()/(float)200));

  printf("\nOK !\n");

  return 0;
}
