-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.c
56 lines (51 loc) · 1.1 KB
/
args.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
/*
** EPITECH PROJECT, 2020
** PSU_strace_2019
** File description:
** automated desc ftw
*/
#include "strace.h"
void load_p(opts_t *as, int *off, int c, const char **v)
{
as->p = true;
if (c <= *off) {
fputs("pid?", stderr);
exit(84);
}
as->grab = atoi(v[*off + 1]);
if (!as->grab) {
fputs("pid?", stderr);
exit(84);
}
*off += 2;
}
void parse_args(opts_t *as, int *off, int c, const char **v)
{
if (c == 1) {
fputs("no arguments; try `--help`?\n", stderr);
exit(84);
}
if (!strcmp(v[1], "--help")) {
fputs("USAGE: ./strace [-s] [-p <pid>|<command>]\n", stdout);
exit(0);
}
if (!strcmp(v[*off], "-s")) {
as->s = true;
(*off)++;
}
if (!strcmp(v[*off], "-p"))
load_p(as, off, c, v);
}
void child(int c, const char **v)
{
char *args[c + 1];
if (c <= 0) {
fputs("any commands?\n", stderr);
exit(84);
}
memcpy(args, v, c * sizeof(*v));
args[c] = NULL;
ptrace(PTRACE_TRACEME, 0, 0, 0);
kill(getpid(), SIGSTOP);
execvp(args[0], args);
}