Skip to content

Calling fopen for writing ends up calling __isatty which sets errno - is it intended? #622

Description

@AsoboEric

Hi everyone,

Running the following bit of code in a WASM module built for Microsoft Flight Simulator (which uses wasi_libc - thanks a lot for your hard work!):

	printf("errno 1 - %d - %s\n", errno, strerror(errno));
	FILE* hFile = fopen("\\work\\testerrno.txt", "w");
	printf("errno 2 - %d - %s\n", errno, strerror(errno));
	fclose(hFile);
	printf("errno 3 - %d - %s\n", errno, strerror(errno));

I noticed that errno is set to 59 (E_NOTTY) after the call to fopen. One important observation is that it does NOT happen when opening a file for reading.

Now, running a similar bit of code (only the path changed) in a Windows Console Application, errno remains at zero all along.

Browsing through the wasi_libc source code, I noticed that fopen calls __fdopen which itself calls __isatty for files opened with "write" rights:

	/* Activate line buffered mode for terminals */
	f->lbf = EOF;
#ifdef __wasilibc_unmodified_upstream // WASI has no syscall
	if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz))
#else
	if (!(f->flags & F_NOWR) && __isatty(fd))
#endif
		f->lbf = '\n';

The issue is that __isatty always sets errno to E_NOTTY if the file descriptor is not that of a TTY, which is the case when opening a regular file.

The only thing I am wondering is whether this behavior is intended or not. If that's the case then we can turn errno to zero in our runtime when fopen returns E_NOTTY (which doesn't really mean much in our case anyway). If not, maybe errno should be saved & restored manually in this bit of code?

Best regards,

Eric / Asobo

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions