By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,046 Members | 2,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,046 IT Pros & Developers. It's quick & easy.

how to write a password routine that doesn't echo to the screen?

P: n/a
Hi,

When entering a password we don't really see the characters - we see
**** instead. How can I write such a routine in C? How can I 'echo
off' the inputs? How can I get a characters/strings from the keyboard
without typing them and ENTER at the end? Like read(kbd,...) in BASIC?

I am running mingw on an XP platform,

Thanks,

Pflanzen.
Nov 14 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Pflanzen Gold <pf**********@yahoo.com> scribbled the following:
Hi, When entering a password we don't really see the characters - we see
**** instead. How can I write such a routine in C? How can I 'echo
off' the inputs? How can I get a characters/strings from the keyboard
without typing them and ENTER at the end? Like read(kbd,...) in BASIC?


You can't do it, within the limits of ISO standard C code. You have to
use OS-specific APIs.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"C++. C++ run. Run, ++, run."
- JIPsoft
Nov 14 '05 #2

P: n/a
pf**********@yahoo.com (Pflanzen Gold) writes:
When entering a password we don't really see the characters - we see
**** instead. How can I write such a routine in C? How can I 'echo
off' the inputs? How can I get a characters/strings from the keyboard
without typing them and ENTER at the end? Like read(kbd,...) in BASIC?

I am running mingw on an XP platform,


It can't be done portably in C. For non-portable solutions, try a
system-specific newsgroup.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3

P: n/a
Pflanzen Gold wrote:

When entering a password we don't really see the characters - we see
**** instead. How can I write such a routine in C? How can I 'echo
off' the inputs? How can I get a characters/strings from the keyboard
without typing them and ENTER at the end? Like read(kbd,...) in BASIC?

I am running mingw on an XP platform,


You can't do it in standard portable C, as discussed here. Any
specific system will probably have means, so you need to find a
newsgroup that discusses your system. The resultant code will not
be portable, and thus is off-topic here.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #4

P: n/a

the topic: he asked how to write the function for mingw for xp, i happe
to answer his question. which was on topic. maybe the wrong forum, bu
it is a forum on c. so i don't understand. i don;t happen to see
mingw forum. i assume that this forums fits in all categories and al
compilers on c programming where the other one doesn't fit. kinda lik
the c++ forum, i do see a vc++ forum. i assume that the c and c++ foru
is for everybody on general programming. i assume. i could be wrong
please tell me which forum to use. by mistake i posted dos, windows
and linux not just the mingw version. so sorry
-
Mooni
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Nov 14 '05 #5

P: n/a
Moonie <Mo***********@mail.codecomments.com> wrote:
the topic: he asked how to write the function for mingw for xp, i happen
to answer his question. which was on topic. maybe the wrong forum, but
it is a forum on c. so i don't understand. i don;t happen to see a
mingw forum. i assume that this forums fits in all categories and all
compilers on c programming where the other one doesn't fit. kinda like
the c++ forum, i do see a vc++ forum. i assume that the c and c++ forum
is for everybody on general programming. i assume. i could be wrong.
please tell me which forum to use. by mistake i posted dos, windows,
and linux not just the mingw version. so sorry.
Your assuption is wrong. This group has always been about the language
C and not about some implementations of C. That's the same difference
as let's say between the abstract concept of a car and a Mercedes Benz
S320 model. So, things that can be only done using system specific
extensions to C and/or rely on some obscure compiler features are off-
topic here. For such questions there are usually enough other groups/
mailing lists that deal with that kind of problems where the experts
for that kind of questions are around and can immediately comment with
authority on it when you make mistakes in the information you post.

