Why mkdir fails to work with tilde (~)?
~
is known only to the shell and not to the mkdir
system call.
But if you try:
system("mkdir ~/foo");
this works as the "mkdir ~/foo"
is passed to a shell and shell expands ~
to $HOME
If you want to make use of the $HOME
with mkdir
, you can make use of the getenv
function as:
char path[MAX];char *home = getenv ("HOME");if (home != NULL) { snprintf(path, sizeof(path), "%s/new_dir", home); // now use path in mkdir mkdir(path, PERM);}