Kebtiz Kebtiz - 2 months ago 6
Linux Question

null terminate an array of strings

I am trying to figure out how to get my array of strings from

get_arguments
to
NULL
terminate, or if that isn't the issue to function in my
execv
call.

char ** get_arguments(const char * string) {
char * copy = strdup(string);
char * remove_newline = "";
for(;;) {
remove_newline = strpbrk(copy, "\n\t");
if (remove_newline) {
strcpy(remove_newline, "");
}
else {
break;
}
}
char (* temp)[16] = (char *) malloc(256 * sizeof(char));
char * token = strtok(copy, " ");
strcpy(temp[0], token);
int i = 1;
while (token && (token = strtok(NULL, " "))) {
strcpy(temp[i], token);
i++;
}
char * new_null;
//new_null = NULL;
//strcpy(temp[i], new_null);
if(!temp[i]) printf("yup\n");
int c = 0;
for ( ; c <= i; c++) {
printf("%s ", temp[c]);
}
return temp;
}


I am trying to read in a string, space separated, similar to
find ./ -name *.h
. I am trying to input them into
execv
.

char (* arguments)[16] = (char **) malloc(256 * sizeof(char));


//...numerous lines of unrelated code

pid = fork();
if (pid == 0) {
arguments = get_arguments(input_string);
char * para[] = {"find", "./","-name", "*.h", NULL};
execv("/usr/bin/find", (char * const *) arguments);
//printf("%s\n", arguments[0]);
printf("\nexec failed: %s\n", strerror(errno)); //ls -l -R
exit(-1);
}


When I swap
arguments
in the
execv
call for
para
it works as intended, but trying to call with
arguments
returns
exec failed: Bad address
. If I remove the
NULL
from
para
I get the same issue. I've tried
strcpy(temp, (char *) NULL)
, the version you see commented out in
get_arguments
, and a number of other things that I can't recall in their entirety, and my program ranges from
Segmentation fault
to failure to compile from attempting to
strcpy
NULL.

Changing the declarations of arguments and temp to
char ** arguments = (char *) malloc(256 * sizeof(char));
``char ** temp = (char *) malloc(256 * sizeof(char));
clears up
warning: initialization from incompatible pointer type
but causes segfault on all calls to
get_arguments`.

Answer

You want this:

char* temp[256]; // an array of 256 char*'s
char * token = strtok(copy, " ");
temp[0] = strdup(token);
int i = 1;
while (token && (token = strtok(NULL, " "))) {
    temp[i] = strdup(token);
    i++;
}
temp[i] = NULL;