Catching ^C and ^Z

Will Crowder will at kfw.COM
Thu Sep 6 03:30:15 AEST 1990


In article <Sep.4.20.08.17.1990.1216 at utopia.rutgers.edu> deen at utopia.rutgers.edu (Cinnamon Raisin) writes:
>
>Hi boys and girls,
>[I want a lockscreen program]
>	The following is a short programme I wrote to test this out, 
>	but it dumps on me, and I don't know why.
>
>
>------------
>
>#include <stdio.h>
>#include <sys/ioctl.h>
>#include <sgtty.h>
>char TMP;			/* to hold keyboard entry */
>struct sgttyb *TTY;		/* From unix ref	  */
>
>int main(void)
>{
>  ioctl(0,TIOCGETP,TTY);	/* Get current status	  */
>  TTY->sg_flags = RAW | ECHO;   /* Unprocessed and echoed */
>  ioctl(0,TIOCSETN,TTY);	/* to the screen	  */
>  while(1 == 1) {TMP = getchar();} 
>  exit(0);
>}

You're calling ioctl() with a pointer to garbage, namely, TTY.  This
ioctl() wants a pointer to the structure to be read from/filled in,
but this structure must be defined somewhere.  Change

struct sgttyb *TTY;

to

struct sgttyb TTY;

and change both of your ioctl() calls to use &TTY instead of just TTY,
and things should be happy.  Oh, and for complete portability and
correctness, cast the &TTY to a char * or caddr_t or whatever your particular
ioctl() wants.  (man ioctl)  Oh, and yeah, you don't need that TMP variable.
Just call getchar() or whatever.  C will throw away the return value.
If you want lint to be quiet about it, say (void) getchar().

Will

P.S.  The 1 == 1 in your while statement is unnecessary.  Use while (1)
      or for (;;).  The latter is considered by many to be superior, because
      many older compilers generate better code for it.  (The reason for
      that is too lengthy to go into in this article.)



More information about the Comp.lang.c mailing list