Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!caen!spool.mu.edu!uunet!ksr!jfw From: jfw@ksr.com (John F. Woods) Newsgroups: comp.unix.ultrix Subject: Re: How to get device information from a file descriptor ? Keywords: UNIX - devices - file descriptor Message-ID: <3861@ksr.com> Date: 4 Jun 91 16:07:37 GMT Article-I.D.: ksr.3861 References: <1991Jun3.104948.20582@info-sparc1.info.ucl.ac.be> Sender: news@ksr.com Lines: 31 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 :-).