forked from ruv1nce/42-exam_beginner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_memory.c
More file actions
71 lines (64 loc) · 965 Bytes
/
Copy pathprint_memory.c
File metadata and controls
71 lines (64 loc) · 965 Bytes
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
#include <unistd.h>
static void print_hex(int x)
{
char *sym = "0123456789abcdef";
char c;
c = sym[x / 16];
write(1, &c, 1);
c = sym[x % 16];
write(1, &c, 1);
}
static void print_ascii(char c)
{
if (c >= 32 && c <= 126)
write(1, &c, 1);
else
write(1, ".", 1);
}
void print_memory(const void *addr, size_t size)
{
size_t i;
size_t tmp;
unsigned char *x;
int cols;
x = (unsigned char *)addr;
i = 0;
while (i < size)
{
tmp = i;
cols = 1;
while (cols <= 16)
{
if (i < size)
{
print_hex(x[i]);
if (!(cols % 2))
write(1, " ", 1);
cols++;
i++;
}
else
{
write(1, " ", 2);
if (!(cols % 2))
write(1, " ", 1);
cols++;
}
}
cols = 1;
i = tmp;
while (cols <= 16 && i < size)
{
print_ascii(x[i]);
cols++;
i++;
}
write(1, "\n", 1);
}
}
int main(void)
{
int tab[1] = {255};
print_memory(tab, sizeof(tab));
return (0);
}