发一波福利,操作系统的实验内容,大家可以借鉴一下,不过我的代码可能也存在一定的问题。

因为在一开始老师是一节一节课教的,当时并不知道后面还会用输入输出重定向,管道等一系列问题,我的兴趣也不在这个方面也没有预习,所以一来代码写的比较丑,二来没有对于代码进行一个合理的规划,写的也比较乱。

代码暂时实现到输入输出重定向,之后可能会加上管道处理等方面的程序。

如果让我重新写这段代码应该会规划的更好一点吧

/*author:Samsons
date:2015.4.10*/
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/wait.h> #define MAX(100)
#define LEN(100) char *arglist[MAX]; //shell指令参数表
int num; //shell指令参数个数 int execute(char* arglist[])//执行外部命令
{
int error;
error=execvp(arglist[],arglist);
if (error==-) printf("failed\n");
exit();
} char* make(char *buf)//将字符串传入参数表内
{
char *cp;
cp=malloc(strlen(buf)+);
if (cp==NULL)
{
fprintf(stderr,"no memory\n");
exit();
}
strcpy(cp,buf);
return cp;
} int my_system(char *buf,char *arglist[])//对于字符串进行分割
{
int num,j,i,last;
char buffer[LEN];
num=;
i=;
while (num<MAX)
{
if (buf[i]=='\n')
{
arglist[num]=NULL;
return num;
}
if (buf[i]==' ') i++;
last=i;
while (buf[i]!=' ' && buf[i]!='\n') i++;
for (j=last;j<i;j++) buffer[j-last]=buf[j];
buffer[j-last]='\0';
arglist[num++]=make(buffer);
}
}
int inner(char *arglist[])//执行内置指令
{
if (strcmp(arglist[],"exit\0")==)//exit
{
exit();
return ;
}
else
if (strcmp(arglist[],"pwd\0")==)//pwd
{
char buf[LEN];
getcwd(buf,sizeof(buf));//获得当前目录
printf("Current dir is:%s\n",buf);
return ;
}
else
if (strcmp(arglist[],"cd\0")==)//cd
{
char buf[LEN];
if (chdir(arglist[])>=)
{
getcwd(buf,sizeof(buf));
printf("Current dir is:%s\n",buf);
}
return ;
}
else return ;
} void cat_in(char *q)//输入重定向
{
char t[];
int fd;
if (q[]=='<')
{
strcpy(t,q+);
fd=open(t,O_RDONLY);
arglist[]=NULL;
if (fd==-)
{
printf("file open failed\n");
return;
}
dup(fd,);
close(fd);
}
} void cat_out(char *q)//输出重定向
{
char t[];
int fd;
if (q[]=='>')
{
strcpy(t,q+);
arglist[num-]=NULL;
num--;
fd=open(t,O_CREAT|O_RDWR);
if (fd==-)
{
printf("file open failed\n");
return;
}
dup2(fd,);
close(fd);
}
} int main()
{
int i,pid;
char buf[LEN];
while ()
{
fgets(buf,LEN,stdin);//读入单行指令
num=my_system(buf,arglist);//指令分割
int inner_flag;
inner_flag=inner(arglist);//内置指令判断
if (inner_flag==)
{
pid=fork();//建立新的进程
if (pid==)
{
if (arglist[]!=NULL)
{
char q[LEN];
strcpy(q,arglist[]);
cat_in(q);//输入重定向
}
if (arglist[num-]!=NULL)
{
char q[LEN];
strcpy(q,arglist[num-]);
cat_out(q);//输出重定向
}
execute(arglist);//执行
}
waitpid(pid,NULL,);
}
}
return ;
}

最新文章

  1. UEditor编辑器使用示例
  2. Oracle 11.2.0.4 DataGuard 环境打PSU,OJVM PSU补丁快速参考
  3. 基于webdriver的jmeter性能测试-Selenium IDE
  4. NOIP模拟赛20161022
  5. Bool 类型变量的使用
  6. 3.3---集合栈(CC150)
  7. jquery的validate.js 和 form.js 的使用方法
  8. mysql管理员操作
  9. HDU5765 Bonds 最小割极
  10. HUOJ-10857 最大的面积 凸包+DP
  11. [学习笔记]设计模式之Chain of Responsibility
  12. 异步方式向WPF ListBox控件中一条一条添加记录
  13. jQuery(3)——DOM操作
  14. 【小错误】WPF代码报错:未将对象引用设置到对象的实例。
  15. aspnetcore.webapi实践k8s健康探测机制 - kubernetes
  16. Devstack: A copy of worked local.conf I&#39;m sharing with you.
  17. 网络模型 - 每天5分钟玩转 Docker 容器技术(169)
  18. 仓储repository概念
  19. php 计算两点地理坐标的距离
  20. python 必学模块collections

热门文章

  1. Spring的IoC与AOP的理解
  2. Id vs Instancetype
  3. Angular2 Pipe
  4. 【React】使用 create-react-app 快速构建 React 开发环境
  5. 5、Spring Cloud-声明式调用 Feign(下)
  6. [Python 多线程] Barrier (十一)
  7. Win32多线程之核心对象
  8. cocos2d-x 音效中断问题
  9. WAPM环境配置(PHP入门2)
  10. Oracle AWR与警报系统一