[comp.unix.ultrix] How to get device information from a file descriptor ?

Ninane@fynu.ucl.ac.be (Alain Ninane - FYNU) (06/03/91)

Dear all of you,
Given a valid file descriptor [from an open(2) call], is there a
standard way to know whether or not this fd points to a specific
special file/device (major number only) ?
I'm particularely interrested to detect:
					/dev/[n]rst*, /dev/[n]rmt*
I'm looking for a non-string(3) operation on the device name.
As I guess this is OS dependent ... I'm using Ultrix 4.1 and SUNOS 4.1
Thank you very much, Alain.
-- 
Dr. Alain H. Ninane, University of Louvain, Nuclear Physics Dept.
Ch. du Cyclotron, 2 -  B-1348 Louvain-la-Neuve,  BELGIUM 
Tel: +32-10-47.32.73, Fax: +32-10-45.21.83, Internet: Ninane@fynu.ucl.ac.be

tchrist@convex.COM (Tom Christiansen) (06/04/91)

From the keyboard of Ninane@fynu.ucl.ac.be (Alain Ninane - FYNU):
:Dear all of you,
:Given a valid file descriptor [from an open(2) call], is there a
:standard way to know whether or not this fd points to a specific
:special file/device (major number only) ?

man 2 fstat

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
	    "Perl is to sed as C is to assembly language."  -me

alan@shodha.enet.dec.com ( Alan's Home for Wayward Notes File.) (06/04/91)

In article <1991Jun3.104948.20582@info-sparc1.info.ucl.ac.be>, Ninane@fynu.ucl.ac.be (Alain Ninane - FYNU) writes:
> Dear all of you,
> Given a valid file descriptor [from an open(2) call], is there a
> standard way to know whether or not this fd points to a specific
> special file/device (major number only) ?

	You can use stat(2) on the file descriptor to get more
	information about the file.  Among the information is
	the mode flag that will bits to indicate whether the
	device is a directory, ordinary file, character special
	device or block special device (and maybe others).  There
	is also field with the major and minor device number.

	On ULTRIX you can get more detailed information about the
	device with the ioctl documented in the devio(4) manual
	page.

> Dr. Alain H. Ninane, University of Louvain, Nuclear Physics Dept.


-- 
Alan Rollow				alan@nabeth.cxn.dec.com

jfw@ksr.com (John F. Woods) (06/04/91)

Ninane@fynu.ucl.ac.be (Alain Ninane - FYNU) writes:
>Dear all of you,
>Given a valid file descriptor [from an open(2) call], is there a
>standard way to know whether or not this fd points to a specific
>special file/device (major number only) ?
>I'm particularely interrested to detect:
>					/dev/[n]rst*, /dev/[n]rmt*
>I'm looking for a non-string(3) operation on the device name.
>As I guess this is OS dependent ... I'm using Ultrix 4.1 and SUNOS 4.1
>Thank you very much, Alain.

There is no standard subroutine which does all this for you already.

To find out if a file descriptor is associated with the same file pointed
to by a given pathname, you would do a stat() of the pathname, an fstat()
of the file descriptor, and then compare the st_dev and st_ino numbers.
If they are equal, the files are identical.  Look up stat(2) and fstat(2)
in the manuals.

To determine whether or not an open file descriptor represents a given device,
first fstat() the file descriptor, check the st_mode field's type bits, and
see if it is either S_IFCHR or S_IFBLK (if not, it isn't a device); if so,
the major number is "major(statbuf.st_rdev)" (the major() macro should be
hiding in an include file somewhere under /usr/include/sys, but (as I recall)
its location varies, and it may or may not have a manual page).  Then you would
stat() files under /dev until you found one which is the same type and has the
same major number.  For the last operation, you should either look up
opendir(3), readdir(3), and closedir(3) if your system has the modern directory
access routines, or you should snag a copy of Doug Gwyn's directory access
routines, have someone install them in your system library, and then look up
opendir(3), readdir(3), and closedir(3) in the updated manuals :-).