-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
183 lines (161 loc) · 3.2 KB
/
main.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include "builtin.h"
#include "parse.h"
#define PROMPT "$ "
#define DELIM " "
#define INVALID_CMD "invalid command: %s\n"
// Supported built-in POSIX commands
#define PWD "pwd"
#define CD "cd"
#define ECHO "echo"
#define EXIT "exit"
#define ENV "env"
void execute(char **args, int count, int fd_in)
{
char *cmd = args[0];
if (cmd == NULL)
{
fprintf(stderr, "Error: No command provided\n");
return;
}
if (strcmp(cmd, PWD) == 0)
{
pwd();
}
else if (strcmp(cmd, ECHO) == 0)
{
echo(args, fd_in);
}
else if (strcmp(cmd, ENV) == 0)
{
env(count, args);
}
else
{
execvp(args[0], args);
fprintf(stderr, "command failed %s: %s\n", args[0], strerror(errno));
}
}
int handle(char **args, size_t argsc)
{
// EXIT and CD are both commands which transform the current process
if (strcmp(args[0], EXIT) == 0)
{
exit(0);
}
else if (strcmp(args[0], CD) == 0)
{
cd(args[1]);
return 0;
}
// save stdin/stdout file descriptors for io redirection
int fd_in = -1, fd_out = -1;
int saved_stdin = dup(STDIN_FILENO);
int saved_stdout = dup(STDOUT_FILENO);
// parse io redirection args
for (size_t i = 0; i < argsc; i++)
{
if (strcmp(args[i], "<") == 0)
{
fd_in = open(args[i + 1], O_RDONLY);
if (fd_in < 0)
{
perror("Failed to open input file");
return -1;
}
dup2(fd_in, STDIN_FILENO);
args[i] = NULL;
}
else if (strcmp(args[i], ">") == 0)
{
fd_out = open(args[i + 1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd_out < 0)
{
perror("Failed to open output file");
return -1;
}
dup2(fd_out, STDOUT_FILENO);
args[i] = NULL;
}
else if (strcmp(args[i], ">>") == 0)
{
fd_out = open(args[i + 1], O_WRONLY | O_CREAT | O_APPEND, 0644);
if (fd_out < 0)
{
perror("Failed to open output file for appending");
return -1;
}
dup2(fd_out, STDOUT_FILENO);
args[i] = NULL;
}
else if (strcmp(args[i], "|") == 0)
{
int pipefd[2];
if (pipe(pipefd) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0)
{ // child process
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
execute(args, i, fd_in);
exit(EXIT_SUCCESS);
}
else
{ // parent process
close(pipefd[1]);
dup2(pipefd[0], fd_in);
close(pipefd[0]);
wait(NULL);
args[i] = NULL;
}
}
}
// execute the command
execute(args, argsc, fd_in);
// restore stdin/stdout file descriptors
dup2(saved_stdin, STDIN_FILENO);
dup2(saved_stdout, STDOUT_FILENO);
close(saved_stdin);
close(saved_stdout);
if (fd_in != -1)
close(fd_in);
if (fd_out != -1)
close(fd_out);
return 0;
}
int main()
{
while (1)
{
char *pwd = getenv("PWD");
printf("%s %s", pwd, PROMPT);
char input[100];
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = 0; // remove the trailing new line character
size_t argsc = 0;
char **args = split(input, &argsc, DELIM);
expand_env_vars(args);
if (argsc == 0)
{
continue;
}
handle(args, argsc);
};
return 0;
}