caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David MENTRE <dmentre@linux-france.org>
To: Basile STARYNKEVITCH <basile@starynkevitch.net>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] How to read a password without displaying it?
Date: Sat, 07 Feb 2004 15:45:43 +0100	[thread overview]
Message-ID: <87d68rrlrc.fsf@linux-france.org> (raw)
In-Reply-To: <20040207131730.GA20547@ours.starynkevitch.net> (Basile STARYNKEVITCH's message of "Sat, 7 Feb 2004 14:17:30 +0100")

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


  parent reply	other threads:[~2004-02-07 14:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-02-07 12:53 David MENTRE
2004-02-07 13:17 ` Basile STARYNKEVITCH
2004-02-07 13:42   ` Maxence Guesdon
2004-02-07 14:45   ` David MENTRE [this message]
2004-02-07 14:54     ` David MENTRE
2004-02-07 14:59     ` Remi Vanicat
2004-02-07 14:50 ` Alain.Frisch
2004-02-07 15:12   ` David MENTRE
2004-02-07 16:31     ` David Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87d68rrlrc.fsf@linux-france.org \
    --to=dmentre@linux-france.org \
    --cc=basile@starynkevitch.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).