execfile.c

来自「基于嵌入式linux下」· C语言 代码 · 共 68 行

C
68
字号
#include "dispatch.h"

const char *env_init[]={"USER=unknow","PATH=/tmp",NULL}; //执行文件标识

int DISPATCH_Exec(const char *pathname)
{
	pid_t pid;
	int fd[2];

	if (pipe(fd)<0)
	{
		printf("pipe error\r\n");
		return -1;
	}
	if ((pid=fork())<0)
	{
		printf("fork error\r\n");
		return -1;
	}
	else if (pid==0)
	{
		//if (execle(pathname,"ppp","myarg1","MY ARG1",(char *)0,env_init)<0)
		
		close(fd[1]);
		if(fd[0]!=STDIN_FILENO)
		{
			if(dup2(fd[0],STDIN_FILENO)!=STDIN_FILENO)
			{
				close(fd[0]);
				printf("dup2 error\r\n");
				return -1;
			}
		}
		if (system(pathname)<0)
		{
			printf("system error\r\n");
			return -1;
		}

		exit(0);
	}
//	else
		
	/*
	if (waitpid(pid,NULL,0)<0)
	{
		printf("wait error\r\n");
		return -1;
	}
		
	if ((pid=fork())<0)
	{
		printf("fork error\r\n");
		return -1;
	}
	else if (pid==0)
	{
		if (system(pathname)<0)
		{
			printf("system error\r\n");
			return -1;
		}
	}*/
	
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?