Hi,
I am trying to gain test coverage on the 'fold' program in unix and am having difficulty executing the following code:
if(ferror (istream))
{
error (0, errno, "%s", filename);
if(!STREQ (filename, "-"))
fclose (istream);
return 1;
}
if (!STREQ (filename, "-") && fclose (istream) == EOF)
{
error (0, errno, "%s", filename);
return 1;
}
How could I cause an error to occur in the input stream using the fold command in the command prompt (i.e. fold < input).
Thank you!
What that checks is the possible errors the read() system call can return. One easy one is:
SIGINT to get EINTR - use a big file for input and try ctrl/c.
EISDIR - run fold on a directory
Chances are most of the above errors are caught long before read. By stat-ing the file. Or signal trapping.
Anyway read the man page for read to see what errors you can create.
Thanks! Oddly enough, the directory tip worked. I originally tried a nonexistent file, and that was caught before fold ran. But, the directory run works.
However, everything inside of an if(STREQ...) statement is still not covered. I do not understand this 'if' statement. I tried creating a directory named "-" to get past the ferror 'if' statement and evaulate to true on the STREQ(filename, "-") statement, but it did not work. Any ideas?
STREQ is usually defined something like this
#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
The idea is to improve string comparison speed that is highly likely to fail on the first character. In the case above, filenames do almost never start with a '-' character.
And it is faster than strcmp() all by itself in this application.
This could be supporting the case:
cat somefile | fold --
if your fold implementation allows - as meaning read from stdin.
I didn't completely understand what you were getting at, but if its just an efficient comparison and - is the beginning of stdin, anything that is not stdin will pass this line?
Focusing on this line:
if (!STREQ (filename, "-") && fclose (istream) == EOF)
I'll still have to deal with fclose(istream) == EOF. To me, this line means once a file has come to its end. So, it would evaluate to true at the end of every file.
So, does this mean to get into this code segment, I just need input from non-standard input into fold? If I'm right, how do I do this? I have limited experience with unix.
Thanks for all the help. 
No. fclose() == EOF means there was an error closing the file. EOF is defined as -1, and is also what fgetc() returns on EOF. The coder chose EOF which is unfortunate if you are not familiar with libc stdio functions. fclose calls close(), cleans up buffers, etc.
If you want to continue with your project you need to understand:
what each function returns on error, and what system call(s) the stdc function uses underneath.
what/how to cause the error
For example, in this case
This could happen when a file descriptor cannot be closed when the filesystem becomes dismounted or corrupted, as can happen with an NSF mount.
If you read the close 2 man page, and can 'create' any of the errors you see there and then fclose will barf.
You could corrupt the FILE * struct,
close(fileno(istream));
but this means you would have to add your code to the original.