Merge branch 'develop' into 'master'

nitpicking and better c standard compliant code

let us...

...use the proper main(void) signature.

...use variable names which speak for their semantic content not their type, as types are explicitly declared.

...use perror() to signal the correct errno message upon error.

...move the password out of the running code, so we can change and find it easily.

...use exit() as it's a linear CLI tool, which can and should exit on certain failure/error states.

...decouple error handling from business logic. (e.g. getline() error handling and strcmp() for password check).

...not free() at the end of the program as it **is** and should **never** be necessary, since the OS **must** handle this.
This commit is contained in:
gnomus 2014-06-10 20:45:25 +00:00
commit 496ebd1158

View file

@ -1,26 +1,28 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h>
int main() { const char *password = "23door42\n";
int ret;
size_t nbytes = 0; int main(void) {
char *input_str = NULL; size_t input_size = 0;
char *password = "23door42\n"; char *input_line = NULL;
printf("Please enter Password: "); printf("Please enter Password: ");
ret = getline(&input_str, &nbytes, stdin);
if (ret == -1) { // read in password from standard input, exit on error.
puts("Error"); if (getline(&input_line, &input_size, stdin) == -1) {
} else if (strcmp(input_str, password) == 0) { perror("Error");
puts("Success"); exit(EXIT_FAILURE);
ret = 0;
} else {
puts("How about no?!");
ret = -1;
} }
free(input_str); // compare password, print info, exit appropriately
return ret; if (strcmp(input_line, password) == 0) {
puts("Success!");
exit(EXIT_SUCCESS);
} else {
puts("How about no?!");
exit(EXIT_FAILURE);
}
} }