home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / cvs-1.8.7-src.tgz / tar.out / fsf / cvs / lib / strippath.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  2KB  |  81 lines

  1. /* strippath.c -- remove unnecessary components from a path specifier
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21.  
  22. #if STDC_HEADERS || HAVE_STRING_H
  23. #include <string.h>
  24. /* An ANSI string.h and pre-ANSI memory.h might conflict. */
  25. #if !STDC_HEADERS && HAVE_MEMORY_H
  26. #include <memory.h>
  27. #endif /* not STDC_HEADERS and HAVE_MEMORY_H */
  28. #else /* not STDC_HJEADERS and not HAVE_STRING_H */
  29. #include <strings.h>
  30. /* memory.h and strings.h conflict on some systems. */
  31. #endif /* not STDC_HEADERS and not HAVE_STRING_H */
  32.  
  33. #include <stdio.h>
  34.  
  35. #if __STDC__
  36. static void remove_component(char *beginc, char *endc);
  37. void strip_trailing_slashes(char *path);
  38. #else
  39. static void remove_component();
  40. void strip_trailing_slashes();
  41. #endif /* __STDC__ */
  42.  
  43. /* Remove unnecessary components from PATH. */
  44.  
  45. void
  46. strip_path (path)
  47.      char *path;
  48. {
  49.   int stripped = 0;
  50.   char *cp, *slash;
  51.  
  52.   for (cp = path; (slash = strchr(cp, '/')) != NULL; cp = slash)
  53.     {
  54.       *slash = '\0';
  55.       if ((!*cp && (cp != path || stripped)) ||
  56.       strcmp(cp, ".") == 0 || strcmp(cp, "/") == 0)
  57.     {
  58.       stripped = 1;
  59.       remove_component(cp, slash);
  60.       slash = cp;
  61.     }
  62.       else
  63.     {
  64.       *slash++ = '/';
  65.     }
  66.     }
  67.   strip_trailing_slashes(path);
  68. }
  69.  
  70. /* Remove the component delimited by BEGINC and ENDC from the path */
  71.  
  72. static void
  73. remove_component (beginc, endc)
  74.      char *beginc;
  75.      char *endc;
  76. {
  77.   for (endc++; *endc; endc++)
  78.     *beginc++ = *endc;
  79.   *beginc = '\0';
  80. }
  81.