progs/C2.c
author Christian Urban <urbanc@in.tum.de>
Tue, 26 Sep 2017 10:36:19 +0100
changeset 531 35ffb7a7eafa
parent 215 06cc38192865
child 546 3d1f65e43065
permissions -rw-r--r--
fixed bug

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

// Since gets() is insecure and produces lots 
// of warnings, thereofre I use my own input 
// function instead.
int i;
char ch;  
    
void get_line(char *dst) {
  char buffer[8];
  i = 0;
  while ((ch = getchar()) != '\n') {
    buffer[i++] = ch;
  }
  buffer[i] = '\0';
  strcpy(dst, buffer);
}

int match(char *s1, char *s2) {
  while(*s1 != '\0' && *s2 != '\0' && *s1 == *s2){
    s1++; s2++;
  }
  return( *s1 - *s2 );
}

void welcome() { printf("Welcome!\n"); exit(0); }
void goodbye() { printf("Wrong identity, exiting!\n"); exit(1); }

int 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();
}