# HG changeset patch # User Christian Urban # Date 1412627353 -3600 # Node ID 166a27693dcdff64b8eb7fa515376188fafca9d3 # Parent 9c2fa54c7c2dbc2c2db6a11e0a098f392d3487fa added test diff -r 9c2fa54c7c2d -r 166a27693dcd progs/test2.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/test2.c Mon Oct 06 21:29:13 2014 +0100 @@ -0,0 +1,54 @@ +#include +#include +#include + +// for installation notes see C0.c +// this program can be called with +// +// ./args2-good | ./C2 +// +// or +// +// ./args2-bad | ./C2 + + +int match(char *s1, char *s2) { + while( *s1 != '\0' && *s2 != 0 && *s1 == *s2 ){ + s1++; s2++; + } + return( *s1 - *s2 ); +} + +// since gets() is insecure and produces lots of warnings, +// I use my own input function instead ;o) +char ch; +int i; + +void get_line(char *dst) { + char buffer[8]; + i = 0; + while ((ch = getchar()) != '\n') { + buffer[i++] = ch; + } + buffer[i] = '\0'; + strcpy(dst, buffer); +} + +void welcome() { printf("Welcome to the Machine!\n"); exit(0); } +void goodbye() { printf("Invalid identity, exiting!\n"); exit(1); } + +main(){ + char name[8]; + char pw[8]; + + printf("login: "); + get_line(name); + printf("password: "); + get_line(pw); + + if(match(name, pw) == 0) + welcome(); + else + goodbye(); +} +