Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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

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
  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

 David Mentré <>

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: