0

I'm creating a shell copy and I have a problem to executable an homemade program. I mean, everything is OK when I want to execute somethings like java, ls, wc, etc... everything that is already present in the PATH variable.

Now I want to be able to execute a "myls" command which is a copy of the original ls. The thing is that my program isn't registered in PATH variable, so when I try to execvp("myls", …), I get an error "no such file or directory".

I would like to know how can I manage this problem and be able to execute my command according this hierarchy :

src
include
bin
makefile
executable <- this one is my main executable
myLs
    executableLS <- would like to be able to call this one through execvp
myPs
    executablePS <- would like to be able to call this one through execvp
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
LenweSeregon
  • 132
  • 1
  • 10
  • 2
    Either put your programs in a directory that is on your `$PATH` or put the directory where the programs are on your `$PATH`, or use an appropriate pathname (relative or absolute) to identify the program to be run. – Jonathan Leffler Dec 27 '17 at 14:56

1 Answers1

2

To allow a program to be executed with execvp(), you must:

  • Either put your programs in a directory already on your $PATH,
  • Or put the directory where the programs live on your $PATH,
  • Or arrange to use the absolute pathname to the programs (/some/where/useful/myLs),
  • Or arrange to use a correct relative pathname to the programs (../useful/myLs or even ./myLs).

Note that execvp() only searches on $PATH when the command name it sees contains no / (so ./myLs stops it looking on $PATH). The relative names are typically least useful; if you change directory, it is likely that the relative pathname changes.

In case of doubt, make sure you have a directory $HOME/bin, put your programs in there (or symlinks to your programs in there), and add $HOME/bin to your path. It lives at the front of my PATH; I assume I know what I'm doing.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278