#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
static char * usercmd[] = {"dir","cop","era","dis","end"};
static char * syscmd[] = {"ls -l","cp","rm -f","echo"};
static char * usage =
"Welcome to use myshell, the follows are supported command\n"
"dir -- show current directory and file\n"
"cop file1 file2 -- copy file1 to file2 \n"
"era file -- delete file\n"
"dis string -- display string on screen\n"
"end -- exit myshell \n"
"NOTICE: Blank is avail before or after command\n";
int main(int argc, char * argv[])
{
char *input=NULL;
char *cmd=NULL,*para=NULL;
char buf[MAXSIZE];
int i,count;
input = malloc(MAXSIZE);
cmd = malloc(MAXSIZE);
para = malloc(MAXSIZE);
if (input == NULL || cmd == NULL || para == NULL)
{
perror("malloc failed");
exit(-1);
}
while(1)
{
printf("myshell>");
fgets(input, 100, stdin);
*(input+(strlen(input)-1)) = '\0';//delete '\n'
while(*input != '\0')
{
if (*input != ' ' && *input != '\t')
break;
++input;
}
if (*input == '\0')//all input is blank
{
printf("%s",usage);
continue;
}
//division input to command and parameter parts
count = strcspn(input, " \t");
strncpy(cmd, input, count);
*(cmd + count) = '\0';
strncpy(para, input+count,strlen(input)-count);
*(para+(strlen(input)-count))='\0';
if (0 == strcmp(cmd, usercmd[0]))
{
//根据这道题,分配的buf够用了,应该不会越界。
//如果要求很严格的话,这里用strncpy,strncat。
strcpy(buf,syscmd[0]);
strcat(buf,para);
system(buf);
continue;
}
if (0 == strcmp(cmd, usercmd[1]))
{
strcpy(buf,syscmd[1]);
strcat(buf,para);
system(buf);
continue;
}
if (0 == strcmp(cmd, usercmd[2]))
{
strcpy(buf,syscmd[2]);
strcat(buf,para);
system(buf);
continue;
}
if (0 == strcmp(cmd, usercmd[3]))
{
strcpy(buf,syscmd[3]);
strcat(buf,para);
system(buf);
continue;
}
if (0 == strcmp(cmd, usercmd[4]))
{
printf("Exit myshell\n");
break;
}
printf("%s",usage);
}
return 0;
}
w.rar_dos命令_拷贝文件 _文件删除拷贝
版权申诉
146 浏览量
2022-09-19
13:53:31
上传
评论
收藏 1KB RAR 举报
局外狗
- 粉丝: 64
- 资源: 1万+
最新资源
- XILINXFPGA源码Xilinxspratan3xcs100E(VGAPS2)
- XILINXFPGA源码XilinxSPARTAN-3E入门开发板实例
- XILINXFPGA源码XilinxSdramVerilog和VHDL版本文档
- 物联网智能家居方案-基于Nucleo-STM32L073&机智云(大赛作品,文档齐全,可直接运行)(文档加Matlab源码)
- XILINXFPGA源码XilinxISE9.xFPGACPLD设计源码
- 成都市地图含高新区(高新南区,高新西区),天府新区,东部新区虚拟行政区划
- XILINXFPGA源码XilinxEDK设计试验
- XILINXFPGA源码XilinxEDKMicroBlaze内置USB固件程序
- 基于 django 的视频点播后台管理系统源代码+数据库
- 基于Java的网上医院预约挂号系统的设计与实现(部署视频)-kaic.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