home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / cdrom / amicdrom / src / path.c < prev    next >
C/C++ Source or Header  |  1977-12-31  |  2KB  |  111 lines

  1. /* path.c:
  2.  *
  3.  * Path lists.
  4.  *
  5.  * ----------------------------------------------------------------------
  6.  * This code is (C) Copyright 1993,1994 by Frank Munkert.
  7.  * All rights reserved.
  8.  * This software may be freely distributed and redistributed for
  9.  * non-commercial purposes, provided this notice is included.
  10.  */
  11.  
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include <exec/memory.h>
  16.  
  17. #include <clib/exec_protos.h>
  18.  
  19. #ifdef LATTICE
  20. #include <pragmas/exec_pragmas.h>
  21. #endif
  22.  
  23. #include "generic.h"
  24.  
  25. typedef struct path_node {
  26.   unsigned long references;
  27.   char *name;
  28.   struct path_node *next;
  29. } t_path_node;
  30.  
  31. /* Append p_name to path list: */
  32.  
  33. t_path_list Append_Path_List (t_path_list p_list, char *p_name)
  34. {
  35.   t_path_node *node;
  36.  
  37.   if (!(node = AllocMem (sizeof (*node), MEMF_PUBLIC)))
  38.     return NULL;
  39.  
  40.   node->references = 1;
  41.   if (!(node->name = AllocMem (strlen (p_name) + 1, MEMF_PUBLIC))) {
  42.     FreeMem (node, sizeof (*node));
  43.     return NULL;
  44.   }
  45.  
  46.   strcpy (node->name, p_name);
  47.   node->next = Copy_Path_List (p_list, FALSE);
  48.   
  49.   return node;
  50. }
  51.  
  52. t_path_list Copy_Path_List (t_path_list p_src, int p_strip)
  53. {
  54.   t_path_node *node, *start;
  55.  
  56.   if (!p_src)
  57.     return NULL;
  58.  
  59.   start = p_strip ? p_src->next : p_src;
  60.  
  61.   for (node = start; node; node = node->next) {
  62.     node->references++;
  63.   }
  64.   
  65.   return start;
  66. }
  67.  
  68. void Free_Path_List (t_path_list p_list)
  69. {
  70.   t_path_node *node, *next;
  71.  
  72.   if (!p_list)
  73.     return;
  74.  
  75.   for (node = p_list; node; node = next) {
  76.     next = node->next;
  77.     if (--node->references == 0) {
  78.       FreeMem (node->name, strlen (node->name) + 1);
  79.       FreeMem (node, sizeof (*node));
  80.     }
  81.   }
  82. }
  83.  
  84. t_bool Path_Name_From_Path_List (t_path_list p_list, char *p_buffer,
  85.                      int p_buffer_length)
  86. {
  87.   int len;
  88.   t_path_node *node;
  89.  
  90.   if (!p_list) {
  91.     strcpy (p_buffer, ":");
  92.     return TRUE;
  93.   }
  94.   
  95.   /* calculate length: */
  96.   for (len=1, node=p_list; node; node = node->next)
  97.     len += strlen (node->name) + 1;
  98.  
  99.   if (len > p_buffer_length)
  100.     return FALSE;
  101.  
  102.   for (node=p_list; node; node = node->next) {
  103.     memcpy (p_buffer + len - strlen (node->name) - 1,
  104.             node->name, strlen (node->name));
  105.     p_buffer[len-1] = (node == p_list) ? 0 : '/';
  106.     len -= strlen (node->name) + 1;
  107.   }
  108.   p_buffer[0] = ':';
  109.   return TRUE;
  110. }
  111.