home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!mcsun!sunic!ericom!eos.ericsson.se!etxmesa
- From: etxmesa@eos.ericsson.se (Michael Salmon)
- Subject: Re: expanding ~'s in a directory name.
- Message-ID: <1992Jul30.090003.5830@ericsson.se>
- Sender: news@ericsson.se
- Nntp-Posting-Host: eos6c02.ericsson.se
- Reply-To: etxmesa@eos.ericsson.se (Michael Salmon)
- Organization: Ericsson Telecom AB
- References: <1992Jul29.012629.14000@gucis.cit.gu.edu.au>
- Date: Thu, 30 Jul 1992 09:00:03 GMT
- Lines: 88
-
- In article <1992Jul29.012629.14000@gucis.cit.gu.edu.au>,
- aroll@gucis.cit.gu.edu.au (Ashley Roll) writes:
- |> I have a program that I'm writing in C in the UNIX environment and I have come
- |> to a part where I need to get a directory name that the program will change
- |> into.
- |>
- |> My problem is how do I expand ~'s. I want to expand then the way that csh does.
- |>
- |> Has anyone written or got code that will do this that I can use? Or is there
- |> a library function that will do this.
-
- This is what xrn uses:
-
- char *
- utTildeExpand(filename)
- char *filename; /* file name, possibly with a '~' */
- /*
- * tilde expand a file name
- *
- * returns: the expanded name of the file (in a static buffer)
- * or NIL(char)
- */
- {
- #ifdef aiws
- static char dummy[MAXPATH];
- #else
- static char dummy[MAXPATHLEN];
- #endif /* aiws */
- char username[USER_NAME_SIZE], *loc;
- struct passwd *pw;
-
- if ((filename == NIL(char)) || STREQ(filename, "")) {
- return(NIL(char));
- }
-
- if (filename[0] != '~') {
- (void) strcpy(dummy, filename);
- return(dummy);
- }
-
- /* tilde at the beginning now */
- if (filename[1] == '/') {
- /* current user */
- char *home, *getenv _ARGUMENTS((const char *));
-
- if ((home = getenv("HOME")) == NIL(char)) {
- #ifndef VMS
- /* fall back on /etc/passwd */
- if ((pw = getpwuid(getuid())) == NIL(struct passwd)) {
- return(NIL(char));
- }
- (void) sprintf(dummy, "%s%s", pw->pw_dir, &filename[1]);
- #else
- return (NIL(char));
- #endif
- } else {
- (void) sprintf(dummy, "%s%s", home, &filename[1]);
- }
-
- } else {
- if ((loc = index(filename, '/')) == NIL(char)) {
- /* error - bad filename */
- return(NIL(char));
- }
- (void) strncpy(username, &filename[1], loc - &filename[1]);
- username[loc - &filename[1]] = '\0';
- #ifndef VMS
- if ((pw = getpwnam(username)) == NIL(struct passwd)) {
- return(NIL(char));
- }
- (void) sprintf(dummy, "%s%s", pw->pw_dir, loc);
- #else
- return(getenv("USER"));
- #endif
- }
- return(dummy);
- }
-
- --
-
- Michael Salmon
-
- #include <standard.disclaimer>
- #include <witty.saying>
- #include <fancy.pseudo.graphics>
-
- Ericsson Telecom AB
- Stockholm
-