Repository navigation
Expand file tree
/
Copy pathtee.asm
More file actions
96 lines (85 loc) · 2.01 KB
/
Copy pathtee.asm
File metadata and controls
96 lines (85 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
;
; BASIC-DOS Tee Utility
;
; @author Jeff Parsons <Jeff@pcjs.org>
; @copyright (c) 2020-2026 Jeff Parsons
; @license MIT <https://basicdos.com/LICENSE.txt>
;
; This file is part of PCjs, a computer emulation software project at pcjs.org
;
; Usage: TEE [-A] file
;
; Copies input to STDOUT and to the specified file, which is replaced, or
; with -A, appended to (or created, if it doesn't exist). The exit code is 0,
; or 2 if the file couldn't be opened or written.
;
include utilvars.inc
CODE SEGMENT
org 100h
ASSUME CS:CODE, DS:CODE, ES:CODE, SS:CODE
DEFPROC main
mov bp,ds:[PSP_HEAP] ; BP -> UTILVARS
call getArgs
mov si,offset SYN_ERR
cmp [bp].UV_NFILES,0 ; was a file specified?
je m8 ; no
mov dx,[bp].UV_FILES ; DX -> file name
mov ax,DOS_HDL_OPENWO
CHKSW 'A' ; appending?
jz m1 ; no
int 21h ; so try opening an existing file
jnc m2 ; and if that fails, create it
m1: sub cx,cx ; CX = attributes (none)
mov ah,DOS_HDL_CREATE
int 21h
jnc m2
call openErr ; report the error (AX)
jmp short m9
m2: xchg si,ax ; SI = file handle
mov bx,si
sub cx,cx
sub dx,dx
mov ax,DOS_HDL_SEEKEND
int 21h ; seek to the end (of an appended file)
m3: lea dx,[bp].UV_INBUF
mov cx,INBUF_LEN
mov bx,STDIN
mov ah,DOS_HDL_READ
int 21h
jc m9
xchg cx,ax ; CX = # bytes read
jcxz m9 ; no more input
mov di,dx
mov al,CHR_CTRLZ
repne scasb ; CTRL-Z ends input
mov cx,di
jne m4 ; no CTRL-Z
dec cx ; exclude the CTRL-Z
sub di,di ; DI = 0 (end of input)
m4: sub cx,dx ; CX = # bytes to write
call writeOut
mov bx,si
mov ah,DOS_HDL_WRITE
int 21h
jc m6
cmp ax,cx ; was everything written (eg, disk full)?
jb m6 ; no
test di,di ; end of input?
jnz m3 ; no
m9: jmp exit
m6: mov si,offset WRITE_ERR
call writeErr
mov si,[bp].UV_FILES
call writeErr
mov si,offset CRLF
m8: call writeErr
mov [bp].UV_ERR,2
jmp m9
ENDPROC main
SW_LIST db "A"
SW_LEN equ $ - SW_LIST
include utilproc.inc
WRITE_ERR db "Unable to write ",0
COMHEAP <size UTILVARS + STACK_LEN>
CODE ENDS
end main