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