Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

138 Files and Directories Chapter 4


•Wecan usually access the major and minor device numbers through two macros
defined by most implementations:majorandminor.Consequently, we don’t
carehow the two numbers arestored in adev_tobject.

Early systems stored the device number in a 16-bit integer,with 8 bits for the major
number and 8 bits for the minor number.FreeBSD 8.0 and Mac OS X 10.6.8 use a 32-bit
integer,with 8 bits for the major number and 24 bits for the minor number.On3 2 - bit
systems, Solaris 10 uses a 32-bit integer fordev_t,with 14 bits designated as the major
number and 18 bits designated as the minor number.On6 4 - bit systems, Solaris 10
representsdev_tas a 64-bit integer,with 32 bits for each number.OnLinux 3.2.0,
althoughdev_tis a 64-bit integer,only 12 bits areused for the major number and 20 bits
areused for the minor number.
POSIX.1 states that thedev_ttype exists, but doesn’t define what it contains or how to
get at its contents. The macrosmajorandminoraredefined by most implementations.
Which header they aredefined in depends on the system. They can be found in
<sys/types.h>on BSD-based systems. Solaris defines their function prototypes in
<sys/mkdev.h>,because the macrodefinitions in<sys/sysmacros.h>areconsidered
obsolete in Solaris. Linux defines these macros in<sys/sysmacros.h>,which is
included by<sys/types.h>.

•Thest_devvalue for every filename on a system is the device number of the
file system containing that filename and its corresponding i-node.
•Only character special files and block special files have anst_rdevvalue. This
value contains the device number for the actual device.

Example


The program in Figure4.25 prints the device number for each command-line argument.
Additionally, if the argument refers to a character special file or a block special file, the
st_rdevvalue for the special file is printed.
#include "apue.h"
#ifdef SOLARIS
#include <sys/mkdev.h>
#endif

int
main(int argc, char *argv[])
{
int i;
struct stat buf;

for (i = 1; i < argc; i++) {
printf("%s: ", argv[i]);
if (stat(argv[i], &buf) < 0) {
err_ret("stat error");
continue;
}

printf("dev = %d/%d", major(buf.st_dev), minor(buf.st_dev));
Free download pdf