-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtechshell.c
More file actions
195 lines (172 loc) · 5.1 KB
/
Copy pathtechshell.c
File metadata and controls
195 lines (172 loc) · 5.1 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define MAX_ARGS 64
#define MAX_ARG_LEN 256
#define MAX_CMD_LEN 512
#define PROMPT_MAX_LEN 256
// Struct to hold command information
struct ShellCommand{
char* argv[MAX_ARGS]; // Command arguments
int argc; // Number of arguments
char* input_file; // Input file name (or NULL)
char* output_file; // Output file name (or NULL)
}
// Display the command prompt, which includes the current working directory.
char* CommandPrompt(){
static char prompt[PROMPT_MAX_LEN];
char cwd[PROMPT_MAX_LEN];
getcwd(cwd, sizeof(cwd));
sprintf(prompt, "%s$ ", cwd);
printf("%s", prompt);
return fgets(prompt, sizeof(prompt), stdin);
}
// Parse the user input as a shell command.
struct ShellCommand ParseCommandLine(char* input){
struct ShellCommand command = { { NULL }, 0, NULL, NULL };
char* arg;
char* token = strtok(input, " \t\n");
while (token != NULL)
{
if (strcmp(token, "<") == 0)
{
token = strtok(NULL, " \t\n");
if (token != NULL)
{
command.input_file = strdup(token);
token = strtok(NULL, " \t\n");
}
else
{
fprintf(stderr, "Error: missing input file name\n");
command.argv[0] = strdup("error");
return command;
}
}
else if (strcmp(token, ">") == 0)
{
token = strtok(NULL, " \t\n");
if (token != NULL)
{
command.output_file = strdup(token);
token = strtok(NULL, " \t\n");
}
else
{
fprintf(stderr, "Error: missing output file name\n");
command.argv[0] = strdup("error");
return command;
}
}
else
{
arg = strdup(token);
command.argv[command.argc++] = arg;
if (command.argc >= MAX_ARGS)
{
break;
}
token = strtok(NULL, " \t\n");
}
}
return command;
}
// Execute a shell command.
void ExecuteCommand(struct ShellCommand command) {
pid_t pid = fork();
if (pid == -1)
{
fprintf(stderr, "Error: fork failed\n");
exit(1);
}
else if (pid == 0)
{
// Child process
if (command.input_file != NULL)
{
int fd = open(command.input_file, O_RDONLY);
if (fd == -1)
{
fprintf(stderr, "Error: cannot open input file '%s': %s\n", command.input_file, strerror(errno));
exit(1);
}
if (dup2(fd, STDIN_FILENO) == -1)
{
fprintf(stderr, "Error: cannot redirect input to '%s': %s\n", command.input_file, strerror(errno));
exit(1);
}
close(fd);
}
if (command.output_file != NULL)
{
int fd = open(command.output_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd == -1)
{
fprintf(stderr, "Error: cannot open output file '%s': %s\n", command.output_file, strerror(errno));
exit(1);
}
if (dup2(fd, STDOUT_FILENO) == -1)
{
fprintf(stderr, "Error: cannot redirect output to '%s': %s\n", command.output_file, strerror(errno));
exit(1);
}
close(fd);
}
if (execvp(command.argv[0], command.argv) == -1)
{
fprintf(stderr, "Error: command '%s' not found\n", command.argv[0]);
exit(1);
}
}
else
{
// Parent process
int status;
if (waitpid(pid, &status, 0) == -1)
{
fprintf(stderr, "Error: waitpid failed\n");
exit(1);
}
if (WIFEXITED(status))
{
int exit_status = WEXITSTATUS(status);
if (exit_status != 0)
{
fprintf(stderr, "Command '%s' failed with exit code %d\n", command.argv[0], exit_status);
}
}
}
}
// Main shell loop
int main(int argc, char* argv[]) {
char input[MAX_CMD_LEN];
while (1)
{
if (CommandPrompt() == NULL)
{
break;
}
struct ShellCommand command = ParseCommandLine(input);
if (command.argv[0] == NULL)
{
continue;
}
if (strcmp(command.argv[0], "exit") == 0)
{
break;
}
ExecuteCommand(command);
for (int i = 0; i < command.argc; i++)
{
free(command.argv[i]);
}
free(command.input_file);
free(command.output_file);
}
return 0;
}