comp.lang.ada
 help / color / mirror / Atom feed
From: kst@alsys.com (Keith Thompson @pulsar)
Subject: Re: I have a question...
Date: Fri, 14 Oct 1994 01:21:10 GMT
Date: 1994-10-14T01:21:10+00:00	[thread overview]
Message-ID: <Cxn2FA.MsF@alsys.com> (raw)
In-Reply-To: EMERY.94Oct12173346@goldfinger.mitre.org

In <EMERY.94Oct12173346@goldfinger.mitre.org> 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)



  reply	other threads:[~1994-10-14  1:21 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1994Oct07.034523.161470@zeus.aix.calpoly.edu>
     [not found] ` <SPIEGEL.94Oct10115716@moskau.bruessel.informatik.uni-stuttgart.de>
1994-10-12 17:33   ` I have a question David Emery
1994-10-14  1:21     ` Keith Thompson @pulsar [this message]
1994-10-14 16:46       ` Andre Spiegel
1994-10-14 14:16         ` David Emery
1994-10-14 21:32           ` Norman H. Cohen
1994-10-17 10:34             ` Ada programs in a UNIX environment (was Re: I have a question...) Andre Spiegel
1994-10-15 11:28       ` I have a question Keith Thompson @pulsar
1994-10-14  7:38     ` Henri Altarac
1994-10-14 21:25     ` Kevin Cline
1994-10-13 17:13 ` James A. Krzyzanowski
1994-10-13 15:29   ` David Emery
     [not found] <1994Oct07.034523.161470@zeus.aix>
1994-10-14 11:16 ` emery
1994-10-14 16:46 ` spiegel
1994-10-15 16:52 ` ncohen
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox