Thread: user input echo off

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    user input echo off

    I have a program where it accepts user input in a password style, and I don't want to have their password show up on the screen as they type it. How would I accomplish this? I was talking to a few friends and they said something about some asm shellcode redirecting the keyboard output elsewhere, but where I would find such shellcode, baffles me.
    If this is NOT the solution that I seek, please help as well.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    #include <conio.h>

    int getch();

    FAQ

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How would I accomplish this?
    Depends on your system. Quite a few Windows compilers support getch and some form of conio.h. On Unix or Linux systems you can fake it using something like this:
    Code:
    #include <termios.h>
    #include <unistd.h>
    
    int setecho(int fd, int onoff)
    {
        struct termios t;
    
        if (tcgetattr(fd, &t) == -1)
            return -1;
    
        if (onoff == 0)
            t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
        else
            t.c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL);
    
        if (tcsetattr(fd, TCSANOW, &t) == -1)
            return -1;
    }
    Code:
    setecho(STDIN_FILENO, 0);
    
    /* Read password */
    
    setecho(STDIN_FILENO, 1);
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    On Unix systems you can also use getpass from <unistd.h>:
    Code:
    #include <unistd.h>
    
    int main(void)
    {
        char *p;
        p = getpass("Password: ");
    }
    $ENV: FreeBSD, gcc, emacs

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Originally posted by cc0d3r
    On Unix systems you can also use getpass from <unistd.h>:
    Code:
    #include <unistd.h>
    
    int main(void)
    {
        char *p;
        p = getpass("Password: ");
    }
    the function getpass() is obsolete and should not be used anymore.

    better do something like
    Code:
    system("stty -echo"); /* switch echo off */
    system("stty echo"); /* switch it on again */

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Originally posted by threahdead
    the function getpass() is obsolete and should not be used anymore.
    Can you provide a little more info on that?
    $ENV: FreeBSD, gcc, emacs

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yea and the system() command is sooo great

  8. #8
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    --------------------------------------------------------------------------------
    Originally posted by threahdead
    the function getpass() is obsolete and should not be used anymore.

    --------------------------------------------------------------------------------



    Can you provide a little more info on that?
    is says so right in its own man pages!!

  9. #9
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    fine... no system...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    
    int spawn(char* program, char** arg_list)
    {
      pid_t child_pid;
      child_pid = fork();
      if(child_pid!=0) //parent process
        return child_pid;
      else //child process (exec_only)
      {
        execvp(program,arg_list);
        fprintf(stderr,"an error has occured in execvp\n");
        abort();
      }
    }
    int main()
    {
      char* list1 = {"stty","-echo",NULL}; //OFF list
      char* list2 = {"stty","echo",NULL}; //ON list
      /* prompt code here */
      spawn("stty",list1); //turn echo off
      /* place code here */
      spawn("stty",list2); //turn echo on
      return 0;
    }
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is says so right in its own man pages!!
    Not mine, I use the latest stable version of FreeBSD.
    My best code is written with the delete key.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Prelude
    >is says so right in its own man pages!!
    Not mine, I use the latest stable version of FreeBSD.
    From "IEEE Std 1003.1, 2003 Edition"
    The following features, marked legacy or obsolescent in the base documents, are not carried forward into IEEE Std 1003.1-2001. Other features from the base documents marked legacy or obsolescent are carried forward unless otherwise noted.

    From XSH5, the following legacy interfaces, headers, and external variables are not carried forward:

    advance(), brk(), chroot(), compile(), cuserid(), gamma(), getdtablesize(), getpagesize(), getpass(), getw(), putw(), re_comp(), re_exec(), regcmp(), regex(), sbrk(), sigstack(), step(), ttyslot(), valloc(), wait3(), <re_comp.h>, <regexp.h>, <varargs.h>, loc1, __loc1, loc2, locs
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM
  4. A breakthrough
    By loopy in forum Linux Programming
    Replies: 4
    Last Post: 11-26-2003, 06:46 PM
  5. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM