getting home directories

John Gordon gordon at osiris.cso.uiuc.edu
Sat Apr 20 09:56:20 AEST 1991


gaumondp at JSP.UMontreal.CA (Gaumond Pierre) writes:

>Suppose a C program has to reead the name of a file on another file and then
>open it (with "fopen").

>Now, suppose the name read has one of the forms:

>   -  ~/dir/file
>   -  ~user/dir/file

>What tools do I have to interpret the ~ and ~user as home directories?
>What tools can split path name in single components?

	Here's how I would do it:  Examine the string that contains the
filespec.  If the first char is a ~, we have to do expansion.  If the second
char is a /, the ~ represents us, else scan up to a / and that is the user
name.  If we are getting our own home dir, go a getenv("HOME") or your
local equivalent.  If we are getting someone else's home dir, do an awk
system command to parse that user's entry in the password file.  There you
will find the home dir for that user.


---
John Gordon
Internet: gordon at osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon at cerl.cecer.army.mil       #include <clever_saying.h>



More information about the Comp.unix.questions mailing list