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 / default.c < prev    next >
C/C++ Source or Header  |  1993-08-01  |  3KB  |  118 lines

  1. /* default.c: Expand extra colons.
  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/default.h>
  23.  
  24.  
  25. /* Check for leading colon first, then trailing, then doubled, since
  26.    that is fastest.  Usually it will be leading or trailing.  */
  27.  
  28. string 
  29. kpse_expand_default P2C(const_string, path,  const_string, fallback)
  30. {
  31.   unsigned path_length;
  32.   string expansion;
  33.   
  34.   /* The default path better not be null.  */
  35.   assert (fallback);
  36.   
  37.   if (path == NULL)
  38.     expansion = (string) fallback;
  39.  
  40.   /* Solitary or leading :?  */
  41.   else if (IS_ENV_SEP (*path))
  42.     {
  43.       expansion = path[1] == 0 ? (string) fallback : concat (fallback, path);
  44.     }
  45.  
  46.   /* Sorry about the assignment in the middle of the expression, but
  47.      conventions were made to be flouted and all that.  I don't see the
  48.      point of calling strlen twice or complicating the logic just to
  49.      avoid the assignment (especially now that I've pointed it out at
  50.      such great length).  */
  51.   else if (path[(path_length = strlen (path)) - 1] == ENV_SEP)
  52.     expansion = concat (path, fallback);
  53.  
  54.   /* OK, not leading or trailing.  Check for doubled.  */
  55.   else
  56.     {
  57.       const_string loc;
  58.  
  59.       /* What we'll return if we find none.  */
  60.       expansion = (string) path;
  61.  
  62.       for (loc = path; *loc && expansion == path; loc++)
  63.         {
  64.           if (IS_ENV_SEP (loc[0]) && IS_ENV_SEP (loc[1]))
  65.             { /* We have a doubled colon.  */
  66.               expansion = xmalloc (path_length + strlen (fallback) + 1);
  67.               
  68.               /* Copy stuff up to and including the first colon.  */
  69.               strncpy (expansion, path, loc - path + 1);
  70.               expansion[loc - path + 1] = 0;
  71.               
  72.               /* Copy in FALLBACK, and then the rest of PATH.  */
  73.               strcat (expansion, fallback);
  74.               strcat (expansion, loc + 1);
  75.             }
  76.         }
  77.     }
  78.   
  79.   return expansion;
  80. }
  81.  
  82. #ifdef TEST
  83.  
  84. void
  85. test_expand_default (const_string path, const_string def)
  86. {
  87.   string answer;
  88.   
  89.   printf ("Expansion of `%s':\t", path ? path : "(null)");
  90.   answer = kpse_expand_default (path, def);
  91.   puts (answer);
  92. }
  93.  
  94. int
  95. main ()
  96. {
  97.   string default_path = "default";
  98.  
  99.   test_expand_default (NULL, default_path);
  100.   test_expand_default ("", default_path);
  101.   test_expand_default ("none", default_path);
  102.   test_expand_default (":", default_path);
  103.   test_expand_default (":first", default_path);
  104.   test_expand_default ("last:", default_path);
  105.   test_expand_default ("middle::elddim", default_path);
  106.   
  107.   return 0;
  108. }
  109.  
  110. #endif /* TEST */
  111.  
  112.  
  113. /*
  114. Local variables:
  115. standalone-compile-command: "gcc -g -I. -I.. -DTEST default.c kpathsea.a"
  116. End:
  117. */
  118.