From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,447a948bb64464c3 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-10-13 23:36:24 PST Newsgroups: comp.lang.ada Path: bga.com!news.sprintlink.net!howland.reston.ans.net!usc!crash!telesoft!kst From: kst@alsys.com (Keith Thompson @pulsar) Subject: Re: I have a question... Message-ID: Sender: news@alsys.com (USENET News Admin @flash) Organization: Alsys Group, San Diego, CA, USA References: <1994Oct07.034523.161470@zeus.aix.calpoly.edu> Date: Fri, 14 Oct 1994 01:21:10 GMT Date: 1994-10-14T01:21:10+00:00 List-Id: In emery@goldfinger.mitre.org (David Emery) writes: > The easiest way to 'solve' this problem is to expand the "~" into the > home directory. The environment variable "HOME" contains the pathname > of the home directory. Thus it's pretty trivial to implement an > operation that replaces (all) occurances of "~" in a pathname with the > string value of HOME. > > To be more accurate/precise, the best thing to do is to obtain the > pathname to the home directory from the /etc/passwd file, since the > value of HOME could have been changed by the user. Alas, it's not quite that easy. The expansion of '~' is done by the C-shell (csh). The Bourne shell (sh) doesn't do this; "echo ~" echoes a single '~' character. Some sh variants (e.g., the Korn shell, ksh) mimic csh's tilde expansion. The C-shell expands '~' to the value of the $HOME environment variable. This is initially the user's home directory, but it can be changed (and there are legitimate reasons for doing so). If you're going to do '~' expansion, don't second-guess the user by looking for the "real" home directory in the password file, just grab the value of $HOME. (Actually, it's the shell variable $home, but csh ties their values together.) This applies only to '~' at the beginning of a word, immediately followed either by the end of a word or a non-alphanumeric character. If '~' (at the beginning of a word) is followed by a sequence of alphanumeric characters, they're taken as a user name, and the sequence is replaced by the name of that user's home directory (regardless of any environment variables). For example, "~buford" might expand to "/home/buford"; "~root" typically expands to "/". Note that "~nosuchuser" is an error; you'll have to decide how to deal with this. If '~' appears other than at the beginning of a word, leave it alone; "a~b" should expand to "a~b". This is important, since many programs append a '~' character to the names of backup files. Also, an experiment I just did shows that the shell doesn't necessarily look for just alphanumeric characters; "echo ~kst.foo" reported "Unknown user: kst.foo." under tcsh (a csh variant), but echoed "~kst.foo" under csh. The man page is less than clear on this subject. So, how do you determine a user's home directory, given the user's name? Reading the file "/etc/passwd" won't necessarily work; many systems don't store all the password information there (YP/NIS). In C, you can call getpwnam() (or getpwnam_r() if you're worried about reentrancy and the system supports it) and extract the directory name from the result's pw_dir field. If your vendor supports the POSIX/Ada interface, the equivalent (assuming User_Name is a string containing the user name) is POSIX_User_Database.Initial_Directory_Of ( POSIX_User_Database.Get_Database_Item ( POSIX.To_POSIX_String ( User_Name ) ) ); Note that the vast majority of Unix programs do *not* support '~' expansion (or environment variable expansion, or wildcard expansion). This job is normally left to the shell. I strongly recommend that you do the same. -- Keith Thompson (The_Other_Keith) kst@alsys.com TeleSoft^H^H^H^H^H^H^H^H Alsys, Inc. 10251 Vista Sorrento Parkway, Suite 300, San Diego, CA, USA, 92121-2718 /user/kst/.signature: I/O error (core dumped)