0

This is an example that our professor gave us for execl(). There are 2 files in a folder, com1.c looks like

#include <stdio.h>
#include <unistd.h>

int main(){
    printf("hello...");
    fflush(stdout);
    execlp("com2","com2",(char*)NULL);
    perror("err at execl");
    return 1;
}

and com2.c looks like

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main(){
    write(1, "...you unicorn ;)",13);
    return 0;
}

At runtime it gives me this message: hello...err at execl: No such file or directory

How can I get "hello...... you unicorn ;" ? Anticipated thanks.

2 Answers2

0

Make sure that the directory containing com2 is in your PATH environment variable.

PATH=$PATH:/path/to/directory

where /path/to/directory is the directory where you put com2.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You haven't specified what OS you're running ... but, in general, the directory that contains the program you wish to exec should be in your $PATH:

https://linux.die.net/man/3/execlp

The execlp(), execvp(), and execvpe() functions duplicate the actions of the shell in searching for an executable file if the specified filename does not contain a slash (/) character. The file is sought in the colon-separated list of directory pathnames specified in the PATH environment variable.

If this variable isn't defined, the path list defaults to the current directory followed by the list of directories returned by confstr(_CS_PATH). (This confstr(3) call typically returns the value "/bin:/usr/bin".) If the specified filename includes a slash character, then PATH is ignored, and the file at the specified pathname is executed.

Note:

On some other systems, the default path (used when the environment does not contain the variable PATH) has the current working directory listed after /bin and /usr/bin, as an anti-Trojan-horse measure. Linux uses here the traditional "current directory first" default path.

paulsm4
  • 114,292
  • 17
  • 138
  • 190