I'm a beginner (and I will probably be forever…), so I visited the FAQ section. Right now I'm studying the
FAQ > Environment and user data page and I try to make the examples there work by modifying them to make sense.
I failed with the last one however.
Here's the example code from that page:
Code:
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
struct passwd *p;
p = getpwnam( "username" );
if (p)
{
if( strcmp( crypt( "Password", p->pw_passwd ), p->pw_passwd ) == 0 )
{
puts( "match" );
}
else
{
puts( "no match" );
}
}
Of course this doesn't work out of the box, so I modified it a bit:
Code:
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <crypt.h>
int
main(int argc, char *argv[])
{
if (argc==2) {
struct passwd *p;
p=getpwnam(getlogin());
if(p) {
if(strcmp(crypt(argv[1], p->pw_passwd), p->pw_passwd)==0)
puts("Right!");
else
puts("Wrong!");
}
return 0;
}
else {
puts("Syntax:");
puts("Password <password>");
return 1;
}
}
Maybe I did an obvious mistake, I don't know. Maybe I just misunderstood the whole concept.
The problem is that whatever I try with as input, as long as there are exactly one parameter, the output is ”Wrong!”, even when the correct password is used.
So what obvious little thing did I miss?
Here's what the output from running it would look like, if the correct password is ”ThisIsTheCorrectPa55word”:
Code:
$ ./PasswordTest
Syntax:
PasswordTest <password>
$ ./PasswordTest aligraljrhgor
Wrong!
$ ./PasswordTest ThisIsTheCorrectPa55word
Wrong!
$
Compiled with:
Code:
gcc PasswordTest.c -lcrypt -o PasswordTest
There were no errors when compiling.
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3