progs/C2.c
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 06 Oct 2014 20:52:53 +0100
changeset 211 e6e160c7ea33
parent 105 40c51038c9e4
child 215 06cc38192865
permissions -rw-r--r--
added files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
     1
#include <string.h>
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
     2
#include <stdio.h>
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
     3
#include <stdlib.h>
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
     4
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
     5
// Since gets() is insecure and produces lots 
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
     6
// of warnings, thereofre I use my own input 
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
     7
// function instead.
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
     8
void get_line(char *dst) {
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
     9
  char buffer[8];
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    10
  int i = 0;
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    11
  char ch;
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    12
  while ((ch = getchar()) != '\n') {
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    13
    buffer[i++] = ch;
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    14
  }
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    15
  buffer[i] = '\0';
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    16
  strcpy(dst, buffer);
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    17
}
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    18
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
int match(char *s1, char *s2) {
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    20
  while(*s1 != '\0' && *s2 != '\0' && *s1 == *s2){
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
    s1++; s2++;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  return( *s1 - *s2 );
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    26
void welcome() { printf("Welcome!\n"); exit(0); }
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    27
void goodbye() { printf("Wrong identity, exiting!\n"); exit(1); }
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    29
int main(){
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  char name[8];
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  char pw[8]; 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  printf("login: "); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  get_line(name);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  printf("password: "); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  get_line(pw);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  if(match(name, pw) == 0)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
    welcome();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  else
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
    goodbye();
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    42
}