Home > Mobile >  strerrno(errno) doesn't return the same message with terminal
strerrno(errno) doesn't return the same message with terminal

Time:12-05

When I am printing the error message of the execution of a non-existent command like "asdf":

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

I get the message No such file or directory. On the other hand, if I run the same command in the terminal, I get

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>

As I am building a pseudo terminal, is there a way to recreate the message send by the actual terminal in Unix? Thank you in advance!

CodePudding user response:

As suggested by @Gerhardh, run your command via the shell:

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

CodePudding user response:

is there a way to recreate the message send by the actual terminal in Unix?

In short pseudocode:

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

See /etc/bash.bashrc near the end - the handler is there.

  • Related