【问题标题】:strerrno(errno) doesn't return the same message with terminalstrerrno(errno) 不会与终端返回相同的消息
【发布时间】:2021-12-04 09:33:14
【问题描述】:

当我打印执行不存在的命令(如“asdf”)的错误消息时:

execl("/bin/asdf", "asdf", NULL);
printf("%s\n", strerror(errno));

我收到消息No such file or directory。另一方面,如果我在终端中运行相同的命令,我会得到

Command 'asdf' not found, did you mean:

  command 'sdf' from deb sdf (2.001+1-7)
  command 'asdfg' from deb aoeui (1.7+20160302.git4e5dee9-2)
  command 'adsf' from deb ruby-adsf (1.4.3+dfsg1-1)
  command 'sadf' from deb sysstat (12.2.0-2ubuntu0.1)

Try: sudo apt install <deb name>

当我正在构建一个伪终端时,有没有办法在 Unix 中重新创建实际终端发送的消息?提前谢谢!

【问题讨论】:

标签: c errno


【解决方案1】:

按照@Gerhardh 的建议,通过 shell 运行您的命令:

execl("/bin/bash", "bash", "-c", "asdf", "/bin/asdf");

【讨论】:

    【解决方案2】:

    有没有办法在 Unix 中重新创建实际终端发送的消息?

    简而言之伪代码:

    execl("/bin/asdf", "asdf", NULL);
    if (errno == command does not exists) {
        if (fork() == 0) {
            execl("/usr/lib/command-not-found", "/bin/asdf", NULL);
        }
        wait();
    }
    

    在结尾处查看/etc/bash.bashrc - 处理程序就在那里。

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 1970-01-01
      • 2011-05-20
      • 2020-08-05
      • 1970-01-01
      • 2012-09-12
      • 2023-03-30
      • 2014-08-17
      • 1970-01-01
      相关资源
      最近更新 更多