Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read from the console with scan without echoing the characters?

Tags:

passwords

r

Here is an example function:

    passwordEntry <- function() {
            cat("Enter your password: ")
            pwd <- scan(n=1, what=character(), quiet=TRUE)
            invisible(pwd)
    }

And to test the function:

    >   passwordEntry()
    Enter your password: 
    1: test
    > 

Is there a way to suppress what the user types? Or replace it with some other character? I have written a tcl/tk function to prompt the user for a password but it doesn't work on our Linux server.

Here is an example from Paul's link below. This does not work either on Linux or Windows (the latter probably because I don't have a proper C compiler so will look into that).

getkey3.c

    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    void mygetch ( int *ch ) 
    {
            struct termios oldt, newt;
            tcgetattr ( STDIN_FILENO, &oldt );
            newt = oldt;
            newt.c_lflag &= ~( ICANON | ECHO );
            tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
            *ch = getchar();
            tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
            return;
    }

And my test.r script:

    system('R CMD SHLIB getkey3.c')
    dyn.load("getkey3.so")
    .C("mygetch",as.integer(0))
    dyn.unload("getkey3.so")

I get this:

    > .C("mygetch",as.integer(0))
    [[1]]
    [1] -1
like image 263
jbryer Avatar asked Sep 20 '12 17:09

jbryer


1 Answers

This post covers how to read individual keystrokes:

Detecting single keystrokes

The accepted answer uses a small piece of C code that returns individual keystrokes. You can then capture the keystrokes for your password and echo nothing to the user or maybe a *.

like image 122
Paul Hiemstra Avatar answered Sep 28 '22 01:09

Paul Hiemstra