-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.c
70 lines (61 loc) · 1.43 KB
/
operation.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
#include "operation.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
operation* parse_operation(char *str) {
operation *r;
char *p;
int i;
r = malloc(sizeof(operation));
if(r == NULL) {
perror("malloc");
goto error;
}
*r = (operation) {
.orig_func = NULL,
.repl_func = NULL,
.orig_got = 0,
.patch_offset = 0,
.next = NULL
};
i = 0;
p = strtok(str, ",");
while(p != NULL) {
if(i == 0) {
r->orig_func = strdup(p);
} else if(i == 1) {
r->repl_func = strdup(p);
} else if(i == 2) {
r->patch_offset = atoi(p);
}
p = strtok(NULL, ",");
if(p != NULL) *(p-1) = ',';
i++;
}
if(i != 2 && i != 3) {
fprintf(stderr, "%s have a wrong format. format: original_function,replacer_function,[patch_offset]\n", str);
goto error;
}
if(r->orig_func == NULL || r->repl_func == NULL) {
perror("strdup");
goto error;
}
return r;
error:
if(r) {
if(r->orig_func) free(r->orig_func);
if(r->repl_func) free(r->repl_func);
free(r);
}
return NULL;
}
void free_operations(operation *root) {
operation *i;
while(root) {
i = root;
root = root->next;
if(i->orig_func) free(i->orig_func);
if(i->repl_func) free(i->repl_func);
free(i);
}
}