[comp.unix.questions] getting home directories

gaumondp@JSP.UMontreal.CA (Gaumond Pierre) (04/16/91)

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?

Pierre Gaumond.
-- 
Pierre Gaumond.                                 | gaumondp@JSP.UMontreal.CA
Services Informatiques, Universite de Montreal. | gaumondp@centrcn.UMontreal.CA
C.P. 6128, Succursale "A", Montreal,            |
Quebec, Canada.   H3C 3J7                       |

jik@athena.mit.edu (Jonathan I. Kamens) (04/19/91)

In article <1991Apr16.155130.2708@jsp.umontreal.ca>, gaumondp@JSP.UMontreal.CA (Gaumond Pierre) writes:
|>    -  ~/dir/file
|>    -  ~user/dir/file
|> 
|> What tools do I have to interpret the ~ and ~user as home directories?

  There are no standard library functions to do this; you'll have to write
code to do it yourself; it isn't that difficult to write.  Either that, or you
can steal the code from one of the many packages that are available on the net
that have it; for example, I believe rn does ~ expansion.

|> What tools can split path name in single components?

  Strtok, if it exists on your system, will do this.  See the man page.  If it
doesn't exist on your system, there are (I believe) several freely
redistributable versions of it floating around the net, or (once again) you
can steal it from another program that has a freely redistributable version of
it.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

gordon@osiris.cso.uiuc.edu (John Gordon) (04/20/91)

gaumondp@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@osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon@cerl.cecer.army.mil       #include <clever_saying.h>