About this function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dgottipati
    New Member
    • Oct 2006
    • 1

    #1

    About this function

    hai,


    could any one explian me abt this function and what it does?

    i found it in a program so i am posting it as it is.

    void setraw(struct termio &tty) { struct termio oldtty; oldtty = tty; oldtty.c_lflag &= ~ICANON; oldtty.c_lflag &= ~ECHO; oldtty.c_cc[VTIME] = 0; oldtty.c_cc[VMIN] = 1; ioctl(0, TCSETA, &oldtty); } void setcanon(struct termio &tty) { ioctl(0, TCSETA, &tty); }
    thanks in advance.

    BYE
    Dinnu.
  • tyreld
    New Member
    • Sep 2006
    • 144

    #2
    These two functions can be used to disable/renable echoing of input to a terminal console (SVR4 based Unixes, this is not compatible with BSD). Generally, you would use this in a utility that gets a password from a user. The effect is that no input is echoed to the console to prevent over the shoulder password theft. The setraw function turns off echo, while setcanon restores it.

    Code:
    #include <sys/ioctl.h>
    #include <termio.h>
    #include <stdio.h>
    
    void setraw(struct termio tty) {
      struct termio oldtty;
      oldtty = tty;
      oldtty.c_lflag &= ~ICANON;
      oldtty.c_lflag &= ~ECHO;
      oldtty.c_cc[VTIME] = 0;
      oldtty.c_cc[VMIN] = 1;
      ioctl(0, TCSETA, &oldtty);
    }
    
    void setcanon(struct termio tty) {
      ioctl(0, TCSETA, &tty);
    }
    
    int main(int argc, char **argv)
    {
      struct termio tty;
      char buf[1024];
    
      // Get current terminal attributes
      ioctl(0, TCGETA, &tty);
    
      printf("Enter some text : ");
    
      // Turn of echo on terminal
      setraw(tty);
    
      scanf("%s", &buf);
    
      // Restore old terminal settings with echo
      setcanon(tty);
    
      printf("\nDid you see anything?\n");
    
      return 0;
    }

    Comment

    Working...