home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / PRTOGGLE.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  2KB  |  92 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 <conio.h>
  24. #include <io.h>
  25.  
  26. #if !defined(__ZTC__) && !defined(__TURBOC__)
  27.  #define MK_FP(seg,offset) \
  28.         ((void far *)(((unsigned long)(seg)<<16) | (unsigned)(offset)))
  29.  #define peek(s,o) (*((unsigned far *)(MK_FP(s,o))))
  30.  #define poke(s,o,w) (*((unsigned far *)(MK_FP(s,o)))=(w))
  31. #endif
  32.  
  33. #define SUCCESS 0
  34. #define ERROR -1
  35.  
  36. static unsigned head, tail, start, end;
  37. static int idx = 0;
  38. static unsigned keystack[16][2];
  39.  
  40. int ungetkey(unsigned key)
  41. {
  42.         int count;
  43.  
  44. #ifdef __ZTC__
  45.         peek(0x40, 0x1a, &head, sizeof(unsigned));
  46.         peek(0x40, 0x1c, &tail, sizeof(unsigned));
  47. #else
  48.         head = peek(0x40, 0x1a);
  49.         tail = peek(0x40, 0x1c);
  50. #endif
  51.         count = tail - head;
  52.         if (0 > count)
  53.                 count += (16 * sizeof(unsigned));
  54.         count >>= 1;
  55.  
  56.         if (15 > count)
  57.         {
  58. #ifdef __ZTC__
  59.                 peek(0x40, tail, &keystack[idx][0], sizeof(unsigned));
  60. #else
  61.                 keystack[idx][0] = peek(0x40, tail);
  62. #endif
  63.                 keystack[idx][1] = tail;
  64. #ifdef __ZTC__
  65.                 poke(0x40, tail, &key, sizeof(unsigned));
  66. #else
  67.                 poke(0x40, tail, key);
  68. #endif
  69.                 tail += sizeof(unsigned);
  70.                 if (0x3e <= tail)
  71.                         tail = 0x1e;
  72. #ifdef __ZTC__
  73.                 poke(0x40, 0x1c, &tail, sizeof(unsigned));
  74. #else
  75.                 poke(0x40, 0x1c, tail);
  76. #endif
  77.                 return key;
  78.         }
  79.         return EOF;
  80. }
  81.  
  82. int prtoggle(void)
  83. {
  84.       if (!isatty(fileno(stdin)) || !isatty(fileno(stdout)))
  85.             return -1;
  86.       while (kbhit())           /* Flush the keyboard buffer            */
  87.             getch();
  88.       ungetkey('P' - 64);       /* Stuff a Ctrl-P into the buffer       */
  89.       system("");               /* Let COMMAND.COM do the work          */
  90.       return 0;
  91. }
  92.