I am using the chdir()
C function to allow a user to change directory.
The function however, doesn't recognize '~'. Do I need to do any explicit conversion, so chdir
doesn't recognize what ~ means? Because mine isn't working. Or am I doing something wrong?
chdir() changes the current working directory of the calling process to the directory specified in path.
If successful, chdir() returns a value of zero. On failure, it returns -1 and sets errno to the one of the following: EACCES.
Tilde expansion is handled by the shell, not by a system call. You could use getenv()
to read the environment variable HOME
and then use that as the argument to chdir()
.
There are system calls to get this information that may be more reliable on an individual system, but they're not completely portable. Look, for example, at getpwuid().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With