home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_03 / 3n03060b < prev    next >
Text File  |  1991-12-04  |  3KB  |  123 lines

  1. ;
  2. ; redira.asm:
  3. ;
  4. ; Assembly language version of redir() function,
  5. ; to detect I/O redirection.
  6. ; To assemble with TASM:
  7. ;   tasm /mx redira
  8. ;
  9.  
  10.     NAME    redir
  11.  
  12. NEARCODE    EQU 01H ; near function
  13. SMALLDATA   EQU 01H ; tiny, small, or medium memory model
  14. MOLASSES    EQU 1   ; define for Microsoft C or Turbo C, else comment out
  15. ;USEPSP     EQU 1   ; use global _psp, comment out to use int 21h command 62h
  16.  
  17. IFDEF   TINY
  18.     IFNDEF  NEARCODE
  19.         NEARCODE    EQU 01H
  20.     ENDIF
  21.     IFNDEF  SMALLDATA
  22.         SMALLDATA   EQU 01H
  23.     ENDIF
  24. ENDIF
  25.  
  26. DGROUP  GROUP   _DATA
  27. _TEXT   SEGMENT PUBLIC BYTE 'CODE'
  28.         ASSUME  CS:_TEXT
  29.  
  30. IFDEF   MOLASSES        ; Microsoft C and Turbo C
  31. PUBLIC  _redir
  32.     IFDEF   NEARCODE    ; if near function
  33. _redir  PROC    NEAR    ; unsigned char _near redir (void);
  34.     ELSE                ; if far function
  35. _redir  PROC    FAR     ; unsigned char _far redir (void);
  36.     ENDIF
  37. ELSE                    ; WATCOM C
  38. PUBLIC  redir_
  39.     IFDEF   NEARCODE    ; if near function
  40. redir_  PROC    NEAR    ; unsigned char __near redir (void);
  41.     ELSE                ; if far function
  42. redir_  PROC    FAR     ; unsigned char __far redir (void);
  43.     ENDIF
  44. ENDIF
  45.  
  46.     PUSH    BX
  47.     PUSH    CX
  48.     PUSH    SI
  49.     PUSH    DI
  50.     PUSH    ES
  51.  
  52. IFNDEF  TINY            ; If not COM file
  53.     PUSH    DS
  54.     IFDEF USEPSP        
  55.         MOV AX, __PSP   ; Use global _psp variable
  56.         MOV DS,AX
  57.     ELSE
  58.         MOV AH,62H      ; Undocumented Get PSP Segment
  59.         INT 21H
  60.  
  61.         MOV DS,BX       ; Put PSP Segment in DS
  62.     ENDIF
  63. ENDIF
  64.  
  65.     MOV SI,0034H        ; DS:SI points to pointer to File Handle Table
  66.  
  67.     LES DI, DWORD PTR DS:[SI]
  68.  
  69. ;   MOV DI,WORD PTR DS:[SI]         ; Load ES:DI
  70. ;   MOV ES,WORD PTR DS:[SI+02H]     ;  with address of File Handle Table
  71.  
  72. IFNDEF  SMALLDATA       ; If compact, large, or huge memory model
  73.     MOV AX,SEG DGROUP   ;  put segment of table of default values
  74.     MOV DS,AX           ;   into DS
  75. ELSE                    ; If tiny, small, or medium memory model
  76.     IFNDEF  TINY
  77.     POP DS              ; Pointed to DGROUP when PUSHed
  78.     ENDIF
  79. ENDIF
  80.  
  81.     MOV SI,OFFSET DGROUP:STDIN  ; pointer to default values in DS:SI
  82.     XOR AX,AX           ; Assume no redirection
  83.     MOV BX,01H          ; Set bit zero of handle_bit
  84.     MOV CX,0005H        ; There are five defaults
  85.  
  86. TOP:
  87.     CMPSB       ; Compare handles to defaults
  88.     JE  SHIFT   ; If equal, prepare for next compare
  89.     OR  AX,BX   ;  else set bit in return value
  90.  
  91. SHIFT:
  92.     SHL BX,1    ; Shift to set next bit of handle_bit
  93.     LOOP    TOP ; If not all standard devices checked, check next
  94.  
  95. IFNDEF  SMALLDATA   ; If compact, large, or huge memory model
  96.     POP DS
  97. ENDIF
  98.  
  99.     POP ES
  100.     POP DI
  101.     POP SI
  102.     POP CX
  103.     POP BX
  104.     RET
  105.  
  106. IFDEF MOLASSES      ;Microsoft C and Turbo C
  107. _redir  ENDP
  108. ELSE
  109. redir_  ENDP
  110. ENDIF
  111.  
  112. _TEXT   ENDS
  113. _DATA   SEGMENT BYTE PUBLIC 'DATA'
  114. STDIN   DB  01H ; CON:
  115. STDOUT  DB  01H ; CON:
  116. STDERR  DB  01H ; CON:
  117. AUXIO   DB  00H ; AUX:
  118. LSTOUT  DB  02H ; PRN:
  119. _DATA   ENDS
  120.     END
  121.  
  122. /* End of File */
  123.