home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / RAMDISK / SRDSK141.ZIP / ENV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-06  |  2.9 KB  |  122 lines

  1. /* ReSizeable RAMDisk - environment utilities
  2. ** Copyright (c) 1992 Marko Kohtala
  3. */
  4.  
  5. #include "srdisk.h"
  6. #include <stdio.h>
  7. #include <dos.h>
  8. #include <string.h>
  9.  
  10. /*
  11.    envptr - returns pointer to parent command.com's copy of the environment
  12.    Provided by Doug Dougherty, original source by S. Palmer.
  13.    Heavily modified for srdisk use.
  14. */
  15.  
  16. char _seg *envptr(int *size)
  17. {
  18.     word parent_p;
  19.  
  20. /*  memory control block */
  21.  
  22.     struct MCB {
  23.         byte id;
  24.         word owner;
  25.         word size;
  26.     } _seg *mcb = NULL;
  27.  
  28.     parent_p = peek(_psp,0x16);     /* find pointer to parent in psp */
  29.     if (!parent_p)          /* If DOS 1.0 or something and no parent... */
  30.         return NULL;
  31.     for (;;) {
  32.         if (peek(parent_p,0x2c) == 0) {
  33.             if (peek(parent_p,0x16) != parent_p)
  34.                 parent_p = peek(parent_p,0x16);
  35.             else
  36.                 return NULL;
  37.         }
  38.         else {
  39.            mcb = (struct MCB _seg *)(peek(parent_p,0x2c) - 1);
  40.            break;
  41.         }
  42.     }
  43.     *size = mcb->size * 16;
  44.     return (char _seg *)(FP_SEG(mcb) + 1);
  45. }
  46.  
  47. /*
  48.    msetenv - place an environment variable in command.com's copy of
  49.              the envrionment.
  50.    Provided by Doug Dougherty, original source by S. Palmer.
  51. */
  52. int msetenv(char *var, char *value)
  53. {
  54.     char _seg *env;
  55.     char near *env1, near *env2;
  56.     char near *cp;
  57.     int size;
  58.     int l;
  59.  
  60.     env = envptr(&size);
  61.     if (!env) return -2;    /* Return error if no environment found */
  62.  
  63.     env1 = env2 = 0;
  64.  
  65.     l = strlen(var);
  66.     strupr(var);
  67.  
  68.     /*
  69.        Delete any existing variable with the name (var).
  70.     */
  71.     while (*(env+env2)) {
  72.         if ((_fstrncmp(var,(env+env2),l) == 0) && ((env+env2)[l] == '=')) {
  73.             cp = env2 + _fstrlen((env+env2)) + 1;
  74.             _fmemcpy((env+env2),(env+cp),size-(cp-env1));
  75.         }
  76.         else {
  77.             env2 += _fstrlen((env+env2)) + 1;
  78.         }
  79.     }
  80.  
  81.     /*
  82.        If the variable fits, shovel it in at the end of the envrionment.
  83.     */
  84.     if (_fstrlen(value) && (size-(env2-env1)) >= (l + _fstrlen(value) + 3)) {
  85.         _fstrcpy((env+env2),var);
  86.         _fstrcat((env+env2),"=");
  87.         _fstrcat((env+env2),value);
  88.         (env+env2)[_fstrlen(env+env2)+1] = 0;
  89.         return 0;
  90.     }
  91.  
  92.     /*
  93.        Return error indication if variable did not fit.
  94.     */
  95.     return -1;
  96. }
  97.  
  98. void set_env()
  99. {
  100.     struct config_s far *conf = mainconf;
  101.     char var[] = "SRDISK1";
  102.     char drive[] = "A";
  103.     int err;
  104.  
  105.     if (verbose > 1) puts("");
  106.     do {
  107.       drive[0] = conf->drive;
  108.  
  109.       err = msetenv(var, drive);
  110.       if (err == -1)
  111.         fatal("Not enough environment space");
  112.       if (err == -2)
  113.         fatal("No environment found to modify");
  114.  
  115.       if (verbose > 1) printf("Set %s=%s\n", var, drive);
  116.  
  117.       var[6]++;
  118.       conf = conf_ptr(conf->next_drive);
  119.     } while ( conf );
  120. }
  121.  
  122.