home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / putenv.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  1KB  |  75 lines

  1. /* functions for manipulating the environment */
  2. /* written by Eric R. Smith and placed in the public domain */
  3.  
  4. #include <stddef.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #ifndef _COMPILER_H
  8. #include <compiler.h>
  9. #endif
  10.  
  11. extern char ** environ;
  12.  
  13. static void del_env __PROTO((const char *strng));
  14.  
  15. static void
  16. del_env(strng)
  17.     const char *strng;
  18. {
  19.     char **var;
  20.     char *name;
  21.     size_t len = 0;
  22.  
  23.     if (!environ) return;
  24.  
  25. /* find the length of "tag" in "tag=value" */
  26.     for (name = (char *)strng; *name && (*name != '='); name++)
  27.         len++;
  28.  
  29. /* find the tag in the environment */
  30.     for (var = environ; (name = *var) != NULL; var++) {
  31.         if (!strncmp(name, strng, len) && name[len] == '=')
  32.             break;
  33.     }
  34.  
  35. /* if it's found, move all the other environment variables down by 1 to
  36.    delete it
  37.  */
  38.     if (name) {
  39.         while (name) {
  40.             name = var[1];
  41.             *var++ = name;
  42.         }
  43.     }
  44. }
  45.  
  46. int
  47. putenv(strng)
  48.     char *strng;
  49. {
  50.     int i = 0;
  51.     char **e;
  52.  
  53.     del_env(strng);
  54.  
  55.     if (!environ)
  56.         e = (char **) malloc(2*sizeof(char *));
  57.     else {
  58.         while(environ[i]) i++ ;
  59.         e = (char **) malloc((i+2)*sizeof(char *));
  60.         if (!e) {
  61.             return -1;
  62.         }
  63.         bcopy(environ, e, (i+1)*sizeof(char *));
  64.         free(environ);
  65.         environ = e;
  66.     }
  67.     if (!e)
  68.         return -1;
  69.  
  70.     environ = e;
  71.     environ[i] = strng;
  72.     environ[i+1] = 0;
  73.     return 0;
  74. }
  75.