Now a few points concerning your getpass() functions (I re-indented
it to make it readable). First of all, it won't do you any good on
Unix (including Linux) since there's already a getpass() function
and your redefition will only make the linker unhappy. Moreover
#else
unsigned char x;
read( 0, &x, 1 );
return (int) x;
#endif
which seems to be thought to be for Linux will definitely _not_
keep the characters from appearing on the screen. And if the
user hits ^D (or the password is read from a file and read() hits
the end of the file) that will return rubbish, reading the value
of an uninitialized variable. And even if it would you would still
have to include <unistd.h>, that's where read(2) is declared.
char* getpass( const char *prompt )
{
int ch, len, maxlen=9;
Since when there's a law that passwords aren't allowed to be
longer than 9 chars?
static char s[10], *p;

len = 0;
puts((char*)prompt);
Get rid of the useless cast. puts() expects a 'const char *'. as
its argument. And puts() also prints an additional '\n', which
usually isn't what you want in that situation.
p = s;

while ((ch = mygetch()) != '\r' && ch != '\n' && ch != 27 &&
len <= maxlen)
That keeps the user from e.g. entering 10 characters and then hitting
the backspace key several times. Instead, a password that the user
knows to be wrong gets returned automatically. And on several systems
hitting the escape key won't stop entering a password. And what
about your mygetch() function returning EOF?
{
if (ch == '\b')
{
if ( len > 0 )
That would be clearer if written as

if ( ch == '\b' && len > 0 )
{
putchar('\b');
putchar(' ');
putchar('\b');
--len;
--p;
}
}
else if (ch < 32 || ch > 127)
;
else
Why don't you simply write

else if ( ch >= 32 || ch <= 127 )

And this will of course only work with a ASCII. And at least 32
could easily be replaced by "' '". Even worse, on some systems
it might be allowed to have characters in passwords not in that
range and people having such a password won't be able to enter
theirs.
{
putchar("*");
There are several systems where nothing at all gets shown while you
enter a password (which, IMHO, is a lot better because nobody can see
how many letters your password has, which is an important piece of
information since it reduces the number of possibilities an
attacker has to try quite a lot).
*p++ = (char) ch;
++len;
}
}

*p = '\0';
return( s );
}


Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #6

P: n/a
Je***********@physik.fu-berlin.de scribbled the following:
Moonie <Mo***********@mail.codecomments.com> wrote:
else if (ch < 32 || ch > 127)
;
else
Why don't you simply write else if ( ch >= 32 || ch <= 127 )


Um, because it's always going to be true? I dare you to find a number
that is neither greater-or-equal than 32 nor less-or-equal than 127.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
Nov 14 '05 #7

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Je***********@physik.fu-berlin.de scribbled the following:
Moonie <Mo***********@mail.codecomments.com> wrote:
else if (ch < 32 || ch > 127)
;
else
Why don't you simply write else if ( ch >= 32 || ch <= 127 )
Um, because it's always going to be true? I dare you to find a number
that is neither greater-or-equal than 32 nor less-or-equal than 127.


Grrrrr;-) Make that

else if ( ch >= 32 && ch <= 127 )

Thanks, Joona.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #8

P: n/a
Moonie wrote:

the topic: he asked how to write the function for mingw for xp, i happen
to answer his question. which was on topic. maybe the wrong forum, but
it is a forum on c. so i don't understand. i don;t happen to see a
mingw forum. i assume that this forums fits in all categories and all
compilers on c programming where the other one doesn't fit. kinda like
the c++ forum, i do see a vc++ forum. i assume that the c and c++ forum
is for everybody on general programming. i assume. i could be wrong.
please tell me which forum to use. by mistake i posted dos, windows,
and linux not just the mingw version. so sorry.


If it isn't portable to all systems meeting the C standard, it is
OT here. Asking if something is OT (off-topic) is on-topic.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Je***********@physik.fu-berlin.de scribbled the following:

[...]
Why don't you simply write

else if ( ch >= 32 || ch <= 127 )


Um, because it's always going to be true? I dare you to find a number
that is neither greater-or-equal than 32 nor less-or-equal than 127.


<OT>i</OT>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

P: n/a
Keith Thompson <ks***@mib.org> scribbled the following:
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Je***********@physik.fu-berlin.de scribbled the following: [...]
> Why don't you simply write

> else if ( ch >= 32 || ch <= 127 )


Um, because it's always going to be true? I dare you to find a number
that is neither greater-or-equal than 32 nor less-or-equal than 127.

<OT>i</OT>.


Well yes, but that's hardly going to fit in a normal char or int type,
is it? Doesn't C99 have a standard complex type? How do you represent
i in that, and how does it compare to real numbers?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
Nov 14 '05 #11

P: n/a

i did say i am sorry. i typed it in about 15 minutes. you have to us
curses with noecho() if you don't want it to display. i wanted it to b
as portable as possible i used stdio which most compilers support. an
with mingw you can getch() from conio instead of mygetch(); i progra
for windows and dos console
-
Mooni
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.