execlp doenst work when im giving the arguments












1














why the first execlp doesn't work and the second one its ok.



int main(){
int fd[2];
int i;
char** arguments = (char**) malloc(5*sizeof(char*));
char *line = (char*) malloc(15*sizeof(char));
char *cfile = (char*) malloc(15*sizeof(char));
char* token;
size_t bufsize = 0;

for(i = 0;i < 5; i++){
arguments[i] = (char*) malloc(15*sizeof(char));
}

getline(&line, &bufsize, stdin);

i = 0;
token = strtok(line,TOK_DELIM);
while( token != NULL ){
(arguments)[i] = token;
strcat((arguments)[i++],"");
token = strtok(NULL,TOK_DELIM_1);
}

if( !fork() ){
execlp((arguments)[1],"show",(arguments)[0],NULL);
close(fd[0]); close(fd[1]);
}
close(fd[0]); close(fd[1]);
waitpid(0,NULL,0);
if( !fork() ){
printf("Executable c file:");
scanf("%s",cfile);
execlp(cfile,"show",(arguments)[0],NULL);
close(fd[0]); close(fd[1]);
}
close(fd[0]); close(fd[1]);
waitpid(0,NULL,0);
return 0;
}




As you can see now cfile and arguments are both vuriables(dynamic char array).
To run this main you have to gcc the show file writing gcc -o show show.c
Then when you run the main you have to type (pwd | ./show) pwd is the command that i want to execute and ./show the executable file that i want to use to print the output of pwd.I save what the user types on line and then i parse the arguments on a 2d dynamic array.On the first cell of arguments(dynamic 2d array) i save the command that i want to execute and at the second one the executable file that i want to run.



This is the show file that i want to use:



int main(int argc, char *argv){
char *arg[4];
printf("Execute commands from other file ...n");
arg[0] = "sh";
arg[1] = "-c";
arg[2] = argv[1];
arg[3] = NULL;
execvp ("/bin/sh", arg);
return 0;


}



And you must compile it with the same name as the executable file that you are gonna give on the main programm,for example if you gonna type :pwd | ./show
on main you must compile the show file like this : gcc -o show show.c



Also im using those libs :




  • stdlib

  • stdio

  • unistd

  • string

  • sys/types

  • errno

  • sys/stat

  • fcntl

  • sys/wait










share|improve this question




















  • 1




    execlp() does not require any of its arguments to be string literals. It is not clear why your particular use is not working as you expect, and in order for us to help, we need you to provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
    – John Bollinger
    Nov 22 at 17:51










  • all i want to make is to give the executable file to execlp from user and not from coder,so user can be albe to execute other c files
    – Σωτηρης Ιωαννιδης
    Nov 22 at 18:29










  • What's in (*arguments)[1] ? You need to prove to yourself that what you think is happening is actually happening - so e.g. do printf("(*arguments)[1] = '%s'n", (*arguments)[1]); before your execlp() call, so you can see what's in that variable. Also, if execl() fails, you can, on the very next line after execlp(), call perror("execlp failed"); and perror will print out why it failed.
    – nos
    Nov 22 at 19:03












  • Please turn your puzzle of code fragments into a Minimal, Complete, and Verifiable example.
    – Yunnosch
    Nov 22 at 19:18










  • @ΣωτηρηςΙωαννιδης, the problem is not that we fail to understand what you are trying to achieve, it is that we don't understand how you are trying to achieve it. As I said already, execlp() does not require any of its arguments to be string literals. To expand on that, it cannot tell whether the pointers in its argument list point to user-provided data. It does not attempt to make any such distinction.
    – John Bollinger
    Nov 22 at 20:02


















1














why the first execlp doesn't work and the second one its ok.



int main(){
int fd[2];
int i;
char** arguments = (char**) malloc(5*sizeof(char*));
char *line = (char*) malloc(15*sizeof(char));
char *cfile = (char*) malloc(15*sizeof(char));
char* token;
size_t bufsize = 0;

for(i = 0;i < 5; i++){
arguments[i] = (char*) malloc(15*sizeof(char));
}

getline(&line, &bufsize, stdin);

i = 0;
token = strtok(line,TOK_DELIM);
while( token != NULL ){
(arguments)[i] = token;
strcat((arguments)[i++],"");
token = strtok(NULL,TOK_DELIM_1);
}

if( !fork() ){
execlp((arguments)[1],"show",(arguments)[0],NULL);
close(fd[0]); close(fd[1]);
}
close(fd[0]); close(fd[1]);
waitpid(0,NULL,0);
if( !fork() ){
printf("Executable c file:");
scanf("%s",cfile);
execlp(cfile,"show",(arguments)[0],NULL);
close(fd[0]); close(fd[1]);
}
close(fd[0]); close(fd[1]);
waitpid(0,NULL,0);
return 0;
}




