I have a C application which provides a “shell” for entering commands. I’m trying to write some automated test-code for the application (Using CUnit). The “shell” input is read from stdin like so:
fgets(buf, sizeof(buf), stdin);
I can “write” commands automatically to the application by freopen()’ning stdin and hooking it to an intermediate file. When the application is executed “normally” the fgets() call blocks untill characters are available because it is “an interactive device”, but not so on the intermediate file. So how can I fake fgets into thinking the intermediate file is an “interactive device”.
The C program is for Windows (XP) compiled using MinGW.
Regards!
fgetsis not blocking when you are reading from a file because it reaches the end of the file which causesEOFto set on the stream and thus calls tofgetsreturn immediately. When you are running from an interactive inputEOFis never set, unless you type Ctrl-Z (or Ctrl-D on UNIX system) of course.If you really want to use an intermediate file I think you’ll need to enhance your shell so that when it hits an EOF it clears and retests it after a suitable wait. A function like this should work I think:-
You could then call this before the
fgets:-