Catching ^C and ^Z

Bob Fisher nts0302 at dsacg3.dsac.dla.mil
Wed Sep 5 21:45:16 AEST 1990


>From article <Sep.4.20.08.17.1990.1216 at utopia.rutgers.edu>, by deen at utopia.rutgers.edu (Cinnamon Raisin):
> 
> 	I am trying to write a quick programme to lock a terminal(TTY)
> 	when I leave it.  Essentially I want to capture all the 
> 	interrupts that can be sent from the keyboard, such as
> 	SIG_INT,SIG_QUIT and so on.  The only reasonable thing I've 
> 	found that lets me do this is ioctl().

Here's a quickie that we use on our BSD systems.  I know this isn't the
right group to post to, but it's small.  It's based on a book on Unix
security - Pat Woods, I think.


#include <stdio.h>
#include <signal.h>
main ()
{
static char rcsid[]="$Header: termlock.c,v 1.2 89/02/13 15:29:12 root Exp $";
char         passbuf[20], *passwd, *getpass ();
int      validentry = 0;
if(strcmp(ttyname(0), "/dev/console") == 0)
	{
	printf("\nYOU CAN'T LOCK THE CONSOLE\n\n");
	return(0);
	}
printf("\n\nThis process enables you to lock your terminal without\n");
printf("logging out.  A password provided by you will be required\n");
printf("to re-activate this terminal.  Don't forget it.\n\n");
while ( ! validentry )
	{
	strcpy (passbuf, getpass ("Enter password: "));
	if ( strlen (passbuf) < 4 )
		printf ("Please enter at least four characters\n");
	else
		validentry = 1;
	}
signal (SIGINT, SIG_IGN);
signal (SIGQUIT, SIG_IGN);
signal (SIGSTOP, SIG_IGN);
signal (SIGTSTP, SIG_IGN);
printf ("\n\n\n\n\n\n\n");
printf ("\t\t********************************************\n");
printf ("\t\t*                                          *\n");
printf ("\t\t*                                          *\n");
printf ("\t\t*                                          *\n");
printf ("\t\t*             TERMINAL LOCKED !!!          *\n");
printf ("\t\t*                                          *\n");
printf ("\t\t*                                          *\n");
printf ("\t\t*                                          *\n");
printf ("\t\t********************************************\n");
printf ("\n\n\n\n\n\n\n");
validentry = 0;
while ( ! validentry )
	{
        passwd = getpass ("Enter password: ");
        if ( strcmp (passwd, passbuf) != 0 )
		{
		printf ("Bad password, try again.\n");
		}
	else
	validentry = 1;
	}
system("clear");
return(0);
}

-- 
Bob Fisher
US Defense Logistics Agency Systems Automation Center
DSAC-TSX, Box 1605, Columbus, OH 43216-5002     614-238-9071 (AV 850-9071)
bfisher at dsac.dla.mil		osu-cis!dsacg1!bfisher



More information about the Comp.lang.c mailing list