home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
MBUG
/
MBUG041.ARC
/
RENAME.MAC
< prev
next >
Wrap
Text File
|
1979-12-31
|
6KB
|
274 lines
;====================================================================
;
; RENAME :- Rename all files matching afn1 to afn2.
;
; PROGRAM
;
;
INCLUDE MACRO.LIB
;====================================================================
;
; Program :-
;
;
ENTRY
LD A, (CPMBUFF)
OR A
JR NZ, RNM ; parameter given
WRITELN 'RENAME :- Rename all files matching afn1 to afn2.'
RET
RNM: LD A, (CCPFCB2 + dr)
OR A
JP NZ, DR2DIFF
FILES ; find source files
JP NZ, NOMEM
LD A, B
OR C
JP Z, NOFILE
LD (SFILES), BC
LD (CHKADDR), DE
FILES CCPFCB2 ; get destination files for checking
JP NZ, NOMEM ; if file of that name already
LD (CFILES), BC ; exists
LD (DSTADDR), DE
XOR A
SBC HL, DE
LD BC, ENDPROG
ADD HL, BC
LD BC, (CHKADDR)
SBC HL, BC
JP C, NOMEM ; insufficient space for destination
LD HL, ENDPROG ; names
LD BC, (SFILES)
NXTNAME:PUSH BC ; at least one source file exists
PUSH HL
PUSH DE
CALL CPYNAME
POP HL
LD BC, 00010h
ADD HL, BC
EX DE, HL ; next destination name
POP HL
ADD HL, BC ; next source name
LD BC, (CFILES)
INC BC ; increase number of files to check
LD (CFILES), BC ; (no more than 2000h files may
POP BC ; exist)
DEC BC
LD A, B
OR C
JR NZ, NXTNAME
LD HL, ENDPROG
LD DE, (DSTADDR)
LD (OLDAT), HL
LD (NEWAT), DE
LD BC, (SFILES)
NXTF: PUSH BC
LD HL, (OLDAT)
LD DE, RENBUFF
LD BC, 00010h
LDIR ; copy source name
LD HL, (NEWAT)
LD BC, 0000Ch
LDIR ; copy destination name
SERVICE RENAME, RENBUFF ; rename file
CP 0FFh
JR Z, BADREN ; if BDOS reports failure
LD BC, (OLDAT)
CALL WTFNAME
WRITE ' renamed to '
LD BC, (NEWAT)
CALL WTFNAME
WRITELN
JR UPDATE
BADREN: WRITE 'BDOS unable to rename '
LD BC, (OLDAT)
CALL WTFNAME
WRITE ' to '
LD BC, (NEWAT)
CALL WTFNAME
WRITELN
UPDATE: LD BC, 00010h
LD HL, (OLDAT)
ADD HL, BC
LD (OLDAT), HL ; next source
LD HL, (NEWAT)
ADD HL, BC
LD (NEWAT), HL ; next destination
POP BC
DEC BC
LD A, B
OR C
JP NZ, NXTF
RET
NOFILE: WRITELN 'No matching files found.'
RET
DR2DIFF:WRITELN 'Destination name can not specify a drive code.'
JR QUIT
NOMEM: WRITELN 'Too many files found for available memory space.'
QUIT: WRITELN 'Aborted, no files renamed.'
JP ERREXIT
DSEG
SFILES: DS WORD ; holds number of source files found
CHKADDR:DS WORD ; address of first file to check name
; doesn't already exist
CFILES: DS WORD ; number of files to check
DSTADDR:DS WORD ; address of first destination name
OLDAT: DS WORD ; address of source name
NEWAT: DS WORD ; address of destination name
RENBUFF:DS 0001Ch ; holds 'FCB' for rename service
CSEG
;====================================================================
;
; Subroutines :-
;
;
CPYNAME: ; Generate new name corresponding to
; old name at HL and place it at
; DE. Check that no files of that
; name already exist, and that name
; is valid, abort otherwise. Set dr
; of new name to 000h.
LD (OLDAT), HL
LD (NEWAT), DE
LD A, 000h
LD (DE), A ; set dr of new name
INC DE
LD HL, CCPFCB2 + fname
LD BC, 0000Bh ; length of fname + ftype
LDIR ; copy new name into memory
LD HL, (OLDAT) ; HL = old name
LD DE, (NEWAT) ; DE = new name
LD B, 00Bh
NXT?: INC HL
INC DE
LD A, (HL)
AND 080h
LD C, A ; store attribute bit
LD A, (DE)
CP '?'
JR NZ, NOT?
LD A, (HL) ; replace '?' from old name
NOT?: OR C ; add attribute bit
LD (DE), A
DJNZ NXT?
LD HL, (NEWAT)
INC HL
LD B, 008h
CALL SPCHK
BIT 7, (HL)
JP NZ, RDONLY ; file is read only
LD B, 003h
CALL SPCHK ; check file name is legal
LD BC, (CFILES)
LD DE, (CHKADDR)
NXTCHK: LD A, B ; check if file of that name already
OR C ; exists
RET Z
PUSH BC
PUSH DE
INC DE
LD HL, (NEWAT)
INC HL
LD B, 00Bh
NXTL: LD A, (DE) ; compare letters
XOR (HL)
AND 07Fh ; mask attribute bit
JR NZ, NOMTCH
INC DE
INC HL
DJNZ NXTL
LD HL, (OLDAT) ; a match has been found
LD DE, (NEWAT)
LD B, 00Bh
NXTBYTE:INC HL ; see if source name is the same as
INC DE ; destination name
LD A, (DE)
CP (HL)
JR NZ, NOTSAME
DJNZ NXTBYTE
JR NOMTCH ; source same as destination, ok
NOTSAME:POP DE ; used to see if file already exists
JP FMATCH
NOMTCH: POP DE
LD HL, 00010h
ADD HL, DE
EX DE, HL
POP BC
DEC BC
JR NXTCHK
SPCHK: ; Check that fname / ftype is legal.
; HL = first letter of word
; B = number of bytes in fname /
; ftype.
; Return HL = HL + B.
LD A, ' '
XOR (HL)
INC HL
AND 07Fh ; mask attribute bit
JR Z, ENDWD
DJNZ SPCHK
RET
ENDWD: DEC B
RET Z
LD A, ' '
XOR (HL)
INC HL
AND 07Fh
JR Z, ENDWD
WRITE 'Renaming '
LD BC, (OLDAT)
CALL TFNAME
WRITE <' would result in an invalid filename, viz '>
LD BC, (NEWAT)
CALL WTFNAME
WRITELN
JP QUIT
RDONLY: LD BC, (OLDAT)
CALL TFNAME
WRITELN ' is R/O, unable to rename read only files.'
JP QUIT
FMATCH: LD HL, (DSTADDR)
EX DE, HL ; DE = first destination name
XOR A ; HL = name which matches
SBC HL, DE
JR C, EXISTS ; if name is in file-already-exists
LD DE, ENDPROG ; portion of memory
ADD HL, DE
WRITE 'Renaming of both '
LD B, H
LD C, L
CALL TFNAME
WRITE ' and '
LD BC, (OLDAT)
CALL TFNAME
WRITE <' would result in a duplicate', RET, LF, ' filename, viz '>
LD BC, (NEWAT)
CALL TFNAME
WRITELN
JP QUIT
EXISTS: WRITE 'Renaming of '
LD BC, (OLDAT)
CALL TFNAME
WRITE ' is not possible as a file called '
LD BC, (NEWAT)
CALL TFNAME
WRITELN <RET, LF, ' already exists.'>
JP QUIT
;--------------------------------------------------------------------
TFNAME ; macro declared subroutines
WTFNAME
FSEARCH
DSEARCH
;====================================================================
ENDPROG:
END