Need help with subdirectories names

Greg Hunt hunt at dg-rtp.rtp.dg.com
Thu May 2 00:04:01 AEST 1991


In article <12746 at uhccux.uhcc.Hawaii.Edu>, denault at hale.ifa.hawaii.edu (Tony Denault) writes:
> I would like to ask for help on the following programming problems concerning
> subdirectories.
> 
> I have an application where the user can type in a directory. I would like
> them to 
> use short cuts names like ~username/whatever or $HOME/whatever. I suppose
> the program
> will need to read these and expand them somehow. How they get expanded is
> where I get
> confused. 
>  
> For the ~username, I was thinking of reading the /etc/passwd file, search
> for the user
> name and determine their home directory.  
>  
> For the $HOME, I need to search the enviroment variables for the variables
> value. 
> 
> Is my thinking correct or am I missing something? Can anyone provide some
> example code
> or routine that expands a relative or shortcut name into a full pathname.

Yup, you're thinking correctly about the problem.  Reading the passwd
file might be a bit hard (and won't work for names in yellow pages), so
if your system has them, try using the getpwent(3c) family of routines:
getpwent, getpwuid, and getpwnam.  They'll read the passwd file for you
and also deal with things like yellow pages.

To get the value of an environment variable, use getenv(3c).  You give
it the name of the environment variable, and it will return its value.

You'll have to parse the pathname yourself and deal with the various
cases you want to support.

One (somewhat slow) way of dealing with the expansions is to have the
shell do it for you.  When you get a pathname, then fork off a shell
(see system(3s)) and let it do the expansion for you.

Good luck!

-- 
Greg Hunt                        Internet: hunt at dg-rtp.rtp.dg.com
DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC, USA  These opinions are mine, not DG's.



More information about the Comp.unix.questions mailing list