denault@hale.ifa.hawaii.edu (Tony Denault) (05/01/91)
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. Thanks. Tony Denault -- /------------------------------------------------------------------------\ | Tony Denault, Institute for Astronomy, | denault@uhifa.ifa.hawaii.edu | | 2680 Woodlawn Drive, Honolulu, HI 96789 | (808) 956-8101 | \------------------------------------------------------------------------/
hunt@dg-rtp.rtp.dg.com (Greg Hunt) (05/01/91)
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.
mike@bria.UUCP (mike.stefanik) (05/04/91)
In an article, denault@hale.ifa.hawaii.edu (Tony Denault) writes: >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. Assume that in reference to $HOME, you are talking about expansion of filenames like ~/this.file (the tilde alone specifies the $HOME path) >For the ~username, I was thinking of reading the /etc/passwd file, search >for the user name and determine their home directory. > >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. Yes, your thinking is essentially correct. Here is a short program that does the tilde expansion. WARNING: Flames regarding the bletcherous hack that follows will be promptly flushed down /dev/null (read: get a life and write some code of your own, instead of reading mine! ;-) --- snip snip snip --------------------------------------------------------- #include <stdio.h> #include <ctype.h> #include <pwd.h> char *explode(); main(argc,argv) int argc; char **argv; { while ( --argc ) puts(explode(*++argv)); return 0; } char *explode(path) char *path; { char *ptr, *getenv(); static char buf[256]; struct passwd *p, *getpwnam(); strncpy(buf,path,256); if ( *path++ == '~' ) { if ( *path == '/' || *path == '\0' ) { if ( (ptr = getenv("HOME")) == NULL ) return(buf); strcpy(buf,ptr); if ( strlen(path) > 0 ) { strcat(buf,"/"); strcat(buf,++path); } return(buf); } ptr = path; while ( isalnum(*++path) ) ; if ( *path != 0 ) *path++ = 0; if ( (p = getpwnam(ptr)) == NULL ) return(buf); strcpy(buf,p->pw_dir); if ( strlen(path) > 0 ) { strcat(buf,"/"); strcat(buf,path); } return(buf); } else return(buf); } ----- snip snip snip ------------------------------------------------------- Well, that's it, in all of it's ugly glory. Enjoy. -- Michael Stefanik, MGI Inc, Los Angeles | Opinions stated are never realistic Title of the week: Systems Engineer | UUCP: ...!uunet!bria!mike ------------------------------------------------------------------------------- If MS-DOS didn't exist, who would UNIX programmers have to make fun of?