-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
54 lines (38 loc) · 1.19 KB
/
commands.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
#include <stdio.h>
#include <string.h>
#include "commands.h"
struct cmd_simple *cmd_extract_fst(struct cmd_compound *cmd) {
return &((*cmd).cmd1);
}
struct cmd_simple *cmd_extract_snd(struct cmd_compound *cmd) {
return &((*cmd).cmd2);
}
char *cmd_extract_arg_at(struct cmd_simple *cmd, int index) {
return &((*cmd).args[index][0]);
}
char *cmd_extract_program(struct cmd_simple *cmd) {
return cmd_extract_arg_at(cmd, 0);
}
void cmd_append_arg(struct cmd_simple *cmd, char *arg) {
char *copy_to = cmd_extract_arg_at(cmd, (*cmd).len);
strcpy(copy_to, arg);
(*cmd).len++;
}
// Writes strings to the internal string buffer from an array of char*'s.
void cmd_copy_from_ptr_arr(struct cmd_simple *cmd, char *arr[], int len) {
(*cmd).len = len;
for(int i = 0; i < len; i++) {
char *arg = cmd_extract_arg_at(cmd, i);
strcpy(arg, arr[i]);
}
char *arg = cmd_extract_arg_at(cmd, len);
*arg = '\0';
}
// Generates an array of char *'s pointing into the internal string buffer.
void cmd_generate_ptr_arr(struct cmd_simple *cmd, char *arr[]) {
for(int i = 0; i < (*cmd).len; i++) {
arr[i] = cmd_extract_arg_at(cmd, i);
}
// Terminate so that execvp knows when to stop!
arr[(*cmd).len] = '\0';
}