Ahmed El-Gamal Ahmed El-Gamal - 1 year ago 123
C Question

How to get Linux command location in C?

I am using

in C, but it demands to get the path of the command to get it executed, For example:

  • To execute
    I must have
    char* command = "/bin/ls";

  • To execute
    I must have char*
    command = "/usr/bin/gedit";

My question is how to get the string
in C ?

Answer Source

You can get the PATH variable from the environment. Then you parse it to get each component, then check in the location of each component whether the given command (file) exists there.

This is basically what the which command does. Source code of linux utilities can be found on-line

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download