1、实验的思路和具体过程
将代码分为如下三个文件:
其中
cmd.h文件是存放所需要的定义的数据类型
cmd.c是存放command处理的子程序,
menu.c是main函数和command的处理入口,代码处理流程:
2、实验中关键代码
结构体定义:
typedef struct st_Cmd { unsigned char *pucCmd; unsigned char *pucCmd_description; void (*func)(void *pParam); struct st_Cmd *next; }st_Cmd_s; /*define the struct*/ static st_Cmd_s g_st_Cmd[CMD_NUM] = { {"rm", "rm command is used to delete files or directory\n", &CMD_Rm_Process, NULL}, {"ifconfig", "ifconfig command is used to get ip info\n", &CMD_Ifconfig_Process, NULL}, {"ls", "ls command is used to display thedirectory files\n", &CMD_Ls_Process, NULL}, {"cd", "cd command is used to jump a directory\n", &CMD_Cd_Process, NULL}, {"ln", "ln command is use to make link\n", &CMD_Ln_Process, NULL}, {"find", "find command is used to look up files\n", &CMD_Find_Process, NULL}, {"grep", "grep command is same as find\n", &CMD_Grep_Process, NULL}, {"cp", "cp command is used to copy files\n", &CMD_Cp_Process, NULL}, {"pwd", "pwd command is used to display the file list\n", &CMD_Pwd_Process, NULL}, {"mkdir", "mkdir command is used to make a directory\n", &CMD_Mkdir_Process, NULL}, {"help", "print the support command list\n", &CMD_Help_Process, NULL}, {"quit", "cmd help is quit, don't continue process!\n", &CMD_Quit_Process, NULL} };
command关键处理函数:
1 void CMD_Rm_Process(void *pParam); 2 void CMD_Ifconfig_Process(void *pParam); 3 void CMD_Ls_Process(void *pParam); 4 void CMD_Cd_Process(void *pParam); 5 void CMD_Mkdir_Process(void *pParam); 6 void CMD_Ln_Process(void *pParam); 7 void CMD_Find_Process(void *pParam); 8 void CMD_Grep_Process(void *pParam); 9 void CMD_Cp_Process(void *pParam); 10 void CMD_Pwd_Process(void *pParam); 11 void CMD_Process(unsigned char *puCmd); 12 void CMD_Help_Process(void *pParam); 13 void CMD_Quit_Process(void *pParam);
command处理函数及main函数:
/********************************************************* * name:SA17225233 * func: CMD_Process * date:2017/09/19 **********************************************************/ void CMD_Process(unsigned char *puCmd) { unsigned int i = 0; st_Cmd_s *pst_Cmd = g_st_Cmd; /*param check*/ if(puCmd == NULL) { printf("cmd is NULL, error!!!\n"); } while(pst_Cmd != NULL) { if(pst_Cmd->pucCmd == NULL) { printf("unknow the command!\n"); return; } if(strcmp(puCmd, pst_Cmd->pucCmd) == 0) { if(pst_Cmd->pucCmd_description != NULL) { puts(pst_Cmd->pucCmd_description); } if(pst_Cmd->func != NULL) { pst_Cmd->func((void *)puCmd); } return; } pst_Cmd++; } return; } /********************************************************* * name:SA17225233 * func: main * date:2017/09/19 **********************************************************/ void main(void) { /*define the var, init cmd */ unsigned char ucCmd[CMD_LEN]; while(CMD_TRUE) { /*clean cmd,input command*/ memset(ucCmd, 0, CMD_LEN); printf("please input cmd:\n"); gets(ucCmd); /*process cmd*/ CMD_Process(ucCmd); } return; }
3、实验关键代码截图
4、实验运行结果截图
5、实验心得
C语言的重要地方就是结构体与指针应用,熟悉并精通指针,就能够写出许多结构化的简洁高效代码!随着实验的进行,我也越来越熟悉指针的运用了!