How to copy an array of char pointers with a larger list of char pointers?
Posted
by Casey Link
on Stack Overflow
See other posts from Stack Overflow
or by Casey Link
Published on 2010-03-15T01:17:05Z
Indexed on
2010/03/15
1:19 UTC
Read the original article
Hit count: 330
My function is being passed a struct containing, among other things, a NULL terminated array of pointers to words making up a command with arguments.
I'm performing a glob match on the list of arguments, to expand them into a full list of files, then I want to replace the passed argument array with the new expanded one.
The globbing is working fine, that is, g.gl_pathv is populated with the list of expected files. However, I am having trouble copying this array into the struct I was given.
#include <glob.h>
struct command {
char **argv;
// other fields...
}
void myFunction( struct command * cmd )
{
char **p = cmd->argv;
char* program = *p++; // save the program name (e.g 'ls', and increment to the first argument
glob_t g;
memset(&g, 0, sizeof(g));
int res = glob(*p, 0, NULL, &g);
*p++ // increment
while (*p)
{
glob(*p++, GLOB_APPEND, NULL, &g); // append the matches
}
// here i want to replace cmd->argv with the expanded g.gl_pathv
memcpy(cmd->argv, g.gl_pathv, g.gl_pathc ); // this doesn't work
globfree(&g);
}
© Stack Overflow or respective owner