Вопрос

I have a Linux program (currently in assembly) that has a check: if a read from STDIN failed, show an error message. The problem is that I do not know how to test this condition, how to execute the program so that it will fail reading from STDIN. IT must be run without STDIn or STDIN couldbe closed some how before the program starts?

Это было полезно?

Решение

Yes, you can close the file descriptor, that will trigger an error. Test using bash:

$ strace ./a.out 0<&-
execve("./a.out", ["./a.out"], [/* 32 vars */]) = 0
[ Process PID=4012 runs in 32 bit mode. ]
read(0, 0xffe13fec, 1)                  = -1 EBADF (Bad file descriptor)

You can also provoke other errors that are listed in the man page, such as:

$ strace ./a.out 0</tmp
execve("./a.out", ["./a.out"], [/* 32 vars */]) = 0
[ Process PID=4056 runs in 32 bit mode. ]
read(0, 0xffed5c0c, 1)                  = -1 EISDIR (Is a directory)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top