home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!elroy.jpl.nasa.gov!news.claremont.edu!ucivax!cigna!nagel
- From: nagel@Cigna.COM (Mark Nagel)
- Subject: Re: expanding ~'s in a directory name.
- Message-ID: <1992Jul31.184817.16673@Cigna.COM>
- Sender: news@Cigna.COM (Network News)
- Nntp-Posting-Host: elvis
- Organization: CIGNA FIRST, Irvine, CA
- References: <1992Jul29.012629.14000@gucis.cit.gu.edu.au> <MJN.92Jul31043705@pseudo.uucp> <1992Jul31.151135.25775@prism.poly.edu>
- Date: Fri, 31 Jul 1992 18:48:17 GMT
- Lines: 69
-
- In <1992Jul31.151135.25775@prism.poly.edu> kapela@prism.poly.edu (Theodore S. Kapela) writes:
-
- >In article <MJN.92Jul31043705@pseudo.uucp> mjn@pseudo.uucp (Murray Nesbitt) writes:
- >>
- >>In article <1992Jul29.012629.14000@gucis.cit.gu.edu.au> aroll@gucis.cit.gu.edu.au (Ashley Roll) writes:
- >>
- >>>My problem is how do I expand ~'s. I want to expand then the way that csh does
- >>
- >>I don't care^H^H^H^Hknow about the csh, but the following should work
- >>with sh and ksh:
- >>
- >>#include <stdlib.h>
- >>
- >>char *expanded_tilde;
- >>
- >> expanded_tilde = getenv("HOME");
-
- >What about ~user instead of just ~? With csh, I can do something
- >like cd ~joeuser/tmp to cd to joeuser's $HOME/tmp. What your function
- >needs to do is check what follows the ~ and act appropriately. You still
- >need to check /etc/passwd for other users.
-
- Try the following:
-
- #include <stdio.h>
- #include <pwd.h>
- #include <sys/errno.h>
-
- int
- tilde(file, exp)
- char *file, *exp;
- {
- *exp = '\0';
- if (file)
- {
- if (*file == '~')
- {
- char user[128];
- struct passwd *pw = NULL;
- int i = 0;
-
- user[0] = '\0';
- file++;
- while (*file != '/' && i < sizeof(user))
- user[i++] = *file++;
- user[i] = '\0';
- if (i == 0)
- {
- char *login = (char *) getlogin();
-
- if (login == NULL && (pw = getpwuid(getuid())) == NULL)
- return (0);
- if (login != NULL)
- strcpy(user, login);
- }
- if (pw == NULL && (pw = getpwnam(user)) == NULL)
- return (0);
- strcpy(exp, pw->pw_dir);
- }
- strcat(exp, file);
- return (1);
- }
- return (0);
- }
- --
- Mark Nagel <nagel@cigna.com> | DISCLAIMER: Any resemblence of these
- Network Administrator | opinions to those of CIGNA are purely
- CIGNA/FIRST | coincidental.
- (Don't start vast projects with half-vast ideas)
-