Jump to content

Encontrar comando em Linux


zecapistolas
 Share

Recommended Posts

MAN which:

It does this by searching the PATH for executable files matching the names of the arguments

Para obteres a variável de ambiente PATH só precisas de fazer:

char * path;
path = getenv("PATH");

Vais obter um string do tipo ":/bin:/etc:/usr/bin:/usr/new/X11:/usr/new:/usr/local/bin". Depois só tens que separar a string pelos ":" (*) e depois procurares o ficheiro binário nessas pastas (**)

*podes usar a função strtok, mas não te esqueças que ela altera a string, portanto tens que copiá-la (strncpy) primeiro.

**podes usar a função stat() para ver se o ficheiro existe:

if(stat("/usr/bin/hello",&st) == 0) printf("o ficheiro existe!");

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.