; code (C) 1992 Peter Thompson
; Do what you like with this code. Just remove my name if it's lame.
;
E_OK = 0
E_WARN = 5
E_FATAL = 20
;
dl_root = 34
rn_info = 24	;bptr
di_info = 4	;bptr to devinfo chain.
dl_next = 0	;bptr to next in chain
dl_type = 4
dl_name = 40	;bptr to bstr name
;
Execbase = 4
;exec library function offsets
FindTask = -294
Forbid = -132
GetMesg = -372
ReplyMesg = -378
WaitPort = -384
OpenLib = -552
CloseLib = -414
;
;dos library function offsets
Output = -60
Write = -48
;
;process structure offsets (used to make WB safe)
pr_TaskNum = 140
pr_MsgPort = 92
pr_TaskPort = $5C
;
;local variable offsets
argc = -4
argv = -8
execlib = -12
taskadr = -16
wbmesg = -20
doslib = -24
devnam = -256
frame = -256
gea:	macro
	lea	\1(PC),\2
	endm
;
vars	equr	a4
retcode	equr	d4
;
A:	link	vars,#frame		; set up local memory
	move.l	d0,argc(vars)		; save arguments
	move.l	a0,argv(vars)
	movem.l	d1-d4/a0-a3/a6,-(SP)	; save all registers I clobber
	moveq	#E_FATAL,retcode		; assume the worst
	move.l	Execbase,a6
	suba.l	a1,a1			;find task address
	jsr	FindTask(a6)
	move.l	d0,taskadr(vars)
	move.l	d0,a3
	tst.l	pr_TaskNum(a3)		;find out if launched from a shell
	bne.S	CLI
	lea	pr_TaskPort(a3),a0	;get and store workbench message
	jsr	WaitPort(a6)
	lea	pr_TaskPort(a3),a0
	jsr	GetMesg(a6)
	move.l	d0,wbmesg(vars)
	bra	exit			;can't do anything if started from WB
CLI:	clr.l	wbmesg(vars)
	gea	dosnam,a1		; open dos library (any version)
	moveq	#0,d0
	jsr	OpenLib(a6)
	move.l	d0,doslib(vars)		; save dos library handle.
	beq	exit			; check we got it.
	move.l	argv(vars),a0		; get pointer to command line
	move.l	argc(vars),d0		; and length
	cmp.l	#128,d0			; at most 128-char devname
	bgt	prusag
; -- turn command line name into capitalised bcpl string
	moveq	#-1,d0			; byte count
	lea	devnam(vars),a1		; destination
	moveq	#0,d1			; character
strspc:	cmp.b	#' ',(a0)+		; remove spaces
	beq.S	strspc
	subq.l	#1,a0
prep:	cmp.b	#'a',d1			; convert to upper case
	blt.S	prstor
	cmp.b	#'z',d1
	bgt.S	prstor
	sub.b	#'a'-'A',d1
prstor:	move.b	d1,(a1)+		; store last char
	addq.b	#1,d0			; inc nchars
	move.b	(a0)+,d1		; get next from command line
	beq.S	dun			; until '\0', ':' or '\n'
	cmp.b	#':',d1
	beq.S	dun
	cmp.b	#10,d1
	bne.S	prep
dun:	move.b	d0,devnam(vars)		; store length of device name
	beq.S	prusag			; empty cmd line == cry for help
;chase down pointers (some bcpl) (see libraries/dosextens.[ih] for details)
;until we get to the list that contains all devices.
	move.l	doslib(vars),a0		; x = dosbase
	move.l	dl_root(a0),a0		; x = x->dl_root
	move.l	rn_info(a0),a0		; x = x->rn_info
	adda.l	a0,a0
	adda.l	a0,a0
	move.l	di_info(a0),d0		; x = x->di_info (device info chain)
	lea	devnam(vars),a0
	moveq	#E_OK,retcode
lookls: move.l	d0,a2			; unbcpl pointer to devinfo
	adda.l	a2,a2
	adda.l	a2,a2
;take shortcut if not device	: uncomment the next lines if you only want
; devices to be tested.
;	tst.l	dl_type
;	bne.s	notdev
	move.l	dl_name(a2),a1		; unbcpl pointer to name
	adda.l	a1,a1
	adda.l	a1,a1
	moveq	#0,d0
bcmp:	move.l	a0,a3			; save pointer to command line name
	move.b	(a1)+,d0
	cmp.b	(a0)+,d0		; compare lengths -
	bne.S	xit			; not equal cannot be same.
	subq.b	#1,d0			; adjust for dbCC counting convention
cmp:	move.b	(a1)+,d1
	cmp.b	#'a',d1			; capitalise current character in name
	blt.S	cmit
	cmp.b	#'z',d1
	bgt.S	cmit
	sub.b	#'a'-'A',d1
cmit:	cmp.b	(a0)+,d1		; compare strings until not equal
	dbne	d0,cmp			; or no more chars
xit:	movea.l	a3,a0			; ! does not affect condition codes
	beq.S	there			; found it!
notdev:	move.l	dl_next(a2),d0		; get pointer to next device node.
	bne.S	lookls			; continue if more to look at.
	moveq	#E_WARN,retcode		; no more so return disappointed.
there:	bra.S	fini
prusag:	move.l	doslib(vars),a6
	jsr	Output(a6)		; get output file handle
	move.l	d0,d1			; and put it in the right place.
	beq.S	fini			; take care of no output window
	gea	usage,a0		; get pointer to text
	move.l	a0,d2
	move.l	#eou-usage,d3		; get length of text
	jsr	Write(a6)		; and let it rip!
fini:	move.l	Execbase,a6		; get rid of dos library.
	move.l	doslib(vars),a1
	jsr	CloseLib(a6)
exit:	move.l	wbmesg(vars),d0		;if necessary return wb message.
	beq.S	notwb
	jsr	Forbid(a6)
	move.l	d0,a1
	jsr	ReplyMesg(a6)
notwb:	move.l	retcode,d0		; stick the return code in d0
	movem.l	(SP)+,d1-d4/a0-a3/a6	; restore all registers
	unlk	vars			; dispose of local stack
ZZ:	rts				; and depart
dosnam:	dc.b	"dos.library",0
usage:	dc.b	"Usage: IsMounted devicename (no",'"',"s)",10
	dc.b	"Return code is 0 if device mounted, WARN if not.",10
	dc.b	"(C) 1992 (freeware) Peter Thompson.",10
eou:	dc.b	0
	END
