-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
105 lines (96 loc) · 2.38 KB
/
main.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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
// Parameters
bool showSize = false;
bool showAddress = true;
char* helptext = "usage: mapfinder <file> <variable> [-hsS]\n\n\
Parameters:\n\n\
\t-h \t Print this help text.\n\n\
\t-s \t Show size of the variable.\n\n\
\t-S \t Show only size of the variable.\n\n\
";
const char *fileName = NULL;
const char *varName = NULL;
int main(int argc, char const *argv[])
{
if (argc <= 1) {
printf("%s", helptext);
exit(-1);
}
for (int i = 1; i<argc; i++) {
if(argv[i][0]=='-') {
if(argv[i][1]=='s') {
showSize = true;
}else if(argv[i][1]=='S') {
showSize = true;
showAddress = false;
} else if(argv[i][1]=='h') {
printf("%s", helptext);
return(0);
} else {
printf("parameter %s is not known\n", argv[i]);
exit(-1);
}
} else {
if (fileName == NULL) {
fileName = argv[i];
} else if (varName == NULL) {
varName = argv[i];
}
}
}
if (fileName == NULL) {
printf("File name missing\n\n");
printf("%s", helptext);
return 0;
}
if (varName == NULL) {
printf("Variable name missing\n\n");
printf("%s", helptext);
return 0;
}
FILE *mapFile = fopen(fileName, "r");
if (mapFile == NULL)
{
printf("File %s not found.\n", fileName);
exit(-1);
}
char ch[300];
int lineIndex = 1;
while(fgets(ch, 300, mapFile) != NULL) {
if (strstr(ch,varName)) { // found, next line contains the value
//printf("found it on line %d\n", lineIndex);
fgets(ch, 300, mapFile);
char *hex = ch;
// find the start of the value
while (isspace(*hex)) hex++;
*(hex + 18) = 0;
char *end = hex;
while (!isspace(*end)) end++;
*end = 0;
int numFomratAddress = strtol(hex, NULL, 16);
if (numFomratAddress == 0) {
printf("Address 0 given, exit.\n");
exit(-1);
}
if (showAddress) printf("0x%x ", numFomratAddress);
if (showSize) {
hex=end+1;
// Skip next spaces to
while (isspace(*hex)) hex++;
end = hex;
while (!isspace(*end)) end++;
*end = 0;
int numFomratSize = strtol(hex, NULL, 16);
printf("0x%x", numFomratSize);
}
printf("\n");
}
lineIndex++;
}
fclose(mapFile);
return 0;
}