getpasswd.cpp
1 /*
2  name: tools/pkg/getpasswd.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include "getpasswd.h"
30 #include <iostream>
31 
32 #ifdef WIN32
33 
34 string getpasswd(string prompt)
35 {
36  string password;
37  cout <<prompt<<endl;
38 
39  DWORD con_mode;
40  DWORD dwRead;
41 
42  HANDLE hIn=GetStdHandle(STD_INPUT_HANDLE);
43 
44  GetConsoleMode( hIn, &con_mode );
45  SetConsoleMode( hIn, con_mode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT) );
46 
47  cin >> password;
48 
49  SetConsoleMode( hIn, con_mode );
50  return password;
51 }
52 
53 #else // !WIN32
54 
55 #include <termios.h>
56 #include <unistd.h>
57 #include <string>
58 using namespace std;
59 
60 string getpasswd(string prompt)
61 {
62  string password;
63 
64  cout << prompt;
65 
66  termios tty;
67  tcgetattr(STDIN_FILENO, &tty);
68 
69  /* we want to disable echo */
70  tty.c_lflag &= ~ECHO;
71  tcsetattr(STDIN_FILENO, TCSANOW, &tty);
72 
73  cin >> password;
74 
75  tty.c_lflag |= ECHO;
76  tcsetattr(STDIN_FILENO, TCSANOW, &tty);
77  return password;
78 }
79 
80 #endif // WIN32