Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!apple!veritas!amdcad!dgcad!dg-rtp!hunt From: hunt@dg-rtp.rtp.dg.com (Greg Hunt) Newsgroups: comp.unix.questions Subject: Re: Need help with subdirectories names Message-ID: <1991May1.140401.16988@dg-rtp.dg.com> Date: 1 May 91 14:04:01 GMT References: <12746@uhccux.uhcc.Hawaii.Edu> Sender: hunt@hobbit (Greg Hunt) Reply-To: hunt@dg-rtp.rtp.dg.com Organization: Data General Corp., Research Triangle Park, NC Lines: 45 In article <12746@uhccux.uhcc.Hawaii.Edu>, denault@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@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.