programs/C2.c
author Christian Urban <urbanc@in.tum.de>
Tue, 16 Oct 2012 12:10:35 +0100
changeset 45 24d08d7c582f
parent 28 10da75d5db5d
permissions -rw-r--r--
added

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

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