English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2004-02-07 (12:53)
From: David MENTRE <dmentre@l...>
Subject: [Caml-list] How to read a password without displaying it?

In my textual OCaml program, I would like to read a password. Currently,
I'm using read_line which is not very satisfying as the password is
display on screen.

  let passwd = read_line () in

Any idea how I might read the password without displaying it? 

I tried the following function, using input_char, but the password is
still displayed on screen:

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

Many thanks in advance for any help,
 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