home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / e_mac / e_getenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-14  |  1.7 KB  |  88 lines  |  [TEXT/CWIE]

  1. /* e_getenv.c */
  2. /* 11Sep95  e */
  3.  
  4. #include <stdlib.h>
  5. #include <Resources.h>
  6. #include "os_mac_str.h"
  7.  
  8. /* the temporary env shouldn't be big (~0..10 entries?)
  9.    so we'll use a linked list
  10. */
  11.  
  12. typedef struct enventry
  13. {
  14.   struct enventry *link;
  15.   Str255 name;
  16.   // the name is followed immediately by the c string value
  17. } enventry;
  18.  
  19. static enventry *mem_env_root = NULL;
  20.  
  21. static enventry *e_getenv_mem_guts(char *name)
  22. {
  23.   enventry *ep = mem_env_root;
  24.   Str255 pname;
  25.   c_to_p( name, pname );
  26.   while( ep != NULL )
  27.   {
  28.     if( compare_p_to_p( pname, ep->name ) == 0 )
  29.       return ep;
  30.     ep = ep->link;
  31.   }
  32.   return NULL;
  33. }
  34.  
  35. static char *e_getenv_mem(char *name)
  36. {
  37.   enventry *ep = e_getenv_mem_guts( name );
  38.   if( ep != NULL )
  39.     return (char *)&ep->name[ep->name[0]+1];
  40.   return NULL;
  41. }
  42.  
  43. static char *e_getenv_res(char *name)
  44. {
  45.   Handle h;
  46.   Str255 buf;
  47.  
  48.   c_to_p( name, buf );
  49.   h = GetNamedResource( 'ENV ', buf );
  50.   if( h == NULL ) return NULL;
  51.   MoveHHi(h);
  52.   HLock(h);
  53.   return (char *)*h;
  54. }
  55.  
  56. char *e_getenv(const char *name)
  57. {
  58.   char              *rslt = e_getenv_mem( (char *)name );
  59.   if( rslt == NULL ) rslt = e_getenv_res( (char *)name );
  60.   return( rslt );
  61. }
  62.  
  63. int e_setenv(const char *name, const char *text, int rewrite)
  64. {
  65.   enventry *ep = e_getenv_mem_guts( (char *)name );
  66.   if( ep != NULL )
  67.   {
  68.     if( rewrite )
  69.       return -1; // don't handle yet (doubly linked list?)
  70.     else
  71.       return 0;
  72.   }
  73.   else
  74.   {
  75.     int size = strlen(name) + strlen(text) + 6;
  76.     ep = (enventry *)NewPtr(size);
  77.     if( ep == NULL )
  78.       return -1;
  79.     ep->link = mem_env_root;
  80.     mem_env_root = ep;
  81.     c_to_p( (char *)name, ep->name );
  82.     strcpy( (char *)&ep->name[ep->name[0]+1], (char *)text );
  83.   }
  84.   return 0; 
  85. }
  86.  
  87. /* end */
  88.