Exemplo n.º 1
0
MAIN()
{
	char		buf[1024], *s;
	Sfio_t*	f;

	f = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND,-1,SF_WRITE|SF_STRING);
	sfsetbuf(sfstdout,buf,sizeof(buf));
	sfsetbuf(sfstderr,buf,sizeof(buf));
	sfset(sfstdout,SF_SHARE,0);
	sfset(sfstderr,SF_SHARE,0);

	if(!sfpool(sfstdout,f,SF_SHARE) || !sfpool(sfstderr,f,SF_SHARE) )
		terror("Pooling\n");

	if(sfputr(sfstdout,"01234",-1) != 5)
		terror("Writing to stderr\n");
	if(sfputr(sfstderr,"56789",-1) != 5)
		terror("Writing to stdout\n");

	if(sfputc(f,'\0') < 0)
		terror("Writing to string stream\n");

	sfseek(f,(Sfoff_t)0,0);
	if(!(s = sfreserve(f,SF_UNBOUND,1)) )
		terror("Peeking\n");
	sfwrite(f,s,0);
	if(strcmp(s,"0123456789") != 0)
		terror("Data is wrong\n");

	TSTEXIT(0);
}
Exemplo n.º 2
0
MAIN()
{
  FILE  *fp;
  int i, c, e;

  if(!(fp = fopen(tstfile(0), "w+")) )
    terror("Can't open temp file\n");

  for(i = 0; i < 256; ++i)
    if(putc(i, fp) < 0)
      terror("Bad putc\n");

  for(i = 1; i < 255; ++i)
  { if(fseek(fp, (long)(-i), SEEK_END) < 0)
      terror("fseek seek_end failed\n");
    if((c = getc(fp)) != (e = 256-i) )
      terror("Bad getc: expect %d, get %d\n", e, c);

    if(fseek(fp, (long)(i), SEEK_SET) < 0)
      terror("fseek seek_set failed\n");
    if((c = getc(fp)) != i)
      terror("Bad getc: expect %d, get %d\n", i, c);

    if(fseek(fp, (long)(-1), SEEK_CUR) < 0)
      terror("fseek seek_cur failed\n");

    if((c = getc(fp)) != i )
      terror("Bad getc: expect %d, get %d\n", i, c);
  }

  TSTEXIT(0);
}
Exemplo n.º 3
0
MAIN()
{
	Sfio_t*	f;
	char	buf[1024], buf2[1024], *data;
	int	n, r;

	/* test to see if malloc() winds up calling mmap() */
	if(!(data = (char*)malloc(8*1024)) )
		terror("Malloc failed\n");
	free(data);
	Success = 0;

	/* our real work */
	if(!(f = sfopen(NIL(Sfio_t*), tstfile(0),"w")) )
		terror("Can't open to write\n");

	for(n = 0; n < sizeof(buf); ++n)
		buf[n] = '0' + (n%10);

	for(n = 0; n < 10; ++n)
		sfwrite(f,buf,sizeof(buf));

	if(!(f = sfopen(f, tstfile(0),"r")) )
		terror("Can't open to read\n");

	for(n = 0; n < 10; ++n)
	{	if((r = sfread(f,buf2,sizeof(buf))) != sizeof(buf))
			terror("Bad read size=%d\n",r);
		if(strncmp(buf,buf2,sizeof(buf)) != 0)
			terror("Get wrong data\n");
	}

	TSTEXIT(0);
}
Exemplo n.º 4
0
MAIN()
{
  Sfio_t* f;
  Sfio_t  sf;

  if(sfopen(sfstdout,"abc","s") != sfstdout)
    terror("Bad reopening of sfstdout\n");
  if(sfopen(sfstdin,"123","s") != sfstdin)
    terror("Bad reopening of sfstdin\n");
  sfclose(sfstdin);

  if(!(f = sfopen(NIL(Sfio_t*),"123","s")) )
    terror("Opening a stream\n");
  sfclose(f);
  if(sfopen(f,"123","s") != NIL(Sfio_t*))
    terror("can't reopen a closed stream!\n");

  if(sfnew(&sf,NIL(char*),(size_t)SF_UNBOUND,0,SF_EOF|SF_READ) != &sf)
    terror("Did not open sf\n");
  sfset(&sf,SF_STATIC,1);
  if(sfclose(&sf) < 0 || !(sfset(&sf,0,0)&SF_STATIC))
    terror("Did not close sf\n");

  /* test for exclusive opens */
  unlink(tstfile(0));
  if(!(f = sfopen(NIL(Sfio_t*),tstfile(0),"wx") ) )
    terror("sfopen failed\n");
  if((f = sfopen(f,tstfile(0),"wx") ) )
    terror("sfopen should not succeed here\n");

  TSTEXIT(0);
}
Exemplo n.º 5
0
MAIN()
{
  int i;
  char  wbuf[1023];
  char  rbuf[1023];
  FILE  *fp;

  for(i = 0; i < sizeof(wbuf); ++i)
    wbuf[i] = (i%26)+'a';
  wbuf[sizeof(wbuf)-1] = '\0';

  if(!(fp = fopen(tstfile(0), "w+")) )
    terror("Opening temp file\n");

  for(i = 0; i < 256; ++i)
    if(fwrite(wbuf,sizeof(wbuf),1,fp) != 1)
      terror("Writing\n");

  fseek(fp,(long)0,0);

  for(i = 0; i < 256; ++i)
  { if(fread(rbuf,sizeof(rbuf),1,fp) != 1)
      terror("Reading\n");

    if(strcmp(rbuf,wbuf) != 0)
      terror("Unmatched record\n");
  }

  TSTEXIT(0);
}
Exemplo n.º 6
0
MAIN()
{
#if _typ_long_double
  long double ldval, ldmax;
  char    *s, *str;

  ldmax = LDBL_MAX;

  if(!(s = sfprints("%Le",ldmax)) )
    terror("sfprints failed1\n");
  if(!(str = malloc(strlen(s)+1)) )
    terror("Malloc failed\n");
  strcpy(str,s);

  if(sfsscanf(str,"%Le",&ldval) != 1)
    terror("sfsscanf failed\n");
  if(!(s = sfprints("%Le",ldval)) )
    terror("sfprints failed2\n");

  if(strcmp(s,str) != 0)
    terror("Bad conversion, expecting %s and getting %s\n",str,s);
#endif

  TSTEXIT(0);
}
Exemplo n.º 7
0
MAIN()
{
	Sfio_t*	f;
	char	buf[1024], *s;
	int	n;
#ifdef DEBUG
	Sfio_t*	logf = sfopen(0,"LOG","a"); sfsetbuf(logf,NIL(Void_t*),0);
#endif

	alarm(10);
	if(argc > 1)
	{	/* coprocess only */
		while((s = sfreserve(sfstdin,-1,0)) )
		{
#ifdef DEBUG
			sfwrite(logf, s, sfvalue(sfstdin));
#endif
			sfwrite(sfstdout, s, sfvalue(sfstdin));
		}
		return 0;
	}

	/* make coprocess */
	if(!(f = sfpopen(NIL(Sfio_t*), sfprints("%s -p",argv[0]), "r+")))
		terror("Opening for read/write\n");
	for(n = 0; n < 10; ++n)
	{	sfsprintf(buf,sizeof(buf),"Line %d",n);
		sfputr(f,buf,'\n');
		if(!(s = sfgetr(f,'\n',1)))
			terror("Did not read back line\n");
		if(strcmp(s,buf) != 0)
			terror("Input=%s, Expect=%s\n",s,buf);
	}

	if(sfputr(f,"123456789",'\n') != 10)
		terror("Bad write");

	if(sfread(f,buf,3) != 3)
		terror("Did not get data back\n");
	if(strncmp(s,"123",3) != 0)
		terror("Wrong data\n");

	if(sfwrite(f,"aaa",3) != 3 || sfputc(f,'\n') != '\n')
		terror("Fail on write\n");

	if(!(s = sfgetr(f,'\n',1)) )
		terror("Should have gotten 456789\n"); 
	if(strcmp(s,"456789") != 0)
		terror("Wrong data2\n");

	if(!(s = sfgetr(f,'\n',1)) )
		terror("Should have gotten aaa\n"); 
	if(strcmp(s,"aaa") != 0)
		terror("Wrong data3\n");

	sfclose(f);
	
	TSTEXIT(0);
}
Exemplo n.º 8
0
void* mmap64()
#endif
{
	if(Success)
		TSTEXIT(0);

	return (void*)(-1);
}
Exemplo n.º 9
0
MAIN()
{
	char	*s = "1234567890\n";
	Sfoff_t	n, i;
	Sfio_t	*f;
	char	buf[1024];
	char*	addr;

	if(sfopen(sfstdout,tstfile(0),"w+") != sfstdout)
		terror("Opening output file\n");
	for(i = 0; i < 10000; ++i)
		if(sfputr(sfstdout,s,-1) < 0)
			terror("Writing data\n");

	if(!(f = sfopen((Sfio_t*)0,tstfile(1),"w")))
		terror("Opening output file \n");

	sfseek(sfstdout,(Sfoff_t)0,0);
	if((n = sfmove(sfstdout,f,(Sfoff_t)SF_UNBOUND,'\n')) != i)
		terror("Move %d lines, Expect %d\n",n,i);

	sfseek(sfstdout,(Sfoff_t)0,0);
	sfseek(f,(Sfoff_t)0,0);
	sfsetbuf(sfstdout,buf,sizeof(buf));
	if((n = sfmove(sfstdout,f,(Sfoff_t)SF_UNBOUND,'\n')) != i)
		terror("Move %d lines, Expect %d\n",n,i);

	sfopen(sfstdin,tstfile(0),"r");
	sfopen(sfstdout,tstfile(1),"w");
	sfmove(sfstdin,sfstdout,(Sfoff_t)SF_UNBOUND,-1);
	if(!sfeof(sfstdin))
		terror("Sfstdin is not eof\n");
	if(sferror(sfstdin))
		terror("Sfstdin is in error\n");
	if(sferror(sfstdout))
		terror("Sfstdout is in error\n");

	sfseek(sfstdin,(Sfoff_t)0,0);
	sfseek(sfstdout,(Sfoff_t)0,0);
	sfsetbuf(sfstdin,buf,sizeof(buf));

	addr = (char*)sbrk(0);
	sfmove(sfstdin,sfstdout,(Sfoff_t)((unsigned long)(~0L)>>1),-1);
	if((ssize_t)((char*)sbrk(0)-addr) > 256*1024)
		terror("Too much space allocated in sfmove\n");

	if(!sfeof(sfstdin))
		terror("Sfstdin is not eof2\n");
	if(sferror(sfstdin))
		terror("Sfstdin is in error2\n");
	if(sferror(sfstdout))
		terror("Sfstdout is in error2\n");

	TSTEXIT(0);
}
Exemplo n.º 10
0
MAIN()
{
	unsigned int	i, r;
	Sfio_t	*fp;

	if(!(fp = sftmp(8)))
		terror("Can't open temp file\n");

	for(i = 10000; i <= 100000; i += 9)
		if(sfputu(fp,i) < 0)
			terror("Writing %u\n",i);

	sfseek(fp,(Sfoff_t)0,0);

	for(i = 10000; i <= 100000; i += 9)
		if((r = (unsigned int)sfgetu(fp)) != i)
			terror("Input=%u, Expect=%u\n",r,i);

	TSTEXIT(0);
}
Exemplo n.º 11
0
MAIN()
{
	Sfio_t	*f;
	int	fd;
	off_t	sk;

	if(!(f = sfopen((Sfio_t*)0,tstfile(0),"w+")))
		terror("Opening file\n");
	fd = sffileno(f);

	if(sfsetfd(f,-1) != -1 || sffileno(f) != -1)
		terror("setfd1\n");
	if(sfputc(f,'a') >= 0)
		terror("sfputc\n");

	if(sfsetfd(f,fd) != fd)
		terror("setfd2\n");

	if(sfwrite(f,"123456789\n",10) != 10)
		terror("sfwrite\n");

	sfseek(f,(Sfoff_t)0,0);
	if(sfgetc(f) != '1')
		terror("sfgetc1\n");

	if(sfsetfd(f,-1) != -1 || sffileno(f) != -1)
		terror("setfd2\n");
	if((sk = lseek(fd, (off_t)0, 1)) != (off_t)1)
		terror("Bad seek address %lld\n", (Sfoff_t)sk );
	if(sfgetc(f) >= 0)
		terror("sfgetc2\n");

	if(sfsetfd(f,fd) != fd)
		terror("setfd2\n");
	if(sfgetc(f) != '2')
		terror("sfgetc3\n");

	TSTEXIT(0);
}
Exemplo n.º 12
0
MAIN()
{
	char	buf[100];
	Sfio_t	*fp;
	int	i;
	char	*s;

	if(!(fp = sftmp(8)))
		terror("Can't open temp file\n");

	sfset(fp,SF_LINE,1);
	for(i = 0; i < 1000; ++i)
	{	sfsprintf(buf,sizeof(buf),"Number: %d",i);
		if(sfputr(fp,buf,'\n') <= 0)
			terror("Writing %s\n",buf);
	}

	sfseek(fp,(Sfoff_t)0,0);

	for(i = 0; i < 1000; ++i)
	{	sfsprintf(buf,sizeof(buf),"Number: %d",i);
		if(!(s = sfgetr(fp,'\n',1)))
			terror("Reading %s\n",buf);
		if(strcmp(s,buf) != 0)
			terror("Input=%s, Expect=%s\n",s,buf);
	}

	sfseek(fp,(Sfoff_t)0,0);
	s = sfgetr(fp,'\0',1);
	if(s)
		terror("Expecting a null string\n");
	s = sfgetr(fp,'\0',-1);
	if(!s)
		terror("Expecting a non-null string\n");
	if(sfvalue(fp) != sfsize(fp))
		terror("Wrong size\n");

	TSTEXIT(0);
}
Exemplo n.º 13
0
/*	Test multiple processes reading/writing from same file
**	descriptor.
*/
MAIN()
{
	char*	s;

	if(argc > 1)
	{	if(strcmp(argv[1],"-r") == 0)	/* doing sfgetr */
		{	if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line2") != 0)
				terror("Coprocess getr did not get Line2\n");
			if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line3") != 0)
				terror("Coprocess getr did not get Line3\n");
		}
		else	/* doing sfmove */
		{	Sfio_t*	f = sfopen(NIL(Sfio_t*),NIL(char*),"swr");
			if(!f)
				terror("Can't open string stream\n");
			if(sfmove(sfstdin,f,(Sfoff_t)2,'\n') != 2)
				terror("Coprocess sfmove failed\n");
			sfseek(f,(Sfoff_t)0,0);
			if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"Line2") != 0)
				terror("Coprocess move did not get Line2\n");
			if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"Line3") != 0)
				terror("Coprocess move did not get Line3\n");
		}
		TSTEXIT(0);
	}

	if(sfopen(sfstdout, tstfile(0), "w") != sfstdout )
		terror("Opening file\n");
	if(sfputr(sfstdout,"Line1",'\n') < 0 ||
	   sfputr(sfstdout,"Line2",'\n') < 0 ||
	   sfputr(sfstdout,"Line3",'\n') < 0 ||
	   sfputr(sfstdout,"Line4",'\n') < 0)
		terror("Writing data\n");
	sfopen(sfstdout,"/dev/null","w");

	/* testing coprocess calling sfgetr */
	if(sfopen(sfstdin, tstfile(0),"r") != sfstdin)
		terror("Opening to read\n");
	if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line1") != 0)
		terror("Did not get Line1 for sfgetr\n");
	sfsync(sfstdin);
	system(sfprints("%s -r",argv[0]));
	sfseek(sfstdin, (Sfoff_t)lseek(sffileno(sfstdin), (off_t)0, 1), 0);
	if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line4") != 0)
		terror("Did not get Line4 for sfgetr\n");

	/* testing coprocess calling sfmove */
	if(sfopen(sfstdin, tstfile(0), "r") != sfstdin)
		terror("Opening to read\n");
	if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line1") != 0)
		terror("Did not get Line1 for sfmove\n");
	sfsync(sfstdin);
	system(sfprints("%s -m",argv[0]));
	sfseek(sfstdin, (Sfoff_t)lseek(sffileno(sfstdin), (off_t)0, 1), 0);
	if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line4") != 0)
		terror("Did not get Line4 for sfmove\n");

	/* testing the head program */
