Well, this is probably something totally obvious, but I just can't see it...
I have some code that loops through some command-line arguments and prompts
to remove them if a flag is set (think rm -i

):
Code:
if (in == 1) {
for (; optind < argc; optind++) {
printf("rm: remove %s ('y' or 'n')? ", argv[optind]);
fgets(&response, 2, stdin);
if (response == 'y' || response == 'Y')
rm(argv[optind]);
}
}
else
for (; optind < argc; optind++) {
printf("removing %s\n", argv[optind]); // printf for debug
rm(argv[optind]);
}
The block on the bottom (without "-i" flag set), works as expected, printing the filename then deleting it. But the top block is acting strangely., as it is not stopping to prompt for every second file it loops over. Here is sample output:
Code:
$ ./rm -i foo bar baz
rm: remove foo ('y' or 'n')? y
rm: remove bar ('y' or 'n')? rm: remove baz ('y' or 'n')? y
As you can see, it prompts for first argument fine, then prints the second, but does not prompt, then prompts for the third. It always does this no matter how many arguments. If I pass 2 arguments it prompts for the first, then prints the second prompt but does nothing (ie: the program exits...).
The files that do get prompted are being deleted or not according to the y or n as they should.
Originally I was using scanf instead of fgets, but behavior was exactly the same.
Any ideas? thanks...