chdir() to home directory
Asked Answered
A

2

22

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?

Antiquary answered 29/2, 2012 at 3:33 Comment(1)
When you need to expand ~username, you'll need to use getpwnam() instead of getpwuid() to find the home directory.Lilia
S
29

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

Synclastic answered 29/2, 2012 at 3:34 Comment(5)
i.e. chdir(getenv("HOME")).Matazzoni
Is chdir() guaranteed not to crash if the argument is NULL? If so, this would be OK; otherwise, a little error checking would be in order!Synclastic
It won't crash, but it'll certainly fail which you might want to know about.Fleece
There's no guarantee as far as I know.Matazzoni
Here's a code snippet for anyone looking to use the latter method. jonathonhill.net/2013-09-03/tilde-expansion-in-phpSortition
L
11

Note that POSIX specifies the semantics of tilde expansion:

2.6.1 Tilde Expansion

A "tilde-prefix" consists of an unquoted <tilde> character at the beginning of a word, followed by all of the characters preceding the first unquoted <slash> in the word, or all the characters in the word if there is no <slash>. In an assignment (see XBD Variable Assignment ), multiple tilde-prefixes can be used: at the beginning of the word (that is, following the <equals-sign> of the assignment), following any unquoted <colon>, or both. A tilde-prefix in an assignment is terminated by the first unquoted <colon> or <slash>. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the <tilde> are treated as a possible login name from the user database. A portable login name cannot contain characters outside the set given in the description of the LOGNAME environment variable in XBD Other Environment Variables. If the login name is null (that is, the tilde-prefix contains only the tilde), the tilde-prefix is replaced by the value of the variable HOME. If HOME is unset, the results are unspecified. Otherwise, the tilde-prefix shall be replaced by a pathname of the initial working directory associated with the login name obtained using the getpwnam() function as defined in the System Interfaces volume of POSIX.1-2008. If the system does not recognize the login name, the results are undefined.

Note in particular that if my username is me, the results of cd ~ and cd ~me may not be the same! Specifically, the HOME environment variable could be set to a value different from the one returned by getpwnam(). I've been using this technique for (way over 25) years to set my HOME where I want it, and the few programs that don't recognize the difference between cd ~ and cd ~me are some (of the many) banes of my life.

Lilia answered 29/2, 2012 at 3:54 Comment(1)
+1 for pointing out that ~ and ~me refer to different sources to get their job done - good to know. But although POSIX specifies tilde expansion, it is in the chapter on shells, not system calls.Voile

© 2022 - 2024 — McMap. All rights reserved.