0
int create_process(char input[]){
 char string[100];
 int j =  (sizeof(input) / sizeof(input[0]));
 char temp[j+1];
 int i = 0;
 for(i = 0; i <= j; j++){
     temp[i] = input[i];
 }
 temp[j+1] = '\0';
 strcpy(string, temp);

pid_t pid=fork();
if (pid==0) { 
    char *params[4]  = {"/bin/ls", "-l",0};
    execv("/bin/ls", params);
    exit(127); 
}
else { 
    printf("Continuing the parent process");
}
return 0; }

Can anybody tell me what is wrong with my code it gives me segmentation fault core dump program exited with code 139

create_process gets an array of char as input. Ultimately I want to dynamically execute programs based on the input[] char array.

Any help would be highly appreciated. Thanks.

Note: I am using Ubuntu 15.04.

for example if I want to run gedit input would be

input[0] = 'g';
input[1] = 'e';
input[2] = 'd';
input[3] = 'i';
input[4] = 't';
Dayem Siddiqui
  • 175
  • 2
  • 11
  • 5
    `int j = (sizeof(input) / sizeof(input[0]));` this will not work. The function has no idea of the length of the array: you have to tell it with another argument. Although one exception is if the argument is a nul terminated string and `strlen` can be applied to it. `sizeof(input)` gives the size of a pointer. I am sure someone will find a dup question, it has been asked so many times. – Weather Vane Oct 27 '16 at 17:59
  • 3
    See: [Finding length of array inside a function](http://stackoverflow.com/questions/17590226/c-finding-length-of-array-inside-a-function). – P.P Oct 27 '16 at 18:00
  • Usage of array syntax in function parameters should be considered harmful. – 2501 Oct 27 '16 at 18:01
  • SO isn't a debugging service. Compile with symbols, run the code inside a debugger to trace through the program(s) line by line inspecting the values of the relevant variables to learn what is really going on. If then a *specific* questions arise feel free to come back here. – alk Oct 27 '16 at 18:11
  • Thank you stackoverflow community! I am actually just getting started with C. Your suggestions solved the problem. – Dayem Siddiqui Oct 27 '16 at 18:12
  • @alk should I delete this question? – Dayem Siddiqui Oct 27 '16 at 18:20
  • Generally improving would be a more constructive approach in the 1st place. – alk Oct 27 '16 at 18:27

0 Answers0