Link to home
Start Free TrialLog in
Avatar of morrin
morrin

asked on

How do get my program to read in input from the terminal which I do not want to appear , i.e as in a password prompt


Hi

What I want is for my program to do something like , prompt for a password such as

%/
   Pleas enter passwd ...

But while I want to read this in to check it , I don't want it to appear on the screen . i.e. I don't want anything to print to the screen until the user presses enter.

If anybody could tell me how to do this in C or using a script I would be grateful

Thanks
ASKER CERTIFIED SOLUTION
Avatar of TheComputerWhisperer
TheComputerWhisperer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TheComputerWhisperer
TheComputerWhisperer

As a matter of fact, this might be a little better...

#include <stdio.h>
#include <conio.h>
#include <ctype.h>

#define MAX_CHARS 80

int main (void)
{
      char password [MAX_CHARS + 1];
      int index = 0;
      char inchar;

      _cputs("Enter your password: ");

      do
      {
            inchar = _getch();

            if (isprint(inchar))
            {
                  if (index < MAX_CHARS)
                  {
                        password[index] = inchar;
                        index++;
                  }
                  else
                  {
                        _putch('\a');
                  }
            }
            else
            {
                  if (inchar == '\b')
                  {
                        if (index > 0)
                        {
                              index--;
                        }
                        else
                        {
                              _putch('\a');
                        }
                  }
                  else if (inchar == '\n')
                  {
                        _getch();
                  }
            }
      } while (inchar != '\r');

      password[index] = '\0';

      _cputs("\n\nYou typed: ");
      _cputs(password);
      _cputs("\n");

      return 0;
}
Avatar of morrin

ASKER


Hi

First up thanks for replying so quickly. However when I try to compile the code I get

/tmp/ccTsP5ye.o: In function `main':
/tmp/ccTsP5ye.o(.text+0x16): undefined reference to `_cputs'
/tmp/ccTsP5ye.o(.text+0x21): undefined reference to `_getch'
/tmp/ccTsP5ye.o(.text+0x45): undefined reference to `_getch'
/tmp/ccTsP5ye.o(.text+0x73): undefined reference to `_cputs'
/tmp/ccTsP5ye.o(.text+0x82): undefined reference to `_cputs'
/tmp/ccTsP5ye.o(.text+0x92): undefined reference to `_cputs'


Obviously I'm making some silly little error in compiling it . Any ideas on where I'm going wrong.

Thanks again
Try removing the underscores from in front of the function names.
Although, if you are using a *nix system, getch won't be available anyway.
Avatar of morrin

ASKER


Yeah , Am using FreeBsd and Red H.Does getch do the same as getc?
LOL @ my MS world.

For C-compliant C code, remove the "_" before the functions' names.

_cputs(...) => cputs(...)
_getch(...) => getch(...)
_putch(...) => putch(...)
Avatar of morrin

ASKER

No actually theres a man page for getch. Just in another header file
Avatar of morrin

ASKER


Here , Thanks a million anyway. I can work on it from here. Here's the points for ya.
No... getch() doesn't echo.  getc(stdin) / getchar() echos.
hi,
Why all this headache. Why don't u use getpass function to do this.