home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / envutil / envsearch.c < prev    next >
C/C++ Source or Header  |  1994-03-04  |  2KB  |  73 lines

  1. /*    envsearch - search environment for given string
  2.  
  3.     usage...
  4.         char buf[25];
  5.         envsearch("ALPHA",buf);        puts value of the environment
  6.                         variable ALPHA into buf
  7.  
  8.     functions called...
  9.  
  10.     _lmove(#bytes,
  11.         source_offset,source_segment,
  12.         target_offset,target_segment)    moves bytes
  13.     _showds()            returns current value of ds register
  14.     _showcs()            returns current value of cs register
  15.  
  16.     notes...
  17.         Compile with MAIN #defined to generate a standalone program
  18.     which searches the environment for the value of the first argument
  19.     (or PATH, by default).  Limitations...
  20.  
  21.         environment size:        64K
  22.         target variable name:        23
  23.         environment variable name:    98
  24.         target value:            98-sizeof(target)
  25.  
  26.     author...
  27.         James R. Van Zandt    (jrv @ mitre-bedford)
  28. */
  29.  
  30. envsearch(target,value) char *target,*value;
  31. {    char buf[100],*s,t[25],*env;
  32.     int nt, offset;
  33.  
  34.     s=t;
  35.     while(*target) *s++=toupper(*target++);
  36.     *s++= '='; *s=0;
  37.     nt = strlen(t);
  38.     offset=0;
  39.  
  40. /* DeSmet C sets up cs register to point 100H past the Program Segment
  41.    Prefix.  The word at offset 44 in the PSP points to the segment with
  42.    the environment */
  43.  
  44.     _lmove(2,44,_showcs()-0x10,&env,_showds()); /* get env. pointer */
  45.     while(1)
  46.         {_lmove(100,offset,env,buf,_showds()); /* get (part of) env. */
  47.         s=buf;
  48.         if(*s)
  49.             {/* printf("examining entry: %s \n",s); getchar(); */
  50.             if (strncmp(t,s,nt)==0) return (strcpy(value,s+nt));
  51.             }
  52.         else
  53.             {*value=0;    /* no value found */
  54.             return;
  55.             }
  56.         offset+=strlen(buf)+1;
  57.         }
  58. }
  59.  
  60.  
  61. #ifdef MAIN
  62.  
  63. char val[25],target[]="path";
  64.  
  65. main(argc,argv) int argc; char **argv;
  66. {    char *t;
  67.     if(argc>1) t=argv[1]; else t=target;
  68.     printf("searching environment for variable \"%s\" \n",t);
  69.     envsearch(t,val);
  70.     printf("the value is \"%s\" \n",val);
  71. }
  72. #endif
  73.