1

I want to read input from user. After the user typed the sequence ::, the rest of the input should be asterisks.

For example: let's say user typed: Alex::vn800. On the screen, the output should be: Alex::*****.

I have a function that reads input from user and display * on screen, but I didn't managed to use it in a middle of reading line.

I tried to manipulate functions getchar() and scanf() to stop reading line after detecting a sequence of ::, and then call the function but nothing worked.

What can I do?

Update: Hey! thanks for the answers. I fainlly solved the problem by using the library conio.h - like in any other simple get-password code, just that I saprated it for cases according to what I want the screen will show and not just '*' for any case.

R.O.T
  • 21
  • 2
  • Which OS? Can you use `getch()`? – Spikatrix Aug 16 '15 at 11:21
  • 3
    Curses library springs to mind. See https://en.wikipedia.org/wiki/Curses_%28programming_library%29 or ncurses – Ed Heal Aug 16 '15 at 11:23
  • Is input always coming from a terminal device? – Filipe Gonçalves Aug 16 '15 at 11:23
  • possible duplicate of [how to show enter password in the form of Asterisks(\*) on terminal](http://stackoverflow.com/questions/25990966/how-to-show-enter-password-in-the-form-of-asterisks-on-terminal) – melpomene Aug 16 '15 at 11:39
  • 2
    This isn't quite a duplicate. But as described in [`man getpass`](http://www.gnu.org/software/libc/manual/html_mono/libc.html#getpass), you'd have to manipulate your terminal settings: First, to read input one character at a time, then to disable echoing. All of this assuming Unix; there's no portable way and you didn't specify a platform. – melpomene Aug 16 '15 at 11:42
  • 1
    And you'll get better responses if you include your code, specifically your function that reads input from user and display `*`s on screen. – Spikatrix Aug 16 '15 at 11:58
  • the easiest way is to turn OFF the echo mode, then for each character read, echo it, unless after '::' then echo '*' for each char input, until a new line is read. Use tcgetattr() and tcsetattr() along with a termios.h to turn the echo mode on/off – user3629249 Aug 17 '15 at 05:39

1 Answers1

1

If it's not strictly necessary to have both username and password in the same line, I would suggest simply getting the username first and then using the getpass() function, like here.

I've tried ataman's method, but it didn't work on OSX 10.9.

Here's a modified version, following goldPseudo's approach:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int readChar;
    int status = 0;
    int semicolonCount = 0;

    system ("/bin/stty raw"); // disable buffering and other stuff
    while ((readChar = getchar()) && (readChar != 13 /* ENTER keycode */))
    {
        if (status == 0)
        {
            printf("%c", readChar);

            if (readChar == ':')
            {
                semicolonCount++;
            } else {
                semicolonCount = 0;
            }

            if (semicolonCount == 2)
            {
                status = 1;
            }
        } else {
            printf("*");
        }
    }
    printf("\r\n"); // print new line
    system ("/bin/stty cooked"); // reenable buffering, might not be the original mode the terminal was in

    return 0;
}

The problem with this approach is that, since you are in "raw mode", special characters, like BACKSPACE, ENTER, Ctrl+D and even Ctrl+C, are not processed. You would have to implement the behaviour for those characters yourself.

Community
  • 1
  • 1
hornobster
  • 717
  • 1
  • 7
  • 18