-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
153 lines (131 loc) · 3.61 KB
/
Copy pathshell.c
File metadata and controls
153 lines (131 loc) · 3.61 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <readline/readline.h>
#include <readline/history.h>
#define MAX_ARGS 64
void execute_command(char **args, char *output_file) {
pid_t pid = fork();
if (pid == 0) {
if (output_file != NULL) {
int fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
printf("myshell: cannot open file: %s\n", output_file);
exit(1);
}
dup2(fd, STDOUT_FILENO);
close(fd);
}
if (execvp(args[0], args) == -1) {
printf("myshell: command not found: %s\n", args[0]);
}
exit(1);
} else if (pid > 0) {
wait(NULL);
} else {
printf("myshell: failed to fork\n");
}
}
char **parse_input(char *input) {
char **args = malloc(MAX_ARGS * sizeof(char *));
int i = 0;
char *token = strtok(input, " ");
while (token != NULL && i < MAX_ARGS - 1) {
args[i++] = token;
token = strtok(NULL, " ");
}
args[i] = NULL;
return args;
}
void execute_pipe(char *cmd1, char *cmd2) {
int pipefd[2];
pipe(pipefd);
char **args1 = parse_input(cmd1);
char **args2 = parse_input(cmd2);
pid_t pid1 = fork();
if (pid1 == 0) {
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[0]);
close(pipefd[1]);
execvp(args1[0], args1);
exit(1);
}
pid_t pid2 = fork();
if (pid2 == 0) {
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[1]);
close(pipefd[0]);
execvp(args2[0], args2);
exit(1);
}
close(pipefd[0]);
close(pipefd[1]);
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
free(args1);
free(args2);
}
int main() {
// Colors
char *prompt = "\033[1;32mmyshell\033[0m\033[1;34m>\033[0m ";
char *input;
while (1) {
// readline handles history and arrow keys
input = readline(prompt);
if (input == NULL) {
printf("\nExiting...\n");
break;
}
if (strlen(input) == 0) {
free(input);
continue;
}
// Add to history so up arrow works
add_history(input);
// Check for pipe
char *pipe_pos = strchr(input, '|');
if (pipe_pos != NULL) {
*pipe_pos = '\0';
char *cmd1 = input;
char *cmd2 = pipe_pos + 1;
while (*cmd2 == ' ') cmd2++;
execute_pipe(cmd1, cmd2);
free(input);
continue;
}
// Check for output redirection
char *output_file = NULL;
char *redir_pos = strchr(input, '>');
if (redir_pos != NULL) {
*redir_pos = '\0';
output_file = redir_pos + 1;
while (*output_file == ' ') output_file++;
char *end = redir_pos - 1;
while (end > input && *end == ' ') *end-- = '\0';
}
char **args = parse_input(input);
if (strcmp(args[0], "exit") == 0) {
printf("Bye!\n");
free(args);
free(input);
exit(0);
}
if (strcmp(args[0], "cd") == 0) {
if (args[1] == NULL) {
chdir(getenv("HOME"));
} else if (chdir(args[1]) != 0) {
printf("myshell: cd: no such directory: %s\n", args[1]);
}
free(args);
free(input);
continue;
}
execute_command(args, output_file);
free(args);
free(input);
}
return 0;
}