转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> SyBase >> 正文
linux下使用系统调用模拟SHELL         ★★★★

linux下使用系统调用模拟SHELL

作者:闵涛 文章来源:闵涛的学习笔记 点击数:645 更新时间:2009/4/22 23:08:05

结构还算清晰,昨天的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;    
    } 
}

 


[聊天工具]MSN Shell v4.2.25 修正版发布__天极Yesky  [聊天工具]中国人的好作品:MSN Shell最新版下载__天极Yesky
[聊天工具]MSN的最佳伴侣MSN Shell__天极Yesky  [常用软件][网络]究级进化!MSN SHELL官方授权评测!
[常用软件][网络]MSN Shell 3.1.0.526评测报告  [VB.NET程序]走近VB.NET十六 SendKeys方法与Shell函数
[Delphi程序]Delphi Shell  [VB.NET程序]在VB和Web页面中使用Shell对象
[Web开发]ASP.NET下使用WScript.Shell执行命令  [SyBase]Linux Shell
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台