用c语言编写一个简单的,支持cd,clr,dir,environ,help,pause,quit的内部指令,所有的其他指令可以通过调用fork与exec完成,还支持输入输出重定向
时间: 2023-05-21 11:02:46 浏览: 139
以下是一个简单的C语言程序,支持cd,clr,dir,environ,help,pause,quit的内部指令,并且可以通过调用fork与exec完成所有的其他指令,同时支持输入输出重定向:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#define MAX_COMMAND_LENGTH 1024
#define MAX_ARGUMENTS 64
void cd(char *path) {
if (path == NULL) {
printf("cd: missing argument\n");
} else if (chdir(path) != 0) {
printf("cd: %s: No such file or directory\n", path);
}
}
void clr() {
printf("\033[2J\033[1;1H");
}
void dir() {
system("ls -al");
}
void environ() {
extern char **environ;
for (char **env = environ; *env != NULL; env++) {
printf("%s\n", *env);
}
}
void help() {
printf("cd [path]\n");
printf("clr\n");
printf("dir\n");
printf("environ\n");
printf("help\n");
printf("pause\n");
printf("quit\n");
}
void pause_command() {
printf("Press Enter to continue...");
fflush(stdout);
getchar();
}
void execute_command(char **args, int input_fd, int output_fd) {
pid_t pid = fork();
if (pid == 0) {
if (input_fd != STDIN_FILENO) {
dup2(input_fd, STDIN_FILENO);
close(input_fd);
}
if (output_fd != STDOUT_FILENO) {
dup2(output_fd, STDOUT_FILENO);
close(output_fd);
}
execvp(args[0], args);
printf("%s: command not found\n", args[0]);
exit(1);
} else if (pid < 0) {
printf("fork failed\n");
} else {
waitpid(pid, NULL, 0);
}
}
int main() {
char command[MAX_COMMAND_LENGTH];
char *args[MAX_ARGUMENTS];
while (1) {
printf("> ");
fflush(stdout);
if (fgets(command, MAX_COMMAND_LENGTH, stdin) == NULL) {
break;
}
command[strcspn(command, "\n")] = '\0';
int argc = 0;
args[argc] = strtok(command, " ");
while (args[argc] != NULL) {
argc++;
if (argc >= MAX_ARGUMENTS) {
printf("too many arguments\n");
argc--;
break;
}
args[argc] = strtok(NULL, " ");
}
if (argc == 0) {
continue;
}
if (strcmp(args[0], "cd") == 0) {
cd(args[1]);
} else if (strcmp(args[0], "clr") == 0) {
clr();
} else if (strcmp(args[0], "dir") == 0) {
dir();
} else if (strcmp(args[0], "environ") == 0) {
environ();
} else if (strcmp(args[0], "help") == 0) {
help();
} else if (strcmp(args[0], "pause") == 0) {
pause_command();
} else if (strcmp(args[0], "quit") == 0) {
break;
} else {
int input_fd = STDIN_FILENO;
int output_fd = STDOUT_FILENO;
for (int i = 1; i < argc; i++) {
if (strcmp(args[i], "<") == 0) {
if (i + 1 < argc) {
input_fd = open(args[i + 1], O_RDONLY);
if (input_fd < 0) {
printf("%s: No such file or directory\n", args[i + 1]);
break;
}
i++;
} else {
printf("missing input file name\n");
break;
}
} else if (strcmp(args[i], ">") == 0) {
if (i + 1 < argc) {
output_fd = open(args[i + 1], O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (output_fd < 0) {
printf("%s: cannot create file\n", args[i + 1]);
break;
}
i++;
} else {
printf("missing output file name\n");
break;
}
} else {
continue;
}
}
args[argc] = NULL;
execute_command(args, input_fd, output_fd);
if (input_fd != STDIN_FILENO) {
close(input_fd);
}
if (output_fd != STDOUT_FILENO) {
close(output_fd);
}
}
}
return 0;
}
```
阅读全文