Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] How to read a password without displaying it?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David MENTRE <dmentre@l...>
Subject: Re: [Caml-list] How to read a password without displaying it?
Thank you Basile and Maxence for your replies,

In fact, I wanted to do that in caml only, to be portable on Windows and
I'm relunctant to bind to C code. However, from what you said, it seems
impossible.

I tried following trick, using '\b' on output. Strangely enough, the
output_char have no effect, even if I do a flush. The '*' are printed
*after* I do a <RETURN> on my terminal.  I don't understand this
behavior. The flush should send the caracters to the terminal.

let read_password () =
  let password_chars = ref [] in
  let loop = ref true in
  while !loop do
    let c = input_char stdin in
    if c <> '\n' then (
      password_chars := c :: !password_chars;
      output_char stdout '\b';
      output_char stdout '*';
      flush_all ();
     ) else (
      loop := false
     )
  done;
  let password = String.create (List.length !password_chars) in
  let _, res = List.fold_right (fun c (i, s) -> s.[i] <- c; (i+1, s))
      !password_chars (0, password) in
  res


Yours,
d.
-- 
 David Mentré <dmentre@linux-france.org>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners