home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_12 / 8n12090a < prev    next >
Text File  |  1990-10-16  |  3KB  |  98 lines

  1.  
  2. /*-------------------------------------------------------------*/
  3. /* Purpose:                                                    */
  4. /*   Enable any key to be pressed while reading and printing   */
  5. /*   a text file to cancel the print.  Uses setjmp() and       */
  6. /*   longjmp()                                                 */
  7. /* Compiled:  cl /c /AL /W3  filename (Microsoft Cv5.1)        */
  8. /*-------------------------------------------------------------*/
  9. #pragma check_stack (off)     /* disable stack checking        */
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <malloc.h>
  14. #include <setjmp.h>
  15. #include <io.h>
  16. #include <dos.h>
  17. #include <conio.h>
  18. #include <conio.h>
  19. #include <fcntl.h>
  20. #include <sys\types.h>
  21. #include <sys\stat.h>
  22.  
  23. static jmp_buf mark;
  24. static void (interrupt far *old_int09) (void);
  25. static void (interrupt far *keyb_rtn_ptr) (void);
  26.  
  27. static int busy=0;
  28.                /* function prototypes                     */
  29. static void interrupt far new_int09(void);
  30. void main(void);
  31.  
  32. /*-------------------------------------------------------------*/
  33. /* main()
  34. /* Purpose:                                                    */
  35. /*   Read and print file indefinitely to test the print cancel.*/
  36. /*-------------------------------------------------------------*/
  37.  
  38. void main()
  39. {
  40. int read_handle;
  41. int prn_handle;
  42. unsigned read_cnt;
  43. char *buff;
  44.  
  45. read_handle = open("TEST.DAT", O_BINARY |  O_RDWR, 0);
  46. prn_handle = fileno(stdprn);     /* get file handle                                                  
  47.                                                for stdprn */
  48. buff = (char *) malloc(4096);
  49.  
  50. if(setjmp(mark) != 0)            /* return here on key 
  51.                                                     press */
  52.           {
  53.           printf("Cancelled print!\n");
  54.           return;               /* print cancelled   */
  55.           }
  56. old_int09 = _dos_getvect(0x09); /* save old keyboard 
  57.                                                   handler */
  58. _dos_setvect(0x09, new_int09);  /* new keyboard 
  59.                                                   handler */
  60.  
  61. while(1)                        /* read forever      */
  62.           {
  63.           read_cnt = read(read_handle, buff, 4096);
  64.           write(prn_handle, buff, read_cnt);
  65.                   lseek(read_handle, 0L, SEEK_SET);
  66.           }
  67. }
  68.  
  69. /*-------------------------------------------------------------*/
  70. /* new_int09()                                                 */ 
  71. /* Purpose:                                                    */
  72. /*   Handles interrupt 09h interrupts.                         */
  73. /*-------------------------------------------------------------*/
  74. static void interrupt far new_int09()
  75. {
  76.  
  77.  (*old_int09)();     /* call former interrupt handler */
  78.  _disable();         /* disable hardware interrupts   */
  79.                      /* while testing and setting the */
  80.                      /* busy flag                     */
  81.  
  82.  if(busy)            /* prevent recursive invocations */
  83.      return;
  84.  busy = 1;
  85.      
  86.  _enable();          /* reenable hardware interrupts - */       
  87.                      /* it's safe                     */
  88.  
  89. if(kbhit())         /* key in buffer                 */         
  90.      {
  91.      longjmp(mark, -1); /* cancel the printing           */
  92.      }
  93.  
  94. busy = 0;
  95. }
  96.  
  97.  
  98.