home *** CD-ROM | disk | FTP | other *** search
/ BBS 1 / BBS#1.iso / maximus / nohup.arj / NOHUP.CC next >
Encoding:
C/C++ Source or Header  |  1992-05-19  |  3.0 KB  |  94 lines

  1. //****************************************************************
  2. //
  3. //      FOSSIL overhead program
  4. //      Prevents short-time carrier losses on bad phone lines
  5. //      Known bug: no check for multiple loading as TSR
  6. //
  7. # if !defined(__TCPLUSPLUS__) || !defined(__LARGE__)
  8. # error assumed borland C++, large memory model
  9. # endif
  10.  
  11. # pragma inline
  12. # pragma option -N-
  13. # pragma option -r-
  14.  
  15. static char _C_[] = "$RCSfile: nohup.~cc $ (C) Business Console Ltd. $Author: Bell $";
  16.  
  17. # include       <alloc.h>
  18. # include       <dos.h>
  19.  
  20. static void             hand_com();
  21. static void interrupt   (*saved_com)(...);
  22.  
  23. //----------------------------------------------------------------
  24.  
  25. int             main()
  26.   {
  27.   const int intr_com = 0x14;                    // steal FOSSIL interrupt
  28.   saved_com = getvect(intr_com);
  29.   setvect(intr_com, (void interrupt (*)(...)) hand_com);
  30.  
  31.   freemem(((int*) MK_FP(_psp, 0x2c))[0]);       // gentlemen should always
  32.                                                 // free environment space
  33.  
  34.   keep(0, FP_SEG(sbrk(0)) - _psp + 1);          // compute program size
  35.                                                 // by sbrk and psp
  36.  
  37.   setvect(intr_com, saved_com);                 // can we ever get here ?
  38.   return 1;
  39.   }
  40.  
  41. //----------------------------------------------------------------
  42.  
  43. //      You can rewrite codes below more precisely, but I don't care
  44.  
  45. # define        PUSH    asm pushf; asm push ax; asm push dx;    \
  46.                         asm push bx; asm push es; asm push ds;  \
  47.                         asm mov ax, DGROUP; asm mov ds, ax;
  48. # define        POP     asm pop ds; asm pop es; asm pop bx;     \
  49.                         asm pop dx; asm pop ax; asm popf;
  50. # define        RET     asm mov sp, bp; asm pop bp; asm ret 2;
  51.  
  52. void            hand_com()
  53.   {
  54.   const int     carrier_bit = 0x80;
  55.   static int    online = -1;
  56.   static int    timeout;
  57.   int* const    timer = (int*) 0x0000046cL;     // timer in BIOS data area
  58.   const int     delay = 100;                    // 100 ticks = 5 sec
  59.  
  60.   char ah = _AH;
  61.   int ax = _AX;
  62.   PUSH;
  63.   void (interrupt* fun)(...) = saved_com;       // copy old vector in stack
  64.   POP;
  65.   fun();                                        // all registers restored
  66.   int ret = _AX;
  67.   PUSH;
  68.   if (ax == 0x600)                              // subfunction 0x6 (drop DTR)
  69.     online = -1;
  70.   else if (ah != 3)                             // not subfunction 0x3 (query)
  71.     ;
  72.   else if (ret & carrier_bit)                   // online
  73.     online = 1;
  74.   else if (online > 0)                          // carrier just lost
  75.     {
  76.     ret |= carrier_bit;
  77.     timeout = *timer + delay;
  78.     online = 0;
  79.     }
  80.   else if (!online)                             // still waiting for carrier
  81.     {
  82.     ret |= carrier_bit;
  83.     int delta = *timer - timeout;               // seems suspicious,
  84.     if (delta >= 0)                             // but still valid
  85.       online = -1;
  86.     }
  87.   else                                          // no hope
  88.     ;
  89.   POP;
  90.   _AX = ret;
  91.   RET;
  92.   }
  93.  
  94.