home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 4044 < prev    next >
Encoding:
Text File  |  1992-07-31  |  2.3 KB  |  82 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!news.claremont.edu!ucivax!cigna!nagel
  3. From: nagel@Cigna.COM (Mark Nagel)
  4. Subject: Re: expanding ~'s in a directory name.
  5. Message-ID: <1992Jul31.184817.16673@Cigna.COM>
  6. Sender: news@Cigna.COM (Network News)
  7. Nntp-Posting-Host: elvis
  8. Organization: CIGNA FIRST, Irvine, CA
  9. References: <1992Jul29.012629.14000@gucis.cit.gu.edu.au> <MJN.92Jul31043705@pseudo.uucp> <1992Jul31.151135.25775@prism.poly.edu>
  10. Date: Fri, 31 Jul 1992 18:48:17 GMT
  11. Lines: 69
  12.  
  13. In <1992Jul31.151135.25775@prism.poly.edu> kapela@prism.poly.edu (Theodore S. Kapela) writes:
  14.  
  15. >In article <MJN.92Jul31043705@pseudo.uucp> mjn@pseudo.uucp (Murray Nesbitt) writes:
  16. >>
  17. >>In article <1992Jul29.012629.14000@gucis.cit.gu.edu.au> aroll@gucis.cit.gu.edu.au (Ashley Roll) writes:
  18. >>
  19. >>>My problem is how do I expand ~'s. I want to expand then the way that csh does
  20. >>
  21. >>I don't care^H^H^H^Hknow about the csh, but the following should work
  22. >>with sh and ksh:
  23. >>
  24. >>#include <stdlib.h>
  25. >>
  26. >>char *expanded_tilde;
  27. >>
  28. >>    expanded_tilde = getenv("HOME");
  29.  
  30. >What about ~user instead of just ~?  With csh, I can do something
  31. >like cd ~joeuser/tmp to cd to joeuser's $HOME/tmp.  What your function
  32. >needs to do is check what follows the ~ and act appropriately.  You still
  33. >need to check /etc/passwd for other users.
  34.  
  35. Try the following:
  36.  
  37. #include <stdio.h>
  38. #include <pwd.h>
  39. #include <sys/errno.h>
  40.  
  41. int
  42. tilde(file, exp)
  43.   char *file, *exp;
  44. {
  45.   *exp = '\0';
  46.   if (file)
  47.   {
  48.     if (*file == '~')
  49.     {
  50.       char user[128];
  51.       struct passwd *pw = NULL;
  52.       int i = 0;
  53.       
  54.       user[0] = '\0';
  55.       file++;
  56.       while (*file != '/' && i < sizeof(user))
  57.         user[i++] = *file++;
  58.       user[i] = '\0';
  59.       if (i == 0)
  60.       {
  61.         char *login = (char *) getlogin();
  62.     
  63.         if (login == NULL && (pw = getpwuid(getuid())) == NULL)
  64.       return (0);
  65.     if (login != NULL)
  66.       strcpy(user, login);
  67.       }
  68.       if (pw == NULL && (pw = getpwnam(user)) == NULL)
  69.         return (0);
  70.       strcpy(exp, pw->pw_dir);
  71.     }
  72.     strcat(exp, file);
  73.     return (1);
  74.   }
  75.   return (0);
  76. }
  77. -- 
  78. Mark Nagel <nagel@cigna.com>    | DISCLAIMER: Any resemblence of these
  79. Network Administrator        | opinions to those of CIGNA are purely
  80. CIGNA/FIRST            | coincidental.
  81.           (Don't start vast projects with half-vast ideas)
  82.