linux下使用系统调用模拟SHELL
来源:互联网 发布:windows启动项管理 编辑:程序博客网 时间:2024/04/29 15:26
结构还算清晰,昨天的blog里已经大致说过了,不多解释了
RTFSC :)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <wait.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
#include <errno.h>
typedef struct command_struct{
int comargc;
char* command;
char* comargv[10];
}command_struct;
int com_inside(char*);
int search(char*,char*,char*);
int main(int argc,char** argv)
{
int flag;
int i,c;
int m,n;
int * status;
char cwd[255];
int exeflag;
char tempcom[10];
char full_pathname[64]="/home/yg/c_test/0921";
char pathname[64];
char scanargv[10][10]={};
command_struct com;
while(1)
{
if((int)getcwd(cwd,64)==-1)
printf("get cwd error");
printf("%s>",cwd);
/*begin init command_struct*/
for(m=0;m<10;m++)
{
for(n=0;n<10;n++)
{
scanargv[m][n]=0;
}
}
i=0;
while((c=getchar())!='/n')
{
if(c!=' ')
{
scanargv[i][0]=c;
scanf("%s",scanargv[i++]+1);
}
else
{
scanf("%s",scanargv[i++]);
}
}
com.comargc=i-1;
com.command=scanargv[0];
i=0;
while(i<10)
{
com.comargv[i]=scanargv[i];
i++;
}
/*end init command_struct*/
if(com.command=="/n")continue;
if((strcmp(com.command,"quit")==0)||(strcmp(com.command,"exit")==0))exit(1);
if((flag=com_inside(com.command))==-1)exit(1);
else if(flag==1)continue;
else if(flag==0)
{
strcpy(tempcom,com.command);
if((exeflag=search(full_pathname,tempcom,pathname))==0)
printf("Bad command or file name/n");
else
{
if(fork()==0)
{
i=0;
while(i<=com.comargc)
{
argv[i]=com.comargv[i];
i++;
}
if(execve(pathname,argv,0)==-1)
{
printf("error is %s/n",strerror(errno));
}
}
else
{
wait(status);
}
}
}
}
return 1;
}
int com_inside(char* command)
{
if(command==NULL)return -1;
if(!(strcmp(command,"ok")))
{
printf("OK/n");
return 1;
}
if(!(strcmp(command,"ver")))
{
printf("0.0.0.1/n");
return 1;
}
return 0;
}
int search(char* path,char* name,char* full_pathname)
{
int flag;
int i;
char pathname[64]={};
DIR * dirp;
struct dirent * direntp;
strcpy(pathname,path);
i=0;
while(pathname[i]!=0)
{
i++;
}
if(pathname[i-1]!=47)
{
strcat(pathname,"/");
}
if((dirp=opendir(path)))
{
while((direntp=readdir(dirp))!=NULL)
{
if(strcmp(direntp->d_name,name)==0)
{
flag=1;
break;
}
else flag=0;
}
closedir(dirp);
if(flag)
{
strcat(pathname,name);
strcpy(full_pathname,pathname);
return 1;
}
else return 0;
}
else
{
return 0;
}
}
- linux下使用系统调用模拟SHELL
- Linux 系统下Fortran调用shell
- 使用readdir系统调用模拟shell ls功能
- Linux下使用c++调用shell命令
- Linux系统下Python调用系统Shell的方法
- Linux系统下查看文件调用情况Shell命令
- Linux系统下查看文件调用情况Shell命令
- Linux系统下高级语言调用shell脚本
- Linux下LAMPP使用root权限调用shell
- linux系统shell使用
- linux下调用shell命令
- Linux下popen调用shell
- linux下系统调用
- Linux下用Shell模拟进度条
- linux下使用系统调用编程实现copy命令功能
- linux下使用系统调用编程实现dir命令功能
- linux下使用系统调用取得当前用户名
- linux下使用系统调用取得当前用户名 (修订)
- VS.NET又不能调试
- 1个破解SQL密码的存储过程
- 那场不期而遇的网恋
- xplanner使用手记(安装)(2)
- VB实现局域网内的文件传输
- linux下使用系统调用模拟SHELL
- [休闲] 一天作息时间的安排
- MapAspect开源项目正式启动
- SVG是个好东西
- 美丽的回忆
- [C/C++]标准MIDI文件格式
- 第一篇文章
- 转一个《真正爱你的女孩》
- Microsoft Application Blocks for .NET