my_popen(), my_pclose()代码
my_popen.c
#include "head.h"
static pid_t *childpid = 0;
static int maxsize = 0;
FILE *my_popen(const char *cmd, const char *type) {
FILE *fp;
int pipefd[2];
pid_t pid;
if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0) {
errno = EINVAL;
return 0;
}
if (!childpid) {
maxsize = sysconf(_SC_OPEN_MAX);
printf("maxsize = %d\n", maxsize);
if (!(childpid = (pid_t *)calloc(maxsize, sizeof(pid_t)))) {
return 0;
}
}
if (pipe(pipefd) < 0) return 0;
if ((pid = fork()) < 0) return 0;
if (!pid) {
if (type[0] == 'r') {
close(pipefd[0]);
if (pipefd[1] != STDOUT_FILENO) {
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
}
} else {
close(pipefd[1]);
if (pipefd[0] != STDIN_FILENO) {
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
}
}
execl("/bin/sh", "sh", "-c", cmd, NULL);
}
if (type[0] == 'r') {
close(pipefd[1]);
if ((fp = fdopen(pipefd[0], type)) == 0) {
return 0;
}
} else {
close(pipefd[0]);
if ((fp = fdopen(pipefd[1], type)) == 0) {
return 0;
}
}
childpid[fileno(fp)] = pid;
return fp;
}
int my_pclose(FILE *fp) {
int status, pid, fd;
fd = fileno(fp);
pid = childpid[fd];
if (!pid) {
errno = EINVAL;
return -1;
}
fflush(fp);
close(fd);
wait4(pid, &status, WUNTRACED, 0);
return status;
}
head.h
#ifndef _HEAD_H
#define _HEAD_H
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <pthread.h>
FILE *my_popen(const char *cmd, const char *type);
int my_pclose(FILE *fp);
#endif
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~