progs/C2.c
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 06 Oct 2014 22:48:09 +0100
changeset 215 06cc38192865
parent 211 e6e160c7ea33
child 546 3d1f65e43065
permissions -rw-r--r--
updated
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.
215
06cc38192865 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 211
diff changeset
     8
int i;
06cc38192865 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 211
diff changeset
     9
char ch;  
06cc38192865 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 211
diff changeset
    10
    
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    11
void get_line(char *dst) {
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    12
  char buffer[8];
215
06cc38192865 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 211
diff changeset
    13
  i = 0;
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    14
  while ((ch = getchar()) != '\n') {
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    15
    buffer[i++] = ch;
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    16
  }
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    17
  buffer[i] = '\0';
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    18
  strcpy(dst, buffer);
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    19
}
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    20
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
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
    22
  while(*s1 != '\0' && *s2 != '\0' && *s1 == *s2){
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
    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
  return( *s1 - *s2 );
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    28
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
    29
void goodbye() { printf("Wrong identity, exiting!\n"); exit(1); }
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    31
int main(){
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  char name[8];
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  char pw[8]; 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  printf("login: "); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  get_line(name);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  printf("password: "); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  get_line(pw);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  if(match(name, pw) == 0)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
    welcome();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  else
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
    goodbye();
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    44
}