-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsegv.c
More file actions
58 lines (49 loc) · 980 Bytes
/
segv.c
File metadata and controls
58 lines (49 loc) · 980 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
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
static void
usage(const char * prog, int status)
{
fprintf(status == 0? stdout: stderr,
"Usage: %s r|w\n", prog);
exit (status);
}
int
main(int argc, char **argv)
{
if (argc != 2)
usage(argv[0], 1);
if (strcmp(argv[1], "-h") == 0
|| strcmp(argv[1], "--help") == 0)
usage(argv[0], 0);
if (argv[1][0] != 'w' && argv[1][0] != 'r')
usage(argv[0], 2);
fprintf(stderr, "(pid: %d) address in hex? ", getpid());
char buf[BUFSIZ];
if (!fgets(buf, sizeof(buf), stdin))
{
fprintf(stderr, "NO INPUT\n");
exit (1);
}
union addr
{
unsigned long num;
char *ptr;
} a;
a.num = strtoul(buf, NULL, 16);
if (a.num == ULONG_MAX)
{
perror("strtoul");
exit (1);
}
char r = 0;
char *ptr = a.ptr;
if (argv[1][0] == 'w')
*a.ptr = 'x';
else
r = *a.ptr;
return r;
}