home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / c / snippets / setenvar.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  4KB  |  170 lines

  1. /*
  2. **  SETENVAR.C - Program which sets the DOS master environment upon exit
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is functionally identical to the
  8. **  version originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <conio.h>
  16. #include <dos.h>
  17.  
  18. #if !defined(MK_FP)
  19.  #define MK_FP(seg,offset) \
  20.         ((void far *)(((unsigned long)(seg)<<16) | (unsigned)(offset)))
  21. #endif
  22.  
  23. #if !defined(__ZTC__) && !defined(__TURBOC__)
  24.  #define peek(s,o) (*((unsigned short _far *)(MK_FP(s,o))))
  25.  #define poke(s,o,w) (*((unsigned short _far *)(MK_FP(s,o)))=(w))
  26. #endif
  27.  
  28. #define SUCCESS 0
  29. #define ERROR -1
  30.  
  31. static unsigned head, tail, start, end;
  32. static int idx = 0;
  33. static unsigned keystack[16][2];
  34.  
  35. /*
  36. **  ungetkey()
  37. **
  38. **  Stuffs characters into the keyboard buffer.
  39. **
  40. **  Parameters: 1 - Extended character to stuff
  41. **
  42. **  Returns: SUCCESS or EOF
  43. **
  44. **  Note: This function assumes that the keyboard buffer is in
  45. **        the normal (for IBM) location of 40:1E.
  46. **
  47. */
  48.  
  49. int ungetkey(unsigned key)
  50. {
  51.     int count;
  52.  
  53. #ifdef __ZTC__
  54.       peek(0x40, 0x1a, &head, sizeof(unsigned));
  55.       peek(0x40, 0x1c, &tail, sizeof(unsigned));
  56.       peek(0x40, 0x80, &start, sizeof(unsigned));
  57.       peek(0x40, 0x82, &end, sizeof(unsigned));
  58. #else
  59.       head  = peek(0x40, 0x1a);
  60.       tail  = peek(0x40, 0x1c);
  61.       start = peek(0x40, 0x80);
  62.       end   = peek(0x40, 0x82);
  63. #endif
  64.       count = tail - head;
  65.       if (0 > count)
  66.             count += (16 * sizeof(unsigned));
  67.       count >>= 1;
  68.  
  69.       if (15 > count)
  70.       {
  71. #ifdef __ZTC__
  72.             int_off();
  73.             peek(0x40, tail, &keystack[idx][0], sizeof(unsigned));
  74. #else
  75.             _disable();
  76.             keystack[idx][0] = peek(0x40, tail);
  77. #endif
  78.             keystack[idx][1] = tail;
  79. #ifdef __ZTC__
  80.             poke(0x40, tail, &key, sizeof(unsigned));
  81. #else
  82.             poke(0x40, tail, key);
  83. #endif
  84.             tail += sizeof(unsigned);
  85.             if (end <= tail)
  86.                   tail = start;
  87. #ifdef __ZTC__
  88.             poke(0x40, 0x1c, &tail, sizeof(unsigned));
  89.             int_on();
  90. #else
  91.             poke(0x40, 0x1c, tail);
  92.             _enable();
  93. #endif
  94.             return key;
  95.       }
  96.       return EOF;
  97. }
  98.  
  99. /*
  100. **  KB_stuff()
  101. **
  102. **  Stuffs strings into the keyboard buffer.
  103. **
  104. **  Parameters: 1 - String to stuff
  105. **
  106. **  Returns: SUCCESS if successful
  107. **           ERROR   in case of error, plus beyboard buffer is
  108. **                   restored
  109. **
  110. **  Note: This function assumes that the keyboard buffer is in
  111. **        the normal (for IBM) location of 40:1E.
  112. */
  113.  
  114. int KB_stuff(char *str)
  115. {
  116.       int ercode = SUCCESS;
  117.  
  118.       idx = 0;
  119.       while (*str)
  120.       {
  121.             if (EOF == ungetkey((unsigned)(*str++)))
  122.             {
  123. #ifdef __ZTC__
  124.                   int_off();
  125. #else
  126.                   _disable();
  127. #endif
  128.                   while (0 <= --idx)
  129.                   {
  130.                         tail = keystack[idx][1];
  131. #ifdef __ZTC__
  132.                         poke(0x40, tail, &keystack[idx][0], sizeof(unsigned));
  133. #else
  134.                         poke(0x40, tail, keystack[idx][0]);
  135. #endif
  136.                   }
  137. #ifdef __ZTC__
  138.                   poke(0x40, 0x1c, &tail, sizeof(unsigned));
  139.                   int_on();
  140. #else
  141.                   poke(0x40, 0x1c, tail);
  142.                   _enable();
  143. #endif
  144.                   ercode = ERROR;
  145.                   break;
  146.             }
  147.             else  ++idx;
  148.       }
  149.       idx = 0;
  150.       return ercode;
  151. }
  152.  
  153. main(int argc, char *argv[])
  154. {
  155.       FILE *bfile;
  156.  
  157.       if (3 > argc)
  158.       {
  159.             puts("\aUsage: SETENVAR envar datum");
  160.             abort();
  161.       }
  162.       bfile = fopen("$TMP$.BAT", "w");
  163.       fprintf(bfile, "SET %s=%s\ndel $tmp$.bat\x1a", argv[1], argv[2]);
  164.       fclose(bfile);
  165.       while (kbhit())
  166.             getch();
  167.       KB_stuff("$tmp$\r");
  168.       return 0;
  169. }
  170.