c - Replace string value with variable in execl and execution stopping -


i'm building linux shell , i'm trying execute external commands in linux "./" in beginning.

this how read commands:

#include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <stdbool.h>  #include "commands/commands.h"  bool starts_with(const char *a, const char *b){ if(strncmp(a,b,strlen(b)) == 0){     return 1; }  return 0; }  int main(int argc, char *argv[]){  char cmd[500];  do{     printf("$ > ");     fgets(cmd, 499, stdin);      if(starts_with(cmd, "./")){         execute_external_command(cmd);     }else{         //execute_interal_command(cmd);     }   }while(strcmp(cmd, "exit\n") != 0);  return 0; } 

and how child process replaced new process, program called "hello". prints "hello world" screen.

else{     //child process     dup2(commpipe[0],0);     close(commpipe[1]);      //replace child process new process     if(execl("hello", "hello", null) == -1){         fprintf(stderr, "error executing new process!\n");         exit(3);     }  } 

}

it works fine. want replace "hello" variable when type "./hello" "hello" part there , use variable in execl() function.

i tried this, seems unable find "hello". tried printing on screen , prints "hello" should. ideas why process not executed ?

else{     //child process     dup2(commpipe[0],0);     close(commpipe[1]);      char program[10];     int len = strlen(cmd) - 2;     memcpy(program, &cmd[2],len);      //replace child process new process     if(execl(program, program, null) == -1){         fprintf(stderr, "error executing new process!\n");         exit(3);     }  } 

}

second question: in first execution method executed fine "hello" string in execl() funcntion. print "$ > " string o screen, asking input. when child process finishes execution, whole program stops. how can make him print "$ > " string on screen again me execute other commands if want. in advance

update: function executes external commands

void execute_external_command(char cmd[]){  pid_t pid; int rv; int commpipe[2];  //creting pipe if(pipe(commpipe)){     fprintf(stderr, "error creating pipe!!\n");     exit(1); }  if((pid=fork()) == -1){     fprintf(stderr, "error forking ew process!\n");     exit(2); }  if(pid){     //parent process     dup2(commpipe[1],1);     close(commpipe[0]);     setvbuf(stdout,(char*)null,_ionbf,0);     wait(&rv);     fprintf(stderr, "child exited %d value", rv); }else{     //child process     dup2(commpipe[0],0);     close(commpipe[1]);      int len = strlen(cmd) - 2;     char program[len];     memcpy(program, &cmd[2],len);     program[len-1] = '\0';      //replace child process new process     if(execl(program, program, null) == -1){         fprintf(stderr, "error executing new process!\n");         exit(3);     }  } } 

fgets includes trailing newline in string. have strip in order call binary correctly. furthermore, memcpy not copy necessary zerobyte string terminator. try:

program[len-1] = '\0'; 

after memcpy(). should help.

another note: code prone buffer overflow, since program fixed 10 bytes, while cmd can longer. do:

int len = strlen(cmd) - 2; char program[len]; memcpy(program, &cmd[2],len); program[len-1] = '\0'; 

to prevent happening. if not care modifying cmd, do:

cmd[strlen(cmd)-1] = '\0';  if (execl(cmd+2, cmd+2, null) ... 

one more thing:

it not necessary remove ./-part, execl work fine it.

here in parent process:

if(pid){     //parent process     dup2(commpipe[1],1); 

you set stdout of process commpipe. thus, subsequent printf's try write broken pipe, , hence sigpipe terminates parent process. not see @ why need commpipe here, think can leave out completely.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -