home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / kpathsea / truncate.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  2KB  |  58 lines

  1. /* truncate.c: truncate too-long components in a filename.
  2.  
  3. Copyright (C) 1993, 95 Karl Berry.
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library 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 GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  18.  
  19. #include <kpathsea/config.h>
  20.  
  21. #include <kpathsea/c-namemx.h>
  22. #include <kpathsea/c-pathch.h>
  23. #include <kpathsea/c-pathmx.h>
  24. #include <kpathsea/truncate.h>
  25.  
  26.  
  27. /* Truncate any too-long components in NAME, returning the result.  It's
  28.    too bad this is necessary.  See comments in readable.c for why.  */
  29.  
  30. string
  31. kpse_truncate_filename P1C(const_string, name)
  32. {
  33.   unsigned c_len = 0;        /* Length of current component.  */
  34.   unsigned ret_len = 0;      /* Length of constructed result.  */
  35.   
  36.   /* Allocate enough space.  */
  37.   string ret = (string) xmalloc (strlen (name) + 1);
  38.  
  39.   for (; *name; name++)
  40.     {
  41.       if (IS_DIR_SEP (*name) || IS_DEVICE_SEP (*name))
  42.         { /* At a directory delimiter, reset component length.  */
  43.           c_len = 0;
  44.         }
  45.       else if (c_len > NAME_MAX)
  46.         { /* If past the max for a component, ignore this character.  */
  47.           continue;
  48.         }
  49.  
  50.       /* Copy this character.  */
  51.       ret[ret_len++] = *name;
  52.       c_len++;
  53.     }
  54.   ret[ret_len] = 0;
  55.  
  56.   return ret;
  57. }
  58.