没有合适的资源?快使用搜索试试~ 我知道了~
运用C编写的shell小程序,在linux下运行。欢迎大家留言并且查看,想要的和博主私信,博主免费送。
资源推荐
资源详情
资源评论
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define MAX_STRING 256
#define SIGNIN "<"
#define SIGNOUT ">"
#define SIGNPINE "|"
const char* expst = "exit"; //控制终端输入exit,shell退出
int strToken(char* buf,char** p) //以空格符为标准分割字符串,字符串头指针存储在指针数组p中
{
int i = 0;
while((p[i] = strtok(buf, " ")) != NULL)
{
i++;
buf = NULL;
}
p[i-1] = NULL; //重点:在linux终端输入字符串是以‘\n’结束的,比如ls -l后回车,则三个字符串分别为ls\0,-l\0,\n\0
return (i-1); //p[i-1] = NULL 的目的就是消除字符串\n\0,为exeve的调用提供结尾参数NULL
}
int strProTok(char* buf, char** p)
{
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define MAX_STRING 256
#define SIGNIN "<"
#define SIGNOUT ">"
#define SIGNPINE "|"
const char* expst = "exit"; //控制终端输入exit,shell退出
int strToken(char* buf,char** p) //以空格符为标准分割字符串,字符串头指针存储在指针数组p中
{
int i = 0;
while((p[i] = strtok(buf, " ")) != NULL)
{
i++;
buf = NULL;
}
p[i-1] = NULL; //重点:在linux终端输入字符串是以‘\n’结束的,比如ls -l后回车,则三个字符串分别为ls\0,-l\0,\n\0
return (i-1); //p[i-1] = NULL 的目的就是消除字符串\n\0,为exeve的调用提供结尾参数NULL
}
int strProTok(char* buf, char** p)
{
int i = 0;
while((p[i] = strtok(buf, " ")) != NULL) //以管道符‘|’分割字符串时,比如第一次ls -l | wc,不是以\n结束的
{
i++;
buf = NULL;
}
return i;
}
void InOrOut(int i, char** p) //以'<','>'分别判断进而重定向
{
int j = 0;
int pidSignIn,pidSignOut;
for(j = 0; j<i; j++)
{
if(strcmp(p[j], SIGNIN) == 0)
{
p[j] = NULL;
pidSignIn = open(p[j+1], O_RDONLY);
if(pidSignIn < 0)
{
perror("open");
exit(1);
}
dup2(pidSignIn, STDIN_FILENO);
close(pidSignIn);
break;
}
剩余7页未读,继续阅读
资源评论
知更鸟11
- 粉丝: 12
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功