-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatecmd.c
67 lines (45 loc) · 1.25 KB
/
createcmd.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
#include "createcmd.h"
// creates an execcmd struct to store
// the args and environ vars of the command
struct cmd* exec_cmd_create(char* buf_cmd) {
struct execcmd* e;
e = (struct execcmd*)calloc(1, sizeof(*e));
e->type = EXEC;
strcpy(e->scmd, buf_cmd);
return (struct cmd*)e;
}
struct cmd* redir_cmd_create(struct cmd* e,
struct file* in,
struct file* out,
struct file* err) {
struct redircmd* r;
r = (struct redircmd*)calloc(1, sizeof(*r));
r->type = REDIR;
r->c = e;
memcpy(&(r->in), in, sizeof(struct file));
memcpy(&(r->out), out, sizeof(struct file));
memcpy(&(r->err), err, sizeof(struct file));
strcpy(r->scmd, e->scmd);
return (struct cmd*)r;
}
// creates a backcmd struct to store the
// background command to be executed
struct cmd* back_cmd_create(struct cmd* c) {
struct backcmd* b;
b = (struct backcmd*)calloc(1, sizeof(*b));
b->type = BACK;
strcpy(b->scmd, c->scmd);
b->c = c;
return (struct cmd*)b;
}
// encapsulates two commands into one pipe struct
struct cmd* pipe_cmd_create(struct cmd* left, struct cmd* right) {
if (!right)
return left;
struct pipecmd* p;
p = (struct pipecmd*)calloc(1, sizeof(*p));
p->type = PIPE;
p->leftcmd = left;
p->rightcmd = right;
return (struct cmd*)p;
}