progs/test2.c
author cu
Mon, 09 Oct 2017 10:07:35 +0100
changeset 547 eae9a2e6ff28
parent 214 166a27693dcd
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
214
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
#include <stdio.h>
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
#include <stdlib.h>
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
#include <string.h>
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
// for installation notes see C0.c
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
// this program can be called with
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
//
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
//  ./args2-good | ./C2
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
//
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
// or
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
//
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
// ./args2-bad | ./C2
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
int match(char *s1, char *s2) {
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  while( *s1 != '\0' && *s2 != 0 && *s1 == *s2 ){
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
    s1++; s2++;
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  }
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  return( *s1 - *s2 );
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
}
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
// since gets() is insecure and produces lots of warnings, 
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
// I use my own input function instead ;o)
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
char ch;
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
int i;
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
void get_line(char *dst) {
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  char buffer[8];
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  i = 0;
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  while ((ch = getchar()) != '\n') {
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
    buffer[i++] = ch; 
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  }
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  buffer[i] = '\0';
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  strcpy(dst, buffer);
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
}
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
void welcome() { printf("Welcome to the Machine!\n"); exit(0); }
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
void goodbye() { printf("Invalid identity, exiting!\n"); exit(1); }
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
main(){
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  char name[8];
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  char pw[8]; 
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  printf("login: "); 
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  get_line(name);
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  printf("password: "); 
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  get_line(pw);
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  if(match(name, pw) == 0)
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
    welcome();
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  else
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
    goodbye();
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
}
166a27693dcd added test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54