Moonie said:
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
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.
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