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

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!mcsun!sunic!ericom!eos.ericsson.se!etxmesa
  3. From: etxmesa@eos.ericsson.se (Michael Salmon)
  4. Subject: Re: expanding ~'s in a directory name.
  5. Message-ID: <1992Jul30.090003.5830@ericsson.se>
  6. Sender: news@ericsson.se
  7. Nntp-Posting-Host: eos6c02.ericsson.se
  8. Reply-To: etxmesa@eos.ericsson.se (Michael Salmon)
  9. Organization: Ericsson Telecom AB
  10. References:  <1992Jul29.012629.14000@gucis.cit.gu.edu.au>
  11. Date: Thu, 30 Jul 1992 09:00:03 GMT
  12. Lines: 88
  13.  
  14. In article <1992Jul29.012629.14000@gucis.cit.gu.edu.au>, 
  15. aroll@gucis.cit.gu.edu.au (Ashley Roll) writes:
  16. |> I have a program that I'm writing in C in the UNIX environment and I have come
  17. |> to a part where I need to get a directory name that the program will change 
  18. |> into. 
  19. |> 
  20. |> My problem is how do I expand ~'s. I want to expand then the way that csh does.
  21. |> 
  22. |> Has anyone written or got code that will do this that I can use? Or is there
  23. |> a library function that will do this.
  24.  
  25. This is what xrn uses:
  26.  
  27. char *
  28. utTildeExpand(filename)
  29. char *filename;    /* file name, possibly with a '~'             */
  30. /*
  31.  * tilde expand a file name
  32.  *
  33.  *   returns: the expanded name of the file (in a static buffer)
  34.  *            or NIL(char) 
  35.  */
  36. {
  37. #ifdef aiws
  38.     static char dummy[MAXPATH];
  39. #else
  40.     static char dummy[MAXPATHLEN];
  41. #endif /* aiws */
  42.     char username[USER_NAME_SIZE], *loc;
  43.     struct passwd *pw;
  44.     
  45.     if ((filename == NIL(char)) || STREQ(filename, "")) {
  46.         return(NIL(char));
  47.     }
  48.  
  49.     if (filename[0] != '~') {
  50.         (void) strcpy(dummy, filename);
  51.         return(dummy);
  52.     }
  53.  
  54.     /* tilde at the beginning now */
  55.     if (filename[1] == '/') {
  56.         /* current user */
  57.         char *home, *getenv _ARGUMENTS((const char *));
  58.  
  59.         if ((home = getenv("HOME")) == NIL(char)) {
  60. #ifndef VMS
  61.             /* fall back on /etc/passwd */
  62.             if ((pw = getpwuid(getuid())) == NIL(struct passwd)) {
  63.                 return(NIL(char));
  64.             }
  65.             (void) sprintf(dummy, "%s%s", pw->pw_dir, &filename[1]);
  66. #else
  67.             return (NIL(char));
  68. #endif
  69.         } else {
  70.             (void) sprintf(dummy, "%s%s", home, &filename[1]);
  71.         }
  72.             
  73.     } else {
  74.         if ((loc = index(filename, '/')) == NIL(char)) {
  75.             /* error - bad filename */
  76.             return(NIL(char));
  77.         }
  78.         (void) strncpy(username, &filename[1], loc - &filename[1]);
  79.         username[loc - &filename[1]] = '\0';
  80. #ifndef VMS
  81.         if ((pw = getpwnam(username)) == NIL(struct passwd)) {
  82.             return(NIL(char));
  83.         }
  84.         (void) sprintf(dummy, "%s%s", pw->pw_dir, loc);
  85. #else
  86.         return(getenv("USER"));
  87. #endif
  88.     }
  89.     return(dummy);
  90. }
  91.  
  92. -- 
  93.  
  94. Michael Salmon
  95.  
  96. #include    <standard.disclaimer>
  97. #include    <witty.saying>
  98. #include    <fancy.pseudo.graphics>
  99.  
  100. Ericsson Telecom AB
  101. Stockholm
  102.