We have two exit code code PAM_SUCCESS(0), PAM_AUTH_ERR(7).
If we wanna return 0 or 7 when we compared password with our password in following code which code is better that this code ?
if (strcmp (expected_password, crypt (password, expected_password)) == 0)
retval = PAM_SUCCESS;
else
retval = PAM_AUTH_ERR;
But we want to decrease our lines of code to one or two lines!
retval = -!!strcmp (expected_password, crypt (password,
expected_password)) & PAM_AUTH_ERR;
I think this code is bad cause we have many character on one line and using bitwise operators is bad practice. We can break this line in following format :
retval = -!!strcmp (expected_password, crypt (password, expected_password));
retval &= PAM_AUTH_ERR;
Also I think this code is bad cause we can refactor it on previous code :))
Actually i don't know what is the best way for refactoring this code.
We also can use following codes :
retval = strcmp (expected_password, crypt (password,
expected_password)) == 0 ? PAM_SUCCESS : PAM_AUTH_ERR;
This code is very long and dirty! we can refactor it in previous codes.
Also we can use this pattern :
retval = strcmp (expected_password, crypt (password, expected_password)) == 0;
retval = retval ? PAM_SUCCESS : PAM_AUTH_ERR;
But on second line we repeated retval word though we can use another codes!
Which is the best code and why ?