Re: [PATCH] Prefer getenv("HOME") to find the UNIX home directory

Anders Kaseorg <andersk@mit.edu>

From: Anders Kaseorg <andersk@mit.edu>
To: Daniel Gustafsson <daniel@yesql.se>, Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Cc: alvherre@alvh.no-ip.org, pgsql-hackers@lists.postgresql.org
Date: 2021-10-20T17:09:51Z
Lists: pgsql-hackers
On 10/20/21 04:55, Daniel Gustafsson wrote:
> Is the proposed change portable across all linux/unix systems we support?
> Reading aobut indicates that it's likely to be, but neither NetBSD nor FreeBSD
> have the upthread referenced wording in their manpages.

Since the proposed change falls back to the old behavior if HOME is 
unset or empty, I assume this is a question about convention and not 
literally about whether it will work on these systems. I don’t find it 
surprising that this convention isn’t explicitly called out in every 
system’s manpage for the wrong function, but it still applies to these 
systems.

POSIX specifies that the shell uses the HOME environment variable for 
‘cd’ with no arguments and for the expansion of ~. This implies by 
reference that this behavior is required of wordexp() as well.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cd.html
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_01
https://pubs.opengroup.org/onlinepubs/9699919799/functions/wordexp.html

libc’s glob() and wordexp() respect HOME in glibc, musl, NetBSD, and 
FreeBSD.

https://sourceware.org/git/?p=glibc.git;a=blob;f=posix/glob.c;hb=glibc-2.34#l622
https://sourceware.org/git/?p=glibc.git;a=blob;f=posix/wordexp.c;hb=glibc-2.34#l293

https://git.musl-libc.org/cgit/musl/tree/src/regex/glob.c?h=v1.2.2#n203
https://git.musl-libc.org/cgit/musl/tree/src/misc/wordexp.c?h=v1.2.2#n111

https://github.com/NetBSD/src/blob/netbsd-9/lib/libc/gen/glob.c#L424
https://github.com/NetBSD/src/blob/netbsd-9/lib/libc/gen/wordexp.c#L129-L150
https://github.com/NetBSD/src/blob/netbsd-9/bin/sh/expand.c#L434-L441

https://github.com/freebsd/freebsd-src/blob/release/13.0.0/lib/libc/gen/glob.c#L457
https://github.com/freebsd/freebsd-src/blob/release/13.0.0/lib/libc/gen/wordexp.c#L171-L190
https://github.com/freebsd/freebsd-src/blob/release/13.0.0/bin/sh/expand.c#L396

(Today I learned that musl and BSD libc literally spawn a shell process 
to handle wordexp(). Wow.)

Anders



Commits

  1. Clean up messy API for src/port/thread.c.

  2. Prefer $HOME when looking up the current user's home directory.