Repository navigation
Expand file tree
/
Copy pathrwslotmachine4.c
More file actions
103 lines (82 loc) · 1.58 KB
/
Copy pathrwslotmachine4.c
File metadata and controls
103 lines (82 loc) · 1.58 KB
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
#include <stdio.h>
#include <stdlib.h>
#define available_reads 3
#define available_writes 1
#define MAX_SLOTS 32
long read_int(int base)
{
char buf[64];
char *endptr;
if (fgets(buf, sizeof(buf), stdin) == NULL) {
puts("Failed to read input!");
exit(1);
}
return strtol(buf, &endptr, base);
}
long slots[MAX_SLOTS];
void setup() {
setbuf(stdin, NULL);
setbuf(stdout, NULL);
setbuf(stderr, NULL);
}
void menu(int reads, int writes) {
puts("==== www / rww ====");
if (reads)
printf("1. Read (%d/%d)\n", reads, available_reads);
else
printf("1. Read (unavailable)\n");
if (writes)
printf("2. Write (%d/%d)\n", writes, available_writes);
else
printf("2. Write (unavailable)\n");
puts("3. Exit");
}
void do_read() {
long index;
printf("Input slot index: ");
index = read_int(10);
if (index >= MAX_SLOTS) {
printf("Index out of bounds!");
exit(3);
}
printf("Slot[%ld]: %04lx\n", index, slots[index]);
}
void do_write() {
long index, value;
printf("Input slot index: ");
index = read_int(10);
printf("Input new slot value: ");
value = read_int(16);
printf("Slot[%ld]:= %04lx\n", index, value);
slots[index] = value;
}
int main() {
int reads = available_reads;
int writes = available_writes;
int choice;
setup();
while (1) {
menu(reads, writes);
printf("> ");
choice = read_int(10);
switch (choice) {
case 1:
if (reads) {
do_read();
-- reads;
}
break;
case 2:
if (writes) {
do_write();
-- writes;
}
break;
case 3:
exit(0);
default:
puts("Option not available!");
break;
}
}
}