Repository navigation
FilesExpand file tree
/
Copy pathex19.c
More file actions
109 lines (85 loc) · 1.91 KB
/
Copy pathex19.c
File metadata and controls
109 lines (85 loc) · 1.91 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
103
104
105
106
107
108
109
#include "dbg.h"
#include <stdio.h>
#include <stdlib.h>
void test_debug() {
debug("I have black hair");
debug("I am %d years old.", 37);
}
void test_log_err() {
log_err("I believe everything is broken");
log_err("There are %d problems in %s", 0, "space");
}
void test_log_warn() {
log_warn("You can safely ignore this");
log_warn("Maybe consider looking at: %s.", "/etc/passwd");
}
void test_log_info() {
log_info("Well I did something mundane");
log_info("It happened %f times today", 1.3);
}
int test_check(char *file_name) {
FILE *input = NULL;
char *block = NULL;
block = malloc(100);
check_mem(
block); // should work, if this fails goes to error label 9 lines below
input = fopen(file_name, "r");
check(input, "Failed to open %s.", file_name);
free(block);
fclose(input);
return 0;
error:
if (block)
free(block);
if (input)
free(input);
return -1;
}
int test_sentinel(int code) {
char *temp = malloc(100);
check_mem(temp);
switch (code) {
case 1:
log_info("It worked");
break;
default:
sentinel("I should't run");
}
free(temp);
return 0;
error:
if (temp)
free(temp);
return -1;
}
int test_check_mem() {
char *test = NULL;
check_mem(test);
free(test);
return 1;
error:
return -1;
}
int test_check_debug() {
int i = 0;
check_debug(i != 0, "Oops, i was 0.");
return 0;
error:
return -1;
}
int main(int argc, char *argv[]) {
check(argc == 2, "Need an argument");
test_debug();
test_log_err();
test_log_warn();
test_log_info();
check(test_check("ex19.c") == 0, "Failed with ex19.c");
check(test_check(argv[1]) == -1, "Failed with argv");
check(test_sentinel(1) == 0, "test_sentinel failed)");
check(test_sentinel(100) == -1, "test_sentinel failed");
check(test_check_mem() == -1, "test_check_mem failed");
check(test_check_debug() == -1, "test_check_debug failed");
return 0;
error:
return 1;
}