Prayer

在一般中寻求卓越
posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

linux下system函数的调用

Posted on 2009-05-05 16:25 Prayer 阅读(1969) 评论(0)  编辑 收藏 引用 所属分类: LINUX/UNIX/AIX
system函数已经被收录在标准c库中,可以直接调用
//mainnew.cpp

#include <stdlib.h>



int main(){

system("mkdir $HOME/.SmartPlatform/");

system("mkdir $HOME/.SmartPlatform/Files/");



system("cp mainnew.cpp $HOME/.SmartPlatform/Files/");

return 0;

}
system函数的源码
#include <sys/types.h>

#include <sys/wait.h>

#include <errno.h>

#include <unistd.h>



int system(const char * cmdstring){

pid_t pid;

int status;



if(cmdstring == NULL){

return (1);

}

if((pid = fork())<0){

status = -1;

}

else if(pid = 0){

execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);

-exit(127); //子进程正常执行则不会执行此语句

}

else{

while(waitpid(pid, &status, 0) < 0){

if(errno != EINTER){

status = -1;

break;

}

}

}

return status;

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理