reduce complexity + just free() in a single place
- therefor if the program gets extended, its unlikly that someone will forget to call free() - also this gets rid of copy-paste free() calls (which look stupid)
This commit is contained in:
parent
bf5f98649d
commit
ea0b0d95e6
|
@ -3,30 +3,24 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int bytes_read;
|
int ret;
|
||||||
size_t nbytes = 0;
|
size_t nbytes = 0;
|
||||||
char *my_string = NULL;
|
char *my_string = NULL;
|
||||||
char *password = "23door42\n";
|
char *password = "23door42\n";
|
||||||
|
|
||||||
puts("Please enter Password: ");
|
puts("Please enter Password: ");
|
||||||
|
ret = getline(&my_string, &nbytes, stdin);
|
||||||
|
|
||||||
bytes_read = getline(&my_string, &nbytes, stdin);
|
if (ret == -1) {
|
||||||
|
|
||||||
if(bytes_read == -1) {
|
|
||||||
puts("Error");
|
puts("Error");
|
||||||
free(my_string);
|
} else if (strcmp(my_string, password) == 0) {
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
if (strcmp(my_string, password) == 0) {
|
|
||||||
puts("Success");
|
puts("Success");
|
||||||
free(my_string);
|
ret = 0;
|
||||||
return 0;
|
|
||||||
} else {
|
} else {
|
||||||
puts("How about no?!");
|
puts("How about no?!");
|
||||||
free(my_string);
|
ret = -1;
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
free(my_string);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue