Changeset 23047 for main/trunk/greenstone2/common-src/src/getpw/getpw.cpp
- Timestamp:
- 2010-10-06T11:18:32+13:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/common-src/src/getpw/getpw.cpp
r19821 r23047 28 28 // #define _XOPEN_SOURCE_EXTENDED 1 29 29 30 #include "gsdlconf.h" 31 32 33 // include crypt 34 #if defined(__WIN32__) 35 #include "crypt.h" 36 #else 37 38 #include "config.h" 39 #if defined(HAVE_CRYPT_H) 40 #include <crypt.h> 41 #endif 42 43 #if defined(HAVE_UNISTD_H) 44 #include <unistd.h> 45 #endif 46 /* we are probably in trouble if we have neither crypt.h or unistd.h, 47 but for now we'll do nothing about it... */ 48 49 #endif /* not WIN32 */ 30 // Windows version seems to be sensitive to the order of <iostream> and <crypt.h> 31 // having <iostream> *after* <crypt.h> lead to linking error such as: 32 // __freea already defined in getpw.obj 33 // which seems to be caused by compiler confusion of whether it should be using 34 // /MD or /MT (which control dynamic or static/multi-threaded linking) 50 35 51 36 #if defined(GSDL_USE_OBJECTSPACE) … … 59 44 // use the standard namespace 60 45 #if !defined (GSDL_NAMESPACE_BROKEN) 61 # if defined(GSDL_USE_OBJECTSPACE)46 # if defined(GSDL_USE_OBJECTSPACE) 62 47 using namespace ospace::std; 63 # else48 # else 64 49 using namespace std; 65 # endif50 # endif 66 51 #endif 67 52 53 #include "gsdlconf.h" 54 55 // include crypt 56 #if defined(__WIN32__) 57 # include <windows.h> 58 # include <stdio.h> 59 # include <crypt.h> 60 #else 61 # include "config.h" 62 # if defined(HAVE_CRYPT_H) 63 # include <crypt.h> 64 # endif 65 66 # if defined(HAVE_UNISTD_H) 67 # include <unistd.h> 68 # endif 69 /* we are probably in trouble if we have neither crypt.h or unistd.h, 70 but for now we'll do nothing about it... */ 71 72 #endif /* not WIN32 */ 68 73 69 74 #include <string.h> 70 75 //#include <pwd.h> 71 76 77 #if defined(__WIN32__) 78 char* getPassword(HANDLE hSTDIN) 79 { 80 char a[129]; 81 int charCount = 0; 82 DWORD cNumRead; 83 INPUT_RECORD irIn[128]; 84 85 bool done = false; 86 while (!done) 87 { 88 if (!ReadConsoleInput(hSTDIN, irIn, 128, &cNumRead)) 89 { 90 cerr << "There was an error reading input from the console\n"; 91 return strdup(""); 92 } 93 94 for(int j = 0; j < cNumRead; j++) 95 { 96 if(irIn[j].EventType == KEY_EVENT) 97 { 98 KEY_EVENT_RECORD keyEvent = irIn[j].Event.KeyEvent; 99 if(keyEvent.bKeyDown == false) 100 { 101 if(keyEvent.wVirtualKeyCode == VK_RETURN) 102 { 103 done = true; 104 cout << "\n"; 105 } 106 else 107 { 108 if(charCount < 129) 109 { 110 a[charCount++] = keyEvent.uChar.AsciiChar; 111 } 112 } 113 } 114 } 115 } 116 } 117 a[charCount] = '\0'; 118 return strdup(a); 119 } 120 #endif 72 121 73 122 int main (int argc, char *argv[]) { … … 79 128 for (i=0; i<3; i++) { 80 129 #if defined(__WIN32__) 81 cerr << "Enter password (will appear on screen): "; 82 char a[129]; 83 cin.getline(a, 128); 130 HANDLE hSTDIN; 131 DWORD fdwMode, fdwPrevMode; 132 int counter=0; 133 134 hSTDIN = GetStdHandle(STD_INPUT_HANDLE); 135 if (hSTDIN == INVALID_HANDLE_VALUE) 136 { 137 cerr << "There was an error getting a handle to the standard input\n"; 138 return 1; 139 } 140 141 if (!GetConsoleMode(hSTDIN, &fdwPrevMode)) 142 { 143 cerr << "There was an error getting a handle to the current console mode\n"; 144 return 2; 145 } 146 147 fdwMode = NULL; 148 if (!SetConsoleMode(hSTDIN, fdwMode)) 149 { 150 cerr << "There was an error setting the new handle mode\n"; 151 return 3; 152 } 153 154 cout << "Enter password:"; 155 char* a = getPassword(hSTDIN); 156 84 157 #else 85 char *a = getpass("Enter password:");158 char* a = getpass("Enter password:"); 86 159 #endif 87 88 160 int len = strlen (a); 89 161 if (len < 3 || len > 128) { … … 93 165 94 166 strcpy (c, a); 95 167 96 168 #if defined(__WIN32__) 97 169 cerr << "Re-enter password: "; 98 char b[129]; 99 cin.getline(b, 128); 170 char* b = getPassword(hSTDIN); 100 171 #else 101 char *b = getpass("Re-enter password:");172 char* b = getpass("Re-enter password:"); 102 173 #endif 103 104 174 if ((strcmp (c, b)) == 0) { 105 175 password_ok = 1; … … 115 185 return -1; 116 186 } 117 118 187 119 188 char *salt = "Tp"; 120 189 char *pw = crypt (c, salt); 121 190 122 191 cout << pw << "\n"; 123 192 124 193 return 0; 125 194 }
Note:
See TracChangeset
for help on using the changeset viewer.