【发布时间】:2018-09-05 08:57:10
【问题描述】:
我当前的项目有一个奇怪的问题。使用 ncurses,我正在制作一个基于 lsh 的 shell,在我介绍 ncurses 之前,它可以正常工作,只需编写 execvp 的输出即可。然而,现在,输出的长度在我的提示之前缩进,这实际上也将与它的 X 坐标移到一边(因此缩进似乎不是行的一部分)。
我认为这是由于分叉到没有 ncurses 的子进程(或类似的东西)。
您可以看到完整的代码here,但这是运行 execvp 的部分:
int shell_launch(char **args) {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
// Child process.
// Check if user is trying to run an allowed program.
for (int i = 0; i < arrlen(allowed_cmds); i++) {
if (strcmp(allowed_cmds[i], args[0]) == 0) {
if (execvp(args[0], args) == -1) {
perror("shell");
}
}
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("shell");
} else {
// Parent process
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
【问题讨论】:
-
听起来您需要使用管道来读取子进程的输出,以便您可以通过 ncurses 输出它