From 297863493f53c39ae21ff6de94f762338d4d6280 Mon Sep 17 00:00:00 2001 From: Oliver Feldt Date: Tue, 10 Jun 2014 22:22:10 +0200 Subject: [PATCH] nitpicking and better c standard compliant code --- challenge/challenge.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/challenge/challenge.c b/challenge/challenge.c index 61bf348..72e2ec6 100644 --- a/challenge/challenge.c +++ b/challenge/challenge.c @@ -1,26 +1,28 @@ #include #include #include +#include -int main() { - int ret; - size_t nbytes = 0; - char *input_str = NULL; - char *password = "23door42\n"; +const char *password = "23door42\n"; + +int main(void) { + size_t input_size = 0; + char *input_line = NULL; printf("Please enter Password: "); - ret = getline(&input_str, &nbytes, stdin); - if (ret == -1) { - puts("Error"); - } else if (strcmp(input_str, password) == 0) { - puts("Success"); - ret = 0; - } else { - puts("How about no?!"); - ret = -1; + // read in password from standard input, exit on error. + if (getline(&input_line, &input_size, stdin) == -1) { + perror("Error"); + exit(EXIT_FAILURE); } - free(input_str); - return ret; + // compare password, print info, exit appropriately + if (strcmp(input_line, password) == 0) { + puts("Success!"); + exit(EXIT_SUCCESS); + } else { + puts("How about no?!"); + exit(EXIT_FAILURE); + } }