nob 修复popen在linux上的错误行为
This commit is contained in:
1 parent
b4831c8d1c
commit
c4d1bbae92
1 file changed
+47
-12
+47
-12
@@ -4,7 +4,7 @@
|
|||||||
#include "nob.h"
|
#include "nob.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Nob_String_Builder out; // stdout 内容
|
Nob_String_Builder out; // stdout + stderr 合并内容
|
||||||
int exit_code; // 实际退出码
|
int exit_code; // 实际退出码
|
||||||
bool ok; // 是否成功启动
|
bool ok; // 是否成功启动
|
||||||
} Nob_Cmd_Result;
|
} Nob_Cmd_Result;
|
||||||
@@ -15,7 +15,7 @@ typedef struct {
|
|||||||
int count;
|
int count;
|
||||||
} Nob_Files;
|
} Nob_Files;
|
||||||
|
|
||||||
// 运行命令,捕获 stdout(Windows 下会合并 stderr)和真实退出码。
|
// 运行命令,捕获 stdout 并合并 stderr;返回真实退出码。
|
||||||
// 返回 true 仅表示命令成功启动并完成,不代表 exit_code == 0。
|
// 返回 true 仅表示命令成功启动并完成,不代表 exit_code == 0。
|
||||||
bool nob_cmd_capture_popen(const char *tname, Nob_Cmd_Result *res) {
|
bool nob_cmd_capture_popen(const char *tname, Nob_Cmd_Result *res) {
|
||||||
memset(res, 0, sizeof(*res));
|
memset(res, 0, sizeof(*res));
|
||||||
@@ -112,21 +112,56 @@ bool nob_cmd_capture_popen(const char *tname, Nob_Cmd_Result *res) {
|
|||||||
CloseHandle(pi.hThread);
|
CloseHandle(pi.hThread);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
// ---------- POSIX: popen ----------
|
// ---------- POSIX: fork + pipe ----------
|
||||||
FILE *fp = popen(tname, "r");
|
// stdout/stderr 汇入同一管道,语义与 Windows 分支一致。
|
||||||
if (!fp) {
|
Nob_Pipe pp;
|
||||||
nob_log(NOB_ERROR, "popen failed: %s", strerror(errno));
|
if (!nob_pipe_create(&pp)) { return false; }
|
||||||
|
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid < 0) {
|
||||||
|
nob_log(NOB_ERROR, "fork failed: %s", strerror(errno));
|
||||||
|
nob_fd_close(pp.read);
|
||||||
|
nob_fd_close(pp.write);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buffer[4096];
|
if (pid == 0) {
|
||||||
while (fgets(buffer, sizeof(buffer), fp)) {
|
// 子进程:把 stdout/stderr 都接到管道写端,再执行 shell 命令。
|
||||||
nob_sb_append_cstr(&res->out, buffer);
|
if (dup2(pp.write, STDOUT_FILENO) < 0 ||
|
||||||
|
dup2(pp.write, STDERR_FILENO) < 0) {
|
||||||
|
_exit(127);
|
||||||
|
}
|
||||||
|
nob_fd_close(pp.read);
|
||||||
|
nob_fd_close(pp.write);
|
||||||
|
execl("/bin/sh", "sh", "-c", tname, (char *)NULL);
|
||||||
|
_exit(127);
|
||||||
}
|
}
|
||||||
|
|
||||||
int status = pclose(fp);
|
// 父进程:关闭写端,读到 EOF 后回收子进程。
|
||||||
if (status == -1) {
|
nob_fd_close(pp.write);
|
||||||
nob_log(NOB_ERROR, "pclose failed: %s", strerror(errno));
|
char buffer[4096];
|
||||||
|
while (true) {
|
||||||
|
ssize_t n = read(pp.read, buffer, sizeof(buffer));
|
||||||
|
if (n > 0) {
|
||||||
|
nob_sb_append_buf(&res->out, buffer, (size_t)n);
|
||||||
|
} else if (n == 0) {
|
||||||
|
break;
|
||||||
|
} else if (errno == EINTR) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
nob_log(NOB_ERROR, "read failed: %s", strerror(errno));
|
||||||
|
nob_fd_close(pp.read);
|
||||||
|
int ignored = 0;
|
||||||
|
while (waitpid(pid, &ignored, 0) < 0 && errno == EINTR) {}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nob_fd_close(pp.read);
|
||||||
|
|
||||||
|
int status = 0;
|
||||||
|
while (waitpid(pid, &status, 0) < 0) {
|
||||||
|
if (errno == EINTR) { continue; }
|
||||||
|
nob_log(NOB_ERROR, "waitpid failed: %s", strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (WIFEXITED(status))
|
if (WIFEXITED(status))
|
||||||
|
|||||||
Reference in new issue
Block a user