Linux下C程序执行shell命令示例

Linux下C程序执行shell命令示例

system()函数

头文件:#include<stdlib.h>

函数原型:int system(const char *cmdstring);

static 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 != EINTR)
{
status = -1;
break;
}
}
}
return(status);
}

#include<stdio.h>
int main()
{
system(“mkdir name”);/
return 0;
}

猜你喜欢

转载自blog.csdn.net/u010835747/article/details/105241130
今日推荐