myopen.c

来自「linux网络编程中的经典源码」· C语言 代码 · 共 37 行

C
37
字号
#include	"unp.h"intmy_open(const char *pathname, int mode){	int			fd, sockfd[2], status;	pid_t		childpid;	char		c, argsockfd[10], argmode[10];	Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd);	if ( (childpid = Fork()) == 0) {		/* child process */		Close(sockfd[0]);		snprintf(argsockfd, sizeof(argsockfd), "%d", sockfd[1]);		snprintf(argmode, sizeof(argmode), "%d", mode);		execl("./openfile", "openfile", argsockfd, pathname, argmode,			  (char *) NULL);		err_sys("execl error");	}	/* parent process - wait for the child to terminate */	Close(sockfd[1]);			/* close the end we don't use */	Waitpid(childpid, &status, 0);	if (WIFEXITED(status) == 0)		err_quit("child did not terminate");	if ( (status = WEXITSTATUS(status)) == 0)		Read_fd(sockfd[0], &c, 1, &fd);	else {		errno = status;		/* set errno value from child's status */		fd = -1;	}	Close(sockfd[0]);	return(fd);}

⌨️ 快捷键说明

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