home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / PRTOGGLE.C < prev    next >
C/C++ Source or Header  |  1991-12-25  |  2KB  |  77 lines

  1. /*
  2. **  prtoggle()
  3. **
  4. **  Tee's all standard output to the printer.
  5. **
  6. **  Parameters: None
  7. **
  8. **  Returns:  0 if operation was successful.
  9. **           -1 if stdout or stdin is redirected.
  10. **
  11. **  Side effects: Flushes the keyboard buffer
  12. **
  13. **  Original Copyright 1988-1991 by Bob Stout as part of
  14. **  the MicroFirm Function Library (MFL)
  15. **
  16. **  This subset version is functionally identical to the
  17. **  version originally published by the author in Tech Specialist
  18. **  magazine and is hereby donated to the public domain.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <io.h>
  24.  
  25. int ungetkey(unsigned key)
  26. {
  27.         int count;
  28.  
  29. #ifdef __ZTC__
  30.         peek(0x40, 0x1a, &head, sizeof(unsigned));
  31.         peek(0x40, 0x1c, &tail, sizeof(unsigned));
  32. #else
  33.         head = peek(0x40, 0x1a);
  34.         tail = peek(0x40, 0x1c);
  35. #endif
  36.         count = tail - head;
  37.         if (0 > count)
  38.                 count += (16 * sizeof(unsigned));
  39.         count >>= 1;
  40.  
  41.         if (15 > count)
  42.         {
  43. #ifdef __ZTC__
  44.                 peek(0x40, tail, &keystack[idx][0], sizeof(unsigned));
  45. #else
  46.                 keystack[idx][0] = peek(0x40, tail);
  47. #endif
  48.                 keystack[idx][1] = tail;
  49. #ifdef __ZTC__
  50.                 poke(0x40, tail, &key, sizeof(unsigned));
  51. #else
  52.                 poke(0x40, tail, key);
  53. #endif
  54.                 tail += sizeof(unsigned);
  55.                 if (0x3e <= tail)
  56.                         tail = 0x1e;
  57. #ifdef __ZTC__
  58.                 poke(0x40, 0x1c, &tail, sizeof(unsigned));
  59. #else
  60.                 poke(0x40, 0x1c, tail);
  61. #endif
  62.                 return key;
  63.         }
  64.         return EOF;
  65. }
  66.  
  67. int cdecl prtoggle(void)
  68. {
  69.       if (!isatty(fileno(stdin)) || !isatty(fileno(stdout)))
  70.             return -1;
  71.       while (kbhit())           /* Flush the keyboard buffer            */
  72.             getch();
  73.       ungetkey('P' - 64);       /* Stuff a Ctrl-P into the buffer       */
  74.       system("");               /* Let COMMAND.COM do the work          */
  75.       return 0;
  76. }
  77.