home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / amiga / normal / getsetenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  1.8 KB  |  90 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <dos/dos.h>
  4. #include <dos/dosextens.h>
  5. #include <inline/stubs.h>
  6. #ifdef __OPTIMIZE__
  7. #include <inline/exec.h>
  8. #include <inline/dos.h>
  9. #else
  10. #include <clib/exec_protos.h>
  11. #include <clib/dos_protos.h>
  12. #endif
  13.  
  14. #include <stdlib.h>
  15.  
  16. void AddMemToTC_MEMENTRY(void *rv);
  17.  
  18. /* we have a small problem with getenv():
  19.    it returns a pointer to the contents of a environment-variable.
  20.    We don't know when the user has used the variable, so we have
  21.    to allocate memory for the environment-variable.
  22.    As we don't want to lose memory, we add the memory to the
  23.    TaskMemList, which is freed ad program termination.
  24.  
  25.    If a program calls genenv() for several times, the available
  26.    memory will get less and less.
  27.  
  28.    All environment variables will get truncated to 100 characters
  29. */
  30.  
  31. char *getenv(const char *name)
  32. {
  33.     LONG len;
  34.     UBYTE *mem;
  35.  
  36.     if( (mem=(char *)AllocVec(100,MEMF_PUBLIC|MEMF_CLEAR)))
  37.     {
  38.         len=GetVar((STRPTR) name, mem, 100, 0);
  39.         if(len != -1)
  40.         {
  41.                 /* memory will be freed at program termination */
  42.  
  43.             AddMemToTC_MEMENTRY(mem);
  44.  
  45.             return mem;
  46.         }
  47.         else
  48.         {
  49.             /* we had no success with our name */
  50.  
  51.             FreeVec(mem);
  52.             return NULL;
  53.         }
  54.     }
  55.  
  56.     return NULL;
  57. }
  58.  
  59. int setenv(const char *name, const char *value, int overwrite)
  60. {
  61.     UBYTE Buffer[100];
  62.  
  63.     if(!overwrite)
  64.     if(GetVar((STRPTR) name, Buffer, 100, 0)==-1)
  65.     {
  66.         overwrite=1;
  67.     }
  68.  
  69.     if(overwrite)
  70.         if(SetVar((STRPTR) name, (STRPTR) value, strlen(value), 0))
  71.             return 0;
  72.         else
  73.             return -1;
  74.     else
  75.         return 0;    /* we are successfull in doing nothing. Is this right ?*/
  76. }
  77.  
  78. #if 0
  79. int putenv(const char *string)
  80. {
  81.     /* ??? I don't know the contents vor value, see getenv.doc */
  82.     return setenv(string, value, 1);
  83. }
  84. #endif
  85.  
  86. void unsetenv(const char *name)
  87. {
  88.     DeleteVar((STRPTR) name, 0);
  89. }
  90.