gets() during signal

Ron Stanonik stanonik at nprdc.navy.mil
Tue Dec 11 05:09:53 AEST 1990


I just ran into a problem while porting a program from 4.3bsd to
sunos4.1.  The problem is that gets() in signal handlers seems to
reset stdio's write pointer, but not the read pointer.  Here's
an example program.  It use SIGINT to confirm that the user wants
to exit.  If the user answers "n", it returns to the main program,
but that answer fouls up the gets() in the main program.  Is this
a bug or a feature?  Putting a rewind(stdin) at the end of the
handler seems to fix it, but that seems like voodoo programming.

Thanks,

Ron Stanonik
stanonik at nprdc.navy.mil

#include <stdio.h>
#include <signal.h>

void onint();

main()
{
	char buf[1024];

	signal(SIGINT, onint);

	puts("first");
	gets(buf); puts(buf);
	puts("second");
	gets(buf); puts(buf);
	puts("third");
	gets(buf); puts(buf);
}

void
onint()
{
	char buf[1024];

	puts("exit?");
	gets(buf);
	if (!strcmp(buf, "y"))
		exit(0);
	/*rewind(stdin);*/
}


Here's a sample run
arctic[1] ./sig
first
hello
hello
second
^Cexit?
n
arf
f
third
o
arctic[2] exit



More information about the Comp.unix.internals mailing list