home *** CD-ROM | disk | FTP | other *** search
/ Computer Club Elmshorn Atari PD / CCE_PD.iso / pc / 0600 / CCE_0678.ZIP / CCE_0678.PD / E_GEM135 / SOURCE / ENV.C < prev    next >
C/C++ Source or Header  |  1993-11-20  |  2KB  |  119 lines

  1.  
  2. #ifdef __MINT_LIB__
  3. #include <basepage.h>
  4. #define _BasPag    _base
  5. #endif
  6.  
  7. #include "e_gem.h"
  8.  
  9. static char    *envbeg = 0l;
  10.  
  11. static void copyenv(char *,char *);
  12. static char *getvar(const char *);
  13.  
  14. char *getenv(const char *var)
  15. {
  16.     reg char *s = _BasPag->p_env;
  17.     reg const char *v;
  18.     
  19.     if (s)
  20.         while (*s)
  21.         {
  22.             for (v=var; (*s) && (*s++ == *v++); )
  23.                 if ((*s == '=') && (*v == '\0'))
  24.                     return(++s);
  25.             while (*s++);
  26.         }
  27.     return(NULL);
  28. }
  29.  
  30. int    putenv(const char *entry)
  31. {
  32.     reg char *d,*s,*e;
  33.     reg long envlen = 0;
  34.     reg unsigned l,new;
  35.     
  36.     s = _BasPag->p_env;
  37.     if ((s != NULL) && (*s))
  38.     {
  39.         while (*s)
  40.         {
  41.             while (*s++);
  42.         }
  43.         envlen = s - _BasPag->p_env;
  44.     }
  45.     
  46.     if (envbeg == NULL)
  47.     {
  48.         s = _BasPag->p_env;
  49.         if ((envbeg = malloc (envlen + 2) ) == NULL)
  50.             return(FALSE);
  51.         if ((s != NULL) && (*s))
  52.             copyenv (s, envbeg);
  53.         else
  54.             envbeg[0] = envbeg[1] = '\0';
  55.         _BasPag->p_env = envbeg;
  56.     }
  57.     
  58.     if ((d = s = getvar(entry)) != NULL)
  59.     {
  60.         while (*s++);
  61.         envlen -= s - d;
  62.         
  63.         if (*s)
  64.             copyenv (s, d);
  65.         else
  66.             d[0] = d[1] = '\0';
  67.     }
  68.     
  69.     for (e = (char *) entry, new = l = 0; *e++; l++)
  70.         if (*e == '=')
  71.             new = l;
  72.     
  73.     if (new)
  74.         envlen += l + 1;
  75.         
  76.     if ((d = malloc (envlen + 2)) == NULL)
  77.         return (FALSE);
  78.     
  79.     copyenv(envbeg, d);
  80.     free(envbeg);
  81.     envbeg = d;
  82.     
  83.     if (new)
  84.     {
  85.         while (*d)
  86.             while (*d++);
  87.         e = (char *) entry;
  88.         while ((*d++ = *e++) != 0);
  89.         *d = 0;
  90.     }
  91.     
  92.     _BasPag->p_env = envbeg;
  93.     return (TRUE);
  94. }
  95.  
  96. static char *getvar(const char *var)
  97. {
  98.     reg char *r,*s = envbeg;
  99.     reg const char *v;
  100.     
  101.     while (*s)
  102.     {
  103.         for ( r = s, v = var; (*s) && (*s++ == *v++); )
  104.             if ((*s == '=') && ((*v == '=') || (*v == '\0')))
  105.                 return (r);
  106.         while (*s++);
  107.     }
  108.     return (NULL);
  109. }
  110.  
  111. static void copyenv(char *s, char *d)
  112. {
  113.     do
  114.     {
  115.         while ((*d++ = *s++) != '\0');
  116.     } while (*s);
  117.     *d = '\0';
  118. }
  119.