加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

执行shell命令的popen和system函数封装

发布时间:2020-12-15 09:15:27 所属栏目:安全 来源:网络整理
导读:linux下执行shell命令的popen和system函数封装: #include stdlib.h#include stdio.h#include errno.h#include string.h#include sys/wait.hint shell_popen(const char * cmd) { if(!cmd) { printf("shell_popen cmd is NULL!n");return -1;} printf("shel
linux下执行shell命令的popen和system函数封装:
 
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>


int shell_popen(const char * cmd) 
{ 	
	if(!cmd) 
	{ 
		printf("shell_popen cmd is NULL!n");
		return -1;
	} 

	printf("shell_popen : %sn",cmd);

	FILE * fp; 
	int ret; 
	char buf[1024]; 

	if((fp = popen(cmd,"r")) == NULL) 
	{ 
		printf("shell_popen error: %s/n",strerror(errno)); 
		return -1; 
	} 
	else
	{
		while(fgets(buf,sizeof(buf),fp)) 
		{ 
			if(buf[strlen(buf)-1] == 'n')
			{
				buf[strlen(buf)-1] = '';
			}

			printf("%sn",buf); 
		} 

		if((ret = pclose(fp)) == -1) 
		{ 
			printf("shell_popen fail = [%s]n",strerror(errno));  
			return -1;
		}
		else 
		{ 
			if(WIFEXITED(ret))
			{  
				if(0 == WEXITSTATUS(ret)) 
				{
					printf("shell_popen run shell script successfully,status = [%d]n",WEXITSTATUS(ret)); 
					ret = 0;
				}
				else
				{
					printf("shell_popen run shell script fail,WEXITSTATUS(ret)); 
					ret = -1;
				}	
			}
			else if(WIFSIGNALED(ret))
			{    
				printf("shell_popen abnormal termination,signal number = [%d]n",WTERMSIG(ret));
				ret = -1;
			}
			else if(WIFSTOPPED(ret))
			{    
				printf("shell_popen process stopped,WSTOPSIG(ret));
				ret = -1;
			}
			else
			{
				printf("shell_popen run shell unknown error,ret = [%d]n",ret); 
				ret = -1;
			}
		} 
	}

	return ret;
} 


int shell_system(const char * cmd)
{ 	
	if(!cmd)
	{		
		printf("shell_system cmd is nulln");
		return -1;
	}

	printf("shell_system : %sn",cmd);

	int ret = 0;
	typedef void (*sighandler_t)(int); 	
	sighandler_t old_handler;    
	old_handler = signal(SIGCHLD,SIG_DFL);  
	ret = system(cmd);   
	signal(SIGCHLD,old_handler);   

	if(ret == -1)
	{    
		printf("shell_system fail = [%s]n",strerror(errno));  
		ret = -1;
	} 
	else
	{
		if(WIFEXITED(ret))
		{  
			if(0 == WEXITSTATUS(ret)) 
			{
				printf("shell_system run shell script successfully,WEXITSTATUS(ret)); 
				ret = 0;
			}
			else
			{
				printf("shell_system run shell script fail,WEXITSTATUS(ret)); 
				ret = -1;
			}	
		}
		else if(WIFSIGNALED(ret))
		{    
			printf("shell_system abnormal termination,WTERMSIG(ret));
			ret = -1;
		}
		else if(WIFSTOPPED(ret))
		{    
			printf("shell_system process stopped,WSTOPSIG(ret));
			ret = -1;
		}
		else
		{
			printf("shell_system run shell unknown error,ret); 
			ret = -1;
		}
	}
	
	return ret;
}


int main(int argc,char* argv[])
{
	if(argc <= 1)
	{
		return 0;
	}
	
	printf("-----------------------------------------n");
	shell_popen(argv[1]);
	printf("-----------------------------------------n");
	shell_system(argv[1]);		
	return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读