I'm creating a program that when launched takes in input a command and some arguments with scanf and calls execvp using those arguments. I'm doing this with strsep. I store the string in an array (char*),then I want to split it and store tokens in a new array (this time it's an array[] so I can use it with the execvp). Arguments saved with scanf should be commands for terminal (as "ls" with "-l" ecc,"pwd"... however variables saved in PATH) so they are separated by " ".
Ex :
./mystring
Type arguments : " ls -l "
It was an example only to specify which kind of input 'll be. I'll do the execvp alone,I need help to split the string in tokens. This is the code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
fflush(NULL); //to clean streams
printf("type the command to start (and arguments if required) \n");
char **st;
char dest[10];
scanf("%s",*st);
int i;
for (i=0;(dest[i]=strsep(st," "))!=NULL;i++)
continue;
for (int c=0;c<i;c++) printf(" arg %d : [%s] ",c,dest[c]);
return 0;
}
Lines 5 and 6 required to call strsep,the 10 in dest[10] is symbolic.
Line 7 to store the input in st.
Line 9 should split on " " and store command and arguments in dest[I](that I'll pass to execvp).
Line 11 to print what dest has stored.
And this is the sad output :
./mystring
type the command to start (and arguments if required)
Segmentation fault: 11
I don't understand how strsep works, someone can help me?
char st[20];
andscanf("%19s",st)
that'll be better alreadyfflush(NULL); //to clean streams
- Where did you read that this should be done?