Hi
Before i start i just wanted to say that i know there is a linux programming board, but it seems like not many people post there, and seeing as this is mostly a C question too i thought it would be ok for me to try it here, sorry if I made a mistake.
Im in the process of making a command line parser and im stuck handling wildcards... I know that i need to use the glob() function and i gave it my best shot but im getting segmentation faults at runtime.
The function in question accepts the argument list, number of args, and offs (which is the amount of tokens containing * in them, that were found during string processing). Basically i thought i would be able to check if there were offs in the string, if so then use glob() and send it to execvp, or if there wasnt any offs, then just send the string to execvp as per normal.
The problem is in the logic used to glob in the string, and i would really like any help at all, my current attempt is below and ive marked where i think the faults are occuring, thanks.
Code:
void childProcess(char **arglist, int args, int extra_proc, int offs)
{
int init, i, first_glob = TRUE;
id_t pid;
glob_t globbuf;
if(offs > 0)
{
globbuf.gl_offs = offs;
for(i=0; i<args; i++)
{
if(strstr(arglist[i], "*") != NULL)
{
if(first_glob)
{
glob(arglist[i], GLOB_DOOFFS, NULL, &globbuf);
first_glob = FALSE;
}
else
{
glob(arglist[i], GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
}
}
else
{
globbuf.gl_pathv[i] = arglist[i]; //here
}
}
}
pid = fork();
if(pid == -1)
{
printf("\n[ERROR_FORK]");
}
else
{
if(pid > 0)
{
if(extra_proc != BACKGROUND)
{
wait(&init);
}
}
else
{
if(offs > 0)
{
if((init = execvp(arglist[0], &globbuf.gl_pathv[0])) == -1) //here
{
printf("\nUnknown command");
}
}
else
{
if((init = execvp(arglist[0], arglist)) == -1)
{
printf("\nUnknown command");
}
}
exit(0);
}
}
}