I'm trying to test absolute paths on a linux machine to find where a program is located so I can run it with my specific arguments. The problem is, when I find it, I keep adding more strings to the correct path as well as memory leak by freeing the dynamically allocated memory. Only fix for the stack dump is to not free(ret). I believe based on gdb that when I run an example with "ls" it finds the program and runs it, but gives strange results.
for(j = 0; j < i; j++, path = NULL)
{
token = strtok_r(path, delim, &saver);
if(token == NULL)
break;
else
{
strncat(ret, token, 80);
strncat(ret, "/", 1);
strncat(ret, command, 80);
args[0] = ret;
printf("%s\n", ret);
m = execv(ret, args);
printf("%d\n", m);
if(m < 0)
{
free(ret);
ret = malloc(120*sizeof(char));
}
else
break;
}
}
Where the delimiter character is a colon (:) and I believe the strncat is done correctly. I'm not sure though so thanks for the help.