progs/C2.c
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 03 Oct 2014 13:14:34 +0100
changeset 192 2cb42412f3fd
parent 105 40c51038c9e4
child 211 e6e160c7ea33
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
int match(char *s1, char *s2) {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
  while( *s1 != '\0' && *s2 != '\0' && *s1 == *s2 ){
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
    s1++; s2++;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
  return( *s1 - *s2 );
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
void welcome() { printf("Welcome to the Machine!\n"); exit(0); }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
void goodbye() { printf("Invalid identity, exiting!\n"); exit(1); }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
main(){
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
  char name[8];
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
  char pw[8]; 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
  printf("login: "); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  get_line(name);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  printf("password: "); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  get_line(pw);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  if(match(name, pw) == 0)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
    welcome();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  else
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
    goodbye();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
}