This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsource.c
116 lines (92 loc) · 2.26 KB
/
source.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void clear_stdin(void)
{
char c = 0;
while((c = getchar()) != -1) {
if (c == "\n") {
break ;
}
}
}
unsigned int get_unum(void)
{
unsigned int input = 0;
fflush(stdout);
scanf("%u", input);
clear_stdin();
return(input);
}
int store_number(unsigned int *data)
{
unsigned int input = 0;
unsigned int index = 0;
printf(" Number: ");
input = get_unum();
printf(" Index: ");
index = get_unum();
if(index % 3 == 0 || (input >> 24) == 0xb7)
{
printf(" *** ERROR! ***\n");
printf(" This index is reserved for wil!\n");
printf(" *** ERROR! ***\n");
return 1;
}
data[index] = input;
return 0;
}
int read_number(unsigned int *data) {
unsigned int index = 0;
printf(" Index: ");
index = get_unum();
printf(" Number at data[%u] is %u\n", index, data[index]);
return 0;
}
int main(int argc, char * argv[], char * envp[]) {
int res = 0;
char cmd[20] = {0};
unsigned int data[100] = {0};
while(*av)
{
memset(*av, 0, strlen(*av) - 1);
*av++;
}
while(*envp)
{
memset(*envp, 0, strlen(*envp) - 1);
*envp++;
}
puts(
"----------------------------------------------------\n"\
"Welcome to wil\'s crappy number stora ge 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: ");
res = 1;
fgets(cmd, sizeof(cmd), stdin);
cmd[strlen(cmd)-1] = '\0';
if(!strncmp(cmd, "store", 5)) {
res = store_number(data);
} else if(!strncmp(cmd, "read", 4)) {
res = read_number(data);
} else if(!strncmp(cmd, "quit", 4))
break;
}
if(res) {
printf(" Failed to do %s command\n", cmd);
} else {
printf(" Completed %s command successfully\n", cmd);
}
memset(cmd, 0, sizeof(cmd));
}
return 0;
}