/media/sda-magnetic/david/Dok-15-2023-11-27/fernuni-hagen/cs-i-ii/old-cs-2-03/yun-proto/prog.c


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

int main (int argc, char *argv[]) {
	FILE *fp_user;
	FILE *fp_password;
	FILE *fp_location;
	FILE *fp_data;
	
	char str_user [1024];
	char str_password [1024];
	char str_location [1024];
    
    if (argc != 3) {
        perror ("Wrong Number of Arguments!");
        exit(-2);
    }
	
	if ((fp_user = fopen ("/home/david/user.txt", "r")) == NULL) {
		perror ("Can't open file");
		exit (-1);
	}
	if ((fp_password = fopen ("/home/david/passwords.txt", "r")) == NULL) {
		perror ("Can't open file");
		exit (-1);
	}
	if ((fp_location = fopen ("/home/david/location.txt", "r")) == NULL) {
		perror ("Can't open file");
		exit (-1);
	}
	
	while (!feof(fp_user) && (!feof(fp_password)) && (!feof(fp_location))) {
		fgets (str_user, 1024, fp_user);
		fgets (str_password, 1024, fp_password);
		fgets (str_location, 1024, fp_location);
		
		if (strcmp (str_user, argv[1]) == 0) {
			if (strcmp (str_user, argv[2]) == 0) {
				if ((fp_data = fopen (str_location, "r")) == NULL) {
					perror ("Can't open file");
					exit (-1);
				}
				while (!feof(fp_data)) {
					putc (fgetc(fp_data), stdout);
				}	 
				fclose (fp_data);
				fclose (fp_location);
				fclose (fp_password);
				fclose (fp_user);
				exit (0);
			}
			else {
				if ((fp_data = fopen ("/home/david/wrongpassword.jpg", "r")) == NULL) {
					perror ("Can't open file");
					exit(-1);
				}
				while (!feof(fp_data)) {
					putc (fgetc(fp_data), stdout);
				}	 
				fclose (fp_data);
				fclose (fp_location);
				fclose (fp_password);
				fclose (fp_user);
				exit (0);
			}
		}
	}
	if ((fp_data = fopen ("/home/david/wrongpassword.jpg", "r")) == NULL) {
		perror ("Can't open file");
		exit(-1);
	}
	while (!feof(fp_data)) {
		putc (fgetc(fp_data), stdout);
	}	 
	fclose (fp_data);
	fclose (fp_location);
	fclose (fp_password);
	fclose (fp_user);
	exit (0);
}