在C语言中执行shell命令

时间:2022-06-01 20:36:25

C语言中执行shell命令

1、system系统调用

int system(const char * string);

system()会调用fork()产生子进程,由子进程来调用/bin/sh -c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置SIGINTSIGQUIT 信号则会被忽略。

返回值 如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。若参数string为空指针(NULL),则返回非零值。如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno 来确认执行成功。

在编写具有SUID/SGID权限的程序时请勿使用system()system()会继承环境变量,通过环境变量可能会造成系统安全的问题。Use the exec(3) family of functions instead, but not execlp(3) or execvp(3).

#include<stdlib.h>

int main()

{

system("ls -al /data/myname");

return 1;

}

2popen(建立管道I/O

FILE *popen(const char *command, const char *type);

int pclose(FILE *stream);

popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh -c command来执行参数command的指令。依照此typer,w)值,popen()会建立管道连到子进程的标准输出设备或标准输入设备,然后返回一个文件指针。随后进程便可利用此文件指针来读取子进程的输出设备或是写入到子进程的标准输入设备中。此外,所有使用文件指针(FILE*)操作的函数也都可以使用,除了fclose()以外。

Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only.

返回值,若成功则返回文件指针,否则返回NULL,错误原因存于errno中。

在编写具SUID/SGID权限的程序时请尽量避免使用popen()popen()会继承环境变量,通过环境变量可能会造成系统安全的问题。

#include<stdio.h>

int main()

{

FILE *fp;

char buffer[80];

fp=popen("cat /etc/passwd","r");

fgets(buffer,sizeof(buffer),fp);

printf("%s",buffer);

pclose(fp);

return 0;

}

3)使用vfork()新建子进程,然后调用exec函数族

#include "unistd.h"

#include "stdio.h"

int main()

{

char *argv[] = {"ls", "-al", "/etc/passwd", "char*"};

if(vfork() == 0)

{

execv("/bin/ls", argv);

}

else

{

printf("parent.\n")

}

return 0;

}

原文

[1]http://www.cnblogs.com/niocai/archive/2011/07/20/2111896.html