home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / find-3.8-src.lha / src / amiga / find-3.8 / lib / nextelem.c < prev    next >
C/C++ Source or Header  |  1993-03-26  |  2KB  |  103 lines

  1. /* Return the next element of a path.
  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. /* Written by David MacKenzie (djm@gnu.ai.mit.edu),
  19.    inspired by John P. Rouillard (rouilj@cs.umb.edu).  */
  20.  
  21. #include <stdio.h>
  22. #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
  23. #include <string.h>
  24. #ifndef index
  25. #define index strchr
  26. #endif
  27. #else
  28. #include <strings.h>
  29. #endif
  30.  
  31. char *strdup ();
  32. void free ();
  33.  
  34. /* Return the next element of a colon-separated path.
  35.    A null entry in the path is equivalent to "." (the current directory).
  36.  
  37.    If NEW_PATH is non-NULL, set the path and return NULL.
  38.    If NEW_PATH is NULL, return the next item in the string, or
  39.    return NULL if there are no more elements.  */
  40.  
  41. char *
  42. next_element (new_path)
  43.      char *new_path;
  44. {
  45.   static char *path = NULL;    /* Freshly allocated copy of NEW_PATH.  */
  46.   static char *end;        /* Start of next element to return.  */
  47.   static int final_colon;    /* If zero, path didn't end with a colon.  */
  48.   char *start;            /* Start of path element to return.  */
  49.  
  50.   if (new_path)
  51.     {
  52.       if (path)
  53.     free (path);
  54.       end = path = strdup (new_path);
  55.       final_colon = 0;
  56.       return NULL;
  57.     }
  58.  
  59.   if (*end == '\0')
  60.     {
  61.       if (final_colon)
  62.     {
  63.       final_colon = 0;
  64.       return ".";
  65.     }
  66.       return NULL;
  67.     }
  68.  
  69.   start = end;
  70.   final_colon = 1;        /* Maybe there will be one.  */
  71.  
  72.   end = index (start, ':');
  73.   if (end == start)
  74.     {
  75.       /* An empty path element.  */
  76.       *end++ = '\0';
  77.       return ".";
  78.     }
  79.   else if (end == NULL)
  80.     {
  81.       /* The last path element.  */
  82.       end = index (start, '\0');
  83.       final_colon = 0;
  84.     }
  85.   else
  86.     *end++ = '\0';
  87.  
  88.   return start;
  89. }
  90.  
  91. #ifdef TEST
  92. int
  93. main ()
  94. {
  95.   char *p;
  96.  
  97.   next_element (getenv ("PATH"));
  98.   while (p = next_element (NULL))
  99.     puts (p);
  100.   exit (0);
  101. }
  102. #endif /* TEST */
  103.