Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lseek SEEK_SET fails on Windows #76

Open
Y-Less opened this issue Apr 27, 2022 · 3 comments
Open

lseek SEEK_SET fails on Windows #76

Y-Less opened this issue Apr 27, 2022 · 3 comments

Comments

@Y-Less
Copy link

Y-Less commented Apr 27, 2022

No idea why, but the function call returns 0 (which is correct), yet future writes continue from the old location not the new one. I had to change fseek to:

  return fseek((FILE *)params[1],params[2],whence);

And flength to:

  int fn=fileno((FILE*)params[1]);
  c=lseek(fn,0,SEEK_CUR); /* save the current position */
  l=lseek(fn,0,SEEK_END); /* return the file position at its end */
  fseek((FILE *)params[1],c,SEEK_SET);   /* restore the file pointer */

The latter uses a mix of lseek and fseek because all of one or the other failed in different ways.

@namazso
Copy link
Contributor

namazso commented Apr 27, 2022

Maybe this happens on a file bigger than 2 GB? windows has _lseeki64 for big files

@Y-Less
Copy link
Author

Y-Less commented Apr 30, 2022

The file I tested it with was 13 bytes:

bool:test_fseek()
{
	fremove("fseek.ini");
	new File:f = fopen("fseek.ini", io_write);
	if (!f)
	{
		print("FAIL: File didn't open\n");
		return false;
	}
	fwrite(f, "hello world");
	fseek(f, 0, seek_start);
	fwrite(f, "hi");
	if (flength(f) != 11)
	{
		print("FAIL: `flength` != 11\n");
		return false;
	}
	fwrite(f, "hello world");
	if (flength(f) != 13)
	{
		print("FAIL: `flength` != 13\n");
		return false;
	}
	fclose(f);
	return true;
}

@namazso
Copy link
Contributor

namazso commented May 1, 2022

Reading through POSIX and various documentations there doesn't seem to be any guarantee of relations between a fd and a FILE. Additionally, the C standard allows FILE to store position information. So I think the code is just simply wrong and built on top of incorrect assumptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants