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

int clear_stdin(void)
{
	char c;
	do
		c = getchar();
	while (c != 10);
	return c;
}

int get_unum()
{
	uint nb;

	fflush(stdout);
	scanf("%u", &nb);
	clear_stdin();
	return nb;
}

int store_number(char *data)
{
	unsigned int number;
	unsigned int index;

	printf(" Number: ");
	number = get_unum();
	printf(" Index: ");
	index = get_unum();
	if (index == 3 * (index / 3) || (number & 0xff00) == 183) {
		puts(" *** ERROR! ***");
		puts("   This index is reserved for wil!");
		puts(" *** ERROR! ***");
		return 1;
	}
	data[4 * index] = number;
	return 0;
}

int read_number(char *data)
{
	int unum;

	printf(" Index: ");
	unum = get_unum();
	printf(" Number at data[%u] is %c\n", unum, data[4 * unum]);
	return 0;
}

int main(int argc, const char **argv, const char **envp)
{
	char data[400]; // 0xffffda74
	int number;
	char s[4];
	int a = 0, b = 0, c = 0, d = 0;

	s[0] = '\0';
	memset(data, 0, sizeof(data));
	while (*argv) {
		memset((void *)*argv, 0, strlen(*argv));
		++argv;
	}
	while (*envp) {
		memset((void *)*envp, 0, strlen(*envp));
		++envp;
	}
	puts("----------------------------------------------------\n"
	     "  Welcome to wil's crappy number storage service!   \n"
	     "----------------------------------------------------\n"
	     " Commands:                                          \n"
	     "    store - store a number into the data storage    \n"
	     "    read  - read a number from the data storage     \n"
	     "    quit  - exit the program                        \n"
	     "----------------------------------------------------\n"
	     "   wil has reserved some storage :>                 \n"
	     "----------------------------------------------------\n");
	while (1) {
		printf("Input command: ");
		number = 1;
		fgets(s, 20, stdin);
		s[strlen(s) - 1] = '\0';
		if (!memcmp(s, "quit", 4))
			return 0;
		if (!memcmp(s, "store", 5))
			number = store_number(data);
		if (!memcmp(s, "read", 4))
			number = read_number(data);
		if (number)
			printf(" Failed to do %s command\n", s);
		else
			printf(" Completed %s command successfully\n", s);
		s[0] = '\0';
		a = 0;
		b = 0;
		c = 0;
		d = 0;
	}
}
