粉丝提问
Solution
要在 Linux 环境下用 C 语言模拟实现一个命令解释器,包含mypwd
, mymkdir
, myrmdir
, mycd
, mylist
, mycp
, mydate
, mycreate
, mydelete
, exit
等基本命令,需要按照以下步骤进行:
-
理解每个命令的功能:
mypwd
: 显示当前工作目录。mymkdir
: 创建一个新目录。myrmdir
: 删除一个空目录。mycd
: 更改当前工作目录。mylist
: 列出目录中的文件和子目录。mycp
: 复制文件或目录。mydate
: 显示或设置系统日期和时间。mycreate
: 创建一个新文件。mydelete
: 删除一个文件。exit
: 退出命令解释器。
-
设置基本框架:
- 创建一个循环,接收用户输入的命令。
- 解析命令,确定要执行的操作。
-
实现各个命令:
- 使用C语言的标准库函数和系统调用来实现每个命令。
-
添加错误处理和注释:
- 确保每个命令都有适当的错误处理。
- 添加注释以解释代码的功能。
-
测试:
- 对每个命令进行测试,确保它们按预期工作。
1. 基本框架
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>int main() {char command[256];while (1) {printf("myprompt> ");fgets(command, 256, stdin);command[strcspn(command, "\n")] = 0; // Remove newline characterif (strcmp(command, "exit") == 0) {break;}// 解析和执行命令...}return 0;
}
2. 实现mypwd
void mypwd() {char cwd[1024];if (getcwd(cwd, sizeof(cwd)) != NULL) {printf("%s\n", cwd);} else {perror("mypwd failed");}
}
类似实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>void mypwd() {char cwd[1024];if (getcwd(cwd, sizeof(cwd)) != NULL) {printf("%s\n", cwd);} else {perror("mypwd failed");}
}void mymkdir(const char *dirname) {if (mkdir(dirname, 0755) == -1) {perror("mymkdir failed");}
}void myrmdir(const char *dirname) {if (rmdir(dirname) == -1) {perror("myrmdir failed");}
}void mycd(const char *path) {if (chdir(path) == -1) {perror("mycd failed");}
}void mylist(const char *path) {DIR *d;struct dirent *dir;d = opendir(path);if (d) {while ((dir = readdir(d)) != NULL) {printf("%s\n", dir->d_name);}closedir(d);} else {perror("mylist failed");}
}void mycp(const char *source, const char *destination) {// 这里的实现非常简单,不处理目录复制,只适用于文件char ch;FILE *sourceFile, *targetFile;sourceFile = fopen(source, "r");if (sourceFile == NULL) {perror("Error opening source file");exit(EXIT_FAILURE);}targetFile = fopen(destination, "w");if (targetFile == NULL) {fclose(sourceFile);perror("Error opening target file");exit(EXIT_FAILURE);}while ((ch = fgetc(sourceFile)) != EOF) {fputc(ch, targetFile);}printf("File copied successfully.\n");fclose(sourceFile);fclose(targetFile);
}void mydate() {time_t t = time(NULL);struct tm *tm = localtime(&t);printf("%s", asctime(tm));
}void mycreate(const char *filename) {FILE *fp = fopen(filename, "w");if (fp) {printf("File created successfully.\n");fclose(fp);} else {perror("mycreate failed");}
}void mydelete(const char *filename) {if (remove(filename) == -1) {perror("mydelete failed");} else {printf("File deleted successfully.\n");}
}int main() {char command[256];char arg1[256];char arg2[256];while (1) {printf("myprompt> ");scanf("%s", command);if (strcmp(command, "exit") == 0) {break;} else if (strcmp(command, "mypwd") == 0) {mypwd();} else if (strcmp(command, "mymkdir") == 0) {scanf("%s", arg1);mymkdir(arg1);} else if (strcmp(command, "myrmdir") == 0) {scanf("%s", arg1);myrmdir(arg1);} else if (strcmp(command, "mycd") == 0) {scanf("%s", arg1);mycd(arg1);} else if (strcmp(command, "mylist") == 0) {scanf("%s", arg1);mylist(arg1);} else if (strcmp(command, "mycp") == 0) {scanf("%s %s", arg1, arg2);mycp(arg1, arg2);} else if (strcmp(command, "mydate") == 0) {mydate();} else if (strcmp(command, "mycreate") == 0) {scanf("%s", arg1);mycreate(arg1);} else if (strcmp(command, "mydelete") == 0) {scanf("%s", arg1);mydelete(arg1);}}return 0;
}