
#include <stdio.h>
#include <stdlib.h>

#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#else
#include <unistd.h>
#endif



static char ibuffer[512];
static char obuffer[512];

int main(int argc, char *argv[])
{
  int cc = 1;
  int bytes = 0;
  int uobuffer = 0;
  int canwrite = 1;
  int exval = 0;
  int i;
  int written = 0;
#ifdef _WIN32
  int oldin = 0, oldout = 0;
#endif

#ifdef _WIN32
  oldin  = _setmode(0, _O_BINARY);
  oldout = _setmode(1, _O_BINARY);
#endif
  while(cc) {
    bytes = read(0, (void *)ibuffer, sizeof(ibuffer));
    if(bytes > 0) {
      for(i = 0; i < bytes; i++) {
        obuffer[uobuffer++] = ibuffer[i];
	if(uobuffer >= sizeof(obuffer)) {
	  if(canwrite) {
	    written = write(1, (void *)obuffer, uobuffer);
	    if(written != uobuffer) {
	      canwrite = 0; exval = 1;
	      fprintf(stderr, "blks: ERROR: Problem in write operation!\n");
	      fflush(stderr);
	    }
	  }
	  uobuffer = 0;
	}
      }
    } else {
      cc = 0;
    }
  }
  if(uobuffer) {
    if(canwrite) {
      written = write(1, (void *)obuffer, uobuffer);
      if(written != uobuffer) {
        exval = 1;
	fprintf(stderr, "blks: ERROR: Problem in write operation!\n");
	fflush(stderr);
      }
    }
  }
#ifdef _WIN32
  _setmode(1, oldout);
  _setmode(0, oldin);
#endif
  exit(exval); return exval;
}


