home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / c / csh4.zip / ENV.C < prev    next >
C/C++ Source or Header  |  1985-09-04  |  3KB  |  163 lines

  1. #include <stdio.h>
  2.  
  3. #define ENVSIZE 4000
  4. extern int _PSP;
  5. void *calloc();
  6. char *environment=NULL, *str_upper();
  7. int env_paragraph;
  8. char *next_env;
  9. #define envlimit(a) &a[ENVSIZE-1]
  10.  
  11. #ifdef MAIN
  12. main
  13. #else
  14. set
  15. #endif
  16. (argc,argv)
  17.     char *argv[];
  18. {
  19.     if (!environment)
  20.         init_env();
  21.     if (argc == 1)
  22.     {
  23.         show_env();
  24.         return 0;
  25.     }
  26.     while(--argc)
  27.     {
  28.         add_env(str_upper(*(++argv)));
  29.     }
  30.     return 0;
  31. }
  32.  
  33. char *
  34. str_upper(c)
  35.     register char *c;
  36. {
  37.     register char *save = c;
  38.     while(*c)
  39.     {
  40.         *c = toupper(*c);
  41.         c++;
  42.     }
  43.     return save;
  44. }
  45.  
  46. char *
  47. str_lower(c)
  48.     register char *c;
  49. {
  50.     register char *save = c;
  51.     while(*c)
  52.     {
  53.         *c = tolower(*c);
  54.         c++;
  55.     }
  56.     return save;
  57. }
  58. init_env()
  59. {
  60.     extern unsigned _dsval;    /* current data segment register value */
  61.     long fudgefactor;
  62.     register int c;
  63.     unsigned envseg;
  64.     unsigned offset = 0;
  65.     envseg = peekw(0x2c,_PSP);
  66.     environment = calloc(1,ENVSIZE+16);
  67.     fudgefactor = (long)_dsval << 4;    /* convert to absolute paragraph */
  68.     fudgefactor += (unsigned)environment + 16;
  69.     fudgefactor &= 0xFFFF0L;
  70.     env_paragraph = (int)((fudgefactor >>4) & 0xFFFF);
  71.     environment = (char *) (fudgefactor - (long)(_dsval << 4));
  72.     next_env = environment;
  73.     while (c = peekb(offset,envseg))
  74.     {
  75.         while (c = peekb(offset++,envseg))
  76.         {
  77.             *next_env++ = c;
  78.         }
  79.         *next_env++ = '\0';
  80.     }
  81. }
  82.  
  83. show_env()
  84. {
  85.     register char *env;
  86.     char c;
  87.     for (env = environment;*env;)
  88.     {
  89.         while (c = *env++)
  90.             write(1,&c,1);
  91.         crlf();
  92.     }
  93. }
  94.  
  95. static char *enverr = "No more environment space\r\n";
  96. static char *enverr2 = "Improper environment string format!!\r\n";
  97.  
  98. add_env(string)
  99.     char *string;
  100.  
  101. {
  102.     char *env_copy, *new, *index();
  103.     char *old = environment;
  104.     char *name_end,*new_name_end;
  105.     int added = 0;
  106.     int namelen;
  107.  
  108.     if (NULL == (env_copy = new = calloc(1,ENVSIZE)))
  109.     {
  110.         write(2,enverr,strlen(enverr));
  111.         return -1;
  112.     }
  113.  
  114.     while (*old)
  115.     {
  116.         if ( NULL == (name_end = index(old,'=')) || 
  117.             NULL == (new_name_end = index(string,'=')) 
  118.         )
  119.         {
  120.             write(2,enverr2,strlen(enverr2));
  121.             free(env_copy);
  122.             return -1;
  123.         }
  124.         namelen = (int)(name_end - old);
  125.         if (!strncmp(old,string,namelen))
  126.         {
  127.             if (new_name_end[1])
  128.             {
  129.                 /* if we don't have a string of the form name= */
  130.                 /* copy new string instead of old string */
  131.                 strcpy(new,string);
  132.             }
  133.             else
  134.             /* if we have a set name= with no string then we want
  135.                to remove the string from the environment
  136.              */
  137.                 ;
  138.             added++;
  139.         }
  140.         else
  141.         {
  142.             strcpy(new,old);
  143.         }
  144.         new = &new[strlen(new)+1];
  145.         old = &old[strlen(old)+1];
  146.         if (new >= envlimit(new))
  147.         {
  148.             write(2,enverr,strlen(enverr));
  149.             free(env_copy);
  150.             return -1;
  151.         }
  152.     }
  153.     if (!added)
  154.     {
  155.         strcpy(new,string);
  156.     }
  157.     new = &new[strlen(new)+1];
  158.     /* copy the copy back to the environment */
  159.     movmem(env_copy,environment,(int)(new-env_copy)+2);
  160.     free(env_copy);
  161.     return 0;
  162. }
  163.