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