Here is my code test.c:
int main(int argc, char* argv[])
{
char* newhome = getenv("HOME");
strcat(newhome, "/myDir");
setenv("HOME", newhome, 1);
printf("ENV: %s\n", getenv("HOME"));
printf("ARG: %s\n", argv[1]);
return 0;
}
The "HOME" env has been set as "/home/user/myDir" in this program. But when I compile and run my code with the "~/" arg, I get such output that the given "~/" arg is still resolved as the default "HOME" env value "/home/user", but not the new set value "/home/user/myDir":
$ cc test.c -o test
$ ./test ~/
ENV: /home/user/myDir
ARG: /home/user
$
So how should I modify my code to fetch the new set value of "HOME" if I give the arg "~/", such as this output:
$ ./test ~/
ENV: /home/user/myDir
ARG: /home/user/myDir
$ ./test ~/../
ENV: /home/user
ARG: /home/user