I am working on an implementation of the Unix shell in C, and I'm currently dealing with the problem of relative paths. Especially when entering commands. At the moment, I have to enter the full path to the executable every time I would rather put "ls" or "cat".
I managed to get the env $ PATH variable. My idea is to split the variable into a ":" character, and then add each new line to the command name and check if the file exists and is executable.
For example, if my PATH is "/ bin: / usr / bin" and I enter "ls", I would like the program to first check if "/ bin / ls" exists and is executable if you don’t go to "/ usr / bin / ".
Two questions:
1) Is this a good way to do this? (Is it not necessary to be the best. I just want to make sure that it works.
2) More importantly, How can I check in C if the file exists and is executable?
Hope I'm clear enough and ... well thanks :)
source
share