blocxx
GetPass.cpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
38
39#include "blocxx/BLOCXX_config.h"
40#include "blocxx/GetPass.hpp"
41#include <cstring>
42extern "C"
43{
44#ifdef BLOCXX_HAVE_UNISTD_H
45#include <unistd.h>
46#endif
47
48#if defined(BLOCXX_WIN32)
49#include <conio.h>
50#include <stdio.h>
51#endif
52}
53
54#ifdef BLOCXX_NETWARE
55#include <screen.h>
56#endif
57
58namespace BLOCXX_NAMESPACE
59{
60
61#if defined(BLOCXX_WIN32)
62#define MAXPASSWORD 128
64GetPass::getPass(const String& prompt)
65{
66 int ch, len = 0;
67 char bfr[MAXPASSWORD+1];
68
69 bfr[0] = 0;
70 _cputs(prompt.c_str());
71
72 do
73 {
74 ch = _getch();
75 if (ch != '\r' && len < MAXPASSWORD)
76 {
77 bfr[len++] = ch;
78 bfr[len] = 0;
79 _putch('*');
80 }
81 } while (ch != '\r');
82 printf("\n");
83 return String(bfr);
84}
85#else
88{
89#ifdef BLOCXX_NETWARE
90 char pw[128];
91 char* ptr = ::getpassword(prompt.c_str(), pw, 128);
92#else
93 char* ptr = ::getpass(prompt.c_str());
94#endif
95 String retStr = ptr;
96 memset(ptr, 0x00, strlen(ptr) * sizeof(char));
97 return retStr;
98}
99#endif
100
101} // end namespace BLOCXX_NAMESPACE
102
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition String.hpp:67
const char * c_str() const
Definition String.cpp:905
BLOCXX_COMMON_API String getPass(const String &prompt)
The getpass function displays a prompt to the standard error output, and reads in a password from /de...
Definition GetPass.cpp:87
Taken from RFC 1321.