As you can see now cfile and arguments are both vuriables(dynamic char array).
To run this main you have to gcc the show file writing gcc -o show show.c
Then when you run the main you have to type (pwd | ./show) pwd is the command that i want to execute and ./show the executable file that i want to use to print the output of pwd.I save what the user types on line and then i parse the arguments on a 2d dynamic array.On the first cell of arguments(dynamic 2d array) i save the command that i want to execute and at the second one the executable file that i want to run.



This is the show file that i want to use:



int main(int argc, char *argv){
char *arg[4];
printf("Execute commands from other file ...n");
arg[0] = "sh";
arg[1] = "-c";
arg[2] = argv[1];
arg[3] = NULL;
execvp ("/bin/sh", arg);
return 0;


}



And you must compile it with the same name as the executable file that you are gonna give on the main programm,for example if you gonna type :pwd | ./show
on main you must compile the show file like this : gcc -o show show.c



Also im using those libs :




  • stdlib

  • stdio

  • unistd

  • string

  • sys/types

  • errno

  • sys/stat

  • fcntl

  • sys/wait










share|improve this question




















  • 1




    execlp() does not require any of its arguments to be string literals. It is not clear why your particular use is not working as you expect, and in order for us to help, we need you to provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
    – John Bollinger
    Nov 22 at 17:51










  • all i want to make is to give the executable file to execlp from user and not from coder,so user can be albe to execute other c files
    – Σωτηρης Ιωαννιδης
    Nov 22 at 18:29










  • What's in (*arguments)[1] ? You need to prove to yourself that what you think is happening is actually happening - so e.g. do printf("(*arguments)[1] = '%s'n", (*arguments)[1]); before your execlp() call, so you can see what's in that variable. Also, if execl() fails, you can, on the very next line after execlp(), call perror("execlp failed"); and perror will print out why it failed.
    – nos
    Nov 22 at 19:03












  • Please turn your puzzle of code fragments into a Minimal, Complete, and Verifiable example.
    – Yunnosch
    Nov 22 at 19:18










  • @ΣωτηρηςΙωαννιδης, the problem is not that we fail to understand what you are trying to achieve, it is that we don't understand how you are trying to achieve it. As I said already, execlp() does not require any of its arguments to be string literals. To expand on that, it cannot tell whether the pointers in its argument list point to user-provided data. It does not attempt to make any such distinction.
    – John Bollinger
    Nov 22 at 20:02
















1












1








1


0





why the first execlp doesn't work and the second one its ok.



int main(){
int fd[2];
int i;
char** arguments = (char**) malloc(5*sizeof(char*));
char *line = (char*) malloc(15*sizeof(char));
char *cfile = (char*) malloc(15*sizeof(char));
char* token;
size_t bufsize = 0;

for(i = 0;i < 5; i++){
arguments[i] = (char*) malloc(15*sizeof(char));
}

getline(&line, &bufsize, stdin);

i = 0;
token = strtok(line,TOK_DELIM);
while( token != NULL ){
(arguments)[i] = token;
strcat((arguments)[i++],"");
token = strtok(NULL,TOK_DELIM_1);
}

if( !fork() ){
execlp((arguments)[1],"show",(arguments)[0],NULL);
close(fd[0]); close(fd[1]);
}
close(fd[0]); close(fd[1]);
waitpid(0,NULL,0);
if( !fork() ){
printf("Executable c file:");
scanf("%s",cfile);
execlp(cfile,"show",(arguments)[0],NULL);
close(fd[0]); close(fd[1]);
}
close(fd[0]); close(fd[1]);
waitpid(0,NULL,0);
return 0;
}




