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

static long check_password(const char *password);
static void print_exam();

int main(int argc, const char *argv[]) {
	if(argc != 2) {
		fprintf(stderr, "Usage: %s <password>\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	
	if(check_password(argv[1])) {
		fprintf(stdout, "Printing exam...\n");
		print_exam();
		return EXIT_SUCCESS;
	} else {
		fprintf(stderr, "Permission denied\n");
		return EXIT_FAILURE;
	}
}

long check_password(const char *password) {
	char buff[8];
	long pass = 0;

	//==== BUFFER OVERFLOW ====
	strcpy(buff, password);

	//printf("\n Enter the password : \n");
	//gets(buff);

	/*
	char *ptrBuff = buff;
	long *ptrPass = &pass;

	printf(" ptrBuff = %p\n", (void *) ptrBuff);
	printf(" ptrPass = %p\n", (void *) ptrPass);
	printf(" pass    = %ld\n", *ptrPass);
	printf(" buff[8] = %c\n", *(ptrBuff + 8));
	*/

	if(strcmp(buff, "spic")) {
		printf("Wrong Password\n");
	} else {
		printf("Correct Password\n");
		pass = 1;
	}

	return pass;
}

void print_exam(void) {
	printf("  \n");
	printf("  SPiC Klausur SS 2020\n");
	printf("  \n");
	printf("  Aufgabe 1)\n");
	printf("  ...\n");
	printf("  \n");
}