#ifdef HEAD
	if(sfopen(sfstdin, tstfile(0), "r") != sfstdin)
		terror("Opening to read\n");
	if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line1") != 0)
		terror("Did not get Line1 for head\n");
	sfsync(sfstdin);
	system("head -2 > /dev/null"); /* for testing the head program */
	sfseek(sfstdin, (Sfoff_t)lseek(sffileno(sfstdin), (off_t)0, 1), 0);
	if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line4") != 0)
		terror("Did not get Line4 for head\n");
#endif

	TSTEXIT(0);
}
Exemplo n.º 14
0
MAIN()
{
  Sfio_t* fw;
  Sfio_t* fr;
  int fds[2];
  int lseek_errno;
  int rv;

  if(!(fw = sfopen(NIL(Sfio_t*), tstfile(0), "w")) )
    terror("Can't create temp file %s to write", tstfile(0));
  if(!(fr = sfopen(NIL(Sfio_t*), tstfile(0), "r")) )
    terror("Can't open temp file %s to read", tstfile(0));

  sfseek(fr, (Sfoff_t)0, SEEK_END);
  if(sfgetc(fr) >= 0 || !sfeof(fr))
    terror("Should have seen eof");

  errno = 0;
  if((rv = sfwrite(fr, "a", 1)) == 1)
    terror("sfwrite returns %d, expecting 1", rv);
  if(errno != EBADF)
    twarn("Wrong errno %d after sfwrite(%d), expecting %d",errno,rv,EBADF);

  /* on some system (eg, apple), lseek does not set errno for this case */
  errno = 0;
  lseek(sffileno(fw), (off_t)(-2), SEEK_SET);
  lseek_errno = errno;
  lseek(sffileno(fw), (off_t)0, SEEK_SET);
  errno = 0;

  if(sfseek(fw, (Sfoff_t)(-2), SEEK_SET) != (Sfoff_t)(-1) )
    terror("sfseek should have failed");
  if(errno != lseek_errno)
    twarn("Wrong errno %d after sfseek, expecting %d", errno, lseek_errno);

  errno = 0;
  if(sfseek(fw, (Sfoff_t)0, SEEK_SET|SEEK_CUR|SEEK_END) >= 0)
    terror("sfseek should not have succeeded");
  if(errno != EINVAL)
    twarn("Wrong errno %d after sfseek, expecting %d", errno, EINVAL);

  if(pipe(fds) < 0)
    terror("Can't create pipes");

  if(!(fw = sfnew(fw, NIL(Void_t*), (size_t)SF_UNBOUND, fds[1], SF_WRITE)) )
    terror("Can't create stream for pipe");

  errno = 0;
  if(sfseek(fw, (Sfoff_t)0, SEEK_SET) >= 0)
    terror("sfseek should have failed on a pipe");
  if(errno != ESPIPE)
    twarn("Wrong errno %d after sfseek, expecting %d", ESPIPE);

  close(sffileno(fw));
  errno = 0;
  if(sfseek(fw, (Sfoff_t)0, SEEK_END) >= 0)
    terror("sfseek should have failed on a closed file descriptor");
  if(errno != EBADF)
    twarn("Wrong errno %d after sfseek, expecting %d", EBADF);

  TSTEXIT(0);
}
Exemplo n.º 15
0
MAIN()
{
  Sfio_t  *f;

  if(argc > 1)
  { if(sfopen(sfstdin,argv[1],"r") != sfstdin)
      terror("Can't reopen stdin");
    sfmove(sfstdin,sfstdout,(Sfoff_t)(-1),-1);
    return 0;
  }

  if(!(f = sfopen((Sfio_t*)0,tstfile(0),"w")))
    terror("Opening to write\n");
  if(sfputc(f,'a') != 'a')
    terror("sfputc\n");
  if(sfgetc(f) >= 0)
    terror("sfgetc\n");
  
  if(!(f = sfopen(f,tstfile(0),"r")))
    terror("Opening to read\n");
  if(sfgetc(f) != 'a')
    terror("sfgetc2\n");
  if(sfputc(f,'b') >= 0)
    terror("sfputc2\n");

  if(!(f = sfopen(f,tstfile(0),"r+")))
    terror("Opening to read/write\n");

  if(sfgetc(f) != 'a')
    terror("sfgetc3\n");
  if(sfputc(f,'b') != 'b')
    terror("sfputc3\n");
  if(sfclose(f) < 0)
    terror("sfclose\n");

  if(!(f = sfpopen(NIL(Sfio_t*),sfprints("%s %s", argv[0], tstfile(0)),"r")))
    terror("sfpopen\n");
  if(sfgetc(f) != 'a')
    terror("sfgetc4\n");
  if(sfgetc(f) != 'b')
    terror("sfgetc5\n");
  if(sfgetc(f) >= 0)
    terror("sfgetc6\n");

  if(!(f = sfopen(f,tstfile(0),"w")) )
    terror("sfopen\n");
  if(sfputc(f,'a') != 'a')
    terror("sfputc1\n");
  sfsetfd(f,-1);
  if(sfputc(f,'b') >= 0)
    terror("sfputc2\n");
  if(sfclose(f) < 0)
    terror("sfclose\n");

  if(!(f = sfopen(NIL(Sfio_t*),tstfile(0),"a+")) )
    terror("sfopen2\n");
  sfset(f,SF_READ,0);
  if(!sfreserve(f,0,-1) )
    terror("Failed on buffer getting\n");
  if(sfvalue(f) <= 0)
    terror("There is no buffer?\n");

  TSTEXIT(0);
}
Exemplo n.º 16
0
MAIN()
{
	Sfio_t*	f;
	char	buf[8*1024];
	int	i;
	Sfoff_t	s;

	/* test file resizing */
#if _lib_ftruncate
	if(!(f = sfopen(NIL(Sfio_t*), tstfile(0), "w")) )
		terror("Can't open file %s", tstfile(0));

	for(i = 0; i < sizeof(buf); ++i)
		buf[i] = '1';

	for(i = 0; i < 1024; ++i)
		if(sfwrite(f, buf, sizeof(buf)) != sizeof(buf) )
			terror("Can't write data");

	if(sfclose(f) < 0)
		terror("Can't sync/close file");
	if(!(f = sfopen(NIL(Sfio_t*), tstfile(0), "r+")) )
		terror("Can't open file %s again", tstfile(0));
	if(sfsize(f) != (s = 1024*sizeof(buf)) )
		terror("Bad file size");

	if(sfresize(f, s + sizeof(buf)) < 0)
		terror("Can't resize file");
	if(sfsize(f) != s+sizeof(buf))
		terror("Bad file size");

	sfseek(f, s, 0);
	if(sfread(f,buf,sizeof(buf)) != sizeof(buf))
		terror("Can't read data");

	for(i = 0; i < sizeof(buf); ++i)
		if(buf[i] != 0)
			terror("Bad data");
	sfclose(f);

	if(!(f = sfopen(NIL(Sfio_t*), tstfile(0), "r+")) )
		terror("Can't open file %s again", tstfile(0));
	if(sfsize(f) != s+sizeof(buf))
		terror("Bad file size");

	if(sfresize(f, s) < 0)
		terror("Can't resize file");
	sfclose(f);

	if(!(f = sfopen(NIL(Sfio_t*), tstfile(0), "r+")) )
		terror("Can't open file %s again", tstfile(0));
	if(sfsize(f) != s)
		terror("Bad file size");
#endif

	/* test resizing string stream */
	if(!(f = sfopen(NIL(Sfio_t*),  NIL(char*), "rws")) )
		terror("Can't open string stream");

	for(i = 0; i < sizeof(buf); ++i)
		buf[i] = '1';

	for(i = 0; i < 1024; ++i)
		if(sfwrite(f, buf, sizeof(buf)) != sizeof(buf) )
			terror("Can't write data");

	if(sfsize(f) != (s = 1024*sizeof(buf)) )
		terror("Bad stream size");

	if(sfresize(f, s + sizeof(buf)) < 0)
		terror("Can't resize stream");
	if(sfsize(f) != s+sizeof(buf))
		terror("Bad stream size");

	if(sfseek(f, s, 0) != s)
		terror("seek failed");
	if(sfread(f,buf,sizeof(buf)) != sizeof(buf))
		terror("Can't read data");

	for(i = 0; i < sizeof(buf); ++i)
		if(buf[i] != 0)
			terror("Bad data");

	if(sfresize(f, s) < 0)
		terror("Can't resize stream");
	if(sfsize(f) != s)
		terror("Bad stream size");

	TSTEXIT(0);
}
Exemplo n.º 17
0
MAIN()
{
  ssize_t size[N_WRITER][N_RECORD];
  int count[N_WRITER];
  char  record[N_WRITER][128], *s;
  Sfio_t* fw[N_WRITER];
  Sfio_t* fr;
  int i, r, done;

  /* create random record sizes */
  for(i = 0; i < N_WRITER; ++i)
  for(r = 0; r < N_RECORD; ++r)
    size[i][r] = (ssize_t)(vrandom()%64) + 2;

  /* records for different processes */
  for(i = 0; i < N_WRITER; ++i)
  for(r = 0; r < 128; ++r)
    record[i][r] = '0'+i;

  /* create file */
  fr = sfopen(NIL(Sfio_t*),tstfile(0),"w+");

  /* create records */
  for(i = 0; i < N_WRITER; ++i)
  { fw[i] = sfopen(NIL(Sfio_t*),tstfile(0),"a");
    count[i] = 0;
  }

  for(done = 0; done < N_WRITER; )
  { i = (int)(vrandom()%N_WRITER);
    if(count[i] < N_RECORD)
    { r = size[i][count[i]];
      if(!(s = sfreserve(fw[i],r,1)) || sfvalue(fw[i]) < r )
        terror("sfreserve fails in process %d\n", i);
      memcpy(s,record[i],r-1);
      s[r-1] = '\n';
      sfwrite(fw[i],s,r);

      if((count[i] += 1) == N_RECORD)
      { done += 1;
        sfclose(fw[i]);
      }
    }
  }

  for(i = 0; i < N_WRITER; ++i)
    count[i] = 0;

  while((s = sfgetr(fr,'\n',0)) )
  { if((i = s[0] - '0') < 0 || i >= N_WRITER)
      terror("Wrong record type\n");

    for(r = sfvalue(fr)-2; r > 0; --r)
      if(s[r] != s[0])
        terror("Bad record%d, count=%d\n", i, count[i]);

    if(sfvalue(fr) != size[i][count[i]])
      terror("Record%d count=%d size=%d sfvalue=%d\n",
        i, count[i], size[i][count[i]], sfvalue(fr));

    count[i] += 1;
  }

  for(i = 0; i < N_WRITER; ++i)
    if(count[i] != N_RECORD)
      terror("Bad count%d %d\n", i, count[i]);

  TSTEXIT(0);
}
Exemplo n.º 18
0
/* test compliance of certain stdio behaviors */
MAIN()
{
	FILE	*f, *f2;
	long	s1, s2;
	int	i, k, fd;
	char	buf[128*1024], rbuf[1024], *sp;

	/* test for shared streams and seek behavior */
	if(!(f = fopen(tstfile(0),"w+")) )
		terror("Opening file to read&write");

	/* write something to a dup file descriptor */
	fd = dup(fileno(f));
	if(write(fd, "0123456789", 10) != 10)
		terror("write failed");
	close(fd);

	/* this fseek should reset the stream back to where we can read */
	if(fseek(f, 0L, SEEK_SET) < 0)
		terror("fseek failed");

	/* see if data is any good */
	if((k = fread(buf, 1, 10, f)) != 10)
		terror("fread failed");

	for(i = 0; i < 10; ++i)
		if(buf[i] != '0'+i)
			terror("Bad data0");

	/* construct a bunch of lines and out put to f */
	sp = buf;
	for(k = 0; k < sizeof(buf)/10; ++k)
	{	for(i = 0; i < 9; ++i)
			*sp++ = '0' + i;
		*sp++ = '\n';
	}

	/* write out a bunch of thing */
	fseek(f, 0L, SEEK_SET);
	if(fwrite(buf, sizeof(buf), 1, f) != 1)
		terror("Writing data");

	if((fd = dup(fileno(f))) < 0)
		terror("Can't dup file descriptor");
	if(!(f2 = fdopen(fd, "r")) )
		terror("Can't create stream");

	/* read a few bytes from this dup stream */
	fseek(f2, 0L, SEEK_SET);
	rbuf[0] = 0;
	if(fread(rbuf, 1, 7, f2) != 7)
		terror("Bad read\n");
	for(i = 0; i < 7; ++i)
		if(rbuf[i] != '0'+i)
			terror("Bad data1");

	if((s2 = ftell(f2)) != 7)
		terror("Bad tell location in f2");

	/* now seek way off on f */
	fseek(f, 1005L, SEEK_SET);
	rbuf[0] = 0;
	fread(rbuf, 5, 1, f);
	for(i = 5; i < 9; ++i)
		if(rbuf[i-5] != '0'+i)
			terror("Bad data2");
	if(rbuf[i-5] != '\n')
		terror("Bad data: did not get new-line");
	if((s1 = ftell(f)) != 1010)
		terror("Bad location in f, s1=%ld", (long)s1);

	fseek(f, 0L, SEEK_CUR); /* switch mode so we can write */
	if(fputc('x',f) < 0)
		terror("fputc failed");
	if(fflush(f) < 0)
		terror("fflush failed");
	if((s1 = ftell(f)) != 1011)
		terror("Bad tell location in f");
	fseek(f, -1L, SEEK_CUR); /* set the seek location in the file descriptor */

	fflush(f2); /* assuming POSIX conformance and to set seek location to 1010 */
	if((s2 = ftell(f2)) != 1010)
		terror("Bad tell2 location=%ld in f2, expecting=%ld", s2, 1010);

	fread(rbuf, 10, 1, f2);
	if(rbuf[0] != 'x')
		terror("Didn't get x");
	for(i = 1; i < 9; ++i)
		if(rbuf[i] != '0'+i)
			terror("Bad data3");
	if(rbuf[i] != '\n')
		terror("Did not get new-line");

	TSTEXIT(0);
}
Exemplo n.º 19
0
MAIN()
{
	Sfio_t*	f;

	if(!(f = sfopen(NIL(Sfio_t*),"ab","sr")) )
		terror("Can't open stream\n");
	if(sfeof(f) || sferror(f))
		terror("Can't be eof or error yet\n");
	if(sfgetc(f) != 'a')
		terror("Got wrong data\n");
	if(sfeof(f) || sferror(f))
		terror("Can't be eof or error yet2\n");
	if(sfgetc(f) != 'b')
		terror("Got wrong data2\n");
	if(sfeof(f) || sferror(f))
		terror("Can't be eof or error yet3\n");
	if(sfgetc(f) >= 0)
		terror("Got wrong data2\n");
	if(!sfeof(f))
		terror("Should be eof now\n");
	if(sfseek(f,(Sfoff_t)(-1),2) != 1)
		terror("Seek error\n");
	if(sfeof(f))
		terror("Shouldn't be eof any more\n");

	if(!(f = sfopen(NIL(Sfio_t*), tstfile(0), "w+")) )
		terror("Can't open stream2\n");
	if(sfeof(f) || sferror(f))
		terror("Can't be eof or error yet2\n");
	if(sfwrite(f,"ab",2) != 2)
		terror("Can't write data\n");
	if(sfseek(f,(Sfoff_t)0,0) != 0)
		terror("Can't seek back\n");
	if(sfgetc(f) != 'a')
		terror("Got wrong data3\n");
	if(sfeof(f) || sferror(f))
		terror("Can't be eof or error yet4\n");
	if(sfgetc(f) != 'b')
		terror("Got wrong data4\n");
	if(sfeof(f) || sferror(f))
		terror("Can't be eof or error yet5\n");
	if(sfgetc(f) >= 0)
		terror("Got wrong data5\n");
	if(!sfeof(f))
		terror("Should be eof now2\n");
	if(sfseek(f,(Sfoff_t)(-1),2) != 1)
		terror("Seek error2\n");
	if(sfeof(f))
		terror("Shouldn't be eof any more2\n");

	if(!(f = sfopen(NIL(Sfio_t*), tstfile(0),"w+")) )
		terror("Reopening %s\n", tstfile(0));
	sfwrite(f,"1234567890",10);
	sfseek(f,(Sfoff_t)0,0);

	if(sfopen(sfstdout, tstfile(1), "w") != sfstdout)
		terror("Opening %s\n", tstfile(1));

	if(sfmove(f,sfstdout,(Sfoff_t)(-1),-1) != 10)
		terror("sfmove failed\n");
	if(!sfeof(f))
		terror("f should be eof\n");
	if(sferror(sfstdout))
		terror("sfstdout should not be in error\n");

	TSTEXIT(0);
}