Hi All,


I have read that the system call "execlp" will take the first arguement as the file name(or path), second arguement as the command and all others are arguements to the command terminated by NULL.

i have written below program.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PATH "/home/FILE/test"

int main(void) {
         int  fd,st;
	fd = fork();
	if(fd == 0) {
	printf("In child\n");
	if (execlp(PATH,"test","hello","how","are","you",(char *)0)){
		printf("Exec succ\n");
	}else {
		printf("Exec fail\n");
	} 
	} else {
	printf("In parent\n");
	wait(&st);
	}

}

the program is working correctly if i use the PATH macro as it gives the full path of the file image.

but if i replace PATH with "test"(image name) i am getting the out put as

In parent
In child
test: extra argument `how'

i dont understand the problem.

please some body point out where it is going wrong.

for your reference test.c

int main(int argc , char **argv)
{
	int i= 0;
	for(;i<argc-1;){
		printf(" %s \n", argv[++i]);
	}
	
	printf("this is a test\n");
	return 0;
}

Thanks in Advance,
Gaiety.

The test: extra argument `how' should tip you off that it's not your program running. Try renaming your program to something like foo and try again. Your shell already has a binary named test that will be used by default. By not providing an explicit path, you have invoked the built-in version of test ;) (this has to do with how the first argument is interpreted if there are no slashes in it - see the man pages for details)

Edited 4 Years Ago by L7Sqr: n/a

Your shell already has a binary named test that will be used by default

sorry i dont have much knowledge in internals.
does shell have a default image called test? or
is it the image that i created and shell treats as default, when we add the PATH env variable our own path.

Your system has a program named test likely in /usr/bin/ . That path also resides in your $PATH environment variable.
The way the shell executes programs is that if no explicit path is provided with the call (e.g. program instead of /home/user/local/program ) then the $PATH variable is checked for places to look for program .
Now, when you call execlp the following applies to the first argument

int execlp(const char *file, const char *arg0, ... /*, (char *)0 */);

       The  argument [i][b][u]file[/u][/b][/i] is used to construct a pathname that identifies the
       new process image file. If the file argument contains a slash  charac-
       ter,  the  file  argument shall be used as the pathname for this file.
       Otherwise, the path prefix for this file is obtained by  a  search  of
       the  directories passed as the environment variable PATH (see the Base
       Definitions volume of  IEEE Std 1003.1-2001,  Chapter  8,  Environment
       Variables).   If this environment variable is not present, the results
       of the search are implementation-defined.

Since in your $PATH there is a program named test that program is invoked instead of your local program.

This question has already been answered. Start a new discussion instead.