root/gsdl/trunk/common-src/src/getpw/getpw.cpp @ 19279

Revision 19279, 2.9 KB (checked in by ak19, 11 years ago)

Changed to using stderr for Windows 'getpass' equivalent

  • Property svn:keywords set to Author Date Id Revision
Line 
1/**********************************************************************
2 *
3 * getpw.cpp --
4 * A component of the Greenstone digital library software
5 * from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 2000  The New Zealand Digital Library Project
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, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 **********************************************************************/
25
26#define _XOPEN_SOURCE 1
27/* following is for solaris */
28#define _XOPEN_SOURCE_EXTENDED 1
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 */
50
51#if defined(GSDL_USE_OBJECTSPACE)
52#  include <ospace\std\iostream>
53#elif defined(GSDL_USE_IOS_H)
54#  include <iostream.h>
55#else
56#  include <iostream>
57#endif
58
59// use the standard namespace
60#if !defined (GSDL_NAMESPACE_BROKEN)
61#if defined(GSDL_USE_OBJECTSPACE)
62using namespace ospace::std;
63#else
64using namespace std;
65#endif
66#endif
67
68
69#include <string.h>
70//#include <pwd.h>
71
72
73int main (int argc, char *argv[]) {
74
75  int password_ok = 0;
76  char c[129];
77  int i;
78 
79  for (i=0; i<3; i++)  {
80#if defined(__WIN32__)
81    cerr << "Enter password  (will appear on screen): ";
82    char a[129];
83    cin.getline(a, 128);
84#else
85    char *a = getpass("Enter password:");
86#endif
87
88    int len = strlen (a);
89    if (len < 3 || len > 128) {
90      cerr << "Password must be between 3 and 128 characters long. Try again\n";
91      continue;
92    }
93
94    strcpy (c, a);
95
96#if defined(__WIN32__)
97    cerr << "Re-enter password: ";
98    char b[129];
99    cin.getline(b, 128);
100#else
101    char *b = getpass("Re-enter password:"); 
102#endif
103
104    if ((strcmp (c, b)) == 0) {
105      password_ok = 1;
106      break;
107    }
108    else {
109      cerr << "Passwords didn't match. Try again.\n";
110    }
111  }
112
113  if (!password_ok) {
114    cerr << "Failed to capture password." << endl;
115    return -1;
116  }
117
118  char* p = c;
119  while (*p != 0) {
120    cerr << "char:" << *p << endl;
121    p++;
122  }
123
124  char *salt = "Tp";
125  char *pw = crypt (c, salt);
126
127  cout << pw << "\n";
128 
129  return 0;
130}
Note: See TracBrowser for help on using the browser.