Use of execl (Arguments)
up vote
-3
down vote
favorite
New to C.
So I have a program called test.c (doesnt need any arguments to start, compiled like this "
gcc test.c -o test")
I want to make my other program execute test
I know I have to use execl but I just cant understand the arguments in the execl function
execl( code here )
c execl
add a comment |
up vote
-3
down vote
favorite
New to C.
So I have a program called test.c (doesnt need any arguments to start, compiled like this "
gcc test.c -o test")
I want to make my other program execute test
I know I have to use execl but I just cant understand the arguments in the execl function
execl( code here )
c execl
2
The args are explained in the Docs. If this doesn't help please be specific which part you don't understand.
– tkausl
Nov 11 at 1:55
its the syntaxe in writing the arguments
– Ventura
Nov 11 at 1:58
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
New to C.
So I have a program called test.c (doesnt need any arguments to start, compiled like this "
gcc test.c -o test")
I want to make my other program execute test
I know I have to use execl but I just cant understand the arguments in the execl function
execl( code here )
c execl
New to C.
So I have a program called test.c (doesnt need any arguments to start, compiled like this "
gcc test.c -o test")
I want to make my other program execute test
I know I have to use execl but I just cant understand the arguments in the execl function
execl( code here )
c execl
c execl
edited Nov 11 at 2:52
asked Nov 11 at 1:53
Ventura
13
13
2
The args are explained in the Docs. If this doesn't help please be specific which part you don't understand.
– tkausl
Nov 11 at 1:55
its the syntaxe in writing the arguments
– Ventura
Nov 11 at 1:58
add a comment |
2
The args are explained in the Docs. If this doesn't help please be specific which part you don't understand.
– tkausl
Nov 11 at 1:55
its the syntaxe in writing the arguments
– Ventura
Nov 11 at 1:58
2
2
The args are explained in the Docs. If this doesn't help please be specific which part you don't understand.
– tkausl
Nov 11 at 1:55
The args are explained in the Docs. If this doesn't help please be specific which part you don't understand.
– tkausl
Nov 11 at 1:55
its the syntaxe in writing the arguments
– Ventura
Nov 11 at 1:58
its the syntaxe in writing the arguments
– Ventura
Nov 11 at 1:58
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
All the arguments to execle()
except the last two are strings — the penultimate one is a null char *
marking the end of the command line arguments, and the last is a char **
specifying the environment. The first is the pathname of the executable, relative to the current directory if the name does not start with a /
slash. The second argument is the name of the program. Subsequent arguments are the extra arguments for the program (the list is terminated by a (char *)0
argument) and then there's a final argument that is the environment for the program (the trailing e
indicates that the environment is passed). Hence, for example:
extern char **environ; // No header declares this!
execle("teste", "pink elephants", (char *)0, environ);
You could use "teste"
or "/bin/bash"
in place of "pink elephants"
, depending on your tastes. Only two of the three program name options suggested are outright fibs. If you replace the environ
argument with (char **)0
or equivalent, then the program is invoked with no environment variables, which is usually regarded as an abuse of the program that's run (rather like telling it that it's name is "pink elephants
" rather than "teste
" is an abuse of it).
You could use variables too:
const char *progname = "teste";
execle(progname, progname, (char *)0, environ);
Ended up using
execlp
and just writingexeclp("./teste",NULL,NULL)
; only solution that worked.
It's impressive how many (small) problems there can be in a single simple line of code. Using execlp("./teste", NULL, NULL);
is dubious on at least these counts:
- You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc.
- The first NULL is sufficient if you're not going to provide a name; the second is never processed.
- Using
"./teste"
means that thep
(path search) part ofexeclp()
is never exercised; you might as well have usedexecle("./teste", (char *)NULL, environ);
. - It's not impossible for
NULL
not to translate to(char *)0
in a variable argument list like withexecle()
. It's not a very likely problem, but#define NULL 0
is legitimate, and ifsizeof(int) == 4
butsizeof(char *) == 8
, then you could have difficulties.
Aside: you'll probably find the execv*()
functions more useful in general than the execl*()
functions. At least, my experience is that the variable length argument list is more often needed by the programs I run than a fixed length list.
Ended up using execlp and just writting execlp './teste",NULL,NULL' only solution that worked
– Ventura
Nov 11 at 2:17
Usingexeclp("./teste", NULL, NULL);
is dubious on at least three counts: (1) You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc, and (2) the first NULL is sufficient if you're not going to provide a name; the second is never processed, and (3) using"./teste"
means that thep
(path search) part ofexeclp()
is never exercised; you might as well have useexecle("./teste", (char *)NULL);
— and (4) it's not impossible for NULL not to translate to(char *)0
in a variable argument list like withexecle()
.
– Jonathan Leffler
Nov 11 at 2:22
execle("./teste", (char *) NULL);
actually didnt work
– Ventura
Nov 11 at 2:30
1
@Ventura: Yes, it wouldn't; I forgot that thee
inexecle()
means 'environment is passed' andexecle("./teste", (char *)NULL);
doesn't provide the environment. My bad — I'm sorry. I've fixed the answer. However, usingexeclp()
is pointless when there's a slash in the string in the first argument — the program name. The PATH is never searched then. POSIX gives the synopsis:int execle(const char *path, const char *arg0, ... /*, (char *)0, char *const envp*/);
for the function. Note the use of(char *)0
explicitly.
– Jonathan Leffler
Nov 11 at 2:33
So what would you say its the best approach to solve this problem knowing that this teste.c is in only one directory The path should be something like/directory/test.c
– Ventura
Nov 11 at 2:42
|
show 6 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
All the arguments to execle()
except the last two are strings — the penultimate one is a null char *
marking the end of the command line arguments, and the last is a char **
specifying the environment. The first is the pathname of the executable, relative to the current directory if the name does not start with a /
slash. The second argument is the name of the program. Subsequent arguments are the extra arguments for the program (the list is terminated by a (char *)0
argument) and then there's a final argument that is the environment for the program (the trailing e
indicates that the environment is passed). Hence, for example:
extern char **environ; // No header declares this!
execle("teste", "pink elephants", (char *)0, environ);
You could use "teste"
or "/bin/bash"
in place of "pink elephants"
, depending on your tastes. Only two of the three program name options suggested are outright fibs. If you replace the environ
argument with (char **)0
or equivalent, then the program is invoked with no environment variables, which is usually regarded as an abuse of the program that's run (rather like telling it that it's name is "pink elephants
" rather than "teste
" is an abuse of it).
You could use variables too:
const char *progname = "teste";
execle(progname, progname, (char *)0, environ);
Ended up using
execlp
and just writingexeclp("./teste",NULL,NULL)
; only solution that worked.
It's impressive how many (small) problems there can be in a single simple line of code. Using execlp("./teste", NULL, NULL);
is dubious on at least these counts:
- You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc.
- The first NULL is sufficient if you're not going to provide a name; the second is never processed.
- Using
"./teste"
means that thep
(path search) part ofexeclp()
is never exercised; you might as well have usedexecle("./teste", (char *)NULL, environ);
. - It's not impossible for
NULL
not to translate to(char *)0
in a variable argument list like withexecle()
. It's not a very likely problem, but#define NULL 0
is legitimate, and ifsizeof(int) == 4
butsizeof(char *) == 8
, then you could have difficulties.
Aside: you'll probably find the execv*()
functions more useful in general than the execl*()
functions. At least, my experience is that the variable length argument list is more often needed by the programs I run than a fixed length list.
Ended up using execlp and just writting execlp './teste",NULL,NULL' only solution that worked
– Ventura
Nov 11 at 2:17
Usingexeclp("./teste", NULL, NULL);
is dubious on at least three counts: (1) You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc, and (2) the first NULL is sufficient if you're not going to provide a name; the second is never processed, and (3) using"./teste"
means that thep
(path search) part ofexeclp()
is never exercised; you might as well have useexecle("./teste", (char *)NULL);
— and (4) it's not impossible for NULL not to translate to(char *)0
in a variable argument list like withexecle()
.
– Jonathan Leffler
Nov 11 at 2:22
execle("./teste", (char *) NULL);
actually didnt work
– Ventura
Nov 11 at 2:30
1
@Ventura: Yes, it wouldn't; I forgot that thee
inexecle()
means 'environment is passed' andexecle("./teste", (char *)NULL);
doesn't provide the environment. My bad — I'm sorry. I've fixed the answer. However, usingexeclp()
is pointless when there's a slash in the string in the first argument — the program name. The PATH is never searched then. POSIX gives the synopsis:int execle(const char *path, const char *arg0, ... /*, (char *)0, char *const envp*/);
for the function. Note the use of(char *)0
explicitly.
– Jonathan Leffler
Nov 11 at 2:33
So what would you say its the best approach to solve this problem knowing that this teste.c is in only one directory The path should be something like/directory/test.c
– Ventura
Nov 11 at 2:42
|
show 6 more comments
up vote
1
down vote
accepted
All the arguments to execle()
except the last two are strings — the penultimate one is a null char *
marking the end of the command line arguments, and the last is a char **
specifying the environment. The first is the pathname of the executable, relative to the current directory if the name does not start with a /
slash. The second argument is the name of the program. Subsequent arguments are the extra arguments for the program (the list is terminated by a (char *)0
argument) and then there's a final argument that is the environment for the program (the trailing e
indicates that the environment is passed). Hence, for example:
extern char **environ; // No header declares this!
execle("teste", "pink elephants", (char *)0, environ);
You could use "teste"
or "/bin/bash"
in place of "pink elephants"
, depending on your tastes. Only two of the three program name options suggested are outright fibs. If you replace the environ
argument with (char **)0
or equivalent, then the program is invoked with no environment variables, which is usually regarded as an abuse of the program that's run (rather like telling it that it's name is "pink elephants
" rather than "teste
" is an abuse of it).
You could use variables too:
const char *progname = "teste";
execle(progname, progname, (char *)0, environ);
Ended up using
execlp
and just writingexeclp("./teste",NULL,NULL)
; only solution that worked.
It's impressive how many (small) problems there can be in a single simple line of code. Using execlp("./teste", NULL, NULL);
is dubious on at least these counts:
- You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc.
- The first NULL is sufficient if you're not going to provide a name; the second is never processed.
- Using
"./teste"
means that thep
(path search) part ofexeclp()
is never exercised; you might as well have usedexecle("./teste", (char *)NULL, environ);
. - It's not impossible for
NULL
not to translate to(char *)0
in a variable argument list like withexecle()
. It's not a very likely problem, but#define NULL 0
is legitimate, and ifsizeof(int) == 4
butsizeof(char *) == 8
, then you could have difficulties.
Aside: you'll probably find the execv*()
functions more useful in general than the execl*()
functions. At least, my experience is that the variable length argument list is more often needed by the programs I run than a fixed length list.
Ended up using execlp and just writting execlp './teste",NULL,NULL' only solution that worked
– Ventura
Nov 11 at 2:17
Usingexeclp("./teste", NULL, NULL);
is dubious on at least three counts: (1) You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc, and (2) the first NULL is sufficient if you're not going to provide a name; the second is never processed, and (3) using"./teste"
means that thep
(path search) part ofexeclp()
is never exercised; you might as well have useexecle("./teste", (char *)NULL);
— and (4) it's not impossible for NULL not to translate to(char *)0
in a variable argument list like withexecle()
.
– Jonathan Leffler
Nov 11 at 2:22
execle("./teste", (char *) NULL);
actually didnt work
– Ventura
Nov 11 at 2:30
1
@Ventura: Yes, it wouldn't; I forgot that thee
inexecle()
means 'environment is passed' andexecle("./teste", (char *)NULL);
doesn't provide the environment. My bad — I'm sorry. I've fixed the answer. However, usingexeclp()
is pointless when there's a slash in the string in the first argument — the program name. The PATH is never searched then. POSIX gives the synopsis:int execle(const char *path, const char *arg0, ... /*, (char *)0, char *const envp*/);
for the function. Note the use of(char *)0
explicitly.
– Jonathan Leffler
Nov 11 at 2:33
So what would you say its the best approach to solve this problem knowing that this teste.c is in only one directory The path should be something like/directory/test.c
– Ventura
Nov 11 at 2:42
|
show 6 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
All the arguments to execle()
except the last two are strings — the penultimate one is a null char *
marking the end of the command line arguments, and the last is a char **
specifying the environment. The first is the pathname of the executable, relative to the current directory if the name does not start with a /
slash. The second argument is the name of the program. Subsequent arguments are the extra arguments for the program (the list is terminated by a (char *)0
argument) and then there's a final argument that is the environment for the program (the trailing e
indicates that the environment is passed). Hence, for example:
extern char **environ; // No header declares this!
execle("teste", "pink elephants", (char *)0, environ);
You could use "teste"
or "/bin/bash"
in place of "pink elephants"
, depending on your tastes. Only two of the three program name options suggested are outright fibs. If you replace the environ
argument with (char **)0
or equivalent, then the program is invoked with no environment variables, which is usually regarded as an abuse of the program that's run (rather like telling it that it's name is "pink elephants
" rather than "teste
" is an abuse of it).
You could use variables too:
const char *progname = "teste";
execle(progname, progname, (char *)0, environ);
Ended up using
execlp
and just writingexeclp("./teste",NULL,NULL)
; only solution that worked.
It's impressive how many (small) problems there can be in a single simple line of code. Using execlp("./teste", NULL, NULL);
is dubious on at least these counts:
- You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc.
- The first NULL is sufficient if you're not going to provide a name; the second is never processed.
- Using
"./teste"
means that thep
(path search) part ofexeclp()
is never exercised; you might as well have usedexecle("./teste", (char *)NULL, environ);
. - It's not impossible for
NULL
not to translate to(char *)0
in a variable argument list like withexecle()
. It's not a very likely problem, but#define NULL 0
is legitimate, and ifsizeof(int) == 4
butsizeof(char *) == 8
, then you could have difficulties.
Aside: you'll probably find the execv*()
functions more useful in general than the execl*()
functions. At least, my experience is that the variable length argument list is more often needed by the programs I run than a fixed length list.
All the arguments to execle()
except the last two are strings — the penultimate one is a null char *
marking the end of the command line arguments, and the last is a char **
specifying the environment. The first is the pathname of the executable, relative to the current directory if the name does not start with a /
slash. The second argument is the name of the program. Subsequent arguments are the extra arguments for the program (the list is terminated by a (char *)0
argument) and then there's a final argument that is the environment for the program (the trailing e
indicates that the environment is passed). Hence, for example:
extern char **environ; // No header declares this!
execle("teste", "pink elephants", (char *)0, environ);
You could use "teste"
or "/bin/bash"
in place of "pink elephants"
, depending on your tastes. Only two of the three program name options suggested are outright fibs. If you replace the environ
argument with (char **)0
or equivalent, then the program is invoked with no environment variables, which is usually regarded as an abuse of the program that's run (rather like telling it that it's name is "pink elephants
" rather than "teste
" is an abuse of it).
You could use variables too:
const char *progname = "teste";
execle(progname, progname, (char *)0, environ);
Ended up using
execlp
and just writingexeclp("./teste",NULL,NULL)
; only solution that worked.
It's impressive how many (small) problems there can be in a single simple line of code. Using execlp("./teste", NULL, NULL);
is dubious on at least these counts:
- You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc.
- The first NULL is sufficient if you're not going to provide a name; the second is never processed.
- Using
"./teste"
means that thep
(path search) part ofexeclp()
is never exercised; you might as well have usedexecle("./teste", (char *)NULL, environ);
. - It's not impossible for
NULL
not to translate to(char *)0
in a variable argument list like withexecle()
. It's not a very likely problem, but#define NULL 0
is legitimate, and ifsizeof(int) == 4
butsizeof(char *) == 8
, then you could have difficulties.
Aside: you'll probably find the execv*()
functions more useful in general than the execl*()
functions. At least, my experience is that the variable length argument list is more often needed by the programs I run than a fixed length list.
edited Nov 11 at 3:23
answered Nov 11 at 2:12
Jonathan Leffler
555k886611013
555k886611013
Ended up using execlp and just writting execlp './teste",NULL,NULL' only solution that worked
– Ventura
Nov 11 at 2:17
Usingexeclp("./teste", NULL, NULL);
is dubious on at least three counts: (1) You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc, and (2) the first NULL is sufficient if you're not going to provide a name; the second is never processed, and (3) using"./teste"
means that thep
(path search) part ofexeclp()
is never exercised; you might as well have useexecle("./teste", (char *)NULL);
— and (4) it's not impossible for NULL not to translate to(char *)0
in a variable argument list like withexecle()
.
– Jonathan Leffler
Nov 11 at 2:22
execle("./teste", (char *) NULL);
actually didnt work
– Ventura
Nov 11 at 2:30
1
@Ventura: Yes, it wouldn't; I forgot that thee
inexecle()
means 'environment is passed' andexecle("./teste", (char *)NULL);
doesn't provide the environment. My bad — I'm sorry. I've fixed the answer. However, usingexeclp()
is pointless when there's a slash in the string in the first argument — the program name. The PATH is never searched then. POSIX gives the synopsis:int execle(const char *path, const char *arg0, ... /*, (char *)0, char *const envp*/);
for the function. Note the use of(char *)0
explicitly.
– Jonathan Leffler
Nov 11 at 2:33
So what would you say its the best approach to solve this problem knowing that this teste.c is in only one directory The path should be something like/directory/test.c
– Ventura
Nov 11 at 2:42
|
show 6 more comments
Ended up using execlp and just writting execlp './teste",NULL,NULL' only solution that worked
– Ventura
Nov 11 at 2:17
Usingexeclp("./teste", NULL, NULL);
is dubious on at least three counts: (1) You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc, and (2) the first NULL is sufficient if you're not going to provide a name; the second is never processed, and (3) using"./teste"
means that thep
(path search) part ofexeclp()
is never exercised; you might as well have useexecle("./teste", (char *)NULL);
— and (4) it's not impossible for NULL not to translate to(char *)0
in a variable argument list like withexecle()
.
– Jonathan Leffler
Nov 11 at 2:22
execle("./teste", (char *) NULL);
actually didnt work
– Ventura
Nov 11 at 2:30
1
@Ventura: Yes, it wouldn't; I forgot that thee
inexecle()
means 'environment is passed' andexecle("./teste", (char *)NULL);
doesn't provide the environment. My bad — I'm sorry. I've fixed the answer. However, usingexeclp()
is pointless when there's a slash in the string in the first argument — the program name. The PATH is never searched then. POSIX gives the synopsis:int execle(const char *path, const char *arg0, ... /*, (char *)0, char *const envp*/);
for the function. Note the use of(char *)0
explicitly.
– Jonathan Leffler
Nov 11 at 2:33
So what would you say its the best approach to solve this problem knowing that this teste.c is in only one directory The path should be something like/directory/test.c
– Ventura
Nov 11 at 2:42
Ended up using execlp and just writting execlp './teste",NULL,NULL' only solution that worked
– Ventura
Nov 11 at 2:17
Ended up using execlp and just writting execlp './teste",NULL,NULL' only solution that worked
– Ventura
Nov 11 at 2:17
Using
execlp("./teste", NULL, NULL);
is dubious on at least three counts: (1) You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc, and (2) the first NULL is sufficient if you're not going to provide a name; the second is never processed, and (3) using "./teste"
means that the p
(path search) part of execlp()
is never exercised; you might as well have use execle("./teste", (char *)NULL);
— and (4) it's not impossible for NULL not to translate to (char *)0
in a variable argument list like with execle()
.– Jonathan Leffler
Nov 11 at 2:22
Using
execlp("./teste", NULL, NULL);
is dubious on at least three counts: (1) You've not provided the executed program with its name, which is discourteous at best and leads to unhelpful error reports, etc, and (2) the first NULL is sufficient if you're not going to provide a name; the second is never processed, and (3) using "./teste"
means that the p
(path search) part of execlp()
is never exercised; you might as well have use execle("./teste", (char *)NULL);
— and (4) it's not impossible for NULL not to translate to (char *)0
in a variable argument list like with execle()
.– Jonathan Leffler
Nov 11 at 2:22
execle("./teste", (char *) NULL);
actually didnt work– Ventura
Nov 11 at 2:30
execle("./teste", (char *) NULL);
actually didnt work– Ventura
Nov 11 at 2:30
1
1
@Ventura: Yes, it wouldn't; I forgot that the
e
in execle()
means 'environment is passed' and execle("./teste", (char *)NULL);
doesn't provide the environment. My bad — I'm sorry. I've fixed the answer. However, using execlp()
is pointless when there's a slash in the string in the first argument — the program name. The PATH is never searched then. POSIX gives the synopsis: int execle(const char *path, const char *arg0, ... /*, (char *)0, char *const envp*/);
for the function. Note the use of (char *)0
explicitly.– Jonathan Leffler
Nov 11 at 2:33
@Ventura: Yes, it wouldn't; I forgot that the
e
in execle()
means 'environment is passed' and execle("./teste", (char *)NULL);
doesn't provide the environment. My bad — I'm sorry. I've fixed the answer. However, using execlp()
is pointless when there's a slash in the string in the first argument — the program name. The PATH is never searched then. POSIX gives the synopsis: int execle(const char *path, const char *arg0, ... /*, (char *)0, char *const envp*/);
for the function. Note the use of (char *)0
explicitly.– Jonathan Leffler
Nov 11 at 2:33
So what would you say its the best approach to solve this problem knowing that this teste.c is in only one directory The path should be something like
/directory/test.c
– Ventura
Nov 11 at 2:42
So what would you say its the best approach to solve this problem knowing that this teste.c is in only one directory The path should be something like
/directory/test.c
– Ventura
Nov 11 at 2:42
|
show 6 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245178%2fuse-of-execl-arguments%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
The args are explained in the Docs. If this doesn't help please be specific which part you don't understand.
– tkausl
Nov 11 at 1:55
its the syntaxe in writing the arguments
– Ventura
Nov 11 at 1:58