progs/C2.c
author cu
Fri, 27 Oct 2017 23:57:21 +0100
changeset 558 86334134abe5
parent 546 3d1f65e43065
permissions -rw-r--r--
updated cw

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

// Since gets() is insecure and produces lots 
// of warnings, therefore I use my own input 
// function instead...of course, why not?
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();
}