zecapistolas Posted October 12, 2009 at 06:48 PM Report Share #291387 Posted October 12, 2009 at 06:48 PM Boas, Preciso de uma função escrita em C, que me procure um determinado comando no Linux, mas não faço ideia por onde começar.... Tipo, o "who" encontra-se em "/usr/bin/who".... Dicas, aceitam-se.... 😁 cumps 👍 Link to comment Share on other sites More sharing options...
zecapistolas Posted October 12, 2009 at 07:04 PM Author Report Share #291393 Posted October 12, 2009 at 07:04 PM Abrindo uma consola em Linux e digitando "which who" o resultado é "/usr/bin/who".... Mas como faço isto em C?! cumps 👍 Link to comment Share on other sites More sharing options...
Ferreira Posted October 12, 2009 at 07:23 PM Report Share #291395 Posted October 12, 2009 at 07:23 PM Se queres uma função em C que faça isso usa um fork seguido de um exec. Se queres fazer a tua própria implementação disso é mais complicado... http://twitter.com/ferreira Link to comment Share on other sites More sharing options...
zecapistolas Posted October 12, 2009 at 07:29 PM Author Report Share #291399 Posted October 12, 2009 at 07:29 PM Se queres uma função em C que faça isso usa um fork seguido de um exec. Se queres fazer a tua própria implementação disso é mais complicado... Pois, mas o resultado de "exec" é um int e não uma String.... Eu quero utilizar o caminho internamente no programa.... cumps 👍 Link to comment Share on other sites More sharing options...
IceBrain Posted October 12, 2009 at 10:45 PM Report Share #291441 Posted October 12, 2009 at 10:45 PM 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 More sharing options...
zecapistolas Posted October 13, 2009 at 01:47 PM Author Report Share #291509 Posted October 13, 2009 at 01:47 PM IceBrain, obrigado pela ajuda.... Já consegui fazer o me indicas-te.... 😁 cumps 👍 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now