-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.c
More file actions
163 lines (139 loc) · 3.27 KB
/
Copy pathcmd.c
File metadata and controls
163 lines (139 loc) · 3.27 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// SPDX-License-Identifier: BSD-3-Clause
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include "cmd.h"
#include "utils.h"
static bool shell_cd(word_t *dir)
{
char *path = get_word(dir);
if (chdir(path) != 0) {
free(path);
return false;
}
free(path);
return true;
}
static int shell_exit(void)
{
return SHELL_EXIT;
}
static int parse_simple(simple_command_t *s, int level, command_t *father)
{
char **argv;
int argc;
argv = get_argv(s, &argc);
if (strcmp(argv[0], "cd") == 0) {
bool success = shell_cd(s->params);
free(argv[0]);
free(argv);
return success ? 0 : -1;
}
if (strcmp(argv[0], "exit") == 0) {
free(argv[0]);
free(argv);
return shell_exit();
}
if (strchr(argv[0], '=') != NULL) {
char *key = strtok(argv[0], "=");
char *value = strtok(NULL, "=");
setenv(key, value, 1);
free(argv[0]);
free(argv);
return 0;
}
pid_t pid = fork();
if (pid == 0) {
if (s->in) {
int fd_in = open(get_word(s->in), O_RDONLY);
dup2(fd_in, STDIN_FILENO);
close(fd_in);
}
if (s->out) {
int fd_out = open(get_word(s->out), O_WRONLY | O_CREAT | O_TRUNC, 0644);
dup2(fd_out, STDOUT_FILENO);
close(fd_out);
}
if (s->err) {
int fd_err = open(get_word(s->err), O_WRONLY | O_CREAT | O_TRUNC, 0644);
dup2(fd_err, STDERR_FILENO);
close(fd_err);
}
execvp(argv[0], argv);
} else {
int status;
waitpid(pid, &status, 0);
}
for (int i = 0; i < argc; i++)
free(argv[i]);
free(argv);
return 0;
}
static bool run_in_parallel(command_t *cmd1, command_t *cmd2, int level, command_t *father)
{
pid_t pid1 = fork();
if (pid1 == 0)
parse_command(cmd1, level + 1, father);
pid_t pid2 = fork();
if (pid2 == 0)
parse_command(cmd2, level + 1, father);
int status1, status2;
waitpid(pid1, &status1, 0);
waitpid(pid2, &status2, 0);
return WIFEXITED(status1) && WEXITSTATUS(status1) == 0 && WIFEXITED(status2) && WEXITSTATUS(status2) == 0;
}
static bool run_on_pipe(command_t *cmd1, command_t *cmd2, int level, command_t *father)
{
int pipefd[2];
if (pipe(pipefd) == -1)
return false;
pid_t pid1 = fork();
if (pid1 == 0) {
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[0]);
close(pipefd[1]);
exit(parse_command(cmd1, level + 1, father));
}
pid_t pid2 = fork();
if (pid2 == 0) {
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
close(pipefd[1]);
exit(parse_command(cmd2, level + 1, father));
}
close(pipefd[0]);
close(pipefd[1]);
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
return true;
}
int parse_command(command_t *c, int level, command_t *father)
{
if (c->op == OP_NONE)
return parse_simple(c->scmd, level, father);
switch (c->op) {
case OP_SEQUENTIAL:
if (parse_command(c->cmd1, level + 1, c) == 0)
return parse_command(c->cmd2, level + 1, c);
return 0;
case OP_PARALLEL:
return run_in_parallel(c->cmd1, c->cmd2, level, c);
case OP_CONDITIONAL_NZERO:
if (parse_command(c->cmd1, level + 1, c) != 0)
return parse_command(c->cmd2, level + 1, c);
return 0;
case OP_CONDITIONAL_ZERO:
if (parse_command(c->cmd1, level + 1, c) == 0)
return parse_command(c->cmd2, level + 1, c);
return 0;
case OP_PIPE:
return run_on_pipe(c->cmd1, c->cmd2, level, c);
default:
return SHELL_EXIT;
}
return 0;
}