Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chdir() to home directory

Tags:

c

unix

chdir

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?

like image 400
darksky Avatar asked Feb 29 '12 03:02

darksky


People also ask

What does chdir do?

chdir() changes the current working directory of the calling process to the directory specified in path.

What does chdir return?

If successful, chdir() returns a value of zero. On failure, it returns -1 and sets errno to the one of the following: EACCES.


1 Answers

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().

like image 155
Ernest Friedman-Hill Avatar answered Sep 26 '22 21:09

Ernest Friedman-Hill