monotone

monotone Mtn Source Tree

Root/unix/read_password.cc

1/* read_password.c: retrieve the password
2 * Nico Schottelius (nico-linux-monotone@schottelius.org)
3 * 13-May-2004
4 */
5
6#include <unistd.h>
7#include <string.h>
8#include <termios.h>
9#include <iostream>
10#include <string>
11
12#include "sanity.hh"
13
14static void
15echo_on(struct termios & save_term)
16{
17 tcsetattr(0, TCSANOW, &save_term);
18}
19
20static void
21echo_off(struct termios & save_term)
22{
23 struct termios temp;
24 tcgetattr(0,&save_term);
25 temp=save_term;
26 temp.c_lflag &= ~(ECHO | ECHOE | ECHOK);
27 tcsetattr(0, TCSANOW, &temp);
28}
29
30void
31read_password(std::string const & prompt, char * buf, size_t bufsz)
32{
33 struct termios save_term;
34 I(buf != NULL);
35 memset(buf, 0, bufsz);
36 std::cout << prompt;
37 std::cout.flush();
38 echo_off(save_term);
39 std::cin.getline(buf, bufsz, '\n');
40 std::cout << std::endl;
41 echo_on(save_term);
42}
43
44// Local Variables:
45// mode: C++
46// fill-column: 76
47// c-file-style: "gnu"
48// indent-tabs-mode: nil
49// End:
50// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status