Q:

How to find the full pathname of a binary within C?

I am writing a simple app in C that is meant to act as a shell. It takes user input from stdin, stores it as a string, and then uses execl with that string to execute it in the terminal as a binary executable. However, execl does not take binaries in the form of "ls" or "who", but instead it expects their absolute pathname. What can I do, within my C code, to find the full pathname of a given binary executable without a specified path, so that I only have to enter executable binaries such as "ls" and "who" into standard input, and execl can still be given their full pathnames. I am doing this on a Linux.

A:

Use execlp or execvp instead of execl or execvp. The p versions of these calls search PATH to find the executable if it doesn't contain any slashes.

