home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / msdos / programm / 8816 < prev    next >
Encoding:
Text File  |  1992-08-26  |  7.2 KB  |  182 lines

  1. Path: sparky!uunet!wupost!sdd.hp.com!nigel.msen.com!yale.edu!yale!news.wesleyan.edu!albatross.wcc.wesleyan.edu!hdtodd
  2. Newsgroups: comp.os.msdos.programmer
  3. Subject: Re: How can you set an environment variable in a program?
  4. Message-ID: <hdtodd.3.714836531@wccnet.wcc.wesleyan.edu>
  5. From: hdtodd@wccnet.wcc.wesleyan.edu (H. D. Todd)
  6. Date: Wed, 26 Aug 1992 13:42:11 GMT
  7. References: <690@aardvark.Rain.COM> <7923PB2w164w@jaflrn.UUCP>
  8. Organization: Wesleyan University, Middletown, CT USA
  9. Keywords: variable, C
  10. Nntp-Posting-Host: albatross.wcc.wesleyan.edu
  11. Lines: 169
  12.  
  13. In article <7923PB2w164w@jaflrn.UUCP> Jon Freivald <jaf@jaflrn.UUCP> writes:
  14. >Subject: Re: How can you set an environment variable in a program?
  15.  
  16. >OK, I don't have TC 2.0, but I do have TC++ 1.0 & 2.0 -- no such critter
  17. >as "setenv()" and "putenv()" only changes the environment for the
  18. >current process -- as soon as you exit, the variable is gone...
  19.  
  20.     I sent this out a couple of weeks ago in response to a similar 
  21. inquiry.  I should submit it to one of the archives (is SIMTEL the best 
  22. starting point?).  It's a Turbo C program to modify the path (hence "mp") in 
  23. the root DOS command.com process.  You could modify it to do whatever else 
  24. you needed to do (though it's pretty handy on its own merits!).
  25.  
  26.                         David Todd
  27.  
  28.  
  29.                            MP -- Modify Path
  30. USAGE
  31.     MP [-{p|a} newpathnode | -r oldpathnode [newpathnode] ]*
  32. FUNCTION
  33.     MP modifies the working path for MS DOS environments by
  34. prepending ("-p") or appending ("-a") a new path node to an existing path
  35. environment or by replacing ("-r") an old path node ("oldpathnode") with
  36. a new path node ("newpathnode") within the existing path.  For the -r
  37. option, the field specifying "newpathnode" can be empty, thus deleting
  38. "oldpathnode" from the search path entirely.
  39.     Note that for the -r switch, the new path node that might replace 
  40. the old path node in the PATH list may be omitted: if so, the old path node
  41. is removed from the PATH list but is not replaced.  Since switches are 
  42. processed in the order entered, a -r followed by a -p or -a can be used
  43. to move a node from the interior of a PATH list to the beginning or end
  44. of that list.
  45.     This program operates on the PARENT process environment, thus
  46. eliminating the requirement to use a batch file or enter a complete new
  47. path specification to make minor modifications to the working path.
  48. IMPLEMENTATION
  49.     MP copies the environment block from the parent process to local
  50. strings, modifies the local string for PATH, then copies the complete
  51. set of environment strings back to the parent environment block.
  52. ERROR CONDITIONS
  53.     MP will report an error and leave the path unmodified if there
  54. is insufficient room in the environment block for the modified PATH
  55. environment variable.  In this situation, the path could not be
  56. modified even at the command level.  To prepare for future use of MP or
  57. changes to the path by other methods in this case, increase the size of
  58. the environment block by modifying the CONFIG.SYS file line specifying 
  59.     COMSPEC=dd:COMMAND.COM/e:nnn
  60. to increase (or specify) the /e: parameter for the size of the
  61. environment block.
  62.     MP also reports if a switch is given with no nodename to process, if 
  63. an invalid switch is given, or if switches are not prefixed with '-'.
  64. LANGUAGE AND COMPATIBILITY
  65.     MP is written in TURBOC and has been tested with DOS 3.3 and DOS
  66. 4.01.  Since it does not use DOS systems calls to effect the
  67. modifications (and cannot, since DOS does not provide a mechanism for a
  68. child process to change the environment block of its parent), MP may be
  69. incompatible with future versions of DOS.
  70. AUTHOR
  71.     MP was written by H. D. Todd, Wesleyan University, December,
  72. 1990 and is supplied in source-code form.  The author would appreciate
  73. receiving bug reports and modifications (send to
  74. HDTodd@eagle.wesleyan.edu or HDTodd@wesleyan.bitnet).
  75.  
  76. /* mp.c
  77.    Program to modify the path environment for MS-DOS.
  78.    Written by H. D. Todd, Wesleyan University, 12/26/90 */
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <string.h>
  82. #include <dos.h>
  83. #define MAXSTRS 100        /* increase this if you have more env strings */
  84. #define MAXPATH 500        /* max size of path string */
  85. #define PATHSEP ";"        /* separates node names in path */
  86. void main(int argc, char *argv[])
  87. {
  88.   int i, num, envsize, len, nstrs=0, nextra, pathindex;
  89.   char dowhat, varstg[MAXPATH], tmpstr[MAXPATH],
  90.         *strptr[MAXSTRS], far *envp, far *envptr, *subloc;
  91.   unsigned far *p_psp, far *p_env;
  92.   struct {
  93.          char link;
  94.          unsigned int owner_psp;
  95.          unsigned int blk_len;
  96.          } far *envmcb;
  97.   if (argc < 2) {
  98.     fprintf(stderr, "Usage: mp [ -{p|a} newnodename | -r oldnodename newnodename]*\n");
  99.     exit(1); }
  100. /* Construct the PSP pointers */
  101.   p_psp = MK_FP(_psp,0x16);    /*pointer to parent's PSP*/
  102.   p_env = MK_FP(*p_psp,0x2c);  /*pointer to parent's environment ptr*/
  103.   envptr = MK_FP(*p_env,0x00); /*pointer to parent's environment */
  104.   envmcb = MK_FP(*p_env-1,0x00); /* pointer to parent env MCB */
  105.   envsize = (*envmcb).blk_len<<4;
  106. /* copy strings from parent's environment (FAR) to local strings */
  107.    envp = envptr;
  108.    while (*envp) {      /* env strings terminated by second null */
  109.          int len;
  110.          char far *fp;
  111.          char *lp;
  112.          for (len=0, fp=envp; *fp; fp++) len++;
  113.          lp = strptr[nstrs++] = malloc(len+1);
  114.          for ( ; (*lp++=*envp++); );
  115.          }
  116. /*  find the PATH= string among the env vars */
  117.    for (pathindex=0; pathindex<nstrs; pathindex++) if (strnicmp(strptr[pathindex],"PATH=",5) ==0 ) break;
  118.    if (pathindex>=nstrs) {
  119.        fprintf(stderr, "MP: Can't find path var!\n");
  120.        exit(1);
  121.    }
  122.    strcpy(varstg,strptr[pathindex]);
  123. /*  now do what the switches tell us to do */
  124.    while (--argc > 0 ) {
  125.     if ( ( (dowhat=(*++argv)[0])) != '-') {
  126.         fprintf(stderr, "MP: begin switches with '-', as in '-%c'\n", dowhat);
  127.              exit(1);
  128.     }
  129.     dowhat = tolower(*++argv[0]);
  130.     if (dowhat!='p' && dowhat!='a' && dowhat!='r') {
  131.        fprintf(stderr, "MP: invalid switch -%c\n", dowhat);
  132.        exit(1);
  133.     };
  134.     if (--argc <=0) {
  135.         fprintf(stderr, "MP: switch -%c missing node name\n", dowhat);
  136.         exit(1);
  137.     }
  138.     switch (dowhat) {
  139.         case 'a':    strcat(varstg,PATHSEP);
  140.                 strcat(varstg,*++argv);
  141.                 break;
  142.         case 'p':    strcpy(tmpstr,"PATH=");
  143.                 strcat(tmpstr,*++argv);
  144.                 strcat(tmpstr,PATHSEP);
  145.                 strcat(tmpstr,strpbrk(varstg,"=")+1);
  146.                 strcpy(varstg,tmpstr);
  147.                 break;
  148.         case 'r':    if ( (subloc=strstr(varstg,*++argv)) == NULL) {
  149.                     fprintf(stderr,"MP: pathnode %s not found in current path\n", *argv);
  150.                     fprintf(stderr,"    replacement ignored");
  151.                     while ( *(argv+1)[0] != '-') {
  152.                         ++argv;
  153.                         argc--;
  154.                     }
  155.                     break;
  156.                     };
  157.                 strcpy(tmpstr,subloc+strlen(*argv));
  158.                 if (*(argv+1)[0] != '-') {
  159.                     strcpy(subloc,*++argv);
  160.                     --argc;
  161.                     }
  162.                   else subloc = '\0';
  163.                 strcat(varstg,tmpstr);
  164.                 break;
  165.     };
  166.    }
  167. /*  make sure we can copy it all back into the parent's env block */    
  168.     strptr[pathindex] = &varstg;
  169.     for (i=0, len=0; i<nstrs; i++) len += strlen(strptr[i]) + 1;
  170.     if (len+1>envsize)  {
  171.         fprintf(stderr,"MP: Env block too small to store updated PATH string\n");
  172.         exit(1);
  173.         }
  174.       else {
  175. /*  OK, do it */
  176.         for (i=0; i<nstrs; i++) {
  177.             for ( ; (*envptr++ = *strptr[i]++) ; ) ;
  178.             *envptr = '\0';
  179.         }
  180.       }
  181. }
  182.