As you can see now cfile and arguments are both vuriables(dynamic char array).
To run this main you have to gcc the show file writing gcc -o show show.c
Then when you run the main you have to type (pwd | ./show) pwd is the command that i want to execute and ./show the executable file that i want to use to print the output of pwd.I save what the user types on line and then i parse the arguments on a 2d dynamic array.On the first cell of arguments(dynamic 2d array) i save the command that i want to execute and at the second one the executable file that i want to run.



This is the show file that i want to use:



int main(int argc, char *argv){
char *arg[4];
printf("Execute commands from other file ...n");
arg[0] = "sh";
arg[1] = "-c";
arg[2] = argv[1];
arg[3] = NULL;
execvp ("/bin/sh", arg);
return 0;


}



And you must compile it with the same name as the executable file that you are gonna give on the main programm,for example if you gonna type :pwd | ./show
on main you must compile the show file like this : gcc -o show show.c



Also im using those libs :




  • stdlib

  • stdio

  • unistd

  • string

  • sys/types

  • errno

  • sys/stat

  • fcntl

  • sys/wait










share|improve this question















why the first execlp doesn't work and the second one its ok.



int main(){
int fd[2];
int i;
char** arguments = (char**) malloc(5*sizeof(char*));
char *line = (char*) malloc(15*sizeof(char));
char *cfile = (char*) malloc(15*sizeof(char));
char* token;
size_t bufsize = 0;

for(i = 0;i < 5; i++){
arguments[i] = (char*) malloc(15*sizeof(char));
}

getline(&line, &bufsize, stdin);

i = 0;
token = strtok(line,TOK_DELIM);
while( token != NULL ){
(arguments)[i] = token;
strcat((arguments)[i++],"");
token = strtok(NULL,TOK_DELIM_1);
}

if( !fork() ){
execlp((arguments)[1],"show",(arguments)[0],NULL);
close(fd[0]); close(fd[1]);
}
close(fd[0]); close(fd[1]);
waitpid(0,NULL,0);
if( !fork() ){
printf("Executable c file:");
scanf("%s",cfile);
execlp(cfile,"show",(arguments)[0],NULL);
close(fd[0]); close(fd[1]);
}
close(fd[0]); close(fd[1]);
waitpid(0,NULL,0);
return 0;
}




As you can see now cfile and arguments are both vuriables(dynamic char array).
To run this main you have to gcc the show file writing gcc -o show show.c
Then when you run the main you have to type (pwd | ./show) pwd is the command that i want to execute and ./show the executable file that i want to use to print the output of pwd.I save what the user types on line and then i parse the arguments on a 2d dynamic array.On the first cell of arguments(dynamic 2d array) i save the command that i want to execute and at the second one the executable file that i want to run.



This is the show file that i want to use:



int main(int argc, char *argv){
char *arg[4];
printf("Execute commands from other file ...n");
arg[0] = "sh";
arg[1] = "-c";
arg[2] = argv[1];
arg[3] = NULL;
execvp ("/bin/sh", arg);
return 0;


}



And you must compile it with the same name as the executable file that you are gonna give on the main programm,for example if you gonna type :pwd | ./show
on main you must compile the show file like this : gcc -o show show.c



Also im using those libs :




  • stdlib

  • stdio

  • unistd

  • string

  • sys/types

  • errno

  • sys/stat

  • fcntl

  • sys/wait







c exec






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 21:14

























asked Nov 22 at 17:37









Σωτηρης Ιωαννιδης

96




