0

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!

BillTheKid
  • 377
  • 1
  • 13
  • 1
    [How is the command suggestion implemented in the bash shell?](https://stackoverflow.com/q/35712703/2752075) – HolyBlackCat Dec 04 '21 at 09:37
  • You would probably need to call the shell and pass the command to it instead of directly calling the command – Gerhardh Dec 04 '21 at 09:38

2 Answers2

2

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

execl("/bin/bash", "bash", "-c", "asdf", "/bin/asdf");
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
1

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111