[comp.lang.c] Determining the names of devices installed in DOS

wnp@killer.UUCP (Wolf Paul) (10/12/87)

Recently I  asked for advice on how to determine if a file name refers to
a device or to a regular file under DOS, in order to implement a function
to simulate the UNIX stat(2) system call.

The most promising advice was from Dan Kegel who wrote:

> 1.  You can find the device driver for any open file and
>     look at its name.  Unfortunately, this is not documented
>     and the method used to get the device driver pointer
>     differs slightly from version to version.
> 
> 2.  Using (1), you can get a list of all device names by opening
>     the NUL device and following the chain of device drivers.
>     Having done this once, it should be easy to evaluate a
>     given filename for deviceness.

Unfortunately he did not elaborate on how to do what he suggested. Can anyone
out there tell me how to

1.	find the device driver header, given a handle for a device; and
2.	how to follow the chain of device drivers from NUL?

I find it interesting that apparently the MSC stat(2) function does not
know how to distinguish between devices and regular files, whereas the
fstat(2) function does make that distinction. I assume they use ioctl
on the file handle passed to fstat.

Wolf Paul
ihnp4!killer!wnp

rwhite@nusdhub.UUCP (Robert C. White Jr.) (10/13/87)

In article <1801@killer.UUCP>, wnp@killer.UUCP (Wolf Paul) writes:
   [Stuff Deleted]
> I find it interesting that apparently the MSC stat(2) function does not
> know how to distinguish between devices and regular files, whereas the
> fstat(2) function does make that distinction. I assume they use ioctl
> on the file handle passed to fstat.

	This is SOOOO.... Simple, that the methodologies described
above look quite convoluted....

1)	Get the "device status word" using ioctl against the open
	file descriptor.  [I don't remember the subcommand, so you'll
	have to look it up]

2)	Mask out the "Block Device" flag.

3)	If the block device flag is set, then you MUST be dealing
	with a file on a disk someware.  If it is not set you have
	an open character device on your hands.


	Only character and block devices are defined in MS-DOS.
Also, the "get device status word" subcommand of the ioctl/int-24
command MUST! return the status word of the device of ANY file
discriptor.  Since MS-DOS does not have pipes, streams, swap-devices,
or "system facilities" defined in the file system.  Because of this
you simply have "Disk Files" and "Character Devices"


Disclaimer:  Neat huh?

Robert.

bobmon@iucs.UUCP (RAMontante [condition that I not be identified]) (10/16/87)

TurboC's doc. for stat and fstat indicate that they return (among other
things) a bitmask which includes and S_IFREG flag for ordinary files; fstat
also returns S_IFCHR to (redundantly?) indicate a device.

rwhite@nusdhub.UUCP (Robert C. White Jr.) (10/16/87)

In article <4717@iucs.UUCP>, bobmon@iucs.UUCP (RAMontante [condition that I not be identified]) writes:

> TurboC's doc. for stat and fstat indicate that they return (among other
> things) a bitmask which includes and S_IFREG flag for ordinary files; fstat
> also returns S_IFCHR to (redundantly?) indicate a device.

I think they did this to make testing easier,
or at least more obvious to the less machine-guts-aware
programmers [to whom the price of turbo-C would
allow possibly the first hope of a C compiler
with documentation.]

If you did a little testing, I think you would find
that these two flags are complements of eachother.
[That is, I can't think when they would not be]

Rob.