Subject: Re: Determine if Floppy is in drive?
To: None <netbsd-help@netbsd.org>
From: James K. Lowden <jklowden@schemamania.org>
List: netbsd-help
Date: 01/30/2003 02:08:22
On Tue, 28 Jan 2003 21:43:44 -0700 (MST), Eric Fox
<eric@fox.phoenix.az.us> wrote:
> Does anyone know of a way to determine if a floppy has been inserted
> into the drive before attempting to mount it?  

This seemed like such an obvious question.  I wrote a test program,
attached, trying to tickle the device to get an error.  The fdc device
refuses to say whether or not there's a diskette in the drive.  Humph.  

It's part of my unix puzzlement, you might say.  How do I know:

1. the printer's online?
2. the modem has DSR raised?
3. the CD is ready?  

I guess this is part of NetBSD's legacy, the assumption that the machine
is at the other end of some tangle of RS-232, and that if [device] is not
ready, sooner or later an operator will ready it.  I guess.  

Reminded of the Dreary Days of DOS, I did what I haven't done in ages. 
The BIOS offers interrupt 13H, function 17H "Determine disk format", which
can return a timeout error, not that that helps you at all.  I have no
idea if modern BIOSes support that; my book is copyright 1988.  

With so many removable devices -- USB, PCMCIA, not to mention CDs -- it
seems odd there's no established "are you there?" protocol.  Or, maybe
there is, and I've been reading the wrong books.  

--jkl

#include <stdlib.h>
#include <sys/fdio.h>
#include <fcntl.h>


int
main( int argc, char *argv[] )
{
	int fd, erc, opts;
	struct fdformat_parms params;
	
	fd = open("/dev/fd0a", O_WRONLY, 0);
	
	printf("%d\n", fd);

	erc = ioctl(fd, FDIOCGETFORMAT, &params); /* get format parms */

	printf("%d\n", erc);
	
	if (erc >= 0) {
		printf( "%u/%u/%u\n", params.nbps, params.ncyl, params.nspt );
	}

	erc = ioctl(fd, FDIOCGETFORMAT, &opts);	/* Fetch drive options */

	printf("%d\n", opts);
	
}

Output, regardless of presence of floppy:

$ ./test_floppy 
3
0
512/80/18
19961120