lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2011/11/8 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
> I am trying to test errors in read operations (io.read - io.line). Does
> anyone know a reliable/automatic way to generate a read error on
> Linux? (That is, a situation where fread returns EOF and ferror returns
> not zero.)

For local tests, you can use a simple FUSE filesystem, eventually
written in Lua. The attached filesystem (errorfs.lua) and test program
(readtest.c) give me the following output:

[doub@blimbox ~]$ make readtest
cc     readtest.c   -o readtest
[doub@blimbox ~]$ mkdir tmp
[doub@blimbox ~]$ ./errorfs.lua tmp
[doub@blimbox ~]$ ./readtest tmp/error
fread => 0
ferror => 1
errno => 5
[doub@blimbox ~]$ fusermount -u tmp

Attachment: errorfs.lua
Description: Binary data

#include <stdio.h>
#include <errno.h>

int main(int argc, char** argv)
{
	FILE* f;
	size_t read;
	char buf[256];
	int haserror, error;
	if (argc < 2)
		return 1;
	f = fopen(argv[1], "r");
	read = fread(buf, 1, 256, f);
	haserror = ferror(f);
	error = errno;
	fclose(f);
	printf("fread => %d\nferror => %d\nerrno => %d\n", read, haserror, error);
	return 0;
}