Let's say I have char ** argv
and that for every argv[i] I'll have a random string but some of them have a env inside, for example:
argv[2] = "asdad$HOMEsada";
If my home directory is /home/user, how can I get argv[2]= "asdad/home/usersada";
IMPORTANT: I have to use sscanf and "%[_a-zA-Z0-9]" format.
I have implemented some code that works but doesn't use those options, here it is:
for (int j = 0; argv[j]; j++)
{
for (int i = 0; argv[j][i]; i++)
{
if (argv[j][i] == '$')
{
char principio[i + 1];
strncpy(principio, argv[j], i);
principio[i] = '\0';
argv[j] = strdup(strcat(principio, getenv(argv[j] + i + 1)));
break;
}
Thanks!