PDA

View Full Version : help needed urgently


mile1982
10-17-2004, 07:00 PM
ive been stuck on this problem for a couple of days now but i still cant figure it out. im new to C and UNIX so ............ have understand if the question does not sound that bad . ah well.
here's the problem. when the user enters eg: ls *.c, i separate the comamnds into [1] ls [2].c [3]*

now i heard that you can use the glob() function to execute it. so i tried it but dont seem to know how to do it. here's what i got so far:


/*Loop through the remaining tokens, writing them to the struct. */
while ((dc = strtok(NULL, break_set)) != NULL) {

printf(" dc = %s \n",dc); // here dc equals to ".c"

#ifdef DEBUG
fprintf(stderr,"[%s]\n",dc);
#endif

result->argv = realloc((void *) result->argv, (lpc + 1) * sizeof(char *));
result->argv[lpc] = strdup(dc);
result->argc++;
lpc++;
}
if ((dc = strtok(NULL, "*")) == NULL) { // was white_space

result->wildcard = "*";
result->argv[lpc] = "*";
result->argc++;
lpc++;
}
}


now how can i combine the separated commands '*' and '.c' to form one command so that it looks like "*.c" ??




globbuf.gl_offs = 1;
glob("[B]*.c[/B]", GLOB_DOOFFS, NULL, &globbuf);
globbuf.gl_pathv[0] = a->com_name;
execvp(a->com_name, &globbuf.gl_pathv[0]);



help would be greatly appreciated.
thanks in advance

mile1982

mile1982
10-17-2004, 07:02 PM
oops, made a small mistake in the glob() code
should look like:


globbuf.gl_offs = 1;
glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
globbuf.gl_pathv[0] = a->com_name;
execvp(a->com_name, &globbuf.gl_pathv[0]);


thanks
mile1982[/code]

i3839
10-17-2004, 07:15 PM
Looked at `man glob` and `man wordexp`?

mile1982
10-17-2004, 09:01 PM
looked at man glob (wasnt helping) but didnt know about the other one.

i3839
10-17-2004, 10:11 PM
It's mentioned in the glob manpage...