home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pathutil / mp.zip / MP.C < prev    next >
C/C++ Source or Header  |  1992-08-30  |  4KB  |  118 lines

  1. /* mp.c
  2.    Program to modify the path environment for MS-DOS.
  3.    Written by H. D. Todd, 12/26/90 */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <dos.h>
  9. #define MAXSTRS 100        /* increase this if you have more env strings */
  10. #define MAXPATH 500        /* max size of path string */
  11. #define PATHSEP ";"        /* separates node names in path */
  12.  
  13. void main(int argc, char *argv[])
  14.  
  15. {
  16.   int i, num, envsize, len, nstrs=0, nextra, pathindex;
  17.   char dowhat, varstg[MAXPATH], tmpstr[MAXPATH],
  18.         *strptr[MAXSTRS], far *envp, far *envptr, *subloc;
  19.   unsigned far *p_psp, far *p_env;
  20.   struct {
  21.          char link;
  22.          unsigned int owner_psp;
  23.          unsigned int blk_len;
  24.          } far *envmcb;
  25.  
  26.   if (argc < 2) {
  27.     fprintf(stderr, "Usage: mp [ -{p|a} newnodename | -r oldnodename newnodename]*\n");
  28.     exit(1); }
  29.  
  30. /* Construct the PSP pointers */
  31.   p_psp = MK_FP(_psp,0x16);    /*pointer to parent's PSP*/
  32.   p_env = MK_FP(*p_psp,0x2c);  /*pointer to parent's environment ptr*/
  33.   envptr = MK_FP(*p_env,0x00); /*pointer to parent's environment */
  34.   envmcb = MK_FP(*p_env-1,0x00); /* pointer to parent env MCB */
  35.   envsize = (*envmcb).blk_len<<4;
  36.  
  37. /* copy strings from parent's environment (FAR) to local strings */
  38.    envp = envptr;
  39.    while (*envp) {      /* env strings terminated by second null */
  40.          int len;
  41.          char far *fp;
  42.          char *lp;
  43.          for (len=0, fp=envp; *fp; fp++) len++;
  44.          lp = strptr[nstrs++] = malloc(len+1);
  45.          for ( ; (*lp++=*envp++); );
  46.          }
  47.  
  48.  
  49. /*  find the PATH= string among the env vars */
  50.    for (pathindex=0; pathindex<nstrs; pathindex++) if (strnicmp(strptr[pathindex],"PATH=",5) ==0 ) break;
  51.    if (pathindex>=nstrs) {
  52.        fprintf(stderr, "MP: Can't find path var!\n");
  53.        exit(1);
  54.    }
  55.    strcpy(varstg,strptr[pathindex]);
  56.  
  57. /*  now do what the switches tell us to do */
  58.    while (--argc > 0 ) {
  59.     if ( ( (dowhat=(*++argv)[0])) != '-') {
  60.         fprintf(stderr, "MP: begin switches with '-', as in '-%c'\n", dowhat);
  61.              exit(1);
  62.     }
  63.     dowhat = tolower(*++argv[0]);
  64.     if (dowhat!='p' && dowhat!='a' && dowhat!='r') {
  65.        fprintf(stderr, "MP: invalid switch -%c\n", dowhat);
  66.        exit(1);
  67.     };
  68.     if (--argc <=0) {
  69.         fprintf(stderr, "MP: switch -%c missing node name\n", dowhat);
  70.         exit(1);
  71.     }
  72.     switch (dowhat) {
  73.         case 'a':    strcat(varstg,PATHSEP);
  74.                 strcat(varstg,*++argv);
  75.                 break;
  76.         case 'p':    strcpy(tmpstr,"PATH=");
  77.                 strcat(tmpstr,*++argv);
  78.                 strcat(tmpstr,PATHSEP);
  79.                 strcat(tmpstr,strpbrk(varstg,"=")+1);
  80.                 strcpy(varstg,tmpstr);
  81.                 break;
  82.         case 'r':    if ( (subloc=strstr(varstg,*++argv)) == NULL) {
  83.                     fprintf(stderr,"MP: pathnode %s not found in current path", *argv);
  84.                     fprintf(stderr," (case-sensitive!)\n");
  85.                     fprintf(stderr,"    replacement ignored");
  86.                     while ( *(argv+1)[0] != '-') {
  87.                         ++argv;
  88.                         argc--;
  89.                     }
  90.                     break;
  91.                     };
  92.                 strcpy(tmpstr,subloc+strlen(*argv));
  93.                 if (*(argv+1)[0] != '-') {
  94.                     strcpy(subloc,*++argv);
  95.                     --argc;
  96.                     }
  97.                   else subloc = '\0';
  98.                 strcat(varstg,tmpstr);
  99.                 break;
  100.     };
  101.    }
  102.  
  103. /*  make sure we can copy it all back into the parent's env block */    
  104.     strptr[pathindex] = &varstg;
  105.     for (i=0, len=0; i<nstrs; i++) len += strlen(strptr[i]) + 1;
  106.     if (len+1>envsize)  {
  107.         fprintf(stderr,"MP: Env block too small to store updated PATH string\n");
  108.         exit(1);
  109.         }
  110.       else {
  111. /*  OK, do it */
  112.         for (i=0; i<nstrs; i++) {
  113.             for ( ; (*envptr++ = *strptr[i]++) ; ) ;
  114.             *envptr = '\0';
  115.         }
  116.       }
  117. }
  118.