1

我正在尝试在 Windows 中通过 C 构建一个简单的 shell,我的平台不好,所以我搜索并从下面的一些代码开始:

shell_terminal = STDIN_FILENO;
shell_is_interactive = isatty (shell_terminal);
if (shell_is_interactive)
{
    /* Loop until we are in the foreground.  */
    while (tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp ()))
    kill (- shell_pgid, SIGTTIN);

    /* Ignore interactive and job-control signals.  */
    signal (SIGINT, SIG_IGN);
    signal (SIGQUIT, SIG_IGN);
    signal (SIGTSTP, SIG_IGN);
    signal (SIGTTIN, SIG_IGN);
    signal (SIGTTOU, SIG_IGN);
    signal (SIGCHLD, SIG_IGN);

    /* Put ourselves in our own process group.  */
    shell_pgid = getpid ();
    if (setpgid (shell_pgid, shell_pgid) < 0)
    {
    perror ("Couldn't put the shell in its own process group");
    exit (1);
    }

    /* Grab control of the terminal.  */
    tcsetpgrp (shell_terminal, shell_pgid);

    /* Save default terminal attributes for shell.  */
    tcgetattr (shell_terminal, &shell_tmodes);
}

但我有一些错误:

[Error] 'tcgetpgrp' was not declared in this scope
[Error] 'getpgrp' was not declared in this scope
[Error] 'SIGTTIN' was not declared in this scope
[Error] 'kill' was not declared in this scope
...

我认为我的环境有问题或错过了一些图书馆。我的问题是什么?谢谢!

4

1 回答 1

0

因为,tcgetpgrpfunction 不是由 定义的ISO C standard,它必须被所有操作系统支持,而是由IEEE POSIX标准定义,这是Unix系统环境最普遍支持的。

例如:

<unistd.h>编译这个程序所需要的,是标准定义的头文件POSIX,不是ISO C标准定义的。

要么升级到POSIX兼容系统,要么使用Windows API,它为 windows 平台提供类似的服务集。

于 2013-08-03T09:32:32.770 回答