概述
1. 编辑程序
vim minishell.c
注:编译完代码:Esc:wq 保存回车
2. 代码内容
#define true 1
#define flase 0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void dir()
{
system("ls -l");
}
void cop(char cmdl[])
{
const char space[2] = " ";
char* arg1, command[80];
char* arg2;
strtok(cmdl, space);
arg1 = strtok(NULL, space);
arg2 = strtok(NULL, space);
if (arg1 == NULL || arg2 == NULL)
{
printf("缺少参数n");
return -1;
}
sprintf(command, "cp %s %s", arg1, arg2);
system(command);
}
void era(char cmdl[])
{
const char space[2] = " ";
char* arg, command[80];
strtok(cmdl, space);
arg = strtok(NULL, space);
if (arg == NULL)
{
printf("缺少参数n");
return -1;
}
sprintf(command, "rm -rf %s", arg);
system(command);
}
void disp(char cmdl[])
{
const char space[2] = " ";
char* arg, command[80];
strtok(cmdl, space);
arg = strtok(NULL, space);
if (arg == NULL)
{
printf("缺少参数n");
return -1;
}
sprintf(command, "echo %s", arg);
system(command);
}
void main()
{
char cmdl[80];
char* token;
char* scwt[] = { "end","dir","cop","era","disp" };
static int cmdnum = 5; //可用的命令数
char cmd[80];
int j, n;
while (true)
{
printf("please input command: ");
gets(cmdl); //取命令行输入
n = strcspn(cmdl, " "); //取命令命令部分
if (n <= 0 && strlen(cmdl) <= 0)
continue;
strncpy(cmd, cmdl, n);
cmd[n] = '