96








  • 1




    execlp() does not require any of its arguments to be string literals. It is not clear why your particular use is not working as you expect, and in order for us to help, we need you to provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
    – John Bollinger
    Nov 22 at 17:51










  • all i want to make is to give the executable file to execlp from user and not from coder,so user can be albe to execute other c files
    – Σωτηρης Ιωαννιδης
    Nov 22 at 18:29










  • What's in (*arguments)[1] ? You need to prove to yourself that what you think is happening is actually happening - so e.g. do printf("(*arguments)[1] = '%s'n", (*arguments)[1]); before your execlp() call, so you can see what's in that variable. Also, if execl() fails, you can, on the very next line after execlp(), call perror("execlp failed"); and perror will print out why it failed.
    – nos
    Nov 22 at 19:03












  • Please turn your puzzle of code fragments into a Minimal, Complete, and Verifiable example.
    – Yunnosch
    Nov 22 at 19:18










  • @ΣωτηρηςΙωαννιδης, the problem is not that we fail to understand what you are trying to achieve, it is that we don't understand how you are trying to achieve it. As I said already, execlp() does not require any of its arguments to be string literals. To expand on that, it cannot tell whether the pointers in its argument list point to user-provided data. It does not attempt to make any such distinction.
    – John Bollinger
    Nov 22 at 20:02
















  • 1




    execlp() does not require any of its arguments to be string literals. It is not clear why your particular use is not working as you expect, and in order for us to help, we need you to provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
    – John Bollinger
    Nov 22 at 17:51










  • all i want to make is to give the executable file to execlp from user and not from coder,so user can be albe to execute other c files
    – Σωτηρης Ιωαννιδης
    Nov 22 at 18:29










  • What's in (*arguments)[1] ? You need to prove to yourself that what you think is happening is actually happening - so e.g. do printf("(*arguments)[1] = '%s'n", (*arguments)[1]); before your execlp() call, so you can see what's in that variable. Also, if execl() fails, you can, on the very next line after execlp(), call perror("execlp failed"); and perror will print out why it failed.
    – nos
    Nov 22 at 19:03












  • Please turn your puzzle of code fragments into a Minimal, Complete, and Verifiable example.
    – Yunnosch
    Nov 22 at 19:18










  • @ΣωτηρηςΙωαννιδης, the problem is not that we fail to understand what you are trying to achieve, it is that we don't understand how you are trying to achieve it. As I said already, execlp() does not require any of its arguments to be string literals. To expand on that, it cannot tell whether the pointers in its argument list point to user-provided data. It does not attempt to make any such distinction.
    – John Bollinger
    Nov 22 at 20:02










1




1




execlp() does not require any of its arguments to be string literals. It is not clear why your particular use is not working as you expect, and in order for us to help, we need you to provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
– John Bollinger
Nov 22 at 17:51




execlp() does not require any of its arguments to be string literals. It is not clear why your particular use is not working as you expect, and in order for us to help, we need you to provide a Minimal, Complete, and Verifiable example that demonstrates the problem.
– John Bollinger
Nov 22 at 17:51












all i want to make is to give the executable file to execlp from user and not from coder,so user can be albe to execute other c files
– Σωτηρης Ιωαννιδης
Nov 22 at 18:29




all i want to make is to give the executable file to execlp from user and not from coder,so user can be albe to execute other c files
– Σωτηρης Ιωαννιδης
Nov 22 at 18:29












What's in (*arguments)[1] ? You need to prove to yourself that what you think is happening is actually happening - so e.g. do printf("(*arguments)[1] = '%s'n", (*arguments)[1]); before your execlp() call, so you can see what's in that variable. Also, if execl() fails, you can, on the very next line after execlp(), call perror("execlp failed"); and perror will print out why it failed.
– nos
Nov 22 at 19:03






What's in (*arguments)[1] ? You need to prove to yourself that what you think is happening is actually happening - so e.g. do printf("(*arguments)[1] = '%s'n", (*arguments)[1]); before your execlp() call, so you can see what's in that variable. Also, if execl() fails, you can, on the very next line after execlp(), call perror("execlp failed"); and perror will print out why it failed.
– nos
Nov 22 at 19:03














Please turn your puzzle of code fragments into a Minimal, Complete, and Verifiable example.
– Yunnosch
Nov 22 at 19:18




Please turn your puzzle of code fragments into a Minimal, Complete, and Verifiable example.
– Yunnosch
Nov 22 at 19:18












@ΣωτηρηςΙωαννιδης, the problem is not that we fail to understand what you are trying to achieve, it is that we don't understand how you are trying to achieve it. As I said already, execlp() does not require any of its arguments to be string literals. To expand on that, it cannot tell whether the pointers in its argument list point to user-provided data. It does not attempt to make any such distinction.
– John Bollinger
Nov 22 at 20:02






@ΣωτηρηςΙωαννιδης, the problem is not that we fail to understand what you are trying to achieve, it is that we don't understand how you are trying to achieve it. As I said already, execlp() does not require any of its arguments to be string literals. To expand on that, it cannot tell whether the pointers in its argument list point to user-provided data. It does not attempt to make any such distinction.
– John Bollinger
Nov 22 at 20:02



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435956%2fexeclp-doenst-work-when-im-giving-the-arguments%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435956%2fexeclp-doenst-work-when-im-giving-the-arguments%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)