-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmylib.c
68 lines (58 loc) · 1.24 KB
/
mylib.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
#include "mylib.h"
char *myfgets(char *str, int num)
{
if (!(fgets(str, num, stdin))) {
return 0;
} else {
str[strlen(str) - 1] = '\0';
}
return str;
}
int confirm_choice(void)
{
char str[3];
printf("Set (Y/N)\n");
while (1) {
myfgets(str, 3);
if (str[0] == 'Y' || str[0] == 'y') {
return 1;
}
if (str[0] == 'N' || str[0] == 'n') {
return 0;
} else {
printf("Wrong key");
}
}
return 0;
}
int print_manual(void)
{
char input_buffer[128];
FILE *fp = fopen("README.MD", "r");
while (fgets(input_buffer, SIZE(input_buffer), fp)) {
printf("%s", input_buffer);
}
printf("\n");
fclose(fp);
return 0;
}
int input_number_in_range(int from, int to)
{
int n;
char *endptr;
char input_buffer[128];
int first = 1;
do {
if (!first) {
printf("Please enter number between %d and %d\n", from, to);
}
myfgets(input_buffer, 128);
n = strtol(input_buffer, &endptr, 10);
first = 0;
} while (*endptr || n < from || n > to);
return n;
}
int random_number_in_range(int from, int to)
{
return from + (rand()) % (to - from + 1);
}