home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / kpathsea / tilde.c < prev    next >
C/C++ Source or Header  |  1993-10-06  |  3KB  |  123 lines

  1. /* tilde.c: Expand user's home directories.
  2.  
  3. Copyright (C) 1993 Karl Berry.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <kpathsea/config.h>
  20.  
  21. #include <kpathsea/c-pathch.h>
  22. #include <kpathsea/tilde.h>
  23.  
  24. #ifdef HAVE_PWD_H
  25. #include <pwd.h>
  26. #endif
  27.  
  28.  
  29. /* If NAME has a leading ~ or ~user, Unix-style, expand it to the user's
  30.    home directory, and return a new malloced string.  If no ~, or no
  31.    <pwd.h>, just return NAME.  */
  32.  
  33. string
  34. kpse_tilde_expand P1C(const_string, name)
  35. {
  36. #ifdef HAVE_PWD_H
  37.   const_string expansion;
  38.   const_string home;
  39.   
  40.   assert (name);
  41.   
  42.   /* If no leading tilde, do nothing.  */
  43.   if (*name != '~')
  44.     expansion = name;
  45.   
  46.   /* If `~' or `~/', use $HOME if it exists, or `.' if it doesn't.  */
  47.   else if (IS_DIR_SEP (name[1]) || name[1] == 0)
  48.     {
  49.       home = getenv ("HOME");
  50.       if (!home)
  51.         home = ".";
  52.         
  53.       expansion = name[1] == 0
  54.                   ? xstrdup (home) : concat3 (home, DIR_SEP_STRING, name + 2);
  55.     }
  56.   
  57.   /* If `~user' or `~user/', look up user in the passwd database.  */
  58.   else
  59.     {
  60.       struct passwd *p;
  61.       string user;
  62.       unsigned c = 2;
  63.       while (!IS_DIR_SEP (name[c]) && name[c] != 0)
  64.         c++;
  65.       
  66.       user = (string) xmalloc (c);
  67.       strncpy (user, name + 1, c - 1);
  68.       user[c - 1] = 0;
  69.       
  70.       /* We only need the cast here for those (deficient) systems
  71.          which do not declare `getpwnam' in <pwd.h>.  */
  72.       p = (struct passwd *) getpwnam (user);
  73.       free (user);
  74.  
  75.       /* If no such user, just use `.'.  */
  76.       home = p == NULL ? "." : p->pw_dir;
  77.       
  78.       expansion = name[c] == 0 ? xstrdup (home) : concat (home, name + c);
  79.     }
  80.   
  81.   /* We may return the same thing as the original, and then we might not
  82.      be returning a malloc-ed string.  */
  83.   return (string) expansion;
  84. #else
  85.   return name;
  86. #endif /* not HAVE_PWD_H */
  87. }
  88.  
  89. #ifdef TEST
  90.  
  91. void
  92. test_expand_tilde (const_string filename)
  93. {
  94.   string answer;
  95.   
  96.   printf ("Tilde expansion of `%s':\t", filename ? filename : "(null)");
  97.   answer = kpse_expand_tilde (filename);
  98.   puts (answer);
  99. }
  100.  
  101. int
  102. main ()
  103. {
  104.   string tilde_path = "tilde";
  105.  
  106.   test_expand_tilde ("");
  107.   test_expand_tilde ("none");
  108.   test_expand_tilde ("~root");
  109.   test_expand_tilde ("~");
  110.   test_expand_tilde ("foo~bar");
  111.   
  112.   return 0;
  113. }
  114.  
  115. #endif /* TEST */
  116.  
  117.  
  118. /*
  119. Local variables:
  120. standalone-compile-command: "gcc -g -I. -I.. -DTEST tilde.c kpathsea.a"
  121. End:
  122. */
  123.