Skip to content

Instantly share code, notes, and snippets.

@lewtds
Created October 23, 2012 08:44
Show Gist options
  • Save lewtds/3937672 to your computer and use it in GitHub Desktop.
Save lewtds/3937672 to your computer and use it in GitHub Desktop.
Chin's password test
// Chin
#include <string.h>
#include <stdio.h>
#define MAX_LEN 10
int fread_string(FILE *in, char out[], size_t len)
{
// TODO investigate further this len thing
if (fgets(out, len+1, in) == NULL) {
if (feof(in)) return 1; // We could have returned EOF here but unfortunately EOF is also defined as -1
else return -1; // Error
}
if (out[strlen(out)-1] == '\n') out[strlen(out)-1] = '\0'; // Replace the last '\n' with '\0'
//strncpy(out, in, STRING_BUF_LEN);
return 0;
}
int main() {
char password[MAX_LEN] = "password\0";
char user_string[MAX_LEN*2];
fread_string(stdin, user_string, MAX_LEN);
if (strlen(user_string) != strlen(password)) {
printf("Bad!\n");
return 0;
}
if (strncmp(password, user_string, strlen(password)) == 0) {
printf("OK!\n");
} else {
printf("Bad!\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment